Building an AI-Powered Document-Based Chat System with Next.js and OpenAI
In today's data-driven landscape, organizations and individuals alike are seeking sophisticated ways to make their content and knowledge accessible through artificial intelligence. For teams working with rich document collections, creating an intelligent search and chat system can significantly enhance accessibility and knowledge discovery. This article demonstrates how to build such a system using Next.js, OpenAI, and Supabase, creating an AI-powered document search and chat functionality that learns from and responds to user queries. Through a step-by-step technical breakdown and practical implementation guide, we'll explore how to transform static document collections into interactive, AI-driven knowledgebases that understand and respond to user intents. Along the way, we'll uncover the technical challenges and solutions that make this ambitious project possible, from processing complex documents to integrating powerful AI services through carefully crafted system architecture.
The Next.js OpenAI Document Search Starter Template integrates Vercel's AI capabilities with OpenAI's API to create context-aware chat capabilities. The process begins at build time, where all .mdx files in the pages directory are processed. Each file is chunked into sections, which are then sent to OpenAI for embedding generation. These embeddings are stored in a PostgreSQL database using the pgvector extension.
The template performs several technical tasks during its implementation. At build time, Vercel processes each .mdx file into manageable sections and requests embeddings from OpenAI's API for each section. These embeddings are then stored in the PostgreSQL database using the pgvector extension. This process ensures that the document content is prepared for efficient retrieval and contextual understanding during runtime.
During the runtime phase, the Edge Function performs vector similarity searches against the stored embeddings to identify relevant documents based on user queries. These selected documents are then combined with the user's query to construct an appropriate prompt for OpenAI's API, which generates the final response.
The development environment requires specific configuration to facilitate this process. Developers must initialize the application by copying .env.example to .env, setting the OPENAI_KEY, and configuring Supabase's environment variables (NEXT_PUBLIC_SUPABASE_ANON_KEY and SUPABASE_SERVICE_ROLE_KEY). The Supabase integration includes automatic database schema configuration through migration file 20230406025118_init.sql.
To maintain data integrity during content updates, the script generates checksums for .mdx files. This helps ensure that only files that have actually changed trigger the regeneration of their embeddings. The system requires manual intervention to regenerate embeddings before restarting the Next.js development server, ensuring that the most up-to-date content is reflected in the search functionality.
The system architecture follows a four-step technical workflow: pre-processing knowledge base at build time, storing embeddings in PostgreSQL with pgvector, performing vector similarity search at runtime, and injecting content into OpenAI GPT-3 text completion prompts.
At build time, Vercel processes .mdx files into sections through two main tasks: chunking and embedding generation. Each section is sent to OpenAI's API for embedding creation, resulting in 1536-dimensional vectors. These embeddings are stored in a PostgreSQL database using the pgvector extension, with checksums generated for each .mdx file to track changes and ensure only updated content regenerates embeddings.
During runtime, the Edge Function handles vector similarity searches using the database's pgvector extension. When a user submits a query, the system performs the search and returns relevant documents. This information is combined with the user's query to construct a prompt for OpenAI's API, which generates the final response. To maintain optimal performance, the system requires careful configuration of environment variables, including OPENAI_KEY, NEXT_PUBLIC_SUPABASE_ANON_KEY, and SUPABASE_SERVICE_ROLE_KEY.
The underlying technology stack includes Next.js for the frontend framework, Supabase for database management, and OpenAI's API for embedding generation and text completion. The system also leverages Vercel's AI SDK for token streaming and client message display. For development and deployment, the process requires several manual steps, including running the Supabase Docker container, initializing the development environment, and configuring the Next.js development server.
To get started with AgentGPT, follow these steps:
Install the necessary prerequisites, including Node.js, Git, Docker, and your chosen editor (Visual Studio Code recommended).
Clone the repository using git clone https://github.com/reworkd/AgentGPT.git.
Navigate to the project directory and run the setup script: ./setup.sh for Mac/Linux users or ./setup.bat for Windows users.
Follow the on-screen instructions to add your OpenAI API key and any optional Serper and Replicate API tokens.
Once the setup is complete, start the development environment by opening your terminal and running npm run dev. You should now be able to access the application at http://localhost:3000 in your web browser.
The AgentGPT platform requires several environment variables for proper operation:
OPENAI_KEY (your OpenAI API key)
NEXT_PUBLIC_SUPABASE_ANON_KEY (database anonymous key)
SUPABASE_SERVICE_ROLE_KEY (database service role key)
SERPER_API_KEY (optional, for search functionality)
REPLICATE_API_TOKEN (optional, for additional functionality)
For advanced users, the platform provides detailed instructions for customizing templates and deploying applications through the E2B SDK. This includes support for custom package installations, code execution, and optional features like streaming responses and third-party integrations.
The development environment also includes several utility commands for managing the application:
npm i to install project dependencies
git clone to download the repository
npm run dev to start the development server
npm run build to build the production environment
npm run start to launch the application
pnpm run embeddings to regenerate document embeddings
These commands facilitate both development and maintenance of the application, allowing users to efficiently update and deploy their AI-powered applications with Langchain and OpenAI technology.
The Supabase integration serves as the database management layer for the system, employing PostgreSQL with the pgvector extension specifically for vector embedding storage. This infrastructure enables efficient vector similarity searches when users query the system. During setup, developers must initialize the Supabase environment, including setting the NEXT_PUBLIC_SUPABASE_ANON_KEY and SUPABASE_SERVICE_ROLE_KEY environment variables. The platform automatically handles database schema configuration through migration file 20230406025118_init.sql, simplifying setup for developers.
The system's database operations revolve around storing and retrieving embeddings created by OpenAI. During the build process, Vercel processes .mdx files into sections, which are then sent to OpenAI for embedding creation. These 1536-dimensional vectors are stored in the PostgreSQL database using the pgvector extension. To ensure data integrity and efficient content updates, the system employs checksum tracking for .mdx files. When a file changes, its embedding must be regenerated before restarting the Next.js development server.
At runtime, the Edge Function performs vector similarity searches using the database's pgvector extension. This allows the system to quickly identify relevant documents based on user queries. The retrieved documents are then combined with the user's query to construct a prompt for OpenAI's API, which generates the final response. The system requires several manual configuration steps for successful deployment, including running Docker to retrieve necessary Supabase keys and ensuring the development environment is correctly set up with required environment variables.
AgentGPT builds upon the technical foundation established by the Next.js OpenAI Document Search Starter Template, expanding its capabilities through integration with Langchain and additional features. The platform leverages a modular architecture that allows users to configure and deploy autonomous AI agents using a suite of tools including Langchain, OpenAI, AutoGPT, and the T3 Stack.
The AgentGPT environment is built using the T3 Stack, which combines Next.js 13 with TypeScript, FastAPI, and Prisma for database management. The stack employs TailwindCSS and HeadlessUI for styling, providing a robust foundation for both frontend and backend development. During setup, users must configure environment variables for database access and API keys for third-party services. The platform supports authentication through NextAuth.js and utilizes Prisma and SQLModel for database operations.
AgentGPT enables the creation of custom AI agents through the Langchain framework, which provides preconfigured helper functions to streamline development while supporting custom graph implementation. The platform's agent capabilities require internet access via the SERPAPI_API_KEY, and developers can customize existing LangGraph agents or create their own. The system supports various AI use cases, including simple chat applications, structured output generation, and complex question answering through retrieval augmented generation (RAG) with both vector stores and agents.
Setting up AgentGPT requires a series of manual steps, including running Docker to retrieve Supabase keys and configuring the development environment with required environment variables. The setup process also involves initializing the Supabase database schema through migration file 20230406025118_init.sql. For development and maintenance, users can run commands like npm i for dependency installation, git clone to download the repository, and npm run dev to start the development server.
The platform's AI components rely on Langchain's LangSmith tracing capabilities for backend tracking, while frontend communication uses Vercel's AI SDK for token streaming and client message display. The system employs Supabase as its default vector store, though users can customize their own agents and switch between different vector stores through code modifications. Document text processing follows a structured workflow, with .mdx files serving as the primary input format for documentation.