122 lines
3.2 KiB
Markdown
122 lines
3.2 KiB
Markdown
# 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-small` by default)
|
||
- **LLM**: OpenAI GPT (`gpt-3.5-turbo` by default)
|
||
- **Text Splitting**: Recursive character splitter (chunk size 1000, overlap 200)
|
||
- **CLI**: Two modes – `ingest` and `query`
|
||
|
||
## Prerequisites
|
||
|
||
- Python 3.10+
|
||
- An OpenAI API key
|
||
|
||
## Installation
|
||
|
||
```bash
|
||
# 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):
|
||
|
||
```dotenv
|
||
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 `.env` file, 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:
|
||
|
||
```bash
|
||
python -m src.index ingest data/
|
||
```
|
||
|
||
The script will:
|
||
|
||
1. Load all `.txt` files.
|
||
2. Split them into chunks.
|
||
3. Generate embeddings.
|
||
4. Store them in ChromaDB.
|
||
|
||
### 2. Query the Agent
|
||
|
||
```bash
|
||
python -m src.index query "What is the capital of France?"
|
||
```
|
||
|
||
The agent will:
|
||
|
||
1. Embed the question.
|
||
2. Retrieve the top `TOP_K` relevant chunks.
|
||
3. Generate an answer using GPT.
|
||
|
||
## Example
|
||
|
||
```bash
|
||
$ 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_KEY` is set in your environment or `.env` file.
|
||
- **ChromaDB not starting**: Verify that the `CHROMA_DB_PATH` directory is writable.
|
||
- **Large documents**: Adjust `CHUNK_SIZE` and `CHUNK_OVERLAP` in the `.env` file.
|
||
|
||
## License
|
||
|
||
MIT License |