99 lines
2.9 KiB
Markdown
99 lines
2.9 KiB
Markdown
# RAG Agent with Qdrant & Ollama
|
||
|
||
This project implements a simple **RAG (Retrieval‑Augmented Generation) agent** that uses:
|
||
|
||
* **Qdrant** – a vector database for storing embeddings.
|
||
* **Ollama** – local LLM and embedding model (`llama3` and `nomic‑embed‑text`).
|
||
* **LangChain** – framework for building the agent and tools.
|
||
|
||
The agent can:
|
||
|
||
* **Add** documents to the knowledge base.
|
||
* **Search** the knowledge base for relevant chunks.
|
||
* Answer user queries using the stored knowledge.
|
||
|
||
## Project structure
|
||
|
||
```
|
||
workspace/
|
||
├── src/
|
||
│ ├── __init__.py
|
||
│ ├── vector_store.py # Qdrant wrapper
|
||
│ ├── tools.py # LangChain tools
|
||
│ ├── agent.py # Agent definition
|
||
│ ├── loader.py # Load all .txt files from a directory
|
||
│ └── cli.py # Interactive command‑line client
|
||
├── requirements.txt
|
||
└── README.md
|
||
```
|
||
|
||
## Installation
|
||
|
||
```bash
|
||
# Pull the required Ollama models
|
||
ollama pull llama3
|
||
ollama pull nomic-embed-text
|
||
|
||
# Install Python dependencies
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
## Usage
|
||
|
||
### 1. Load documents into the knowledge base
|
||
|
||
```bash
|
||
python -m src.loader /path/to/text/files
|
||
```
|
||
|
||
All `.txt` files in the directory (recursively) are added to the vector store.
|
||
|
||
### 2. Start the interactive CLI
|
||
|
||
```bash
|
||
python -m src.cli
|
||
```
|
||
|
||
Once started you can use the following commands:
|
||
|
||
| Command | Description |
|
||
|---------|-------------|
|
||
| `/add <title> <file_path>` | Add a single file to the knowledge base. |
|
||
| `/search <query>` | Search the knowledge base and display top results. |
|
||
| `/quit` | Exit the program. |
|
||
| `/help` | Show help. |
|
||
| Any other text | Sent to the agent as a user query. |
|
||
|
||
### 3. Example session
|
||
|
||
```
|
||
RAG Agent CLI. Type /help for commands.
|
||
> /add example docs/example.txt
|
||
Document 'example' added.
|
||
> /search quantum
|
||
Results:
|
||
1. [example - chunk 0] Quantum mechanics is the branch of physics that deals with...
|
||
> Tell me more about quantum.
|
||
Sure! Here is what I found in the knowledge base: ...
|
||
> /quit
|
||
Goodbye.
|
||
```
|
||
|
||
## How it works
|
||
|
||
* **Vector Store** – `KnowledgeBase` wraps a `QdrantVectorStore`. It creates the collection only if it does not exist, preventing accidental data loss.
|
||
* **Chunking** – Documents are split into 500‑character chunks with 50‑character overlap using `RecursiveCharacterTextSplitter`.
|
||
* **Tools** – Two LangChain tools are exposed:
|
||
* `search_knowledge_base(query, max_results)` – returns a list of relevant chunks.
|
||
* `add_to_knowledge_base(content, title)` – adds a document.
|
||
* **Agent** – Built with `create_agent` from `langchain.agents`. It uses the local `ChatOllama` model (`llama3`).
|
||
|
||
## Extending
|
||
|
||
* Replace the embedding model by editing `KnowledgeBase.__init__`.
|
||
* Add more tools (e.g., delete from knowledge base) following the same pattern.
|
||
* Deploy the agent as a web service by wrapping `run_query` in a FastAPI endpoint.
|
||
|
||
## License
|
||
|
||
MIT License. |