This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
# Agent with RAG Memory
|
||||
# RAG Agent with Ollama Embeddings
|
||||
|
||||
This project implements a retrieval‑augmented generation (RAG) agent that uses **Qdrant** as the vector store and **Ollama** for local LLM inference.
|
||||
The agent follows the latest LangChain API and is fully configurable via environment variables.
|
||||
This project demonstrates a simple Retrieval-Augmented Generation (RAG) agent that uses **OllamaEmbeddings** for vector similarity search and a local in‑memory knowledge base.
|
||||
The agent is built with **LangChain** and exposes two tools:
|
||||
|
||||
## Features
|
||||
- `search_knowledge_base`: Search the knowledge base for relevant documents.
|
||||
- `add_to_knowledge_base`: Add new content to the knowledge base.
|
||||
|
||||
- **Qdrant** vector store (via `langchain-qdrant`)
|
||||
- **Ollama** local LLM integration (via `langchain-ollama`)
|
||||
- Retrieval‑augmented generation with conversation memory
|
||||
- Simple CLI interface for quick testing
|
||||
## Prerequisites
|
||||
|
||||
- Python 3.10+
|
||||
- An Ollama server running locally (e.g., `ollama serve`).
|
||||
- The Ollama model you want to use (default is `mistral`).
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -17,82 +19,72 @@ The agent follows the latest LangChain API and is fully configurable via environ
|
||||
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: .venv\\Scripts\\activate
|
||||
# Create a virtual environment
|
||||
python -m venv venv
|
||||
source venv/bin/activate # On Windows: venv\Scripts\activate
|
||||
|
||||
# Install dependencies
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
`requirements.txt` contains:
|
||||
|
||||
```text
|
||||
langchain
|
||||
langchain-community
|
||||
openai
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Create a `.env` file in the project root (or set environment variables directly):
|
||||
|
||||
```dotenv
|
||||
# Qdrant
|
||||
QDRANT_HOST=localhost
|
||||
QDRANT_PORT=6333
|
||||
QDRANT_API_KEY= # leave empty if no key
|
||||
|
||||
# Ollama
|
||||
OLLAMA_HOST=localhost
|
||||
OLLAMA_PORT=11434
|
||||
OLLAMA_MODEL=llama3
|
||||
|
||||
# Optional: collection name
|
||||
QDRANT_COLLECTION=documents
|
||||
```
|
||||
|
||||
> **Note**: The Qdrant instance must be running and accessible at the specified host/port.
|
||||
> The Ollama server must be running locally and expose the chosen model.
|
||||
|
||||
## Usage
|
||||
|
||||
### Adding Documents
|
||||
|
||||
```python
|
||||
from src.agent import Agent
|
||||
from langchain_core.documents import Document
|
||||
|
||||
agent = Agent()
|
||||
|
||||
docs = [
|
||||
Document(page_content="Python is a programming language.", metadata={"source": "python.txt"}),
|
||||
Document(page_content="LangChain is a framework for LLM applications.", metadata={"source": "langchain.txt"}),
|
||||
]
|
||||
|
||||
agent.add_documents(docs)
|
||||
```
|
||||
|
||||
### Querying the Agent
|
||||
Set the Ollama model via environment variable (optional):
|
||||
|
||||
```bash
|
||||
python -m src.agent "What is LangChain?"
|
||||
export OLLAMA_MODEL=mistral # or any other model available in Ollama
|
||||
```
|
||||
|
||||
or from Python:
|
||||
If you run the Ollama server on a non‑default host/port, set:
|
||||
|
||||
```python
|
||||
response = agent.run("What is LangChain?")
|
||||
print(response["answer"])
|
||||
```bash
|
||||
export OLLAMA_HOST=http://localhost:11434
|
||||
```
|
||||
|
||||
The response will include the answer and the source documents used.
|
||||
## Running the Agent
|
||||
|
||||
## Project Structure
|
||||
```bash
|
||||
python src/agent.py
|
||||
```
|
||||
|
||||
You will see a prompt:
|
||||
|
||||
```
|
||||
agent-s-rag-pamyatyu/
|
||||
├── src/
|
||||
│ ├── agent.py
|
||||
│ ├── config.py
|
||||
│ └── vector_store.py
|
||||
├── requirements.txt
|
||||
├── pyproject.toml
|
||||
└── README.md
|
||||
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 **in‑memory**; data will be lost when the program exits.
|
||||
- For persistent storage, replace the in‑memory implementation with a vector database such as Chroma or FAISS.
|
||||
- The LLM used for generation is OpenAI’s GPT‑3.5 via the `openai` package. Adjust the `OpenAI` initialization if you prefer another model.
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
Reference in New Issue
Block a user