How to build an AI search engine inspired by Perplexity with Next.js and Together AI
<input>
and control it using some new React state:
/getSources
:
/getSources
:
app/api/getSources/route.js
file:
.env.local
:
/api/getAnswer
, passing along the question and sources in the request body:
/api/getAnswer
. Let’s create the second route!
Make a newapp/api/getAnswer/route.js
file:
jsdom
and @mozilla/readability
libraries:
getTextFromURL
function:
Promise.all
to kick off our functions in parallel:
answer
to store the text from our LLM:
ChatCompletionStream
helper from Together’s SDK to read the stream and update our answer
state with each new chunk:
getAnswer
endpoint responds with the first chunk, we’ll see the answer text start streaming into our UI!
The core features of our app are working great.