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

This commit is contained in:
2026-06-30 15:29:58 +03:00
parent e95da4c295
commit 1279deaaa6
6 changed files with 275 additions and 151 deletions
+68 -65
View File
@@ -1,95 +1,98 @@
# Agent with RAG Memory (ChromaDB)
# Agent with RAG Memory
This project implements a Retrieval-Augmented Generation (RAG) agent that uses **ChromaDB** as its sole vector store. The agent can ingest documents, store their embeddings, retrieve relevant passages, and generate answers using OpenAIs GPT models.
This project implements a retrievalaugmented generation (RAG) agent that uses **Qdrant** as the vector store and **Ollama** for local LLM inference.
The agent follows the latest LangChain API and is fully configurable via environment variables.
## Features
- **Vector Store** Uses ChromaDB for storing and querying embeddings.
- **Embeddings** Generated with OpenAIs `text-embedding-ada-002`.
- **Chat** Generates responses with OpenAIs `gpt-3.5-turbo`.
- **Public API** The `Agent` class exposes `init`, `ingest`, and `ask` methods, keeping the original interface unchanged.
- **Qdrant** vector store (via `langchain-qdrant`)
- **Ollama** local LLM integration (via `langchain-ollama`)
- Retrievalaugmented generation with conversation memory
- Simple CLI interface for quick testing
## Setup
## Installation
1. **Clone the repository**
```bash
# Clone the repository
git clone https://git.brojs.ru/kuzakhmetovartur/agent-s-rag-pamyatyu.git
cd agent-s-rag-pamyatyu
```bash
git clone https://git.brojs.ru/kuzakhmetovartur/agent-s-rag-pamyatyu.git
cd agent-s-rag-pamyatyu
```
# Create a virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\\Scripts\\activate
2. **Install dependencies**
# Install dependencies
pip install -r requirements.txt
```
```bash
npm install
```
## Configuration
3. **Configure environment variables**
Create a `.env` file in the project root (or set environment variables directly):
Create a `.env` file in the project root (or export the variables in your shell):
```dotenv
# Qdrant
QDRANT_HOST=localhost
QDRANT_PORT=6333
QDRANT_API_KEY= # leave empty if no key
```dotenv
# ChromaDB
CHROMA_URL=localhost
CHROMA_PORT=8000
# Ollama
OLLAMA_HOST=localhost
OLLAMA_PORT=11434
OLLAMA_MODEL=llama3
# OpenAI
OPENAI_API_KEY=YOUR_OPENAI_API_KEY
```
# Optional: collection name
QDRANT_COLLECTION=documents
```
- `CHROMA_URL` and `CHROMA_PORT` point to your ChromaDB instance.
- `OPENAI_API_KEY` is required for embeddings and chat completions.
4. **Run ChromaDB**
Ensure a ChromaDB server is running on the specified host/port. You can start a local instance with Docker:
```bash
docker run -d -p 8000:8000 chromadb/chroma
```
> **Note**: The Qdrant instance must be running and accessible at the specified host/port.
> The Ollama server must be running locally and expose the chosen model.
## Usage
```js
const { Agent } = require('./src');
### Adding Documents
(async () => {
const agent = new Agent();
await agent.init();
```python
from src.agent import Agent
from langchain_core.documents import Document
// Ingest documents
await agent.ingest('The quick brown fox jumps over the lazy dog.', { source: 'example.txt' });
agent = Agent()
// Ask a question
const answer = await agent.ask('What did the fox do?');
console.log(answer);
})();
docs = [
Document(page_content="Python is a programming language.", metadata={"source": "python.txt"}),
Document(page_content="LangChain is a framework for LLM applications.", metadata={"source": "langchain.txt"}),
]
agent.add_documents(docs)
```
## API
| Method | Description |
|--------|-------------|
| `init()` | Initializes the vector store (creates collection if needed). |
| `ingest(text, metadata)` | Adds a document to the vector store. |
| `ask(question)` | Retrieves relevant passages and generates an answer. |
## Testing
If you have a test suite, run:
### Querying the Agent
```bash
npm test
python -m src.agent "What is LangChain?"
```
All tests should pass after the ChromaDB integration.
or from Python:
## Notes
```python
response = agent.run("What is LangChain?")
print(response["answer"])
```
- The agents public API remains unchanged; only the underlying vector store implementation has been swapped to ChromaDB.
- No new external services are introduced beyond ChromaDB and the existing OpenAI usage.
- Ensure that the ChromaDB server is reachable; otherwise, the agent will throw connection errors.
The response will include the answer and the source documents used.
---
## Project Structure
Happy coding!
```
agent-s-rag-pamyatyu/
├── src/
│ ├── agent.py
│ ├── config.py
│ └── vector_store.py
├── requirements.txt
├── pyproject.toml
└── README.md
```
## License
MIT License