Update README.md
This commit is contained in:
@@ -1,77 +1,79 @@
|
||||
# RAG‑Agent with ChromaDB and Web Search
|
||||
# RAG Agent with ChromaDB and Tavily
|
||||
|
||||
This repository implements a simple RAG (Retrieval‑Augmented Generation) agent that can answer questions using a local knowledge base stored in **ChromaDB** or by searching the web via **Tavily**. The agent automatically chooses the appropriate source and reports it in the answer.
|
||||
This repository contains a lightweight RAG (Retrieval‑Augmented Generation) agent that:
|
||||
|
||||
## Features
|
||||
1. Stores local knowledge in **ChromaDB** using **Ollama** embeddings.
|
||||
2. Performs semantic search over the local store.
|
||||
3. Falls back to **Tavily** web search for up‑to‑date information.
|
||||
4. Decides automatically which source to use and indicates the source in the answer.
|
||||
|
||||
* **Local semantic search** – Uses a ChromaDB vector store backed by Ollama embeddings.
|
||||
* **Web search** – Uses Tavily to fetch up‑to‑date information.
|
||||
* **Automatic source selection** – The agent decides whether to query the local KB or the web.
|
||||
* **Persisted vector store** – Data is stored on disk and reused across runs.
|
||||
* **Simple CLI** – Chat loop with `exit` to quit.
|
||||
## Prerequisites
|
||||
|
||||
## Setup
|
||||
* Python 3.10+ (recommended via `pyenv` or `conda`).
|
||||
* [Ollama](https://ollama.ai/) installed locally.
|
||||
* A Tavily API key – set it in a `.env` file.
|
||||
|
||||
```bash
|
||||
# 1. Clone the repo
|
||||
git clone <repo-url>
|
||||
cd <repo-dir>
|
||||
|
||||
# 2. (Optional) Create a virtual environment
|
||||
python -m venv venv
|
||||
source venv/bin/activate # Windows: venv\Scripts\activate
|
||||
|
||||
# 3. Install dependencies
|
||||
pip install -r requirements.txt
|
||||
|
||||
# 4. Pull required Ollama models
|
||||
# Pull the required models
|
||||
ollama pull llama3
|
||||
ollama pull nomic-embed-text
|
||||
```
|
||||
|
||||
# 5. Set Tavily API key
|
||||
export TAVILY_API_KEY=your_api_key # Windows: set TAVILY_API_KEY=your_api_key
|
||||
## Installation
|
||||
|
||||
# 6. Prepare documents
|
||||
# Place any .txt or .md files you want to index in the ./documents folder.
|
||||
# They will be automatically loaded into ChromaDB on first run.
|
||||
|
||||
# 7. Run the agent
|
||||
python main.py
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```text
|
||||
Запрос: Какие последние новости про AI-агентов?
|
||||
[Web Search]
|
||||
1. AI Agents: The Future of Automation: ...
|
||||
2. ...
|
||||
Source: tavily
|
||||
```bash
|
||||
# Create a .env file with your Tavily key
|
||||
# TAVILY_API_KEY=YOUR_KEY
|
||||
|
||||
Запрос: Что в наших конспектах про LangGraph?
|
||||
[Local KB]
|
||||
1. LangGraph is a ...
|
||||
2. ...
|
||||
Source: chromadb
|
||||
# Populate the vector store from the documents folder
|
||||
python main.py
|
||||
```
|
||||
|
||||
You will be presented with a prompt. Type your question and press **Enter**.
|
||||
Type `exit` to quit.
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
├── agent.py # Core agent logic and tools
|
||||
├── vectorstore.py # ChromaDB creation and document loading
|
||||
├── rag_tools.py # Web search tool
|
||||
├── agent.py # Agent definition
|
||||
├── main.py # CLI entry point
|
||||
├── tools.py # Local KB and web search tools
|
||||
├── vectorstore.py # ChromaDB helpers
|
||||
├── requirements.txt
|
||||
└── README.md
|
||||
├── README.md
|
||||
└── documents/ # Folder with .txt/.md files to ingest
|
||||
```
|
||||
|
||||
## How It Works
|
||||
|
||||
1. **Vector Store** – `vectorstore.py` creates a ChromaDB instance backed by
|
||||
`OllamaEmbeddings`. Documents from `documents/` are chunked with
|
||||
`RecursiveCharacterTextSplitter` and added to the store.
|
||||
|
||||
2. **Tools** – `tools.py` exposes two LangChain tools:
|
||||
* `search_local_kb` – semantic search in ChromaDB.
|
||||
* `web_search` – web search via Tavily.
|
||||
|
||||
3. **Agent** – `agent.py` builds an OpenAI‑functions‑style agent that
|
||||
chooses between the two tools based on the user’s query. The system prompt
|
||||
instructs the LLM to use `search_local_kb` for knowledge‑base queries and
|
||||
`web_search` for recent facts. The answer always contains a source tag.
|
||||
|
||||
4. **CLI** – `main.py` ties everything together: it loads the vector store,
|
||||
creates the agent and runs an interactive chat loop.
|
||||
|
||||
## Extending
|
||||
|
||||
* **Add more tools** – Define new functions decorated with `@tool` and add them to the `tools` list.
|
||||
* **Change LLM** – Swap `ChatOllama` for another provider (e.g., OpenAI) by adjusting the import and model name.
|
||||
* **Custom prompt** – Edit `agent_prompt` in `agent.py` to modify the agent’s instruction.
|
||||
* Replace the LLM with any other LangChain‑compatible model.
|
||||
* Add more tools (e.g., database queries, file system access).
|
||||
* Persist the vector store across runs – it already does this via `persist_directory`.
|
||||
|
||||
---
|
||||
|
||||
Happy querying!
|
||||
Happy experimenting!
|
||||
Reference in New Issue
Block a user