Spaces:
Paused
Paused
matt HOFFNER
commited on
Commit
·
e4fae68
1
Parent(s):
2016044
refactor to get request
Browse files- src/app/search/web/page.jsx +13 -31
- src/pages/api/llm.js +3 -3
src/app/search/web/page.jsx
CHANGED
@@ -9,39 +9,21 @@ export default function WebSearchPage({ searchParams }) {
|
|
9 |
const startIndex = searchParams.start || "1";
|
10 |
|
11 |
useEffect(() => {
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
console.log(response);
|
18 |
-
throw new Error("Something went wrong");
|
19 |
-
}
|
20 |
-
const data = await response.json();
|
21 |
-
setResults(data.items);
|
22 |
-
*/
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
// Fetch AI response via server-side route
|
28 |
-
const openaiRes = new EventSource('/api/llm', {
|
29 |
-
method: 'POST',
|
30 |
-
headers: {
|
31 |
-
'Content-Type': 'application/json',
|
32 |
-
},
|
33 |
-
body: JSON.stringify({
|
34 |
-
'question': searchParams.searchTerm
|
35 |
-
})
|
36 |
-
});
|
37 |
-
|
38 |
-
// Listen for AI responses and append to state
|
39 |
-
openaiRes.onmessage = function(event) {
|
40 |
setAiResponse(aiResponse => aiResponse + event.data);
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
45 |
}, [searchParams, startIndex]);
|
46 |
|
47 |
if (!results) {
|
|
|
9 |
const startIndex = searchParams.start || "1";
|
10 |
|
11 |
useEffect(() => {
|
12 |
+
const url = new URL('/api/llm', window.location.origin);
|
13 |
+
url.searchParams.append('question', searchParams.searchTerm);
|
14 |
+
url.searchParams.append('startIndex', startIndex);
|
15 |
+
|
16 |
+
const openaiRes = new EventSource(url);
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
// Listen for AI responses and append to state
|
19 |
+
openaiRes.onmessage = function(event) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
setAiResponse(aiResponse => aiResponse + event.data);
|
21 |
+
};
|
22 |
+
|
23 |
+
// Close connection when component unmounts
|
24 |
+
return () => {
|
25 |
+
openaiRes.close();
|
26 |
+
};
|
27 |
}, [searchParams, startIndex]);
|
28 |
|
29 |
if (!results) {
|
src/pages/api/llm.js
CHANGED
@@ -2,15 +2,15 @@ import { Configuration, OpenAIApi } from "openai";
|
|
2 |
import { GoogleCustomSearch } from "openai-function-calling-tools";
|
3 |
|
4 |
export default function handler(req, res) {
|
5 |
-
if (req.method !== '
|
6 |
res.status(405).send({ error: 'Method Not Allowed', method: req.method });
|
7 |
return;
|
8 |
}
|
9 |
|
10 |
-
const QUESTION = req.
|
11 |
|
12 |
if (!QUESTION) {
|
13 |
-
res.status(400).send({ error: 'Question is missing in request
|
14 |
return;
|
15 |
}
|
16 |
|
|
|
2 |
import { GoogleCustomSearch } from "openai-function-calling-tools";
|
3 |
|
4 |
export default function handler(req, res) {
|
5 |
+
if (req.method !== 'GET') {
|
6 |
res.status(405).send({ error: 'Method Not Allowed', method: req.method });
|
7 |
return;
|
8 |
}
|
9 |
|
10 |
+
const QUESTION = req.query.question;
|
11 |
|
12 |
if (!QUESTION) {
|
13 |
+
res.status(400).send({ error: 'Question is missing in request' });
|
14 |
return;
|
15 |
}
|
16 |
|