nextjs-openai-doc-search

supabase-communityApache-2.0更新放缓

基于 Next.js、OpenAI 和 Supabase 的文档搜索模板,帮你快速搭一个 ChatGPT 风格的问答式文档检索站。

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

暂无版本1,727star309fork同步于 2026-11-07 07:29 UTC

怎么上手

由 README 提炼,执行前请确认
cp .env.example .env
supabase start
pnpm dev
硬件要求按 README 说明

README 未说明;本地运行需安装并启动 Docker 以运行 Supabase

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

用 Next.js、OpenAI 和 Supabase 快速搭建自定义 ChatGPT 式文档搜索

基于 Next.js、OpenAI 和 Supabase 的文档搜索模板,帮你快速搭一个 ChatGPT 风格的问答式文档检索站。

核心能力

  • 构建时把 pages 目录的 .mdx 分块并生成 embedding
  • 用 pgvector 在 Postgres 中存储 embedding 向量
  • 运行时做向量相似度检索并注入 GPT-3 补全提示
  • 用 checksum 判断文件变更,只重新生成改动过的 embedding
  • 响应以 text/event-stream 流式返回给客户端

容易踩的坑

需先在 .env 中设置 OPENAI_KEY,并运行 supabase status 获取两个 Supabase 密钥;文档需为 .mdx 格式,改完要跑 pnpm run embeddings 重新生成 embedding;本地需 Docker 运行 Supabase,且 embeddings 脚本执行时 Supabase 必须已启动

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Next.js OpenAI Doc Search Starter

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

    Deploy

    Deploy this starter to Vercel. The Supabase integration will automatically set the required environment variables and configure your Database Schema. All you have to do is set your OPENAI_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 your pages folder).
    2. [👷 Build time] Store embeddings in Postgres with pgvector.
    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 the generate-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
    

    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
    

    The relevant files for this are the SearchDialog (Client) component and the vector-search (Edge Function).

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

    Local Development

    Configuration

    • cp .env.example .env
    • Set your OPENAI_KEY in the newly created .env file.
    • Set NEXT_PUBLIC_SUPABASE_ANON_KEY and SUPABASE_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 retrieve NEXT_PUBLIC_SUPABASE_ANON_KEY and SUPABASE_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. Run pnpm run embeddings to regenerate embeddings.

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

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

    Learn More

    Video: How I Built Supabase’s OpenAI Doc Search

    Licence

    Apache 2.0