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

This commit is contained in:
2026-06-30 15:41:27 +03:00
parent 42fd00d924
commit 62c0063d9d
7 changed files with 310 additions and 164 deletions
+41 -66
View File
@@ -1,90 +1,65 @@
# RAG Agent with Ollama Embeddings
# RAG Agent with Ollama
This project demonstrates a simple Retrieval-Augmented Generation (RAG) agent that uses **OllamaEmbeddings** for vector similarity search and a local inmemory knowledge base.
The agent is built with **LangChain** and exposes two tools:
This project implements a simple Retrieval-Augmented Generation (RAG) agent that uses **Ollama** for both embeddings and LLM inference.
The agent stores documents in memory, retrieves the most relevant ones for a query, and generates an answer using the retrieved context.
- `search_knowledge_base`: Search the knowledge base for relevant documents.
- `add_to_knowledge_base`: Add new content to the knowledge base.
## Features
## Prerequisites
- **Embeddings** Uses Ollamas embedding endpoint (`ollama.embeddings`) with caching.
- **LLM** Uses Ollamas chat endpoint (`ollama.chat`) for generation.
- **RAG** Cosine similarity based retrieval of topk documents.
- **FastAPI** Exposes a REST API for adding documents and asking questions.
- **Docker** Containerized with Ollama and FastAPI.
- Python 3.10+
- An Ollama server running locally (e.g., `ollama serve`).
- The Ollama model you want to use (default is `mistral`).
## Installation
## Setup
```bash
# Clone the repository
# Clone the repo
git clone https://git.brojs.ru/kuzakhmetovartur/agent-s-rag-pamyatyu.git
cd agent-s-rag-pamyatyu
# Create a virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Build Docker image
docker build -t rag-agent .
# Install dependencies
pip install -r requirements.txt
# Run container
docker run -p 8000:8000 rag-agent
```
`requirements.txt` contains:
The API will be available at `http://localhost:8000`.
```text
langchain
langchain-community
openai
```
## API Endpoints
## Configuration
| Method | Path | Description |
|--------|-----------|---------------------------------|
| POST | /documents | Add a document to the agent. |
| POST | /ask | Ask a question; returns answer. |
Set the Ollama model via environment variable (optional):
### Example
```bash
export OLLAMA_MODEL=mistral # or any other model available in Ollama
# Add a document
curl -X POST http://localhost:8000/documents \
-H "Content-Type: application/json" \
-d '{"text":"Python is a programming language."}'
# Ask a question
curl -X POST http://localhost:8000/ask \
-H "Content-Type: application/json" \
-d '{"query":"What is Python?"}'
```
If you run the Ollama server on a nondefault host/port, set:
## Dependencies
```bash
export OLLAMA_HOST=http://localhost:11434
```
- `ollama` Ollama client for embeddings and chat.
- `fastapi` Web framework.
- `uvicorn` ASGI server.
- `numpy` Numerical operations.
- `pydantic` Data validation.
## Running the Agent
```bash
python src/agent.py
```
You will see a prompt:
```
Welcome to the RAG Agent. Type 'exit' to quit.
User:
```
- **Add knowledge**:
`add_to_knowledge_base This is a new piece of information.`
- **Search knowledge**:
`search_knowledge_base information`
The agent will automatically decide which tool to use based on the user query.
## Example Session
```
User: add_to_knowledge_base Python is a versatile programming language.
Agent: Document added. Total documents: 1.
User: search_knowledge_base programming language
Agent: Python is a versatile programming language.
```
## Notes
- The knowledge base is **inmemory**; data will be lost when the program exits.
- For persistent storage, replace the inmemory implementation with a vector database such as Chroma or FAISS.
- The LLM used for generation is OpenAIs GPT3.5 via the `openai` package. Adjust the `OpenAI` initialization if you prefer another model.
All dependencies are listed in `requirements.txt`.
## License
MIT License
MIT License
---
This implementation follows the assignment constraints: **only Ollama** is used for embeddings and LLM, no OpenAI services are involved.