matt HOFFNER commited on
Commit
d92a0cd
·
1 Parent(s): 29df9bc

move to server

Browse files
src/app/search/web/page.jsx CHANGED
@@ -1,6 +1,5 @@
1
  "use client"
2
  import { useEffect, useState } from "react";
3
- import openai from 'openai';
4
  import WebSearchResults from "@/components/WebSearchResults";
5
  import Link from "next/link";
6
 
@@ -11,20 +10,19 @@ export default function WebSearchPage({ searchParams }) {
11
 
12
  useEffect(() => {
13
  async function fetchData() {
14
- const response = await fetch(
15
- `https://www.googleapis.com/customsearch/v1?key=${process.env.API_KEY}&cx=${process.env.CONTEXT_KEY}&q=${searchParams.searchTerm}&start=${startIndex}`
16
- );
17
-
18
  if (!response.ok) {
19
  console.log(response);
20
  throw new Error("Something went wrong");
21
  }
22
-
23
  const data = await response.json();
24
  setResults(data.items);
25
 
 
26
  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?`;
27
 
 
28
  const openaiRes = new EventSource('/api/llm', {
29
  method: 'POST',
30
  headers: {
@@ -33,10 +31,10 @@ export default function WebSearchPage({ searchParams }) {
33
  body: JSON.stringify({ aiPrompt }),
34
  });
35
 
 
36
  openaiRes.onmessage = function(event) {
37
  setAiResponse(aiResponse => aiResponse + event.data);
38
  };
39
-
40
  }
41
 
42
  fetchData();
 
1
  "use client"
2
  import { useEffect, useState } from "react";
 
3
  import WebSearchResults from "@/components/WebSearchResults";
4
  import Link from "next/link";
5
 
 
10
 
11
  useEffect(() => {
12
  async function fetchData() {
13
+ // Fetch Google search results via server-side route
14
+ const response = await fetch(`/api/search?searchTerm=${searchParams.searchTerm}&start=${startIndex}`);
 
 
15
  if (!response.ok) {
16
  console.log(response);
17
  throw new Error("Something went wrong");
18
  }
 
19
  const data = await response.json();
20
  setResults(data.items);
21
 
22
+ // Prepare AI prompt
23
  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?`;
24
 
25
+ // Fetch AI response via server-side route
26
  const openaiRes = new EventSource('/api/llm', {
27
  method: 'POST',
28
  headers: {
 
31
  body: JSON.stringify({ aiPrompt }),
32
  });
33
 
34
+ // Listen for AI responses and append to state
35
  openaiRes.onmessage = function(event) {
36
  setAiResponse(aiResponse => aiResponse + event.data);
37
  };
 
38
  }
39
 
40
  fetchData();
src/pages/api/google.js ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // pages/api/search.js
2
+
3
+ export default async function handler(req, res) {
4
+ const { searchTerm, startIndex = "1" } = req.query;
5
+
6
+ if (!searchTerm) {
7
+ res.status(400).json({ error: 'Search term is required' });
8
+ return;
9
+ }
10
+
11
+ const response = await fetch(
12
+ `https://www.googleapis.com/customsearch/v1?key=${process.env.API_KEY}&cx=${process.env.CONTEXT_KEY}&q=${searchTerm}&start=${startIndex}`
13
+ );
14
+
15
+ if (!response.ok) {
16
+ res.status(response.status).json({ error: 'Failed to fetch search results' });
17
+ return;
18
+ }
19
+
20
+ const data = await response.json();
21
+
22
+ res.status(200).json(data);
23
+ }
24
+