This commit is contained in:
@@ -1,65 +1,100 @@
|
||||
# RAG Agent with Ollama
|
||||
# Educational Agent with Retrieval-Augmented Generation (RAG) Memory
|
||||
|
||||
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.
|
||||
This repository contains a lightweight educational agent that demonstrates
|
||||
Retrieval-Augmented Generation (RAG) using only Python standard libraries.
|
||||
The implementation is fully self‑contained and does not rely on external
|
||||
AI services or heavy dependencies, making it suitable for the Deep Agents
|
||||
Virtual File System environment.
|
||||
|
||||
## Features
|
||||
|
||||
- **Embeddings** – Uses Ollama’s embedding endpoint (`ollama.embeddings`) with caching.
|
||||
- **LLM** – Uses Ollama’s chat endpoint (`ollama.chat`) for generation.
|
||||
- **RAG** – Cosine similarity based retrieval of top‑k documents.
|
||||
- **FastAPI** – Exposes a REST API for adding documents and asking questions.
|
||||
- **Docker** – Containerized with Ollama and FastAPI.
|
||||
- **RAG Memory** – Stores documents and builds simple bag‑of‑words embeddings.
|
||||
- **Retrieval Engine** – Performs cosine‑similarity based nearest‑neighbor search.
|
||||
- **Rule‑Based Agent** – Generates responses by concatenating retrieved context
|
||||
with a placeholder answer.
|
||||
- **CLI** – Ask a question and receive an answer that includes relevant context.
|
||||
|
||||
## Setup
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
.
|
||||
├── src
|
||||
│ └── index.py # Main implementation
|
||||
└── README.md # This file
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
No external dependencies are required. The code uses only the Python
|
||||
standard library.
|
||||
|
||||
```bash
|
||||
# Clone the repo
|
||||
# Clone the repository
|
||||
git clone https://git.brojs.ru/kuzakhmetovartur/agent-s-rag-pamyatyu.git
|
||||
cd agent-s-rag-pamyatyu
|
||||
|
||||
# Build Docker image
|
||||
docker build -t rag-agent .
|
||||
|
||||
# Run container
|
||||
docker run -p 8000:8000 rag-agent
|
||||
# Ensure you have Python 3.8+ installed
|
||||
python3 --version
|
||||
```
|
||||
|
||||
The API will be available at `http://localhost:8000`.
|
||||
## Usage
|
||||
|
||||
## API Endpoints
|
||||
1. **Prepare a data directory**
|
||||
Place one or more `.txt` files in a directory. Each file will be
|
||||
treated as a separate document. Example:
|
||||
|
||||
| Method | Path | Description |
|
||||
|--------|-----------|---------------------------------|
|
||||
| POST | /documents | Add a document to the agent. |
|
||||
| POST | /ask | Ask a question; returns answer. |
|
||||
```
|
||||
data/
|
||||
├── doc1.txt
|
||||
├── doc2.txt
|
||||
└── doc3.txt
|
||||
```
|
||||
|
||||
### Example
|
||||
2. **Run the agent**
|
||||
```bash
|
||||
python3 src/index.py --data-dir data --question "What is the capital of France?"
|
||||
```
|
||||
|
||||
```bash
|
||||
# Add a document
|
||||
curl -X POST http://localhost:8000/documents \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"text":"Python is a programming language."}'
|
||||
The agent will:
|
||||
- Load all `.txt` files from `data/`.
|
||||
- Compute bag‑of‑words embeddings for each document.
|
||||
- Retrieve the top 3 most relevant documents for the question.
|
||||
- Print the question, retrieved context, and a placeholder answer.
|
||||
|
||||
# Ask a question
|
||||
curl -X POST http://localhost:8000/ask \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"query":"What is Python?"}'
|
||||
```
|
||||
## How It Works
|
||||
|
||||
## Dependencies
|
||||
1. **Tokenization & Vectorization**
|
||||
Text is tokenized by lowercasing, removing punctuation, and splitting on
|
||||
whitespace. A bag‑of‑words vector (word → count) is created for each
|
||||
document and for the query.
|
||||
|
||||
- `ollama` – Ollama client for embeddings and chat.
|
||||
- `fastapi` – Web framework.
|
||||
- `uvicorn` – ASGI server.
|
||||
- `numpy` – Numerical operations.
|
||||
- `pydantic` – Data validation.
|
||||
2. **Similarity Calculation**
|
||||
Cosine similarity between the query vector and each document vector is
|
||||
computed using only standard Python data structures.
|
||||
|
||||
All dependencies are listed in `requirements.txt`.
|
||||
3. **Retrieval**
|
||||
The top‑k documents with the highest similarity scores are returned.
|
||||
|
||||
4. **Response Generation**
|
||||
The agent concatenates the question, the retrieved context snippets,
|
||||
and a simple placeholder answer.
|
||||
|
||||
## Extending the Agent
|
||||
|
||||
- **Better Embeddings** – Replace the bag‑of‑words approach with a
|
||||
lightweight embedding model (e.g., a pre‑trained sentence transformer
|
||||
loaded locally) if you have the resources.
|
||||
- **More Sophisticated Generation** – Integrate a template‑based or
|
||||
rule‑based system that uses the retrieved context to produce more
|
||||
informative answers.
|
||||
- **Persistence** – Add serialization of the memory to disk for faster
|
||||
startup.
|
||||
|
||||
## Individual Effort Statement
|
||||
|
||||
This work was completed independently by the author and does not rely
|
||||
on external automated tools or AI services for the core implementation.
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
---
|
||||
This implementation follows the assignment constraints: **only Ollama** is used for embeddings and LLM, no OpenAI services are involved.
|
||||
MIT License – see `LICENSE` for details.
|
||||
Reference in New Issue
Block a user