Overview
RAG Chat provides four core functions for managing document processing, vector storage, and question answering. All functions are designed to work seamlessly with LangChain and OpenAI embeddings.load_existing_vector_store()
Loads an existing Chroma vector store from the persistent directory.Signature
Returns
Returns a Chroma vector store instance if the persistent directory exists, otherwise returns
None.Behavior
- Checks if the
dbdirectory exists in the project root - If found, initializes a Chroma vector store with OpenAI embeddings
- Uses the same embedding function as document ingestion for consistency
Example
Source Reference
Defined inapp.py:16-23
process_file(file)
Processes a PDF file and splits it into document chunks suitable for vector storage.Signature
Parameters
A file object from Streamlit’s file uploader. Must be a PDF file.
Returns
A list of LangChain Document objects, split into chunks with overlap for better context preservation.
Processing Details
- Temporary File Creation: Creates a temporary file with
.pdfsuffix - PDF Loading: Uses PyPDFLoader to extract text from all pages
- Text Splitting: Applies RecursiveCharacterTextSplitter with:
chunk_size: 1000 characterschunk_overlap: 400 characters (40% overlap for context preservation)
- Cleanup: Automatically removes the temporary file after processing
Example
Source Reference
Defined inapp.py:26-40
add_to_vector_store(documents, vector_store=None)
Adds document chunks to an existing vector store or creates a new one.Signature
Parameters
A list of LangChain Document objects to add to the vector store. Typically output from
process_file().An existing Chroma vector store instance. If
None, a new vector store will be created.Returns
The updated or newly created Chroma vector store instance.
Behavior
- If
vector_storeis provided: Adds documents to the existing store usingadd_documents() - If
vector_storeisNone: Creates a new Chroma vector store with:- OpenAI embeddings
- Persistent storage in the
dbdirectory
Example
Source Reference
Defined inapp.py:43-52
ask_question(model, query, vector_store)
Processes a user question using RAG (Retrieval-Augmented Generation) with conversation history.Signature
Parameters
The OpenAI model identifier to use for generating responses. Supported models:
gpt-3.5-turbogpt-4gpt-4-turbogpt-4o-minigpt-4o
The user’s question or prompt.
A Chroma vector store instance containing the document embeddings for retrieval.
Returns
The AI-generated response in markdown format, based on retrieved context and conversation history.
RAG Pipeline
- LLM Initialization: Creates a ChatOpenAI instance with the specified model
- Retriever Setup: Converts vector store to a retriever for semantic search
- Prompt Construction:
- System prompt instructs the model to answer based on context
- Includes full conversation history from
st.session_state.messages - Responses are formatted in markdown with interactive visualizations
- Chain Execution:
- Retrieves relevant document chunks
- Passes context and query to the LLM
- Returns formatted response
System Prompt Behavior
The function uses a Portuguese system prompt that:- Instructs the model to use retrieved context for answers
- Asks for explanations when information is not available
- Requests markdown formatting with interactive visualizations
Example
Source Reference
Defined inapp.py:55-82
Notes
All functions require the
OPENAI_API_KEY environment variable to be set. See Environment Variables for setup instructions.