🤖 AI Search (Retrieval Augmented Generation - RAG)
CICADA employs an AI-powered search system based on Retrieval Augmented Generation (RAG) to provide users with accurate and contextually relevant answers from the constitutional archives. This is primarily handled by thecleoChat Supabase Edge Function (located in supabase/functions/test/index.ts in the provided codebase, its content and purpose align with a RAG chat bot).
Overview of RAG
Retrieval Augmented Generation (RAG) is an AI framework that enhances the responses of Large Language Models (LLMs) by grounding them in specific, up-to-date information retrieved from an external knowledge base. Instead of solely relying on the LLM’s pre-trained (and potentially outdated or too general) knowledge, a RAG system first:- Retrieves: Fetches relevant documents or text snippets from a designated knowledge source (in CICADA’s case, the uploaded constitutional documents and their processed embeddings).
- Augments: Adds this retrieved information as context to the user’s original query.
- Generates: Passes the augmented query (original query + context) to an LLM to produce an answer.
Workflow in CICADA
- User Query: A user submits a natural language question through the chat interface (
src/components/chat/ChatWindow.tsx). - Query Embedding:
- The
cleoChatEdge Function receives the user’s question. - It utilizes a text embedding model (e.g., from Google Gemini, such as
text-embedding-004) to convert the user’s question into a numerical vector representation (an embedding). This embedding captures the semantic meaning of the query.
- The
- Vector Search (Retrieval):
- The generated query embedding is used to search for semantically similar embeddings stored in the
private_vectorsandpublic_vectorstables within the Supabase PostgreSQL database. - This search is performed by the
match_documentsPostgreSQL function (supabase/seed/Document_function.sql). This function calculates similarity scores (e.g., based on cosine similarity or L2 distance) between the query embedding and the pre-computed embeddings of document chunks. - The function returns the most relevant document chunks (snippets of text from the original documents) that match the query’s intent. It searches both public vectors and, if the user is authenticated and has private documents, their private vectors as well.
- The generated query embedding is used to search for semantically similar embeddings stored in the
- Context Augmentation:
- The text content from these retrieved document chunks is compiled to form a rich, relevant context.
- LLM Prompting (Generation):
- The
cleoChatEdge Function constructs a carefully designed prompt for a powerful LLM (e.g., Google Gemini’sgemini-1.5-flash-latestmodel). This prompt typically includes:- The original user question.
- The chat history (if it’s a follow-up question, for conversational context).
- The retrieved document chunks (the augmented context).
- Specific instructions for the LLM, such as to base its answer only on the provided context, to answer concisely, and to cite the source documents it used.
- The
- Response Generation & Streaming:
- The LLM processes this augmented prompt and generates an answer.
- The
cleoChatfunction is designed to stream this response back to the client application (ChatWindow.tsx). This means the answer appears word by word or sentence by sentence, improving perceived performance. - The streamed response may also include structured information about the source documents that were heavily relied upon for the answer.
- Display to User:
- The
ChatWindow.tsxcomponent receives and renders the streamed AI response. - It also processes and displays information about the source documents (e.g., titles, snippets, links to view the full document via
SourceCard.tsxandSourceModal.tsx), allowing users to verify the information and explore the original texts.
- The
Key Components Enabling RAG
-
Document Processing Pipeline (
parseUploadedDocumentsEdge Function):- Text Extraction: When new documents are uploaded, this function extracts their textual content.
- Chunking: The extracted text is divided into smaller, semantically meaningful chunks. This is important because embeddings are typically generated for these smaller pieces rather than entire large documents.
- Embedding Generation: Each chunk is converted into a vector embedding using a text embedding model.
- Storage: These embeddings, along with their corresponding text content and a reference to the parent document ID, are stored in the
private_vectorsorpublic_vectorstables in Supabase.
-
Vector Database (Supabase with
pgvector):- Supabase PostgreSQL, enhanced with the
pgvectorextension, serves as the vector database. - The
private_vectorsandpublic_vectorstables store the embeddings. - Specialized indexes (HNSW - Hierarchical Navigable Small World) are created on the embedding columns to enable extremely fast and efficient similarity searches over millions of vectors.
- Supabase PostgreSQL, enhanced with the
-
match_documentsPostgreSQL Function:- This custom SQL function is the engine for the retrieval step. It takes a query embedding and search parameters (like the number of results to return and a similarity threshold).
- It efficiently queries the vector tables, calculates similarity, and returns the top-matching document chunks along with their original document metadata.
- It handles filtering for user-specific data when searching
private_vectors.
-
cleoChatEdge Function (RAG Orchestrator):- This function orchestrates the entire RAG pipeline as described in the workflow above. It integrates query embedding, vector search (via
match_documents), prompt engineering for the LLM, and response streaming. - It may also handle conversational memory (chat history) to provide context for follow-up questions.
- This function orchestrates the entire RAG pipeline as described in the workflow above. It integrates query embedding, vector search (via
-
Frontend Chat Interface (
src/components/chat/ChatWindow.tsx):- Provides the user interface for submitting queries.
- Communicates with the
cleoChatEdge Function (viachatService.ts). - Renders the AI’s streamed responses and the identified source documents in a user-friendly manner.
Benefits of RAG in CICADA
- Enhanced Accuracy and Relevance: Answers are grounded in the actual content of the uploaded constitutional documents, significantly reducing the risk of the LLM generating incorrect or “hallucinated” information.
- Access to Specific and Current Information: The system can answer questions based on the very latest documents added to the archive, unlike LLMs that rely solely on their general training data which has a knowledge cut-off.
- Transparency and Verifiability: By citing and allowing users to inspect the source documents, CICADA builds trust and allows users to verify the AI’s claims and delve deeper into the original texts.
- Improved Contextual Understanding: LLMs, when augmented with relevant retrieved context, can better understand nuanced queries and synthesize information from multiple parts of the archive.
- Reduced Need for Fine-Tuning: RAG provides a way to make LLMs knowledgeable about specific domains without the expensive and complex process of fine-tuning the entire model on that domain’s data.