matt HOFFNER commited on
Commit
e772681
·
1 Parent(s): a8cb4d1

coding without thinking

Browse files
Files changed (1) hide show
  1. 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(`/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,23 +29,23 @@ export default function WebSearchPage({ searchParams }) {
29
  const decoder = new TextDecoder();
30
  let text = '';
31
 
32
- while (true) {
33
- const { value, done } = await reader.read();
34
-
35
- if (done) {
36
- // When the stream ends, we can parse the complete text
37
- try {
38
  const json = JSON.parse(text);
39
  console.log(json);
40
  setAiResponse(json);
41
  console.log("Stream complete");
42
- } catch (error) {
43
- console.error("Failed to parse JSON", error);
44
  }
45
- break;
 
46
  }
47
-
48
- text += decoder.decode(value);
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);