-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadme.txt
More file actions
419 lines (317 loc) · 11.6 KB
/
readme.txt
File metadata and controls
419 lines (317 loc) · 11.6 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# Context-Aware Research Chatbot
A sophisticated conversational agent that answers domain questions using web search, local RAG (Retrieval-Augmented Generation), and mathematical tools with comprehensive source citations.
## 🚀 Features
- **Multi-Modal Intelligence**: Combines web search, local knowledge base, and mathematical calculations
- **Smart Routing**: Automatically routes queries to the most appropriate tool based on intent
- **Conversational Memory**: Maintains context across conversations with session management
- **Source Citations**: Provides detailed source attributions for all responses
- **Comprehensive Evaluation**: Built-in evaluation framework for faithfulness and groundedness
- **Multiple Interfaces**: FastAPI backend, Streamlit UI, and Gradio interface
- **Scalable Architecture**: Modular design using LangChain components
## 🏗️ Architecture
```
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Streamlit UI │ │ Gradio UI │ │ FastAPI │
│ │ │ │ │ │
└─────────┬───────┘ └─────────┬───────┘ └─────────┬───────┘
│ │ │
└──────────────────────┼──────────────────────┘
│
┌─────────────▼─────────────┐
│ Context-Aware │
│ Research Chatbot │
│ │
│ ┌─────────────────────┐ │
│ │ Query Router │ │
│ └─────────┬───────────┘ │
│ │ │
│ ┌─────────▼───────────┐ │
│ │ Tools │ │
│ │ ┌─────┐ ┌─────┐ │ │
│ │ │ RAG │ │ Web │ │ │
│ │ └─────┘ └─────┘ │ │
│ │ ┌─────┐ │ │
│ │ │Math │ │ │
│ │ └─────┘ │ │
│ └─────────────────────┘ │
│ │
│ ┌─────────────────────┐ │
│ │ Memory Manager │ │
│ └─────────────────────┘ │
└──────────────────────────┘
│
┌─────────────▼─────────────┐
│ Data Layer │
│ │
│ ┌─────┐ ┌─────┐ ┌─────┐ │
│ │FAISS│ │SQLite│ │PDFs │ │
│ └─────┘ └─────┘ └─────┘ │
└──────────────────────────┘
```
## 📋 Prerequisites
- Python 3.8+
- OpenAI API key
- (Optional) SerpAPI or Tavily API key for web search
- PDF documents for your domain knowledge base
## 🛠️ Installation
### 1. Clone and Setup
```bash
git clone <repository-url>
cd context-aware-research-chatbot
pip install -r requirements.txt
```
### 2. Configure Environment
```bash
cp .env.example .env
# Edit .env with your API keys
```
### 3. Initial Setup
```bash
python main.py setup
```
### 4. Add Your PDFs
Place your PDF documents in the `data/pdfs/` directory:
```bash
cp your-documents/*.pdf data/pdfs/
```
### 5. Process Documents
```bash
python main.py process-pdfs
```
### 6. Test System
```bash
python main.py test
```
## 🚀 Quick Start
### Option 1: Complete Workflow
```bash
python main.py all
```
### Option 2: Step by Step
1. **Start the API server:**
```bash
python main.py start-api
# Server runs on http://localhost:8000
```
2. **Start the Streamlit UI:**
```bash
python main.py start-ui
# UI available at http://localhost:8501
```
3. **Or start the Gradio UI:**
```bash
python gradio_ui.py
# UI available at http://localhost:7860
```
## 📊 Evaluation
Run comprehensive evaluation:
```bash
python main.py eval
```
Or run custom evaluation:
```bash
python evaluation.py --dataset custom_dataset.json
```
## 🔧 Configuration
Key configuration options in `.env`:
```env
# Required
OPENAI_API_KEY=your_key_here
# Optional - for web search
SERPAPI_API_KEY=your_serpapi_key
TAVILY_API_KEY=your_tavily_key
# Model settings
LLM_MODEL=gpt-3.5-turbo
EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L6-v2
# Data settings
CHUNK_SIZE=1000
CHUNK_OVERLAP=200
TOP_K_RETRIEVAL=5
# Vector store
VECTOR_STORE_TYPE=faiss # or chroma
```
## 📚 Usage Examples
### API Usage
```python
import requests
# Create session
response = requests.post("http://localhost:8000/sessions",
json={"user_id": "your_user_id"})
session_id = response.json()["session_id"]
# Chat
response = requests.post("http://localhost:8000/chat", json={
"message": "What are the latest AI safety guidelines?",
"session_id": session_id
})
result = response.json()
print(f"Response: {result['response']}")
print(f"Tool used: {result['tool_used']}")
print(f"Sources: {result['sources']}")
```
### Direct Python Usage
```python
from chatbot import get_chatbot
# Initialize chatbot
bot = get_chatbot()
# Create session
session_id = bot.create_session("user123")
# Chat
result = bot.chat("What is GDPR?", session_id)
print(result["response"])
```
## 🎯 Query Types & Routing
The system automatically routes queries to appropriate tools:
| Query Type | Example | Tool Used |
|------------|---------|-----------|
| Domain Knowledge | "What does GDPR say about AI?" | RAG (Local Knowledge) |
| Current Events | "Latest AI news today" | Web Search |
| Calculations | "Calculate 15% of 250000" | Math Tool |
| General AI Policy | "AI safety guidelines" | RAG → Web Search fallback |
## 🔍 Evaluation Metrics
The system evaluates responses on:
- **Faithfulness**: Does the response accurately reflect the source material?
- **Relevance**: Is the response relevant to the question?
- **Tool Routing Accuracy**: Was the correct tool used?
- **Source Quality**: Are sources properly cited and accessible?
## 📁 Project Structure
```
context-aware-research-chatbot/
├── README.md
├── requirements.txt
├── .env.example
├── main.py # Main CLI interface
├── config.py # Configuration management
├── data_processor.py # PDF processing & vector store
├── tools.py # Web search, math, RAG tools
├── chatbot.py # Core chatbot logic
├── database.py # Database models & management
├── api.py # FastAPI backend
├── streamlit_ui.py # Streamlit frontend
├── gradio_ui.py # Gradio frontend
├── evaluation.py # Evaluation framework
├── docker-compose.yml # Docker configuration
├── tests/ # Test files
│ ├── test_chatbot.py
│ ├── test_tools.py
│ └── test_evaluation.py
└── data/ # Data directory
├── pdfs/ # Place your PDF files here
├── vector_store/ # Generated vector store
└── eval_dataset.json # Evaluation dataset
```
## 🧪 Testing
Run the test suite:
```bash
python -m pytest tests/ -v
```
Run specific tests:
```bash
python -m pytest tests/test_chatbot.py -v
```
## 🐳 Docker Deployment
```bash
# Build and run with docker-compose
docker-compose up --build
# The services will be available at:
# - API: http://localhost:8000
# - Streamlit UI: http://localhost:8501
# - Gradio UI: http://localhost:7860
```
## 📈 Monitoring & Analytics
### Session Statistics
- Track conversations per session
- Monitor tool usage patterns
- Analyze user engagement
### Performance Metrics
- Response times by tool
- Success rates for different query types
- Source retrieval effectiveness
### Access Analytics
```bash
# Get global stats
curl http://localhost:8000/stats
# Get session stats
curl http://localhost:8000/sessions/{session_id}/stats
```
## 🔒 Security Considerations
- API keys are stored in environment variables
- Database uses SQLite by default (configure for production)
- CORS is enabled for development (configure for production)
- Session management prevents data leakage between users
## 🎛️ Customization
### Adding New Tools
1. Create tool class in `tools.py`:
```python
class CustomTool:
def process(self, query: str) -> str:
# Your tool logic
return response
```
2. Update router in `tools.py`:
```python
def route(self, query: str) -> str:
if "custom_condition" in query.lower():
return "custom_tool"
# ... existing logic
```
3. Integrate in `chatbot.py`:
```python
def _handle_custom_query(self, query: str, memory) -> Tuple[str, List[Dict]]:
# Handle custom tool queries
pass
```
### Custom Evaluation Metrics
Add custom evaluators in `evaluation.py`:
```python
def evaluate_custom_metric(self, question: str, answer: str) -> Dict[str, Any]:
# Your custom evaluation logic
return {"score": score, "reasoning": reasoning}
```
### UI Customization
- Modify `streamlit_ui.py` for Streamlit customizations
- Modify `gradio_ui.py` for Gradio customizations
- Both UIs consume the same FastAPI backend
## 🐛 Troubleshooting
### Common Issues
1. **Vector store not found**
```bash
python main.py process-pdfs --force
```
2. **API connection failed**
- Check if API server is running on port 8000
- Verify OpenAI API key in `.env`
3. **No PDF files found**
- Add PDF files to `data/pdfs/` directory
- Check file permissions
4. **Memory issues with large PDFs**
- Reduce `CHUNK_SIZE` in `.env`
- Process PDFs in batches
5. **Evaluation failing**
- Ensure test dataset exists
- Check OpenAI API quota
### Debug Mode
Enable debug logging:
```bash
export LOG_LEVEL=DEBUG
python main.py test
```
## 🤝 Contributing
1. Fork the repository
2. Create a feature branch
3. Add tests for new functionality
4. Run the test suite
5. Submit a pull request
## 📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
## 🙏 Acknowledgments
- Built with [LangChain](https://github.com/hwchase17/langchain)
- UI powered by [Streamlit](https://streamlit.io/) and [Gradio](https://gradio.app/)
- Vector storage with [FAISS](https://github.com/facebookresearch/faiss) and [Chroma](https://www.trychroma.com/)
- Backend with [FastAPI](https://fastapi.tiangolo.com/)
## 📞 Support
For issues and questions:
- Check the troubleshooting section
- Review the test suite for examples
- Open an issue on GitHub
---
**Happy Chatting! 🤖**