This commit is contained in:
@@ -1,113 +1,64 @@
|
||||
# Agent with RAG Memory
|
||||
# Agent with RAG Memory using Qdrant and Ollama
|
||||
|
||||
This repository contains a simple **Retrieval‑Augmented Generation (RAG)** agent
|
||||
implemented with LangChain, FAISS for vector storage, and OpenAI embeddings
|
||||
and LLM. It also provides an `auto_check_graph` function that verifies the
|
||||
generated answer against a ground‑truth mapping and returns a `verdict_row`.
|
||||
This project demonstrates a simple Retrieval-Augmented Generation (RAG) agent built with LangChain, Qdrant, and Ollama. The agent uses Ollama embeddings for vector representation and Qdrant as the vector store.
|
||||
|
||||
> **Important**
|
||||
> The auto‑check graph must return a `verdict_row`. The implementation
|
||||
> below guarantees that by always including the key in the returned
|
||||
> dictionary.
|
||||
## Prerequisites
|
||||
|
||||
## Features
|
||||
|
||||
- **RAG Agent** – Load documents, embed them, store in FAISS, and answer queries.
|
||||
- **Auto‑Check Graph** – Run a query, generate an answer, compare it to a
|
||||
ground‑truth answer, and return a verdict (`PASS`, `FAIL`, or `UNKNOWN`).
|
||||
- **Unit Tests** – Verify that the agent and auto‑check graph work as
|
||||
expected.
|
||||
- Python 3.10+
|
||||
- Qdrant server running locally or accessible remotely
|
||||
- Ollama server running locally or accessible remotely
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
# Create a virtual environment (recommended)
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate # On Windows: .venv\Scripts\activate
|
||||
# Clone the repository
|
||||
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 use `venv\Scripts\activate`
|
||||
|
||||
# Install dependencies
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
`requirements.txt` contains:
|
||||
## Configuration
|
||||
|
||||
```
|
||||
langchain
|
||||
openai
|
||||
faiss-cpu
|
||||
pytest
|
||||
```
|
||||
|
||||
> **OpenAI API Key**
|
||||
> If you want to use real embeddings and LLM, set the environment variable
|
||||
> `OPENAI_API_KEY`:
|
||||
|
||||
```bash
|
||||
export OPENAI_API_KEY="sk-..."
|
||||
```
|
||||
|
||||
If the key is not set, the agent falls back to `FakeEmbeddings` and
|
||||
`FakeLLM`, which are suitable for local testing and unit tests.
|
||||
|
||||
## Usage
|
||||
Edit `config.py` to match your environment:
|
||||
|
||||
```python
|
||||
from src.index import RAGAgent, auto_check_graph
|
||||
# Qdrant settings
|
||||
QDRANT_HOST = "localhost"
|
||||
QDRANT_PORT = 6333
|
||||
QDRANT_API_KEY = None
|
||||
QDRANT_COLLECTION = "rag_collection"
|
||||
|
||||
# Create agent
|
||||
agent = RAGAgent()
|
||||
|
||||
# Add documents (e.g., from a directory)
|
||||
agent.add_documents([
|
||||
"The capital of France is Paris.",
|
||||
"William Shakespeare wrote Hamlet."
|
||||
])
|
||||
|
||||
# Define ground truth mapping
|
||||
ground_truth = {
|
||||
"What is the capital of France?": "Paris",
|
||||
"Who wrote Hamlet?": "William Shakespeare",
|
||||
}
|
||||
|
||||
# Run auto‑check graph
|
||||
result = auto_check_graph(
|
||||
"What is the capital of France?",
|
||||
agent,
|
||||
ground_truth
|
||||
)
|
||||
|
||||
print(result)
|
||||
# Output:
|
||||
# {
|
||||
# "verdict_row": "PASS",
|
||||
# "answer": "Paris",
|
||||
# "expected": "Paris"
|
||||
# }
|
||||
# Ollama settings
|
||||
OLLAMA_MODEL = "llama3"
|
||||
```
|
||||
|
||||
## Running Tests
|
||||
## Running the Agent
|
||||
|
||||
```bash
|
||||
pytest
|
||||
python src/main.py
|
||||
```
|
||||
|
||||
The tests cover:
|
||||
The script will:
|
||||
|
||||
- Adding documents and querying.
|
||||
- Auto‑check graph returning `PASS`, `FAIL`, and `UNKNOWN` verdicts.
|
||||
- Handling of empty queries and missing ground‑truth.
|
||||
1. Connect to Qdrant.
|
||||
2. Create an Ollama embeddings instance.
|
||||
3. Add sample documents to the collection if it is empty.
|
||||
4. Build a RetrievalQA chain using the Ollama LLM.
|
||||
5. Execute a sample query and print the answer.
|
||||
|
||||
## Project Structure
|
||||
## Extending
|
||||
|
||||
```
|
||||
src/
|
||||
├── index.py # Main implementation
|
||||
tests/
|
||||
├── test_agent.py # Unit tests
|
||||
README.md
|
||||
requirements.txt
|
||||
```
|
||||
- Replace the sample documents with your own corpus.
|
||||
- Adjust the `chain_type` in `src/agent.py` if you need a different retrieval strategy.
|
||||
- Use environment variables or a `.env` file to store sensitive information like `QDRANT_API_KEY`.
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
MIT License
|
||||
---
|
||||
Reference in New Issue
Block a user