Legal document source: https://github.com/LawRefBook/Laws
Project template: https://github.com/supabase-community/nextjs-openai-doc-search
This project takes all .mdx files from the pages directory and processes them into a custom context for use in OpenAI text autocomplete prompts.
![]() Smart Writing Assistant | ![]() AI Encyclopedia | ![]() Prompt generator |
![]() AI Translation Expert | ❤️ Reward and sponsorship ❤️ |
Deploy this starter to Vercel. Supabase integration automatically sets the required environment variables and configures your database profile. You just need to set OPENAI_KEY and you're ready to go!
The poster is too lazy, thanks to GoJun for helping to write the tutorial: https://eibot3u32o.feishu.cn/docx/L46Pdp3fjouPUvxaNzPckKctno3
Building your own custom ChatGPT involves four steps:
.mdx file in your pages folder). Steps 1 and 2 occur at build time, such as when Vercel builds your Next.js application. At this time, the generate-embeddings script is executed, which performs the following tasks:
sequenceDiagram
participant Vercel
participant DB (pgvector)
participant OpenAI (API)
loop 1. Preprocessing knowledge base Vercel->>Vercel: Divide the .mdx page into parts loop 2. Create and store embed Vercel->>OpenAI (API): Create embed OpenAI (API) for the page part->>Vercel: embed vector (1536)
Vercel->>DB (pgvector): Embed end of the page part
end
In addition to storing the embed vectors, this script generates a checksum for each .mdx file and stores it in another database table to ensure that the embed vectors are regenerated only if the file changes.
Steps 3 and 4 occur at runtime, i.e. when the user submits the problem. When this happens, perform the following series of tasks:
sequenceDiagram
participant Client
participant Edge Function
participant DB (pgvector)
participant OpenAI (API)
Client->>Edge Function: { query: lorem ispum }
critical 3. Perform vector similarity search Edge Function->>OpenAI (API): Create an embedded OpenAI (API) for a query ->>Edge Function: Embed vector (1536)
Edge Function->>DB (pgvector): Vector Similarity Search DB (pgvector)->>Edge Function: Related Document Content End
critical 4. Inject content into the prompt Edge Function->>OpenAI (API): Complete request prompt: Query + related document content OpenAI (API)-->>Client: text/event-stream: Automatic completion of response end
This is the relevant file responsible for SearchDialog(客户端) component and vector-search(边缘函数) .
The initialization of the database, including the settings for pgvector extension, is stored in supabase/migrations folder and is automatically applied to the local PostgreSQL instance when supabase start is run.
cp .env.example .envOPENAI_KEY in the newly created .env file.Make sure Docker is installed and running locally. Then run
npx supabase startRun in a new terminal window
pnpm dev Just deploy this starter to Vercel. Supabase integration automatically sets the required environment variables and configures your database Schema. You just need to set OPENAI_KEY and get started!
此文件由 ChatGPT 提供翻译