Update README.md
This commit is contained in:
@@ -1,84 +1,99 @@
|
|||||||
# RAG‑Agent with Qdrant & Ollama
|
# RAG Agent with Qdrant & Ollama
|
||||||
|
|
||||||
This repository contains a minimal but functional implementation of a **RAG (Retrieval‑Augmented Generation) agent** that:
|
This project implements a simple **RAG (Retrieval‑Augmented Generation) agent** that uses:
|
||||||
|
|
||||||
* Stores embeddings in a **Qdrant** vector database.
|
* **Qdrant** – a vector database for storing embeddings.
|
||||||
* Generates embeddings with **Ollama** (`nomic-embed-text`).
|
* **Ollama** – local LLM and embedding model (`llama3` and `nomic‑embed‑text`).
|
||||||
* Uses **LangChain** (v1+) for the agent, tools and prompt‑engineering.
|
* **LangChain** – framework for building the agent and tools.
|
||||||
|
|
||||||
The agent can:
|
The agent can:
|
||||||
|
|
||||||
* Search the knowledge base (`/search`).
|
* **Add** documents to the knowledge base.
|
||||||
* Add new documents (`/add`).
|
* **Search** the knowledge base for relevant chunks.
|
||||||
* Interact through a simple CLI.
|
* 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
|
## Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 1. Pull required Ollama models
|
# Pull the required Ollama models
|
||||||
ollama pull llama3
|
ollama pull llama3
|
||||||
ollama pull nomic-embed-text
|
ollama pull nomic-embed-text
|
||||||
|
|
||||||
# 2. Install Python dependencies
|
# Install Python dependencies
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
|
|
||||||
# 3. Run the CLI
|
|
||||||
python -m src.cli
|
|
||||||
```
|
|
||||||
|
|
||||||
> **Note**: Qdrant must be running locally on port 6333. You can start it using Docker:
|
|
||||||
>
|
|
||||||
> ```bash
|
|
||||||
> docker run -p 6333:6333 qdrant/qdrant
|
|
||||||
> ```
|
|
||||||
|
|
||||||
## Directory structure
|
|
||||||
|
|
||||||
```
|
|
||||||
workspace/task-6a02e23da6fe2e4ac16acf65/
|
|
||||||
├─ src/
|
|
||||||
│ ├─ vector_store.py # Qdrant wrapper
|
|
||||||
│ ├─ tools.py # LangChain tools
|
|
||||||
│ ├─ agent.py # Agent implementation
|
|
||||||
│ ├─ loader.py # Utility for bulk loading
|
|
||||||
│ └─ cli.py # Interactive CLI
|
|
||||||
├─ requirements.txt
|
|
||||||
└─ README.md
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### Load documents from a folder
|
### 1. Load documents into the knowledge base
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python -m src.loader /path/to/text/files
|
python -m src.loader /path/to/text/files
|
||||||
```
|
```
|
||||||
|
|
||||||
### Start the interactive CLI
|
All `.txt` files in the directory (recursively) are added to the vector store.
|
||||||
|
|
||||||
|
### 2. Start the interactive CLI
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python -m src.cli
|
python -m src.cli
|
||||||
```
|
```
|
||||||
|
|
||||||
- `/add` – add a new document.
|
Once started you can use the following commands:
|
||||||
- `/search` – perform a semantic search.
|
|
||||||
- `/quit` – exit.
|
|
||||||
|
|
||||||
Any other input is treated as a user message and processed by the agent.
|
| 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
|
## How it works
|
||||||
|
|
||||||
1. **Vector store** – `KnowledgeBase` wraps `QdrantVectorStore`. It splits documents into chunks using `RecursiveCharacterTextSplitter`, embeds them with `OllamaEmbeddings`, and stores the vectors.
|
* **Vector Store** – `KnowledgeBase` wraps a `QdrantVectorStore`. It creates the collection only if it does not exist, preventing accidental data loss.
|
||||||
2. **Tools** – Two tools (`search_knowledge_base`, `add_to_knowledge_base`) are exposed to the agent via LangChain's `@tool` decorator.
|
* **Chunking** – Documents are split into 500‑character chunks with 50‑character overlap using `RecursiveCharacterTextSplitter`.
|
||||||
3. **Agent** – Built with `create_tool_calling_agent` and `AgentExecutor`. The system prompt encourages the assistant to use the tools.
|
* **Tools** – Two LangChain tools are exposed:
|
||||||
4. **CLI** – Provides a simple REPL for adding documents, searching, and chatting with the agent.
|
* `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
|
## Extending
|
||||||
|
|
||||||
* Replace the embedding model with any Ollama model.
|
* Replace the embedding model by editing `KnowledgeBase.__init__`.
|
||||||
* Swap Qdrant for another vector store supported by LangChain.
|
* Add more tools (e.g., delete from knowledge base) following the same pattern.
|
||||||
* Add more tools (e.g., delete, update) following the same pattern.
|
* Deploy the agent as a web service by wrapping `run_query` in a FastAPI endpoint.
|
||||||
|
|
||||||
---
|
## License
|
||||||
|
|
||||||
Happy experimenting!
|
MIT License.
|
||||||
Reference in New Issue
Block a user