134 lines
3.3 KiB
Markdown
134 lines
3.3 KiB
Markdown
# RAG‑Agent with ChromaDB and Web Search
|
||
|
||
A lightweight AI agent that can answer user questions by searching a local knowledge base stored in **ChromaDB** and the web via **Tavily**.
|
||
The agent automatically decides which source to use, making it ideal for exam‑style assignments or quick prototyping.
|
||
|
||
---
|
||
|
||
## Table of Contents
|
||
- [Features](#features)
|
||
- [Prerequisites](#prerequisites)
|
||
- [Installation](#installation)
|
||
- [Project Structure](#project-structure)
|
||
- [Running the Agent](#running-the-agent)
|
||
- `vectorstore.py`
|
||
- `main.py`
|
||
- [Example Usage](#example-usage)
|
||
- [License](#license)
|
||
|
||
---
|
||
|
||
## Features
|
||
| Feature | Description |
|
||
|---------|-------------|
|
||
| **Local RAG** | Stores documents in a persistent ChromaDB collection. |
|
||
| **Web Search** | Uses Tavily to fetch up‑to‑date information from the internet. |
|
||
| **LLM & Embeddings** | Powered by Ollama (`llama3` for generation, `nomic-embed-text` for embeddings). |
|
||
| **Agent** | LangChain agent that chooses between local and web sources automatically. |
|
||
| **Easy Setup** | One‑liner install script and minimal configuration. |
|
||
|
||
---
|
||
|
||
## Prerequisites
|
||
|
||
| Requirement | Command / Note |
|
||
|-------------|----------------|
|
||
| Python | `>=3.10` (recommended 3.11+) |
|
||
| Ollama | Install from <https://ollama.ai> |
|
||
| Tavily API Key | Sign up at <https://tavily.com> and set `TAVILY_API_KEY` in `.env`. |
|
||
|
||
---
|
||
|
||
## Installation
|
||
|
||
```bash
|
||
# Pull required models into Ollama
|
||
ollama pull llama3
|
||
ollama pull nomic-embed-text
|
||
|
||
# Install Python dependencies
|
||
pip install langchain langchain-chroma langchain-tavily langchain-ollama tavily-python chromadb python-dotenv
|
||
```
|
||
|
||
Create a `.env` file in the project root:
|
||
|
||
```dotenv
|
||
TAVILY_API_KEY=your_tavily_api_key_here
|
||
```
|
||
|
||
---
|
||
|
||
## Project Structure
|
||
|
||
```
|
||
.
|
||
├── vectorstore.py # Helpers for creating/loading ChromaDB and adding docs
|
||
├── main.py # Agent entry point
|
||
└── .env # Tavily API key (not committed)
|
||
```
|
||
|
||
- **`vectorstore.py`**
|
||
* `create_vectorstore(persist_directory)` – returns a ready‑to‑use Chroma collection.
|
||
* `load_documents(directory, vectorstore)` – reads `.txt/.md`, splits into chunks, and adds them to the store.
|
||
|
||
- **`main.py`**
|
||
Sets up the LangChain agent with two tools:
|
||
- `search_local_kb(query, top_k)` – semantic search in Chroma.
|
||
- `web_search(query)` – Tavily web search.
|
||
The agent decides which tool to invoke based on the query.
|
||
|
||
---
|
||
|
||
## Running the Agent
|
||
|
||
### 1. Prepare the Knowledge Base
|
||
|
||
```bash
|
||
# Place your .txt or .md files into a folder, e.g., ./docs
|
||
mkdir docs
|
||
echo "Hello world!" > docs/hello.txt
|
||
|
||
# Load them into ChromaDB
|
||
python -c "
|
||
from vectorstore import create_vectorstore, load_documents
|
||
vs = create_vectorstore()
|
||
load_documents('docs', vs)
|
||
print('Documents loaded')
|
||
"
|
||
```
|
||
|
||
### 2. Start the Agent
|
||
|
||
```bash
|
||
python main.py
|
||
```
|
||
|
||
You will see a prompt:
|
||
|
||
```
|
||
> What would you like to know?
|
||
```
|
||
|
||
Type any question; the agent will answer using either the local KB or Tavily.
|
||
|
||
---
|
||
|
||
## Example Usage
|
||
|
||
```text
|
||
> Who is the current President of France?
|
||
|
||
Agent: The current President of France is Emmanuel Macron. (Source: web_search)
|
||
|
||
> Summarize the contents of hello.txt
|
||
|
||
Agent: The file contains a simple greeting: "Hello world!". (Source: search_local_kb)
|
||
```
|
||
|
||
The agent automatically selects the most relevant source.
|
||
|
||
---
|
||
|
||
## License
|
||
|
||
MIT © 2026 – feel free to adapt and extend. |