Update README.md
This commit is contained in:
@@ -1,61 +1,82 @@
|
|||||||
# RAG Agent with ChromaDB and Web Search
|
# RAG Agent with ChromaDB and Web Search
|
||||||
|
|
||||||
This repository implements a simple RAG (Retrieval‑Augmented Generation) agent that can answer questions using a local knowledge base stored in **ChromaDB** and also perform real‑time web search via **Tavily**. The agent automatically decides which source to use and reports the chosen source in the answer.
|
This repository implements a simple RAG (Retrieval-Augmented Generation) agent that can:
|
||||||
|
|
||||||
## Features
|
1. Search a local knowledge base stored in **ChromaDB** using semantic embeddings from **Ollama**.
|
||||||
|
2. Perform real‑time web search via **Tavily**.
|
||||||
|
3. Decide automatically which source to use and indicate the source in the final answer.
|
||||||
|
|
||||||
- **Local knowledge base** – Text files (.txt, .md) are loaded, chunked, and stored in a persistent ChromaDB collection.
|
## Prerequisites
|
||||||
- **Semantic search** – Uses Ollama embeddings (`nomic-embed-text`).
|
|
||||||
- **Web search** – Powered by Tavily.
|
|
||||||
- **Automatic source selection** – The agent chooses between local and web search based on the query.
|
|
||||||
- **CLI** – Simple chat loop with `exit` to quit.
|
|
||||||
|
|
||||||
## Setup
|
- Python 3.10+ (recommended via `pyenv` or `conda`).
|
||||||
|
- Ollama installed locally and the following models pulled:
|
||||||
|
```bash
|
||||||
|
ollama pull llama3
|
||||||
|
ollama pull nomic-embed-text
|
||||||
|
```
|
||||||
|
- A Tavily API key. Create a `.env` file in the project root with:
|
||||||
|
```text
|
||||||
|
TAVILY_API_KEY=YOUR_KEY_HERE
|
||||||
|
```
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 1. Create a virtual environment (optional but recommended)
|
# Optional: create a virtual environment
|
||||||
python -m venv venv
|
python -m venv venv
|
||||||
source venv/bin/activate # Windows: venv\Scripts\activate
|
source venv/bin/activate # Windows: venv\Scripts\activate
|
||||||
|
|
||||||
# 2. Install dependencies
|
# Install dependencies
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
|
|
||||||
# 3. Pull required Ollama models
|
|
||||||
ollama pull llama3
|
|
||||||
ollama pull nomic-embed-text
|
|
||||||
|
|
||||||
# 4. Set your Tavily API key
|
|
||||||
export TAVILY_API_KEY=YOUR_KEY # Windows: set TAVILY_API_KEY=YOUR_KEY
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Preparing the Knowledge Base
|
||||||
|
|
||||||
1. **Load documents** – Place your `.txt` or `.md` files in the `documents/` folder.
|
Place any `.txt` or `.md` files you want the agent to know about in the `documents/` folder.
|
||||||
2. **Run the agent**
|
Run the following command once to load them into ChromaDB:
|
||||||
```bash
|
|
||||||
python agent.py
|
|
||||||
```
|
|
||||||
3. **Chat** – Type your question. Type `exit` to quit.
|
|
||||||
|
|
||||||
## Example
|
```bash
|
||||||
|
python -c "from vectorstore import create_vectorstore, load_documents; store=create_vectorstore(); load_documents('./documents', store)"
|
||||||
|
```
|
||||||
|
|
||||||
|
The vector store is persisted in the `chroma_db/` directory, so the data will be available for subsequent runs.
|
||||||
|
|
||||||
|
## Running the Agent
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python main.py
|
||||||
|
```
|
||||||
|
|
||||||
|
You will see a simple chat loop. Type your questions and the agent will answer.
|
||||||
|
|
||||||
```
|
```
|
||||||
Query: Какие последние новости про AI-агентов?
|
Welcome to the RAG agent. Type 'exit' to quit.
|
||||||
[Web Search] ...
|
|
||||||
Источник: tavily
|
|
||||||
|
|
||||||
Query: Что в наших конспектах про LangGraph?
|
User: What is LangGraph?
|
||||||
[Local KB] ...
|
|
||||||
Источник: chromadb
|
Assistant: LangGraph is a framework for building ...
|
||||||
|
Source: chromadb
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If the information is not present locally, the agent will automatically perform a web search and label the answer with `Source: tavily`.
|
||||||
|
|
||||||
## Project Structure
|
## Project Structure
|
||||||
|
|
||||||
- `vectorstore.py` – Functions to create and load the ChromaDB vector store.
|
```
|
||||||
- `rag_tools.py` – Two LangChain tools: `search_local_kb` and `web_search`.
|
├── agent.py # Core agent logic
|
||||||
- `agent.py` – Main script that sets up the agent and runs the chat loop.
|
├── rag_tools.py # Tool implementations
|
||||||
- `requirements.txt` – Python dependencies.
|
├── vectorstore.py # ChromaDB utilities
|
||||||
- `README.md` – This file.
|
├── main.py # Entry point
|
||||||
|
├── requirements.txt
|
||||||
|
├── .gitignore
|
||||||
|
└── README.md
|
||||||
|
```
|
||||||
|
|
||||||
|
## Extending
|
||||||
|
|
||||||
|
- Add more tools by creating new functions decorated with `@tool`.
|
||||||
|
- Replace the LLM or embeddings with other Ollama models.
|
||||||
|
- Switch to a different vector store (e.g., Qdrant) by updating `vectorstore.py`.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user