An Introduction to the ChatGPT-PGVector Project
The ChatGPT-PGVector project is a unique starter application created to enhance ChatGPT's ability to handle domain-specific knowledge. While ChatGPT adeptly manages casual and general inquiries, it struggles with specialized queries, often fabricating answers to fill knowledge gaps. This project bridges that gap, leveraging embeddings and vector search to refine how OpenAI's chat completions API is utilized to interact with domain-specific data.
Embeddings and Vectors: A Simple Explanation
Embeddings are numeric vector representations of text strings. They are invaluable for various tasks, such as ranking search results, clustering data, and classifying information. Text embeddings identify the "relatedness" between strings through cosine similarity—a measure where a similarity score close to 1 signifies a high degree of similarity. In this project, embeddings are used to create a vector representation of a document, enabling efficient search for similar documents based on a given query, and subsequently crafting a meaningful response.
A Technological Overview
The project employs several modern technologies to achieve its sophisticated functionalities:
- Next.js: A React framework that builds the app's user interface.
- Vercel: The hosting platform for deploying the Next.js app.
- Supabase: Acts as the vector database utilizing pgvector to store and search embeddings.
- OpenAI API: Responsible for generating text embeddings and facilitating chat completions.
- TailwindCSS: Utilized for styling the app's front-end interfaces.
Key Functional Mechanics
Creating and Storing Embeddings:
- Data Collection: Web pages are extracted into plain text and divided into 1000-character segments.
- Embedding Generation: Using OpenAI's "text-embedding-ada-002" model, embeddings are created for each text segment.
- Database Storage: These embeddings are stored in a Supabase PostgreSQL table, with columns for document text, source URL, and corresponding vectors.
Query Response Process:
- Prompt Embedding: An embedding is generated from a user's query.
- Similarity Search: This embedding is matched against the vector database to find the most relevant documents.
- Prompt Construction: The fetched data helps formulate a prompt for GPT-3.5 or GPT-4.
- Response Delivery: The generated response is then sent to the user.
Starting Your Journey
To get started with this project, a foundational understanding of React and Next.js is ideal, although detailed experience with OpenAI APIs and Supabase is not mandatory. Below is a succinct guide to setting up the environment:
Setting Up Supabase
- Create a Supabase Account: Visit their website and create a new project.
- Enable Vector Extension: This can be done through the web portal under
Database
→Extensions
. Alternatively, add the vector extension via SQL:create extension vector;
- Create a Documents Table: Execute the following in the SQL Editor:
create table documents ( id bigserial primary key, content text, url text, embedding vector (1536) );
- Set Up a Similarity Search Function: Run this SQL query:
create or replace function match_documents ( query_embedding vector(1536), similarity_threshold float, match_count int ) returns table ( id bigint, content text, url text, similarity float ) language plpgsql as $$ begin return query select documents.id, documents.content, documents.url, 1 - (documents.embedding <=> query_embedding) as similarity from documents where 1 - (documents.embedding <=> query_embedding) > similarity_threshold order by documents.embedding <=> query_embedding limit match_count; end; $$;
Local Environment Configuration
- Clone the Repository: Use GitHub to clone the project.
gh repo clone gannonh/chatgpt-pgvector
- Open in an Editor: Open the project in your preferred code editor (e.g., VS Code).
cd chatgpt-pgvector code .
- Install Dependencies: Execute the following command:
npm install
- Configure Environment Variables: Create a
.env.local
file from the example provided:cp .env.local.example .env.local
- Add your Supabase project URL and API key, and your OPENAI API key to this file.
- Running the Application: Start the development server.
npm run dev
- Access the Application: Visit
http://localhost:3000
in your web browser to explore the app.
The ChatGPT-PGVector project is a compelling dive into enhancing chat applications with domain-specific knowledge through cutting-edge technology like embeddings and vector databases. Whether you're enhancing existing chat interfaces or exploring new applications, this project delivers significant insights and tools to achieve those goals.