Create a production-ready MVP for securely chatting with your documents.
- Interactive Chat Interface: Interact with your documentation, leveraging the capabilities of OpenAIโs GPT models and retrieval augmented generation (RAG).
- Login With <3rd Party>: Integrate one-click 3rd party login with any of our 18 auth providers and user/password.
- Document Storage: Securely upload, store, and retrieve user uploaded documents.
- REST API: Expose a flexible REST API that weโll consume to build the interactive front-end.
- Row-level Security: Secure all of your user data user data with production-ready row-level security.
Thanks for joining! Let's dive in.
-
Clone repo: Clone this repo at tag
step-1
:git clone -b step-1 https://github.com/supabase-community/chatgpt-your-files.git
This will automatically clone at step 1, our starting point.
-
Git checkpoints: The workshop is broken down into steps (git tags). There's a step for every major feature we are building.
Feel free to follow along live with the presenter. When it's time to jump to the next step, run:
git stash push -u # stash your working directory git checkout step-X # jump to a checkpoint (replace X wit step #)
-
Step-by-step guide: These steps are written out line-by-line. Feel free to follow along using the steps below.
If you can't make the full workshop, no worries! We'll be putting out a YouTube video later that goes over the same content.
If you'd like to get notified when the video is available, feel free to drop your name and email on this form and we'll send you the link!
- Unix-based OS (if Windows, WSL2)
- Docker
- Node.js 18+
This repository includes 3 sample markdown files that we'll use to test the app:
./sample-files/roman-empire-1.md
./sample-files/roman-empire-2.md
./sample-files/roman-empire-3.md
Jump to a step:
Step 0 - Setup (Optional)
Use this command to jump to the step-0
checkpoint.
git checkout step-0
The beginning of step 0 is aka to:
npx create-next-app -e with-supabase
Refer to this step if you want to learn about the additions added on top of create-next-app
to get us up and running quicker for this workshop (VS Code settings, UI components/styles/layouts). Otherwise, skip straight to step-1
.
-
Install Supabase as dev dependency.
npm i -D [email protected]
-
(Optional) Setup VSCode environment.
mkdir -p .vscode && cat > .vscode/settings.json <<- EOF { "deno.enable": true, "deno.lint": true, "deno.unstable": false, "deno.enablePaths": [ "supabase" ], "deno.importMap": "./supabase/functions/import_map.json" } EOF
-
(Optional) Setup VSCode recommended extensions.
cat > .vscode/extensions.json <<- EOF { "recommendations": [ "denoland.vscode-deno", "esbenp.prettier-vscode", "dbaeumer.vscode-eslint", "bradlc.vscode-tailwindcss", ], } EOF
Then
cmd
+shift
+p
โ>show recommended extensions
โ install all (or whichever you like) -
Create
import_map.json
with dependencies for our Supabase Edge Functions. We'll talk more about this in step 2.cat > supabase/functions/import_map.json <<- EOF { "imports": { "@std/": "https://deno.land/[email protected]/", "@xenova/transformers": "https://cdn.jsdelivr.net/npm/@xenova/[email protected]", "@supabase/supabase-js": "https://esm.sh/@supabase/[email protected]", "openai": "https://esm.sh/[email protected]", "common-tags": "https://esm.sh/[email protected]", "ai": "https://esm.sh/[email protected]", "mdast-util-from-markdown": "https://esm.sh/[email protected]", "mdast-util-to-markdown": "https://esm.sh/[email protected]", "mdast-util-to-string": "https://esm.sh/[email protected]", "unist-builder": "https://esm.sh/[email protected]", "mdast": "https://esm.sh/v132/@types/[email protected]/index.d.ts", "https://esm.sh/v132/[email protected]/esnext/decode-named-character-reference.mjs": "https://esm.sh/[email protected]?target=deno" } } EOF
We use shadcn/ui
for our UI components.
-
Initialize
shadcn-ui
.npx shadcn-ui@latest init
Would you like to use TypeScript (recommended)? yes Which style would you like to use? โบ Default Which color would you like to use as base color? โบ Slate Where is your global CSS file? โบ โบ app/globals.css Do you want to use CSS variables for colors? โบ yes Where is your tailwind.config.js located? โบ tailwind.config.js Configure the import alias for components: โบ @/components Configure the import alias for utils: โบ @/lib/utils Are you using React Server Components? โบ yes
-
Add components.
npx shadcn-ui@latest add button input toast
-
Install dependencies.
npm i @tanstack/react-query three-dots
-
Wrap the app in a
<QueryClientProvider>
. -
Build layouts.
Use this command to jump to the step-1
checkpoint.
git checkout step-1
We'll start by handling file uploads. Supabase has a built-in object storage (backed by S3 under the hood) that integrates directly with your Postgres database.
First install NPM dependencies.
npm i
-
Next initialize and start a local version of Supabase (runs in Docker).
npx supabase init npx supabase start
-
Store Supabase URL & public anon key in
.env.local
for Next.js.npx supabase status -o env \ --override-name api.url=NEXT_PUBLIC_SUPABASE_URL \ --override-name auth.anon_key=NEXT_PUBLIC_SUPABASE_ANON_KEY | grep NEXT_PUBLIC > .env.local
-
Create migration file.
npx supabase migration new files
A new file will be created under
./supabase/migrations
. -
Within that file, create a private schema.
create schema private;
-
Add bucket called 'files' via the
buckets
table in thestorage
schema.insert into storage.buckets (id, name) values ('files', 'files');
-
Add RLS policies to restrict access to files.
create policy "Authenticated users can upload files" on storage.objects for insert to authenticated with check ( bucket_id = 'files' and owner = auth.uid() ); create policy "Users can view their own files" on storage.objects for select to authenticated using ( bucket_id = 'files' and owner = auth.uid() ); create policy "Users can update their own files" on storage.objects for update to authenticated with check ( bucket_id = 'files' and owner = auth.uid() ); create policy "Users can delete their own files" on storage.objects for delete to authenticated using ( bucket_id = 'files' and owner = auth.uid() );
Next let's update ./app/files/page.tsx
to support file upload.
-
Setup Supabase client at the top of the component.
const supabase = createClientComponentClient();
-
Handle file upload in the
<Input>
'sonChange
prop.await supabase.storage .from('files') .upload(`${crypto.randomUUID()}/${selectedFile.name}`, selectedFile);
We can improve our previous RLS policy to require a UUID in the uploaded file path.
-
Create
uuid_or_null()
function.create or replace function private.uuid_or_null(str text) returns uuid language plpgsql as $$ begin return str::uuid; exception when invalid_text_representation then return null; end; $$;
-
Modify insert policy to check for UUID in the first path segment (Postgres arrays are 1-based).
create policy "Authenticated users can upload files" on storage.objects for insert to authenticated with check ( bucket_id = 'files' and owner = auth.uid() and private.uuid_or_null(path_tokens[1]) is not null );
-
Apply the migration to our local database.
npx supabase migration up
Jump to a step:
Use these commands to jump to the step-2
checkpoint.
git stash push -u -m "my work on step-1"
git checkout step-2
Next we'll need to process our files for retrieval augmented generation (RAG). Specifically we'll split the contents of our markdown documents by heading, which will allow us to query smaller and more meaningful sections.
Let's create a documents
and document_sections
table to store our processed files.
-
Create migration file.
npx supabase migration new documents
-
Enable
pgvector
andpg_net
extensions.We'll use
pg_net
later to send HTTP requests to our edge functions.create extension if not exists pg_net with schema extensions; create extension if not exists vector with schema extensions;
-
Create
documents
table.create table documents ( id bigint primary key generated always as identity, name text not null, storage_object_id uuid not null references storage.objects (id), created_by uuid not null references auth.users (id) default auth.uid(), created_at timestamp with time zone not null default now() );
-
We'll also create a view
documents_with_storage_path
that provides easy access to the storage object path.create view documents_with_storage_path with (security_invoker=true) as select documents.*, storage.objects.name as storage_object_path from documents join storage.objects on storage.objects.id = documents.storage_object_id;
-
Create
document_sections
table.create table document_sections ( id bigint primary key generated always as identity, document_id bigint not null references documents (id), content text not null, embedding vector (384) );
-
Add HNSW index.
Unlike IVFFlat indexes, HNSW indexes can be create immediately on an empty table.
create index on document_sections using hnsw (embedding vector_ip_ops);
-
Setup RLS to control who has access to which documents.
alter table documents enable row level security; alter table document_sections enable row level security; create policy "Users can insert documents" on documents for insert to authenticated with check ( auth.uid() = created_by ); create policy "Users can query their own documents" on documents for select to authenticated using ( auth.uid() = created_by ); create policy "Users can insert document sections" on document_sections for insert to authenticated with check ( document_id in ( select id from documents where created_by = auth.uid() ) ); create policy "Users can update their own document sections" on document_sections for update to authenticated using ( document_id in ( select id from documents where created_by = auth.uid() ) ) with check ( document_id in ( select id from documents where created_by = auth.uid() ) ); create policy "Users can query their own document sections" on document_sections for select to authenticated using ( document_id in ( select id from documents where created_by = auth.uid() ) );
-
Add
supabase_url
secret to./supabase/seed.sql
. We will use this to query our Edge Functions within our local environment. In production, set this to your Supabase project's API URL.select vault.create_secret( 'http://api.supabase.internal:8000', 'supabase_url' );
-
Create a function to retrieve the URL.
create function supabase_url() returns text language plpgsql security definer as $$ declare secret_value text; begin select decrypted_secret into secret_value from vault.decrypted_secrets where name = 'supabase_url'; return secret_value; end; $$;
-
Create a trigger to process new documents when they're inserted. This uses
pg_net
to send an HTTP request to our Edge Function (coming up next).create function private.handle_storage_update() returns trigger language plpgsql as $$ declare document_id bigint; result int; begin insert into documents (name, storage_object_id, created_by) values (new.path_tokens[2], new.id, new.owner) returning id into document_id; select net.http_post( url := supabase_url() || '/functions/v1/process', headers := jsonb_build_object( 'Content-Type', 'application/json', 'Authorization', current_setting('request.headers')::json->>'authorization' ), body := jsonb_build_object( 'document_id', document_id ) ) into result; return null; end; $$; create trigger on_file_upload after insert on storage.objects for each row execute procedure private.handle_storage_update();
-
Apply the migration to our local database.
npx supabase migration up
-
Create the Edge Function file.
npx supabase functions new process
This will create the file
./supabase/functions/process/index.ts
.Make sure you have the latest version of
deno
installedbrew install deno
-
First let's note how dependencies are resolved using an import map -
./supabase/functions/import_map.json
.Import maps aren't required in Deno, but they can simplify imports and keep dependency versions consistent. All of our dependencies come from NPM, but since we're using Deno we fetch them from a CDN like https://esm.sh or https://cdn.jsdelivr.net.
{ "imports": { "@std/": "https://deno.land/[email protected]/", "@xenova/transformers": "https://cdn.jsdelivr.net/npm/@xenova/[email protected]", "@supabase/supabase-js": "https://esm.sh/@supabase/[email protected]", "openai": "https://esm.sh/[email protected]", "common-tags": "https://esm.sh/[email protected]", "ai": "https://esm.sh/[email protected]", "mdast-util-from-markdown": "https://esm.sh/[email protected]", "mdast-util-to-markdown": "https://esm.sh/[email protected]", "mdast-util-to-string": "https://esm.sh/[email protected]", "unist-builder": "https://esm.sh/[email protected]", "mdast": "https://esm.sh/v132/@types/[email protected]/index.d.ts", "https://esm.sh/v132/[email protected]/esnext/decode-named-character-reference.mjs": "https://esm.sh/[email protected]?target=deno" } }
Note: URL based imports and import maps aren't a Deno invention. These are a web standard that Deno follows as closely as possible.
-
In
process/index.ts
, first grab the Supabase environment variables.import { createClient } from '@supabase/supabase-js'; import { processMarkdown } from '../_lib/markdown-parser.ts'; // These are automatically injected const supabaseUrl = Deno.env.get('SUPABASE_URL'); const supabaseAnonKey = Deno.env.get('SUPABASE_ANON_KEY'); Deno.serve(async (req) => { if (!supabaseUrl || !supabaseAnonKey) { return new Response( JSON.stringify({ error: 'Missing environment variables.', }), { status: 500, headers: { 'Content-Type': 'application/json' }, } ); } });
Note: These environment variables are automatically injected into the edge runtime for you. Even so, we check for their existence as a TypeScript best practice (type narrowing).
-
(Optional) If you are using VS Code, you may get prompted to cache your imported dependencies. You can do this by hitting
cmd
+shift
+p
and type>Deno: Cache Dependencies
. -
Create Supabase client and configure it to inherit the original userโs permissions via the authorization header. This way we can continue to take advantage of our RLS policies.
const authorization = req.headers.get('Authorization'); if (!authorization) { return new Response( JSON.stringify({ error: `No authorization header passed` }), { status: 500, headers: { 'Content-Type': 'application/json' }, } ); } const supabase = createClient(supabaseUrl, supabaseAnonKey, { global: { headers: { authorization, }, }, auth: { persistSession: false, }, });
-
Grab the
document_id
from the request body and query it.const { document_id } = await req.json(); const { data: document } = await supabase .from('documents_with_storage_path') .select() .eq('id', document_id) .single(); if (!document?.storage_object_path) { return new Response( JSON.stringify({ error: 'Failed to find uploaded document' }), { status: 500, headers: { 'Content-Type': 'application/json' }, } ); }
-
Use the Supabase client to download the file by storage path.
const { data: file } = await supabase.storage .from('files') .download(document.storage_object_path); if (!file) { return new Response( JSON.stringify({ error: 'Failed to download storage object' }), { status: 500, headers: { 'Content-Type': 'application/json' }, } ); } const fileContents = await file.text();
-
Process the markdown file and store the resulting subsections into the
document_sections
table.Note:
processMarkdown()
is pre-built into this repository for convenience. Feel free to read through its code to learn how it splits the markdown content.const processedMd = processMarkdown(fileContents); const { error } = await supabase.from('document_sections').insert( processedMd.sections.map(({ content }) => ({ document_id, content, })) ); if (error) { console.error(error); return new Response( JSON.stringify({ error: 'Failed to save document sections' }), { status: 500, headers: { 'Content-Type': 'application/json' }, } ); } console.log( `Saved ${processedMd.sections.length} sections for file '${document.name}'` );
-
Return a success response.
return new Response(null, { status: 204, headers: { 'Content-Type': 'application/json' }, });
-
In a new terminal we'll serve the edge functions locally.
npx supabase functions serve
Note: Local Edge Functions are automatically served as part of
npx supabase start
, but this command allows us to also monitor their logs.
Let's update ./app/files/page.tsx
to list out the uploaded documents.
-
At the top of the component, fetch documents using the
useQuery
hook:const { data: documents } = useQuery(['files'], async () => { const { data, error } = await supabase .from('documents_with_storage_path') .select(); if (error) { toast({ variant: 'destructive', description: 'Failed to fetch documents', }); throw error; } return data; });
-
In each document's
onClick
handler, download the respective file.const { data, error } = await supabase.storage .from('files') .createSignedUrl(document.storage_object_path, 60); if (error) { toast({ variant: 'destructive', description: 'Failed to download file. Please try again.', }); return; } window.location.href = data.signedUrl;
Jump to a step:
Use these commands to jump to the step-3
checkpoint.
git stash push -u -m "my work on step-2"
git checkout step-3
Now let's add logic to generate embeddings automatically anytime new rows are added to the document_sections
table.
-
Create migration file
npx supabase migration new embed
-
Create
embed()
trigger function. We'll use this general purpose trigger function to asynchronously generate embeddings on arbitrary tables using a newembed
Edge Function (coming up).create function private.embed() returns trigger language plpgsql as $$ declare content_column text = TG_ARGV[0]; embedding_column text = TG_ARGV[1]; batch_size int = TG_ARGV[2]; batch_count int = ceiling((select count(*) from inserted) / batch_size::float); result int; begin for i in 0 .. (batch_count-1) loop select net.http_post( url := supabase_url() || '/functions/v1/embed', headers := jsonb_build_object( 'Content-Type', 'application/json', 'Authorization', current_setting('request.headers')::json->>'authorization' ), body := jsonb_build_object( 'ids', (select json_agg(ds.id) from (select id from inserted limit batch_size offset i*batch_size) ds), 'table', TG_TABLE_NAME, 'contentColumn', content_column, 'embeddingColumn', embedding_column ) ) into result; end loop; return null; end; $$;
-
Add embed trigger to
document_sections
tablecreate trigger embed_document_sections after insert on document_sections referencing new table as inserted for each statement execute procedure private.embed(content, embedding, 10);
Note we pass 3 arguments to
embed()
:- The first specifies which column contains the text content to embed.
- The second specifies the destination column to save the embedding into.
- The third specifies the number of records to include in each edge function call.
-
Apply the migration to our local database.
npx supabase migration up
-
Create edge function file
npx supabase functions new embed
-
In
embed/index.ts
, create an embedding pipeline using Transformers.js.import { createClient } from '@supabase/supabase-js'; import { env, pipeline } from '@xenova/transformers'; // Configuration for Deno runtime env.useBrowserCache = false; env.allowLocalModels = false; const generateEmbedding = await pipeline( 'feature-extraction', 'Supabase/gte-small' );
-
Just like before, grab the Supabase variables and check for their existence (type narrowing).
// These are automatically injected const supabaseUrl = Deno.env.get('SUPABASE_URL'); const supabaseAnonKey = Deno.env.get('SUPABASE_ANON_KEY'); Deno.serve(async (req) => { if (!supabaseUrl || !supabaseAnonKey) { return new Response( JSON.stringify({ error: 'Missing environment variables.', }), { status: 500, headers: { 'Content-Type': 'application/json' }, } ); } });
-
Create a Supabase client and configure to inherit the userโs permissions.
const authorization = req.headers.get('Authorization'); if (!authorization) { return new Response( JSON.stringify({ error: `No authorization header passed` }), { status: 500, headers: { 'Content-Type': 'application/json' }, } ); } const supabase = createClient(supabaseUrl, supabaseAnonKey, { global: { headers: { authorization, }, }, auth: { persistSession: false, }, });
-
Fetch the text content from the specified table/column.
const { ids, table, contentColumn, embeddingColumn } = await req.json(); const { data: rows, error: selectError } = await supabase .from(table) .select(`id, ${contentColumn}` as '*') .in('id', ids) .is(embeddingColumn, null); if (selectError) { return new Response(JSON.stringify({ error: selectError }), { status: 500, headers: { 'Content-Type': 'application/json' }, }); }
-
Generate an embedding for each piece of text and update the respective rows.
for (const row of rows) { const { id, [contentColumn]: content } = row; if (!content) { console.error(`No content available in column '${contentColumn}'`); continue; } const output = await generateEmbedding(content, { pooling: 'mean', normalize: true, }); const embedding = JSON.stringify(Array.from(output.data)); const { error } = await supabase .from(table) .update({ [embeddingColumn]: embedding, }) .eq('id', id); if (error) { console.error( `Failed to save embedding on '${table}' table with id ${id}` ); } console.log( `Generated embedding ${JSON.stringify({ table, id, contentColumn, embeddingColumn, })}` ); }
-
Return a success response.
return new Response(null, { status: 204, headers: { 'Content-Type': 'application/json' }, });
Jump to a step:
Use these commands to jump to the step-4
checkpoint.
git stash push -u -m "my work on step-3"
git checkout step-4
Finally, let's implement the chat functionality. For this workshop, we're going to generate our query embedding client side using a new custom hook called usePipeline()
.
-
Install dependencies
npm i @xenova/transformers ai
-
Configure
next.config.js
to support Transformers.jswebpack: (config) => { config.resolve.alias = { ...config.resolve.alias, sharp$: false, 'onnxruntime-node$': false, }; return config; },
-
Import dependencies
import { usePipeline } from '@/lib/hooks/use-pipeline'; import { createClientComponentClient } from '@supabase/auth-helpers-nextjs'; import { useChat } from 'ai/react';
Note:
usePipeline()
was pre-built into this repository for convenience. It uses Web Workers to asynchronously generate embeddings in another thread. We'll be releasing this hook and more into a dedicated NPM package shortly. -
Create a Supabase client in
chat/page.tsx
.const supabase = createClientComponentClient();
-
Create embedding pipeline.
const generateEmbedding = usePipeline( 'feature-extraction', 'Supabase/gte-small' );
Note: it's important that the embedding model you set here matches the model used in the Edge Function, otherwise your future matching logic will be meaningless.
-
Manage chat messages and state with
useChat()
.const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({ api: `${process.env.NEXT_PUBLIC_SUPABASE_URL}/functions/v1/chat`, });
Note:
useChat()
is a convenience hook provided by Vercel'sai
package to handle chat message state and streaming. We'll point it to an Edge Function calledchat
(coming up). -
Set the ready status to
true
when pipeline has loaded.const isReady = !!generateEmbedding;
-
Connect
input
andhandleInputChange
to our<Input>
props.<Input type="text" autoFocus placeholder="Send a message" value={input} onChange={handleInputChange} />
-
Generate an embedding and submit messages on form submit.
if (!generateEmbedding) { throw new Error('Unable to generate embeddings'); } const output = await generateEmbedding(input, { pooling: 'mean', normalize: true, }); const embedding = JSON.stringify(Array.from(output.data)); const { data: { session }, } = await supabase.auth.getSession(); if (!session) { return; } handleSubmit(e, { options: { headers: { authorization: `Bearer ${session.access_token}`, }, body: { embedding, }, }, });
-
Disable send button until the component is ready.
<Button type="submit" disabled={!isReady}> Send </Button>
-
Create migration file for a new match function
npx supabase migration new match
-
Create a
match_document_sections
Postgres function.create or replace function match_document_sections( embedding vector(384), match_threshold float ) returns setof document_sections language plpgsql as $$ #variable_conflict use_variable begin return query select * from document_sections where document_sections.embedding <#> embedding < -match_threshold order by document_sections.embedding <#> embedding; end; $$;
This function uses pgvector's negative inner product operator
<#>
to perform similarity search. Inner product requires less computations than other distance functions like cosine distance<=>
, and therefore provides better query performance.Note: Our embeddings are normalized, so inner product and cosine similarity are equivalent in terms of output. Note though that pgvector's
<=>
operator is cosine distance, not cosine similarity, soinner product == 1 - cosine distance
.We also filter by a
match_threshold
in order to return only the most relevant results (1 = most similar, 0 = least similar).Note:
match_threshold
is negated because<#>
is a negative inner product. See the pgvector docs for more details on why<#>
is negative. -
Apply the migration to our local database.
npx supabase migration up
-
First generate an API key from OpenAI and save it in
supabase/functions/.env
.cat > supabase/functions/.env <<- EOF OPENAI_API_KEY=<your-api-key> EOF
-
Create the edge function file.
npx supabase functions new chat
-
Load the OpenAI and Supabase keys.
import { createClient } from '@supabase/supabase-js'; import { OpenAIStream, StreamingTextResponse } from 'ai'; import { codeBlock } from 'common-tags'; import OpenAI from 'openai'; const openai = new OpenAI({ apiKey: Deno.env.get('OPENAI_API_KEY'), }); // These are automatically injected const supabaseUrl = Deno.env.get('SUPABASE_URL'); const supabaseAnonKey = Deno.env.get('SUPABASE_ANON_KEY');
-
Since our frontend is served at a different domain origin than our Edge Function, we must handle cross origin resource sharing (CORS).
export const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', }; Deno.serve(async (req) => { // Handle CORS if (req.method === 'OPTIONS') { return new Response('ok', { headers: corsHeaders }); } });
Handle CORS simply by checking for an
OPTIONS
HTTP request and returning the CORS headers (*
= allow any domain). In production, consider limiting the origins to specific domains that serve your frontend. -
Check for environment variables and create Supabase client.
if (!supabaseUrl || !supabaseAnonKey) { return new Response( JSON.stringify({ error: 'Missing environment variables.', }), { status: 500, headers: { 'Content-Type': 'application/json' }, } ); } const authorization = req.headers.get('Authorization'); if (!authorization) { return new Response( JSON.stringify({ error: `No authorization header passed` }), { status: 500, headers: { 'Content-Type': 'application/json' }, } ); } const supabase = createClient(supabaseUrl, supabaseAnonKey, { global: { headers: { authorization, }, }, auth: { persistSession: false, }, });
-
The first step of RAG is to perform similarity search using our
match_document_sections()
function. Postgres functions are executed using the.rpc()
method.const { chatId, message, messages, embedding } = await req.json(); const { data: documents, error: matchError } = await supabase .rpc('match_document_sections', { embedding, match_threshold: 0.8, }) .select('content') .limit(5); if (matchError) { console.error(matchError); return new Response( JSON.stringify({ error: 'There was an error reading your documents, please try again.', }), { status: 500, headers: { 'Content-Type': 'application/json' }, } ); }
-
The second step of RAG is to build our prompt, injecting in the relevant documents retrieved from our previous similarity search.
const injectedDocs = documents && documents.length > 0 ? documents.map(({ content }) => content).join('\n\n') : 'No documents found'; const completionMessages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [ { role: 'user', content: codeBlock` You're an AI assistant who answers questions about documents. You're a chat bot, so keep your replies succinct. You're only allowed to use the documents below to answer the question. If the question isn't related to these documents, say: "Sorry, I couldn't find any information on that." If the information isn't available in the below documents, say: "Sorry, I couldn't find any information on that." Do not go off topic. Documents: ${injectedDocs} `, }, ...messages, ];
Note: the
codeBlock
template tag is a convenience function that will strip away indentations in our multiline string. This allows us to format our code nicely while preserving the intended indentation. -
Finally, create a completion stream and return it.
const completionStream = await openai.chat.completions.create({ model: 'gpt-3.5-turbo-0613', messages: completionMessages, max_tokens: 1024, temperature: 0, stream: true, }); const stream = OpenAIStream(completionStream); return new StreamingTextResponse(stream, { headers: corsHeaders });
OpenAIStream
andStreamingTextResponse
are convenience helpers from Vercel'sai
package that translate OpenAI's response stream into a format thatuseChat()
understands on the frontend.Note: we must also return CORS headers here (or anywhere else we send a response).
Let's try it out! Here are some questions you could try asking:
- What kind of buildings did they live in?
- What was the most common food?
- What did people do for fun?
Jump to a step:
Use these commands to jump to the step-5
checkpoint.
git stash push -u -m "my work on step-4"
git checkout step-5
You may have noticed that all of our DB data coming back from the supabase
client has had an any
type (such as documents
or document_sections
). This isn't great, since we're missing relevant type information and lose type safety (making our app more error-prone).
The Supabase CLI comes with a built-in command to generate TypeScript types based on your database's schema.
-
Generate TypeScript types based on local DB schema.
supabase gen types typescript --local > supabase/functions/_lib/database.ts
-
Add the
<Database>
generic to all Supabase clients across our project.-
In React
import { Database } from '@/supabase/functions/_lib/database'; const supabase = createClientComponentClient<Database>();
import { Database } from '@/supabase/functions/_lib/database'; const supabase = createServerComponentClient<Database>();
-
In Edge Functions
import { Database } from '../_lib/database.ts'; const supabase = createClient<Database>(...);
-
-
Fix type errors ๐
-
Looks like we found a type error in
./app/files/page.tsx
! Let's add this check to top of the document's click handler (type narrowing).if (!document.storage_object_path) { toast({ variant: 'destructive', description: 'Failed to download file, please try again.', }); return; }
-
๐ Congrats! You've built your own full stack pgvector app in 2 hours.
If you would like to jump directly to the completed app, simply checkout the main
branch:
git checkout main
Jump to a previous step:
Up until now we've been developing the app locally. Use these instructions to deploy your app to a production Supabase project.
-
Create a new Supabase project.
-
Link the CLI with your Supabase project.
npx supabase link --project-ref=<project-ref>
You can grab your project's reference ID in your projectโs settings.
-
Push migrations to remote database.
npx supabase db push
-
Set Edge Function secrets (OpenAI key).
npx supabase secrets set OPENAI_API_KEY=<openai-key>
-
Deploy Edge Functions.
npx supabase functions deploy
-
Deploy to Vercel (or CDN of your choice - must support Next.js API routes for authentication).
-
Follow Vercelโs deploy instructions.
-
Be sure to set
NEXT_PUBLIC_SUPABASE_URL
andNEXT_PUBLIC_SUPABASE_ANON_KEY
for your Supabase project.You can find these in your projectโs API settings.
-
Feel free to extend this app in any way you like. Here are some ideas for next steps:
- Record message history in the database (and generate embeddings on them for RAG memory)
- Support more file formats than just markdown
- Pull in documents from the Notion API
- Restrict chat to user-selected documents
- Perform RAG on images using CLIP embeddings
Please file feedback and issues on the on this repo's issue board.