This is a Next.js project bootstrapped with create-next-app
.
First is to update the environment variables
OPENAI_API_KEY=""
SUPABASE_URL=""
SUPABASE_SERVICE_KEY=""
For the first time run of the application, install all the packages:
npm install
Run the application locally
npm run dev
Run the scripts in your supabase account to allow pg_vector support and prepare the database table
SQL Script
-- Enable the pgvector extension to work with embedding vectors
create extension vector;
-- Create a table to store your documents
create table documents (
id bigserial primary key,
content text, -- corresponds to Document.pageContent
metadata jsonb, -- corresponds to Document.metadata
embedding vector(1536) -- 1536 works for OpenAI embeddings, change if needed
);
-- Create a function to search for documents
create function match_documents (
query_embedding vector(1536),
match_count int DEFAULT null,
filter jsonb DEFAULT '{}'
) returns table (
id bigint,
content text,
metadata jsonb,
embedding jsonb,
similarity float
)
language plpgsql
as $$
#variable_conflict use_column
begin
return query
select
id,
content,
metadata,
(embedding::text)::jsonb as embedding,
1 - (documents.embedding <=> query_embedding) as similarity
from documents
where metadata @> filter
order by documents.embedding <=> query_embedding
limit match_count;
end;
$$;
npx create-next-app@latest
npm i @tanstack/react-query
npm install --save openai
npm install @supabase/supabase-js
npm install pdf-parse
npm install --save langchain @langchain/openai @langchain/community
We will be using tanstack to run server actions using mutations
npm install --save-dev prettier-eslint
Add a prettier config file `.prettierrc`
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": false,
"singleQuote": true
}
npx shadcn-ui@latest init
npx shadcn-ui@latest add button card input textarea