matt HOFFNER commited on
Commit
e4fae68
·
1 Parent(s): 2016044

refactor to get request

Browse files
Files changed (2) hide show
  1. src/app/search/web/page.jsx +13 -31
  2. 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
- async function fetchData() {
13
- // Fetch Google search results via server-side route
14
- /*
15
- const response = await fetch(`/api/search?searchTerm=${searchParams.searchTerm}&start=${startIndex}`);
16
- if (!response.ok) {
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
- // Prepare AI prompt
25
- // const aiPrompt = `You're creating a search engine experience. You got the following search results for the term "${searchParams.searchTerm}": ${JSON.stringify(data.items)}. How can you present these results in a helpful way?`;
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
- fetchData();
 
 
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 !== 'POST') {
6
  res.status(405).send({ error: 'Method Not Allowed', method: req.method });
7
  return;
8
  }
9
 
10
- const QUESTION = req.body.question;
11
 
12
  if (!QUESTION) {
13
- res.status(400).send({ error: 'Question is missing in request body' });
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