Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Template for building your own custom ChatGPT style doc search powered by Next.js, OpenAI, and Supabase.

License

NotificationsYou must be signed in to change notification settings

supabase-community/nextjs-openai-doc-search

Repository files navigation

This starter takes all the.mdx files in thepages directory and processes them to use as custom context withinOpenAI Text Completion prompts.

Deploy

Deploy this starter to Vercel. The Supabase integration will automatically set the required environment variables and configure yourDatabase Schema. All you have to do is set yourOPENAI_KEY and you're ready to go!

[Deploy with Vercel

Technical Details

Building your own custom ChatGPT involves four steps:

  1. [👷 Build time] Pre-process the knowledge base (your.mdx files in yourpages folder).
  2. [👷 Build time] Store embeddings in Postgres withpgvector.
  3. [🏃 Runtime] Perform vector similarity search to find the content that's relevant to the question.
  4. [🏃 Runtime] Inject content into OpenAI GPT-3 text completion prompt and stream response to the client.

👷 Build time

Step 1. and 2. happen at build time, e.g. when Vercel builds your Next.js app. During this time thegenerate-embeddings script is being executed which performs the following tasks:

sequenceDiagram    participant Vercel    participant DB (pgvector)    participant OpenAI (API)    loop 1. Pre-process the knowledge base        Vercel->>Vercel: Chunk .mdx pages into sections        loop 2. Create & store embeddings            Vercel->>OpenAI (API): create embedding for page section            OpenAI (API)->>Vercel: embedding vector(1536)            Vercel->>DB (pgvector): store embedding for page section        end    end
Loading

In addition to storing the embeddings, this script generates a checksum for each of your.mdx files and stores this in another database table to make sure the embeddings are only regenerated when the file has changed.

🏃 Runtime

Step 3. and 4. happen at runtime, anytime the user submits a question. When this happens, the following sequence of tasks is performed:

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 embedding for query        OpenAI (API)->>Edge Function: embedding vector(1536)        Edge Function->>DB (pgvector): vector similarity search        DB (pgvector)->>Edge Function: relevant docs content    end    critical 4. Inject content into prompt        Edge Function->>OpenAI (API): completion request prompt: query + relevant docs content        OpenAI (API)-->>Client: text/event-stream: completions response    end
Loading

The relevant files for this are theSearchDialog (Client) component and thevector-search (Edge Function).

The initialization of the database, including the setup of thepgvector extension is stored in thesupabase/migrations folder which is automatically applied to your local Postgres instance when runningsupabase start.

Local Development

Configuration

  • cp .env.example .env
  • Set yourOPENAI_KEY in the newly created.env file.
  • SetNEXT_PUBLIC_SUPABASE_ANON_KEY andSUPABASE_SERVICE_ROLE_KEY run:

    Note: You have to run supabase to retrieve the keys.

Start Supabase

Make sure you have Docker installed and running locally. Then run

supabase start

To retrieveNEXT_PUBLIC_SUPABASE_ANON_KEY andSUPABASE_SERVICE_ROLE_KEY run:

supabase status

Start the Next.js App

In a new terminal window, run

pnpm dev

Using your custom .mdx docs

  1. By default your documentation will need to be in.mdx format. This can be done by renaming existing (or compatible) markdown.md file.
  2. Runpnpm run embeddings to regenerate embeddings.

    Note: Make sure supabase is running. To check, runsupabase status. If is not running runsupabase start.

  3. Runpnpm dev again to refresh NextJS localhost:3000 rendered page.

Learn More

Video: How I Built Supabase’s OpenAI Doc Search

Licence

Apache 2.0

About

Template for building your own custom ChatGPT style doc search powered by Next.js, OpenAI, and Supabase.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp