Vector search with Next.js and OpenAI
Learn how to build a ChatGPT-style doc search powered by Next.js, OpenAI, and Supabase.
While our Headless Vector search provides a toolkit for generative Q&A, in this tutorial we'll go more in-depth, build a custom ChatGPT-like search experience from the ground-up using Next.js. You will:
- Convert your markdown into embeddings using OpenAI.
- Store you embeddings in Postgres using pgvector.
- Deploy a function for answering your users' questions.
You can read our Supabase Clippy blog post for a full example.
We assume that you have a Next.js project with a collection of .mdx files nested inside your pages directory. We will start developing locally with the Supabase CLI and then push our local database changes to our hosted Supabase project. You can find the full Next.js example on GitHub.
Create a project
- Create a new project in the Supabase Dashboard.
- Enter your project details.
- Wait for the new database to launch.
Prepare the database
Let's prepare the database schema. We can use the "OpenAI Vector Search" quickstart in the SQL Editor, or you can copy/paste the SQL below and run it yourself.
- Go to the SQL Editor page in the Dashboard.
- Click OpenAI Vector Search.
- Click Run.
Pre-process the knowledge base at build time
With our database set up, we need to process and store all .mdx files in the pages directory. You can find the full script here, or follow the steps below:
Generate Embeddings
Create a new file lib/generate-embeddings.ts and copy the code over from GitHub.
1curl \2https://raw.githubusercontent.com/supabase-community/nextjs-openai-doc-search/main/lib/generate-embeddings.ts \3-o "lib/generate-embeddings.ts"Set up environment variables
We need some environment variables to run the script. Add them to your .env file and make sure your .env file is not committed to source control!
You can get your local Supabase credentials by running supabase status.
1NEXT_PUBLIC_SUPABASE_URL=2NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=3SUPABASE_SERVICE_ROLE_KEY=45# Get your key at https://platform.openai.com/account/api-keys6OPENAI_API_KEY=Run script at build time
Include the script in your package.json script commands to enable Vercel to automatically run it at build time.
1"scripts": {2 "dev": "next dev",3 "build": "pnpm run embeddings && next build",4 "start": "next start",5 "embeddings": "tsx lib/generate-embeddings.ts"6},Create text completion with OpenAI API
Anytime a user asks a question, we need to create an embedding for their question, perform a similarity search, and then send a text completion request to the OpenAI API with the query and then context content merged together into a prompt.
All of this is glued together in a Vercel Edge Function, the code for which can be found on GitHub.
Create Embedding for Question
In order to perform similarity search we need to turn the question into an embedding.
1const embeddingResponse = await fetch('https://api.openai.com/v1/embeddings', {2 method: 'POST',3 headers: {4 Authorization: `Bearer ${openAiKey}`,5 'Content-Type': 'application/json',6 },7 body: JSON.stringify({8 model: 'text-embedding-ada-002',9 input: sanitizedQuery.replaceAll('\n', ' '),10 }),11})1213if (embeddingResponse.status !== 200) {14 throw new ApplicationError('Failed to create embedding for question', embeddingResponse)15}1617const {18 data: [{ embedding }],19} = await embeddingResponse.json()Perform similarity search
Using the embeddingResponse we can now perform similarity search by performing an remote procedure call (RPC) to the database function we created earlier.
1const { error: matchError, data: pageSections } = await supabaseClient.rpc(2 'match_page_sections',3 {4 embedding,5 match_threshold: 0.78,6 match_count: 10,7 min_content_length: 50,8 }9)Perform text completion request
With the relevant content for the user's question identified, we can now build the prompt and make a text completion request via the OpenAI API.
If successful, the OpenAI API will respond with a text/event-stream response that we can forward to the client where we'll process the event stream to smoothly print the answer to the user.
1const prompt = codeBlock`2 ${oneLine`3 You are a very enthusiastic Supabase representative who loves4 to help people! Given the following sections from the Supabase5 documentation, answer the question using only that information,6 outputted in markdown format. If you are unsure and the answer7 is not explicitly written in the documentation, say8 "Sorry, I don't know how to help with that."9 `}1011 Context sections:12 ${contextText}1314 Question: """15 ${sanitizedQuery}16 """1718 Answer as markdown (including related code snippets if available):19`2021const completionOptions: CreateCompletionRequest = {22 model: 'gpt-3.5-turbo-instruct',23 prompt,24 max_tokens: 512,25 temperature: 0,26 stream: true,27}2829const response = await fetch('https://api.openai.com/v1/completions', {30 method: 'POST',31 headers: {32 Authorization: `Bearer ${openAiKey}`,33 'Content-Type': 'application/json',34 },35 body: JSON.stringify(completionOptions),36})3738if (!response.ok) {39 const error = await response.json()40 throw new ApplicationError('Failed to generate completion', error)41}4243// Proxy the streamed SSE response from OpenAI44return new Response(response.body, {45 headers: {46 'Content-Type': 'text/event-stream',47 },48})Display the answer on the frontend
In a last step, we need to process the event stream from the OpenAI API and print the answer to the user. The full code for this can be found on GitHub.
1const handleConfirm = React.useCallback(2 async (query: string) => {3 setAnswer(undefined)4 setQuestion(query)5 setSearch('')6 dispatchPromptData({ index: promptIndex, answer: undefined, query })7 setHasError(false)8 setIsLoading(true)910 const eventSource = new SSE(`api/vector-search`, {11 headers: {12 apikey: process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY ?? '',13 Authorization: `Bearer ${process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY}`,14 'Content-Type': 'application/json',15 },16 payload: JSON.stringify({ query }),17 })1819 function handleError<T>(err: T) {20 setIsLoading(false)21 setHasError(true)22 console.error(err)23 }2425 eventSource.addEventListener('error', handleError)26 eventSource.addEventListener('message', (e: any) => {27 try {28 setIsLoading(false)2930 if (e.data === '[DONE]') {31 setPromptIndex((x) => {32 return x + 133 })34 return35 }3637 const completionResponse: CreateCompletionResponse = JSON.parse(e.data)38 const text = completionResponse.choices[0].text3940 setAnswer((answer) => {41 const currentAnswer = answer ?? ''4243 dispatchPromptData({44 index: promptIndex,45 answer: currentAnswer + text,46 })4748 return (answer ?? '') + text49 })50 } catch (err) {51 handleError(err)52 }53 })5455 eventSource.stream()5657 eventSourceRef.current = eventSource5859 setIsLoading(true)60 },61 [promptIndex, promptData]62)Learn more
Want to learn more about the awesome tech that is powering this?
- Read about how we built ChatGPT for the Supabase Docs.
- Read the pgvector Docs for Embeddings and vector similarity
- Watch Greg's video for a full breakdown: