3.5 KiB
RAG‑Agent with ChromaDB and Web Search
A lightweight RAG (Retrieval‑Augmented Generation) agent that uses a local ChromaDB vector store for knowledge retrieval and Tavily for live web search.
The agent automatically decides whether to answer from the local knowledge base or to fetch fresh information from the web.
Prerequisites
• Python 3.10+
• Ollama (LLM & embeddings)
• Tavily API key
📦 Project Structure
.
├── vectorstore.py # Vector store creation & document ingestion
├── agent.py # RAG agent implementation (not shown in the prompt)
├── .env # Tavily API key
├── requirements.txt # Dependencies
└── README.md
🚀 Installation
# 1. Pull required models into Ollama
ollama pull llama3
ollama pull nomic-embed-text
# 2. Install Python dependencies
pip install -r requirements.txt
requirements.txt
langchain
langchain-chroma
langchain-tavily
langchain-ollama
tavily-python
chromadb
python-dotenv
Note
:
If you use a different LLM or embeddings provider, adjust thecreate_vectorstorefunction accordingly.
⚙️ Configuration
Create a .env file in the project root:
TAVILY_API_KEY=your_tavily_api_key_here
The key is used by the Tavily client for web search.
📚 Using the Vector Store
1. Create the store
from vectorstore import create_vectorstore
vectorstore = create_vectorstore("./chroma_db")
2. Load documents into the store
from vectorstore import load_documents
# Directory containing .txt or .md files
load_documents("./knowledge_base", vectorstore)
The function will:
- Read all
.txtand.mdfiles in the given directory. - Split them into chunks using
RecursiveCharacterTextSplitter. - Add the chunks to the Chroma collection.
🧩 Running the Agent
Assumption:
agent.pycontains the main RAG agent logic that importsvectorstore.py.
The agent automatically chooses between the local vector store and Tavily search.
python agent.py
The agent will:
- Prompt the user for a question.
- Query the vector store for relevant chunks.
- If the answer is insufficient, perform a web search via Tavily.
- Generate a final answer with the chosen source.
🔧 Example Workflow
$ python agent.py
Enter your question (or 'exit' to quit): What is the capital of France?
Answer: The capital of France is Paris.
Source: Local knowledge base (retrieved from chroma_db)
If the question is about a very recent event:
$ python agent.py
Enter your question (or 'exit' to quit): Who won the 2024 World Series?
Answer: The 2024 World Series was won by the Texas Rangers.
Source: Web search (Tavily)
📦 Adding New Documents
- Drop your
.txtor.mdfiles into theknowledge_base/directory. - Run:
python -c "from vectorstore import load_documents, create_vectorstore; load_documents('knowledge_base', create_vectorstore())"
The new documents will be indexed automatically.
🛠️ Troubleshooting
| Symptom | Likely Cause | Fix |
|---|---|---|
ModuleNotFoundError: No module named 'langchain_ollama' |
Missing dependency | pip install langchain-ollama |
Ollama not running |
Ollama daemon stopped | ollama serve |
| Tavily errors | Invalid API key | Verify .env and restart |
📄 License
MIT License – feel free to adapt and extend.