feat: solution for 'Агент с RAG-памятью'
CI / build (push) Has been cancelled

This commit is contained in:
2026-07-01 14:11:13 +03:00
parent 39d55136ad
commit a6940ef3ee
6 changed files with 383 additions and 189 deletions
+67 -43
View File
@@ -1,70 +1,94 @@
# RAG Agent with Ollama Embeddings and Qdrant
# RAG Agent with ChromaDB
This project implements a Retrieval-Augmented Generation (RAG) agent that uses:
This project implements a simple Retrieval-Augmented Generation (RAG) agent that uses **ChromaDB** as the vector store.
The agent loads text documents, indexes them with embeddings, and answers user questions by retrieving relevant passages and generating a response with an OpenAI LLM.
- **OllamaEmbeddings** from `langchain-community` for local embeddings.
- **Qdrant** as the vector store for efficient similarity search.
- **OpenAI LLM** for generating responses.
## Features
## Prerequisites
- **ChromaDB** persistence for fast similarity search.
- OpenAI embeddings (`text-embedding-3-small`) for vector representation.
- OpenAI LLM (`gpt-4o-mini` by default) for answer generation.
- Simple commandline interface to index documents and ask questions.
- Backwardcompatible API: `RAGAgent` exposes `add_documents`, `ask`, `get_document_count`, and `clear_store`.
- Python 3.10+
- A running local Ollama instance (default: `http://localhost:11434`).
- A running local Qdrant instance (default: `http://localhost:6333`).
- An OpenAI API key for the LLM.
## Requirements
```text
chromadb==0.4.24
langchain==0.1.13
openai==1.12.0
tqdm==4.66.1
pydantic==2.6.3
python-dotenv==1.0.1
```
Install them with:
```bash
pip install -r requirements.txt
```
## Setup
```bash
# Clone the repository
git clone https://git.brojs.ru/kuzakhmetovartur/agent-s-rag-pamyatyu.git
cd agent-s-rag-pamyatyu
1. **OpenAI API Key**
The agent uses OpenAI services for embeddings and LLM.
Set your key in an environment variable:
# Create a virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows use .venv\\Scripts\\activate
```bash
export OPENAI_API_KEY="sk-..."
```
# Install dependencies
pip install -r requirements.txt
# or using Poetry
# poetry install
```
2. **Prepare Documents**
Place all `.txt` files you want to index in a directory, e.g., `data/`.
Create a `.env` file in the project root with your OpenAI key:
```
OPENAI_API_KEY=sk-...
```
## Running the Agent
## Usage
```bash
python src/main.py
python -m src.main --docs data/ --question "What is the capital of France?"
```
You can then interact with the agent in the console. Type `exit` or `quit` to stop.
### Arguments
## Adding Documents
| Argument | Description | Default |
|----------|-------------|---------|
| `--docs` | Path to directory with `.txt` files. | **Required** |
| `--question` | The question to ask the agent. | **Required** |
| `--persist` | Directory where ChromaDB stores its data. | `./chromadb` |
| `--model` | OpenAI LLM model to use. | `gpt-4o-mini` |
| `--k` | Number of documents to retrieve for RAG. | `4` |
The agent automatically creates a Qdrant collection named `rag_collection`. To add documents, you can extend the `vector_store.py` module or use the Qdrant client directly. For example:
The first run will index all documents. Subsequent runs reuse the persisted index.
## API
```python
from vector_store import get_vector_store
from src.vector_store import ChromaDBVectorStore
from src.agent import RAGAgent
from langchain.schema import Document
vs = get_vector_store()
vs.add_texts(["Hello world", "Another document"])
# Create vector store
store = ChromaDBVectorStore(persist_directory="./chromadb")
# Add documents
docs = [Document(page_content="Hello world", metadata={"source": "greeting.txt"})]
store.add_documents(docs)
# Create agent
agent = RAGAgent(vector_store=store)
# Ask a question
answer = agent.ask("What is this?")
print(answer)
```
## Testing
The project includes a minimal test suite (not shown here). To run tests:
The project includes no automated tests, but you can manually verify:
```bash
pytest
```
Ensure that your local Ollama and Qdrant instances are running before executing tests.
1. Run the CLI with a small set of documents.
2. Ask a question that should be answered using the indexed content.
3. Verify that the answer references the correct context.
## License
MIT License
MIT License.