Spaces:
Paused
Paused
matt HOFFNER
commited on
Commit
·
e772681
1
Parent(s):
a8cb4d1
coding without thinking
Browse files- src/app/search/web/page.jsx +14 -14
src/app/search/web/page.jsx
CHANGED
@@ -15,13 +15,13 @@ export default function WebSearchPage({ searchParams }) {
|
|
15 |
const signal = controller.signal;
|
16 |
|
17 |
async function fetchData() {
|
18 |
-
const response = await fetch(
|
19 |
method: 'POST',
|
20 |
headers: { 'Content-Type': 'application/json' },
|
21 |
body: JSON.stringify({ question: searchTerm || "Seattle activities this weekend" }),
|
22 |
signal,
|
23 |
});
|
24 |
-
|
25 |
if (!response.ok) {
|
26 |
throw new Error(`HTTP error! status: ${response.status}`);
|
27 |
} else {
|
@@ -29,23 +29,23 @@ export default function WebSearchPage({ searchParams }) {
|
|
29 |
const decoder = new TextDecoder();
|
30 |
let text = '';
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
const json = JSON.parse(text);
|
39 |
console.log(json);
|
40 |
setAiResponse(json);
|
41 |
console.log("Stream complete");
|
42 |
-
|
43 |
-
console.error("Failed to parse JSON", error);
|
44 |
}
|
45 |
-
|
|
|
46 |
}
|
47 |
-
|
48 |
-
|
49 |
}
|
50 |
}
|
51 |
}
|
@@ -55,7 +55,7 @@ export default function WebSearchPage({ searchParams }) {
|
|
55 |
});
|
56 |
|
57 |
return () => controller.abort();
|
58 |
-
}, [searchParams, searchTerm]);
|
59 |
|
60 |
|
61 |
console.log(aiResponse);
|
|
|
15 |
const signal = controller.signal;
|
16 |
|
17 |
async function fetchData() {
|
18 |
+
const response = await fetch('/api/llm', {
|
19 |
method: 'POST',
|
20 |
headers: { 'Content-Type': 'application/json' },
|
21 |
body: JSON.stringify({ question: searchTerm || "Seattle activities this weekend" }),
|
22 |
signal,
|
23 |
});
|
24 |
+
|
25 |
if (!response.ok) {
|
26 |
throw new Error(`HTTP error! status: ${response.status}`);
|
27 |
} else {
|
|
|
29 |
const decoder = new TextDecoder();
|
30 |
let text = '';
|
31 |
|
32 |
+
try {
|
33 |
+
while (true) {
|
34 |
+
const { value, done } = await reader.read();
|
35 |
+
|
36 |
+
if (done) {
|
37 |
+
// When the stream ends, we can parse the complete text
|
38 |
const json = JSON.parse(text);
|
39 |
console.log(json);
|
40 |
setAiResponse(json);
|
41 |
console.log("Stream complete");
|
42 |
+
break;
|
|
|
43 |
}
|
44 |
+
|
45 |
+
text += decoder.decode(value, {stream: true});
|
46 |
}
|
47 |
+
} catch (error) {
|
48 |
+
console.error("Failed to parse JSON", error);
|
49 |
}
|
50 |
}
|
51 |
}
|
|
|
55 |
});
|
56 |
|
57 |
return () => controller.abort();
|
58 |
+
}, [searchParams, searchTerm]);
|
59 |
|
60 |
|
61 |
console.log(aiResponse);
|