|
| 1 | +# BrainUs AI Python Examples |
| 2 | + |
| 3 | +Complete Python examples for integrating BrainUs AI API with popular frameworks and patterns. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Framework Examples](#framework-examples) |
| 8 | + - [Django REST Framework](#django-rest-framework) |
| 9 | + - [Flask](#flask) |
| 10 | + - [FastAPI](#fastapi) |
| 11 | +- [Pattern Examples](#pattern-examples) |
| 12 | + - [Async/Await Patterns](#asyncawait-patterns) |
| 13 | + - [Batch Processing](#batch-processing) |
| 14 | + - [Error Handling](#error-handling) |
| 15 | +- [Basic Usage](#basic-usage) |
| 16 | +- [Setup](#setup) |
| 17 | +- [Running Examples](#running-examples) |
| 18 | + |
| 19 | +## Framework Examples |
| 20 | + |
| 21 | +### Django REST Framework |
| 22 | + |
| 23 | +**File:** [django_example.py](django_example.py) |
| 24 | + |
| 25 | +Demonstrates integration with Django REST Framework with async views and caching support. |
| 26 | + |
| 27 | +**Features:** |
| 28 | + |
| 29 | +- Async API views (Django 3.1+) |
| 30 | +- Query caching with Django cache framework |
| 31 | +- Clean error handling |
| 32 | +- RESTful response formatting |
| 33 | + |
| 34 | +**Requirements:** |
| 35 | + |
| 36 | +```bash |
| 37 | +pip install djangorestframework brainus-ai |
| 38 | +``` |
| 39 | + |
| 40 | +**Usage:** |
| 41 | +Add to your Django project's views and configure in `settings.py`: |
| 42 | + |
| 43 | +```python |
| 44 | +BRAINUS_API_KEY = os.getenv('BRAINUS_API_KEY') |
| 45 | +``` |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +### Flask |
| 50 | + |
| 51 | +**File:** [flask_example.py](flask_example.py) |
| 52 | + |
| 53 | +Flask 2.0+ application with async route support and retry logic. |
| 54 | + |
| 55 | +**Features:** |
| 56 | + |
| 57 | +- Async routes (Flask 2.0+) |
| 58 | +- Rate limit retry mechanism |
| 59 | +- Health check endpoint |
| 60 | +- JSON response handling |
| 61 | + |
| 62 | +**Requirements:** |
| 63 | + |
| 64 | +```bash |
| 65 | +pip install flask brainus-ai |
| 66 | +``` |
| 67 | + |
| 68 | +**Usage:** |
| 69 | + |
| 70 | +```bash |
| 71 | +export BRAINUS_API_KEY=your_api_key |
| 72 | +python flask_example.py |
| 73 | +``` |
| 74 | + |
| 75 | +Test the API: |
| 76 | + |
| 77 | +```bash |
| 78 | +curl -X POST http://localhost:5000/api/query \ |
| 79 | + -H "Content-Type: application/json" \ |
| 80 | + -d '{"query": "What is photosynthesis?"}' |
| 81 | +``` |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | +### FastAPI |
| 86 | + |
| 87 | +**File:** [fastapi_example.py](fastapi_example.py) |
| 88 | + |
| 89 | +Modern FastAPI application built for async operations. |
| 90 | + |
| 91 | +**Features:** |
| 92 | + |
| 93 | +- Native async support |
| 94 | +- Pydantic models for validation |
| 95 | +- CORS middleware |
| 96 | +- Background tasks for logging |
| 97 | +- OpenAPI documentation |
| 98 | +- Health check endpoint |
| 99 | + |
| 100 | +**Requirements:** |
| 101 | + |
| 102 | +```bash |
| 103 | +pip install fastapi uvicorn brainus-ai |
| 104 | +``` |
| 105 | + |
| 106 | +**Usage:** |
| 107 | + |
| 108 | +```bash |
| 109 | +export BRAINUS_API_KEY=your_api_key |
| 110 | +python fastapi_example.py |
| 111 | +``` |
| 112 | + |
| 113 | +Or with uvicorn: |
| 114 | + |
| 115 | +```bash |
| 116 | +uvicorn fastapi_example:app --reload |
| 117 | +``` |
| 118 | + |
| 119 | +Access the API documentation at: http://localhost:8000/docs |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +## Pattern Examples |
| 124 | + |
| 125 | +### Async/Await Patterns |
| 126 | + |
| 127 | +**File:** [async_patterns.py](async_patterns.py) |
| 128 | + |
| 129 | +Demonstrates various async patterns for efficient query processing. |
| 130 | + |
| 131 | +**Features:** |
| 132 | + |
| 133 | +- Single query execution |
| 134 | +- Parallel multiple queries |
| 135 | +- Query with timeout |
| 136 | +- Fallback store patterns |
| 137 | +- Error handling in async context |
| 138 | + |
| 139 | +**Usage:** |
| 140 | + |
| 141 | +```bash |
| 142 | +export BRAINUS_API_KEY=your_api_key |
| 143 | +python async_patterns.py |
| 144 | +``` |
| 145 | + |
| 146 | +**Key Patterns:** |
| 147 | + |
| 148 | +```python |
| 149 | +# Parallel queries |
| 150 | +results = await query_multiple(queries) |
| 151 | + |
| 152 | +# With timeout |
| 153 | +result = await query_with_timeout(query, timeout=10.0) |
| 154 | + |
| 155 | +# With fallback stores |
| 156 | +result = await query_with_fallback(query, ["primary", "secondary", "default"]) |
| 157 | +``` |
| 158 | + |
| 159 | +--- |
| 160 | + |
| 161 | +### Batch Processing |
| 162 | + |
| 163 | +**File:** [batch_processing.py](batch_processing.py) |
| 164 | + |
| 165 | +Process large batches of queries with rate limiting and progress tracking. |
| 166 | + |
| 167 | +**Features:** |
| 168 | + |
| 169 | +- Batch processing with configurable size |
| 170 | +- Rate limiting between batches |
| 171 | +- Progress tracking |
| 172 | +- CSV file input/output |
| 173 | +- Error handling per query |
| 174 | +- Comprehensive statistics |
| 175 | + |
| 176 | +**Requirements:** |
| 177 | + |
| 178 | +```bash |
| 179 | +pip install brainus-ai pandas |
| 180 | +``` |
| 181 | + |
| 182 | +**Usage:** |
| 183 | + |
| 184 | +```bash |
| 185 | +export BRAINUS_API_KEY=your_api_key |
| 186 | +python batch_processing.py |
| 187 | +``` |
| 188 | + |
| 189 | +**CSV Processing:** |
| 190 | + |
| 191 | +```python |
| 192 | +await process_csv_file( |
| 193 | + input_file='queries.csv', |
| 194 | + output_file='results.csv', |
| 195 | + query_column='query', |
| 196 | + batch_size=10 |
| 197 | +) |
| 198 | +``` |
| 199 | + |
| 200 | +--- |
| 201 | + |
| 202 | +### Error Handling |
| 203 | + |
| 204 | +**File:** [error_handling.py](error_handling.py) |
| 205 | + |
| 206 | +Comprehensive error handling patterns for production use. |
| 207 | + |
| 208 | +**Features:** |
| 209 | + |
| 210 | +- All error types handling |
| 211 | +- Retry logic with exponential backoff |
| 212 | +- Fallback mechanisms |
| 213 | +- Input validation |
| 214 | +- Rate limit handling |
| 215 | +- Batch error handling |
| 216 | + |
| 217 | +**Usage:** |
| 218 | + |
| 219 | +```bash |
| 220 | +export BRAINUS_API_KEY=your_api_key |
| 221 | +python error_handling.py |
| 222 | +``` |
| 223 | + |
| 224 | +**Error Types Handled:** |
| 225 | + |
| 226 | +- `AuthenticationError` - Invalid API key |
| 227 | +- `RateLimitError` - Rate limit exceeded (with retry) |
| 228 | +- `QuotaExceededError` - Usage quota exceeded |
| 229 | +- `APIError` - General API errors |
| 230 | +- `BrainusError` - Base exception class |
| 231 | + |
| 232 | +--- |
| 233 | + |
| 234 | +## Basic Usage |
| 235 | + |
| 236 | +**File:** [basic_usage.py](basic_usage.py) |
| 237 | + |
| 238 | +Simple example to get started quickly. |
| 239 | + |
| 240 | +**Usage:** |
| 241 | + |
| 242 | +```bash |
| 243 | +export BRAINUS_API_KEY=your_api_key |
| 244 | +python basic_usage.py |
| 245 | +``` |
| 246 | + |
| 247 | +--- |
| 248 | + |
| 249 | +## Setup |
| 250 | + |
| 251 | +### 1. Install the SDK |
| 252 | + |
| 253 | +```bash |
| 254 | +pip install brainus-ai |
| 255 | +``` |
| 256 | + |
| 257 | +### 2. Set Your API Key |
| 258 | + |
| 259 | +**Option A: Environment Variable (Recommended)** |
| 260 | + |
| 261 | +```bash |
| 262 | +export BRAINUS_API_KEY=your_api_key_here |
| 263 | +``` |
| 264 | + |
| 265 | +**Option B: In Code (Not Recommended for Production)** |
| 266 | + |
| 267 | +```python |
| 268 | +client = BrainusAI(api_key="your_api_key_here") |
| 269 | +``` |
| 270 | + |
| 271 | +### 3. Install Framework Dependencies |
| 272 | + |
| 273 | +Choose based on your needs: |
| 274 | + |
| 275 | +```bash |
| 276 | +# For Django examples |
| 277 | +pip install djangorestframework |
| 278 | + |
| 279 | +# For Flask examples |
| 280 | +pip install flask |
| 281 | + |
| 282 | +# For FastAPI examples |
| 283 | +pip install fastapi uvicorn |
| 284 | + |
| 285 | +# For batch processing |
| 286 | +pip install pandas |
| 287 | +``` |
| 288 | + |
| 289 | +--- |
| 290 | + |
| 291 | +## Running Examples |
| 292 | + |
| 293 | +### Quick Start |
| 294 | + |
| 295 | +```bash |
| 296 | +# Set your API key |
| 297 | +export BRAINUS_API_KEY=your_api_key |
| 298 | + |
| 299 | +# Run any example |
| 300 | +python examples/basic_usage.py |
| 301 | +python examples/async_patterns.py |
| 302 | +python examples/error_handling.py |
| 303 | +``` |
| 304 | + |
| 305 | +### Web Frameworks |
| 306 | + |
| 307 | +**Flask:** |
| 308 | + |
| 309 | +```bash |
| 310 | +python examples/flask_example.py |
| 311 | +# API available at http://localhost:5000 |
| 312 | +``` |
| 313 | + |
| 314 | +**FastAPI:** |
| 315 | + |
| 316 | +```bash |
| 317 | +python examples/fastapi_example.py |
| 318 | +# API available at http://localhost:8000 |
| 319 | +# Docs at http://localhost:8000/docs |
| 320 | +``` |
| 321 | + |
| 322 | +--- |
| 323 | + |
| 324 | +## Common Patterns |
| 325 | + |
| 326 | +### Context Manager (Recommended) |
| 327 | + |
| 328 | +```python |
| 329 | +async with BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) as client: |
| 330 | + result = await client.query(query="What is AI?", store_id="default") |
| 331 | +``` |
| 332 | + |
| 333 | +### Manual Management |
| 334 | + |
| 335 | +```python |
| 336 | +client = BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) |
| 337 | +try: |
| 338 | + result = await client.query(query="What is AI?", store_id="default") |
| 339 | +finally: |
| 340 | + await client.close() |
| 341 | +``` |
| 342 | + |
| 343 | +### Error Handling |
| 344 | + |
| 345 | +```python |
| 346 | +from brainus_ai import BrainusAI, BrainusError, RateLimitError |
| 347 | + |
| 348 | +try: |
| 349 | + async with BrainusAI(api_key=api_key) as client: |
| 350 | + result = await client.query(query="What is AI?") |
| 351 | +except RateLimitError as e: |
| 352 | + await asyncio.sleep(e.retry_after) |
| 353 | +except BrainusError as e: |
| 354 | + print(f"Error: {e}") |
| 355 | +``` |
| 356 | + |
| 357 | +--- |
| 358 | + |
| 359 | +## Example Output |
| 360 | + |
| 361 | +```python |
| 362 | +result = await client.query(query="What is photosynthesis?") |
| 363 | + |
| 364 | +print(result.answer) |
| 365 | +# "Photosynthesis is the process by which..." |
| 366 | + |
| 367 | +print(result.has_citations) |
| 368 | +# True |
| 369 | + |
| 370 | +if result.citations: |
| 371 | + for citation in result.citations: |
| 372 | + print(f"Source: {citation.source}") |
| 373 | +``` |
| 374 | + |
| 375 | +--- |
| 376 | + |
| 377 | +## Next Steps |
| 378 | + |
| 379 | +- **[Python SDK Documentation](../src/brainus_ai/)** - Complete SDK reference |
| 380 | +- **[GitHub Repository](https://github.com/brainuslk/brainus-ai-python)** - Source code and issues |
| 381 | +- **[API Documentation](https://brainus.ai/docs)** - Full API documentation |
| 382 | + |
| 383 | +--- |
| 384 | + |
| 385 | +## Support |
| 386 | + |
| 387 | +If you encounter any issues or have questions: |
| 388 | + |
| 389 | +- 📧 Email: developers@brainus.lk |
| 390 | +- 🐛 Issues: [GitHub Issues](https://github.com/brainuslk/brainus-ai-python/issues) |
| 391 | +- 📖 Docs: [brainus.ai/docs](https://developers.brainus.lk/docs) |
| 392 | + |
| 393 | +--- |
| 394 | + |
| 395 | +## License |
| 396 | + |
| 397 | +These examples are provided under the same license as the BrainUs AI Python SDK. |
0 commit comments