90 lines
2.2 KiB
Markdown
90 lines
2.2 KiB
Markdown
# RAG Agent with Ollama Embeddings
|
||
|
||
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:
|
||
|
||
- `search_knowledge_base`: Search the knowledge base for relevant documents.
|
||
- `add_to_knowledge_base`: Add new content to the knowledge base.
|
||
|
||
## Prerequisites
|
||
|
||
- Python 3.10+
|
||
- An Ollama server running locally (e.g., `ollama serve`).
|
||
- The Ollama model you want to use (default is `mistral`).
|
||
|
||
## Installation
|
||
|
||
```bash
|
||
# Clone the repository
|
||
git clone https://git.brojs.ru/kuzakhmetovartur/agent-s-rag-pamyatyu.git
|
||
cd agent-s-rag-pamyatyu
|
||
|
||
# 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
|
||
|
||
Set the Ollama model via environment variable (optional):
|
||
|
||
```bash
|
||
export OLLAMA_MODEL=mistral # or any other model available in Ollama
|
||
```
|
||
|
||
If you run the Ollama server on a non‑default host/port, set:
|
||
|
||
```bash
|
||
export OLLAMA_HOST=http://localhost:11434
|
||
```
|
||
|
||
## Running the Agent
|
||
|
||
```bash
|
||
python src/agent.py
|
||
```
|
||
|
||
You will see a prompt:
|
||
|
||
```
|
||
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 |