File size: 8,305 Bytes
04d4d26 94e0eef 04d4d26 94e0eef 04d4d26 5121ff0 94e0eef 5121ff0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
---
title: RAG Agent
emoji: π΅π»ββοΈ
colorFrom: indigo
colorTo: indigo
sdk: gradio
sdk_version: 6.0.1
app_file: main.py
pinned: false
hf_oauth: true
hf_oauth_expiration_minutes: 480
---
# π Project Structure
```
mai-rag-agent/
β
βββ π agent/ # Core agent logic
β βββ graph.py # LangGraph workflow definition
β βββ nodes.py # Agent nodes (router, vectordb, web_search, generate)
β βββ prompts.py # System prompts and templates
β βββ state.py # Agent state management (AgentState, RAG_method)
β βββ tools.py # Tool definitions (Tavily, Wikipedia, ArXiv, ChromaDB)
β
βββ π core/ # Business logic layer
β βββ llm.py # LLM initialization (Anthropic Claude)
β βββ rag_agent.py # Main RAGAgent class with graph orchestration
β
βββ π ui/ # User interface
β βββ gradio_components.py # Gradio web interface components
β
βββ π knowledge_base/ # scripts for setting up Chroma
β
βββ π chroma_data/ # Artifacts for Chroma
β
βββ π docs/ # Source documents (PDFs, text files)
β
βββ π main.py # Application entry point
βββ π config.py # Configuration settings
βββ π test_scripts.py # Agent testing script
β
βββ π .env # Environment variables (API keys)
βββ π .gitignore # Git ignore rules
β
βββ π requirements.txt # Python dependencies
βββ π pyproject.toml # Project metadata (if using uv)
β
βββ π README.md # Project documentation (this file)
```
## π Key Components
### π€ Agent Module (`agent/`)
- **`graph.py`**: Defines the LangGraph workflow with conditional routing
- **`nodes.py`**: Implements agent nodes:
- `router_node`: Classifies queries (RAG/WEBSEARCH/GENERAL)
- `vectordb_node`: Retrieves from local ChromaDB
- `web_search_agent_node`: Executes web searches
- `generate_node`: Generates final responses
- **`state.py`**: Defines `AgentState` with message history, routing method, and context
- **`tools.py`**: Tool implementations for Tavily, Wikipedia, ArXiv, and ChromaDB
- **`prompts.py`**: System prompts for routing and generation
### π― Core Module (`core/`)
- **`llm.py`**: Initializes the LLM (Anthropic Claude Sonnet 4.5)
- **`rag_agent.py`**: Main `RAGAgent` class that orchestrates the graph
### π₯οΈ UI Module (`ui/`)
- **`gradio_components.py`**: Gradio web interface with chat functionality
### π Data Module (`data/`)
- **`documents/`**: Raw source documents for ingestion
- **`chroma_db/`**: Persisted vector embeddings
### βοΈ Configuration
- **`config.py`**: Centralized configuration (model names, paths, API settings)
- **`.env`**: API keys (ANTHROPIC_API_KEY, TAVILY_API_KEY)
### π Entry Points
- **`main.py`**: Launches the Gradio UI
- **`test_scripts.py`**: Runs agent tests
## π Data Flow
```
User Query
β
[Router Node] β Classifies intent (RAG/WEBSEARCH/GENERAL)
β
βββ [VectorDB Node] β Retrieves from ChromaDB β [Generate Node]
βββ [Web Search Agent] β Calls Tavily/Wikipedia β [Generate Node]
βββ [Generate Node] β Uses LLM knowledge only
β
Response to User
```
## π οΈ Technology Stack
- **LangChain**: Framework for LLM applications
- **LangGraph**: Workflow orchestration
- **Anthropic Claude**: LLM (Sonnet 4.5)
- **ChromaDB**: Vector database
- **Gradio**: Web UI framework
- **HuggingFace**: Embeddings model
- **Tavily**: Web search API
- **UV**: Python package manager
## π Quick Start with UV
### Prerequisites
- Python 3.10+
- UV package manager ([Install UV](https://github.com/astral-sh/uv))
- API Keys: Anthropic, Tavily
### 1οΈβ£ Clone the Repository
```bash
git clone https://github.com/yourusername/mai-rag-agent.git
cd mai-rag-agent
```
### 2οΈβ£ Create Virtual Environment with UV
```bash
# Create a new virtual environment
uv venv
# Activate the environment
source .venv/bin/activate # Linux/macOS
# or
.venv\Scripts\activate # Windows
```
### 3οΈβ£ Install Dependencies
```bash
# Install all dependencies from requirements.txt
uv pip install -r requirements.txt
# Or install directly from pyproject.toml (if available)
uv pip install -e .
```
### 4οΈβ£ Set Up Environment Variables
```bash
# Copy example environment file
cp .env.example .env
# Edit .env and add your API keys
nano .env # or use your preferred editor
```
**Required environment variables:**
```bash
GOOGLE_API_KEY=xxxxxxxxxxxxx # Gemini API key
TAVILY_API_KEY=tvly-xxxxxxxxxxxxx # Enable web search
```
### 5οΈβ£ Prepare Data
```bash
# Create necessary directories
mkdir -p data/documents data/chroma_db
# Add your documents to data/documents/
# Then run ingestion (if you have an ingestion script)
# python ingest_data.py
```
### 6οΈβ£ Run the Application
```bash
# Launch the Gradio UI
python main.py
```
### 7οΈβ£ Run Tests (Optional)
```bash
# Test the agent functionality
python test_scripts.py
```
---
## π³ Quick Start with Dev Container (Alternative)
If you're using VS Code with Dev Containers:
```bash
# 1. Open in VS Code
code .
# 2. Reopen in Container
# Command Palette (Ctrl+Shift+P) β "Dev Containers: Reopen in Container"
# 3. Inside container, install dependencies
uv pip install -r requirements.txt
# 4. Set up .env file
cp .env.example .env
# Edit .env with your API keys
# 5. Run the app
python main.py
```
---
## π¦ UV-Specific Commands
```bash
# Update all dependencies
uv pip install --upgrade -r requirements.txt
# List installed packages
uv pip list
# Freeze current environment
uv pip freeze > requirements.txt
# Install a new package
uv pip install package-name
# Uninstall a package
uv pip uninstall package-name
# Sync environment (removes unused packages)
uv pip sync requirements.txt
```
---
## π§ Troubleshooting
### Issue: `uv` command not found
```bash
# Install UV
curl -LsSf https://astral.sh/uv/install.sh | sh
# Add to PATH (if needed)
export PATH="$HOME/.cargo/bin:$PATH"
```
### Issue: API key not loading
```bash
# Check if .env exists
cat .env | grep -i api
# Ensure no typos in variable names
# Should be: ANTHROPIC_API_KEY and TAVILY_API_KEY
```
### Issue: ChromaDB not found
```bash
# Ensure data directories exist
mkdir -p data/chroma_db
# Check permissions
chmod -R 755 data/
```
### Issue: Port 7860 already in use
```bash
# Find and kill the process
lsof -ti:7860 | xargs kill -9
# Or use a different port in main.py
# demo.launch(server_port=7861)
```
---
## π― Next Steps
1. β
Add your documents to `data/documents/`
2. β
Configure embeddings model in `config.py`
3. β
Customize prompts in `agent/prompts.py`
4. β
Test with sample queries in the Gradio UI
5. β
Deploy to production (see deployment docs)
---
## π Additional Resources
- [UV Documentation](https://github.com/astral-sh/uv)
- [LangGraph Docs](https://langchain-ai.github.io/langgraph/)
- [Gemini API](https://ai.google.dev/gemini-api/docs/api-key)
- [Tavily API](https://docs.tavily.com/)
- [ChromaDB Docs](https://docs.trychroma.com/)
## π Reference (Used for demo as document in vector store)
1. Wei, H., Sun, Y., & Li, Y. (2025). Deepseek-ocr: Contexts optical compression. arXiv preprint arXiv:2510.18234.
2. Chen, X., Chu, F. J., Gleize, P., Liang, K. J., Sax, A., Tang, H., ... & SAM 3D Team. (2025). SAM 3D: 3Dfy Anything in Images. arXiv preprint arXiv:2511.16624.
3. Carion, N., Gustafson, L., Hu, Y. T., Debnath, S., Hu, R., Suris, D., ... & Feichtenhofer, C. (2025). SAM 3: Segment Anything with Concepts. arXiv preprint arXiv:2511.16719.
4. Yan, B. Y., Li, C., Qian, H., Lu, S., & Liu, Z. (2025). General Agentic Memory Via Deep Research. arXiv preprint arXiv:2511.18423.
5. Zhang, S., Fan, J., Fan, M., Li, G., & Du, X. (2025). Deepanalyze: Agentic large language models for autonomous data science. arXiv preprint arXiv:2510.16872. |