122 lines
2.8 KiB
Markdown
122 lines
2.8 KiB
Markdown
# RAG Agent with ChromaDB and Web Search
|
||
|
||
This repository contains a lightweight Retrieval‑Augmented Generation (RAG) agent that uses **ChromaDB** as the vector store and performs a simple web search to augment the retrieved context before generating an answer with OpenAI's GPT model.
|
||
|
||
> **Deadline**: 31.08.2026
|
||
> **Version**: 14
|
||
|
||
## Features
|
||
|
||
- **Vector Store** – ChromaDB (local, no external service required)
|
||
- **Embeddings** – OpenAI `text-embedding-ada-002`
|
||
- **LLM** – OpenAI `gpt-3.5-turbo`
|
||
- **Web Search** – DuckDuckGo (no API key needed)
|
||
- **Command‑line interface** for adding documents and asking questions
|
||
|
||
## Prerequisites
|
||
|
||
- Python 3.9+
|
||
- An OpenAI API key
|
||
|
||
## Installation
|
||
|
||
```bash
|
||
# Clone the repository
|
||
git clone https://git.brojs.ru/kuzakhmetovartur/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:
|
||
|
||
```
|
||
openai>=1.0.0
|
||
chromadb>=0.4.0
|
||
requests>=2.31.0
|
||
beautifulsoup4>=4.12.0
|
||
```
|
||
|
||
## Configuration
|
||
|
||
Set your OpenAI API key as an environment variable:
|
||
|
||
```bash
|
||
export OPENAI_API_KEY="sk-..."
|
||
```
|
||
|
||
On Windows:
|
||
|
||
```cmd
|
||
set OPENAI_API_KEY=sk-...
|
||
```
|
||
|
||
## Usage
|
||
|
||
### 1. Add Documents
|
||
|
||
Add a text file to the vector store. The file will be split into chunks (≈500 tokens each) and embedded.
|
||
|
||
```bash
|
||
python -m src.index add path/to/document.txt
|
||
```
|
||
|
||
Example:
|
||
|
||
```bash
|
||
python -m src.index add data/biology.txt
|
||
```
|
||
|
||
### 2. Ask a Question
|
||
|
||
Query the RAG agent. It will:
|
||
|
||
1. Retrieve the top‑5 nearest chunks from ChromaDB.
|
||
2. Perform a DuckDuckGo web search for the query.
|
||
3. Combine the retrieved context and web snippets.
|
||
4. Generate an answer with GPT.
|
||
|
||
```bash
|
||
python -m src.index ask "What is the function of mitochondria?"
|
||
```
|
||
|
||
### 3. Help
|
||
|
||
```bash
|
||
python -m src.index
|
||
```
|
||
|
||
## Example
|
||
|
||
```bash
|
||
$ python -m src.index add sample.txt
|
||
Added 4 chunks from sample.txt to the collection.
|
||
|
||
$ python -m src.index ask "Explain the water cycle."
|
||
Answer:
|
||
The water cycle, also known as the hydrologic cycle, describes the continuous movement of water on, above, and below the surface of the Earth. ...
|
||
```
|
||
|
||
## Project Structure
|
||
|
||
```
|
||
src/
|
||
├── index.py # Main script
|
||
README.md
|
||
requirements.txt
|
||
```
|
||
|
||
## Notes
|
||
|
||
- **ChromaDB Persistence** – The vector store is persisted in `./chromadb`. Delete this folder to reset the store.
|
||
- **Token Limits** – The embedding model `text-embedding-ada-002` supports up to 8191 tokens per request. The chunking logic approximates a 500‑token limit per chunk.
|
||
- **Web Search** – DuckDuckGo is used for simplicity. For production use, consider a dedicated search API (e.g., SerpAPI, Bing Search API).
|
||
|
||
## License
|
||
|
||
MIT License |