Introducing the Next.js OpenAI Doc Search Starter
The Next.js OpenAI Doc Search Starter is an innovative project designed to leverage the power of OpenAI's Text Completion to enhance document search capabilities. It processes .mdx
files, found in the pages
directory, to use as a custom context in OpenAI prompts, allowing for a tailored and intelligent search experience.
How to Deploy
Deploying this starter on Vercel is straightforward. The integration with Supabase automates the necessary setup, including the configuration of environment variables and the database schema. All users need to do is set their OPENAI_KEY
, and the system is ready to operate.
Technical Overview
Creating a custom ChatGPT experience with this starter involves four essential steps:
- Pre-process the Knowledge Base: During the build time, the system processes
.mdx
files, breaking them into sections that can be used for search contexts. - Store Embeddings in Postgres: It uses
pgvector
to store the embeddings, which are vector representations of the document sections, in a Postgres database. - Vector Similarity Search: At runtime, when a user queries the system, it performs a vector similarity search to find the most relevant content.
- Inject Content into OpenAI Prompt: Finally, the relevant content is integrated into an OpenAI GPT-3 text completion prompt to generate a response streamed back to the user.
Build Time Processing
During the build process, Vercel executes a script that processes the .mdx
files by creating embeddings with OpenAI's API and storing these embeddings in a database. A checksum for each file ensures embeddings are only updated when the file changes.
Runtime Processing
When a user submits a query, the system generates an embedding for the query, performs a similarity search in the database to find relevant document sections, and injects these sections into an OpenAI prompt. The result is a dynamic and relevant response streamed to the user.
Setting Up Locally
To set up the project locally, follow these steps:
- Copy the example environment file and configure your
OPENAI_KEY
. - Start Supabase via Docker, retrieve your Supabase keys, and input them into your environment file.
- Run
pnpm dev
to launch the Next.js app for local development.
Custom Documentation
Users can customize their documentation search by adding .mdx
files to the pages directory. By regenerating embeddings with pnpm run embeddings
, the system updates its knowledge base for the most recent content.
Additional Resources
For those interested in more details on the development of this project, there is a blog post discussing how the ChatGPT functionality was built for Supabase Docs. Additional technical documentation is available on the Supabase website, and an insightful video walkthrough by a developer named Greg offers a comprehensive overview of the construction process.
This project is licensed under Apache 2.0, making it a flexible and powerful tool for those looking to enhance document search functionalities using AI.