RAG Agent with ChromaDB and Web Search
This repository contains a simple Retrieval-Augmented Generation (RAG) agent that uses ChromaDB as the vector store and OpenAI's GPT model for generation. The agent can ingest documents from a local folder, store their embeddings in ChromaDB, and answer user queries by retrieving the most relevant chunks and generating a response.
Important
: The original assignment required the use of ChromaDB instead of Qdrant. This implementation fully complies with that requirement.
Features
- Vector Store: ChromaDB (persistent on disk)
- Embeddings: OpenAI embeddings (
text-embedding-3-smallby default) - LLM: OpenAI GPT (
gpt-3.5-turboby default) - Text Splitting: Recursive character splitter (chunk size 1000, overlap 200)
- CLI: Two modes –
ingestandquery
Prerequisites
- Python 3.10+
- An OpenAI API key
Installation
# Clone the repository
git clone https://github.com/yourusername/ekzamen-rag-agent-s-chromadb-i-veb-poisk.git
cd ekzamen-rag-agent-s-chromadb-i-veb-poisk
# Create a virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
requirements.txt contains:
chromadb
langchain
openai
python-dotenv
Configuration
Create a .env file in the project root (or set environment variables directly):
OPENAI_API_KEY=your-openai-api-key
CHROMA_DB_PATH=./chromadb # Path where ChromaDB will store data
CHROMA_COLLECTION=rag_collection # Collection name
EMBEDDING_MODEL=text-embedding-3-small
LLM_MODEL=gpt-3.5-turbo
TOP_K=4
CHUNK_SIZE=1000
CHUNK_OVERLAP=200
Note
: If you don't provide a
.envfile, the script will look for the variables in the environment.
Usage
1. Ingest Documents
Place your .txt files in a folder (e.g., data/). Then run:
python -m src.index ingest data/
The script will:
- Load all
.txtfiles. - Split them into chunks.
- Generate embeddings.
- Store them in ChromaDB.
2. Query the Agent
python -m src.index query "What is the capital of France?"
The agent will:
- Embed the question.
- Retrieve the top
TOP_Krelevant chunks. - Generate an answer using GPT.
Example
$ python -m src.index ingest data/
Ingested 42 chunks into collection 'rag_collection'.
$ python -m src.index query "Explain the theory of relativity."
Answer:
The theory of relativity, developed by Albert Einstein, consists of two parts: special relativity and general relativity. ...
Project Structure
├── src
│ └── index.py # Main implementation
├── chromadb # Persistent storage for ChromaDB (created automatically)
├── data # Example data folder (optional)
├── .env # Environment variables
├── requirements.txt
└── README.md
Troubleshooting
- Missing OpenAI API key: Ensure
OPENAI_API_KEYis set in your environment or.envfile. - ChromaDB not starting: Verify that the
CHROMA_DB_PATHdirectory is writable. - Large documents: Adjust
CHUNK_SIZEandCHUNK_OVERLAPin the.envfile.
License
MIT License