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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# COPGPT - RAG-based Policy Recommendation Chatbot
|
| 2 |
+
|
| 3 |
+
A sophisticated Retrieval-Augmented Generation (RAG) chatbot built with FastAPI, LangChain, and OpenAI, designed to provide intelligent policy recommendations and sustainability insights with a focus on environmental topics and carbon emissions.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- **Hybrid Search Architecture**: Combines FAISS vector search with Google Search fallback for comprehensive information retrieval
|
| 8 |
+
- **Conversational Memory**: Maintains context across multiple interactions for coherent conversations
|
| 9 |
+
- **Document Processing**: Supports multiple file formats (PDF, DOCX, TXT, CSV, XLSX, HTML, MD, PPT)
|
| 10 |
+
- **Metadata Preservation**: Retains source information and references for all retrieved documents
|
| 11 |
+
- **Real-time Web Search**: Falls back to Google Search when local knowledge base lacks information
|
| 12 |
+
- **Clean Response Formatting**: Provides well-structured responses with proper references
|
| 13 |
+
- **FastAPI Backend**: High-performance asynchronous API endpoints
|
| 14 |
+
|
| 15 |
+
## Architecture
|
| 16 |
+
|
| 17 |
+
```
|
| 18 |
+
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
|
| 19 |
+
│ Web Interface │────▶│ FastAPI Server │────▶│ RAG Pipeline │
|
| 20 |
+
└─────────────────┘ └──────────────────┘ └─────────────────┘
|
| 21 |
+
│
|
| 22 |
+
┌──────────────────────────┴───────────────────────────┐
|
| 23 |
+
│ │
|
| 24 |
+
┌───────▼────────┐ ┌────────▼────────┐
|
| 25 |
+
│ FAISS Vector │ │ Google Search │
|
| 26 |
+
│ Store │ │ (Fallback) │
|
| 27 |
+
└────────────────┘ └─────────────────┘
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
## Prerequisites
|
| 31 |
+
|
| 32 |
+
- Python 3.8+
|
| 33 |
+
- OpenAI API Key
|
| 34 |
+
- Google Serper API Key (for web search functionality)
|
| 35 |
+
- FAISS-compatible system
|
| 36 |
+
|
| 37 |
+
## Installation
|
| 38 |
+
|
| 39 |
+
1. **Clone the repository**
|
| 40 |
+
```bash
|
| 41 |
+
git clone <repository-url>
|
| 42 |
+
cd COP29_RAG_Chatbot
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
2. **Create a virtual environment**
|
| 46 |
+
```bash
|
| 47 |
+
python -m venv venv
|
| 48 |
+
source venv/bin/activate # On Windows: venv\Scripts\activate
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
3. **Install dependencies**
|
| 52 |
+
```bash
|
| 53 |
+
pip install -r requirements.txt
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
4. **Set up environment variables**
|
| 57 |
+
Create a `.env` file in the root directory:
|
| 58 |
+
```env
|
| 59 |
+
OPENAI_API_KEY=your_openai_api_key_here
|
| 60 |
+
SERPER_API_KEY=your_serper_api_key_here
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
## Project Structure
|
| 64 |
+
|
| 65 |
+
```
|
| 66 |
+
COP29_RAG_Chatbot/
|
| 67 |
+
│
|
| 68 |
+
├── app.py # FastAPI application entry point
|
| 69 |
+
├── retriever.py # Main RAG pipeline and chat logic
|
| 70 |
+
├── embeddings.py # Document embedding and vector store management
|
| 71 |
+
├── file_loader.py # Multi-format document loader
|
| 72 |
+
├── metadata.py # Metadata inspection utilities
|
| 73 |
+
├── requirements.txt # Python dependencies
|
| 74 |
+
│
|
| 75 |
+
├── models/ # Data models
|
| 76 |
+
│ └── index.py # Chat model definitions
|
| 77 |
+
│
|
| 78 |
+
├── templates/ # HTML templates
|
| 79 |
+
│ └── index.html # Chat interface
|
| 80 |
+
│
|
| 81 |
+
├── static/ # Static assets (CSS, JS, images)
|
| 82 |
+
│
|
| 83 |
+
└── test2_db/ # FAISS vector database storage
|
| 84 |
+
└── document_chunks111/
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
## Configuration
|
| 88 |
+
|
| 89 |
+
### Vector Database Setup
|
| 90 |
+
|
| 91 |
+
1. **Prepare your documents**
|
| 92 |
+
Place your documents in a folder for processing.
|
| 93 |
+
|
| 94 |
+
2. **Generate embeddings**
|
| 95 |
+
```bash
|
| 96 |
+
python embeddings.py
|
| 97 |
+
```
|
| 98 |
+
Follow the prompts to specify your document folder path.
|
| 99 |
+
|
| 100 |
+
3. **Update database path**
|
| 101 |
+
Ensure the `db_path` in `retriever.py` points to your FAISS database:
|
| 102 |
+
```python
|
| 103 |
+
db_path = r"path/to/your/faiss_db"
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
### Embedding Model
|
| 107 |
+
|
| 108 |
+
The system uses OpenAI's `text-embedding-3-large` model. You can modify this in `embeddings.py`:
|
| 109 |
+
```python
|
| 110 |
+
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
## Running the Application
|
| 114 |
+
|
| 115 |
+
1. **Start the FastAPI server**
|
| 116 |
+
```bash
|
| 117 |
+
uvicorn app:app --reload --host 0.0.0.0 --port 8000
|
| 118 |
+
```
|
| 119 |
+
|
| 120 |
+
2. **Access the chatbot**
|
| 121 |
+
Open your browser and navigate to:
|
| 122 |
+
```
|
| 123 |
+
http://localhost:8000
|
| 124 |
+
```
|
| 125 |
+
|
| 126 |
+
## Usage
|
| 127 |
+
|
| 128 |
+
### Web Interface
|
| 129 |
+
- Type your questions in the chat interface
|
| 130 |
+
- The bot will search its knowledge base first
|
| 131 |
+
- If needed, it will perform web searches for current information
|
| 132 |
+
- References are provided for all responses
|
| 133 |
+
|
| 134 |
+
### API Endpoint
|
| 135 |
+
Send POST requests to `/chat`:
|
| 136 |
+
```bash
|
| 137 |
+
curl -X POST "http://localhost:8000/chat" \
|
| 138 |
+
-H "Content-Type: application/json" \
|
| 139 |
+
-d '{"query": "What is carbon neutrality?"}'
|
| 140 |
+
```
|
| 141 |
+
|
| 142 |
+
### Supported Queries
|
| 143 |
+
- Environmental policy questions
|
| 144 |
+
- Carbon emission inquiries
|
| 145 |
+
- Sustainability best practices
|
| 146 |
+
- COP29-related information
|
| 147 |
+
- General conversational queries
|
| 148 |
+
|
| 149 |
+
## Key Components
|
| 150 |
+
|
| 151 |
+
### Retriever Pipeline (`retriever.py`)
|
| 152 |
+
- Manages the hybrid search strategy
|
| 153 |
+
- Maintains conversation history
|
| 154 |
+
- Handles query preprocessing and response formatting
|
| 155 |
+
|
| 156 |
+
### Document Processing (`file_loader.py`)
|
| 157 |
+
- Supports multiple file formats
|
| 158 |
+
- Preserves metadata during loading
|
| 159 |
+
- Implements fallback loaders for reliability
|
| 160 |
+
|
| 161 |
+
### Vector Store (`embeddings.py`)
|
| 162 |
+
- Creates and manages FAISS indexes
|
| 163 |
+
- Handles document chunking with overlap
|
| 164 |
+
- Preserves metadata through the embedding process
|
| 165 |
+
|
| 166 |
+
## Development
|
| 167 |
+
|
| 168 |
+
### Adding New Document Types
|
| 169 |
+
|
| 170 |
+
Extend the `FILE_LOADER_MAPPING` in `file_loader.py`:
|
| 171 |
+
```python
|
| 172 |
+
FILE_LOADER_MAPPING = {
|
| 173 |
+
".new_ext": (YourLoaderClass, {"param": "value"}),
|
| 174 |
+
# ... existing mappings
|
| 175 |
+
}
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
### Customizing Responses
|
| 179 |
+
|
| 180 |
+
Modify the `predefined_responses` dictionary in `retriever.py` to add custom responses for common queries.
|
| 181 |
+
|
| 182 |
+
### Adjusting Search Parameters
|
| 183 |
+
|
| 184 |
+
Configure search behavior in `hybrid_chain()`:
|
| 185 |
+
- `k=5`: Number of documents to retrieve
|
| 186 |
+
- `chunk_size=1000`: Size of text chunks
|
| 187 |
+
- `chunk_overlap=100`: Overlap between chunks
|
| 188 |
+
|
| 189 |
+
## Troubleshooting
|
| 190 |
+
|
| 191 |
+
### Common Issues
|
| 192 |
+
|
| 193 |
+
1. **FAISS Loading Errors**
|
| 194 |
+
- Ensure `allow_dangerous_deserialization=True` is set
|
| 195 |
+
- Check file permissions on the database directory
|
| 196 |
+
|
| 197 |
+
2. **API Key Issues**
|
| 198 |
+
- Verify `.env` file is in the root directory
|
| 199 |
+
- Check API key validity
|
| 200 |
+
|
| 201 |
+
3. **Memory Issues**
|
| 202 |
+
- Reduce chunk size or number of retrieved documents
|
| 203 |
+
- Consider using a smaller embedding model
|
| 204 |
+
|
| 205 |
+
### Debug Mode
|
| 206 |
+
|
| 207 |
+
Enable detailed logging:
|
| 208 |
+
```python
|
| 209 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 210 |
+
```
|
| 211 |
+
|
| 212 |
+
## Performance Optimization
|
| 213 |
+
|
| 214 |
+
- **Async Processing**: FastAPI handles requests asynchronously
|
| 215 |
+
- **Caching**: Consider implementing Redis for response caching
|
| 216 |
+
- **Batch Processing**: Process multiple documents simultaneously
|
| 217 |
+
- **Index Optimization**: Regularly rebuild FAISS indexes for optimal performance
|
| 218 |
+
|
| 219 |
+
## Contributing
|
| 220 |
+
|
| 221 |
+
1. Fork the repository
|
| 222 |
+
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
|
| 223 |
+
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
|
| 224 |
+
4. Push to the branch (`git push origin feature/AmazingFeature`)
|
| 225 |
+
5. Open a Pull Request
|
| 226 |
+
|
| 227 |
+
## License
|
| 228 |
+
|
| 229 |
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
| 230 |
+
|
| 231 |
+
## Acknowledgments
|
| 232 |
+
|
| 233 |
+
- Built with [LangChain](https://langchain.com/) for RAG capabilities
|
| 234 |
+
- Powered by [OpenAI](https://openai.com/) for embeddings and language models
|
| 235 |
+
- [FAISS](https://github.com/facebookresearch/faiss) for efficient similarity search
|
| 236 |
+
- [FastAPI](https://fastapi.tiangolo.com/) for the web framework
|
| 237 |
+
|
| 238 |
+
## Contact
|
| 239 |
+
|
| 240 |
+
For questions or support, please contact: info@carbonnote.ai
|
| 241 |
+
|
| 242 |
+
## Contributors
|
| 243 |
+
|
| 244 |
+
1. Elizabeth Osanyinro, University of Bradford, UK
|
| 245 |
+
|
| 246 |
+
2. Oluwole Fagbohun, Carbonnote, USA
|
| 247 |
+
|
| 248 |
+
3. Ernest Effiong Offiong, Carbonnote, USA
|
| 249 |
+
|
| 250 |
+
4. Maxwell Nwanna, RideNear, UK
|
| 251 |
+
|
| 252 |
+
5. Grace Farayola, University of Buckingham, UK
|
| 253 |
+
|
| 254 |
+
6. Olaitan Olaonipekun, Vuhosi Limited, UK
|
| 255 |
+
|
| 256 |
+
7. Abiola Oludotun, Readrly Limited, UK
|
| 257 |
+
|
| 258 |
+
8. Sayo Agunbiade, Independent Researcher, UK
|
| 259 |
+
|
| 260 |
+
9. Oladotun Fasogbon, Independent Researcher, UK
|
| 261 |
+
|
| 262 |
+
10. Ogheneruona Maria Esegbon-Isikeh, Readrly Limited, UK
|
| 263 |
+
|
| 264 |
+
11. Lanre Shittu, Independent Researcher, UK
|
| 265 |
+
|
| 266 |
+
12. Toyese Oloyede, Independent Researcher, UK
|
| 267 |
+
|
| 268 |
+
13. Sa'id Olanrewaju, Readrly Limited, UK
|
| 269 |
+
|
| 270 |
+
14. Christopher J Ozurumba, Independent Researcher, UK
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
**Note**: This is a beta version. For production use, please ensure proper security measures, rate limiting, and error handling are implemented.
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
---
|
| 278 |
+
license: apache-2.0
|
| 279 |
+
---
|