"use client" import { useEffect, useState } from "react"; import openai from 'openai'; import WebSearchResults from "@/components/WebSearchResults"; import Link from "next/link"; export default function WebSearchPage({ searchParams }) { const [results, setResults] = useState(null); const [aiResponse, setAiResponse] = useState(null); const startIndex = searchParams.start || "1"; useEffect(() => { async function fetchData() { const response = await fetch( `https://www.googleapis.com/customsearch/v1?key=${process.env.API_KEY}&cx=${process.env.CONTEXT_KEY}&q=${searchParams.searchTerm}&start=${startIndex}` ); if (!response.ok) { console.log(response); throw new Error("Something went wrong"); } const data = await response.json(); setResults(data.items); 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?`; const openaiRes = new EventSource('/api/llm', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ aiPrompt }), }); openaiRes.onmessage = function(event) { setAiResponse(aiResponse => aiResponse + event.data); }; } fetchData(); }, [searchParams, startIndex]); if (!results) { return (

No results found

Try searching for something else or go back to the homepage{" "} Home

); } return <>{results && }; }