Dhruv Pawar
commited on
Commit
·
3e1d0f5
0
Parent(s):
Initial commit
Browse files- .gitignore +12 -0
- README.md +285 -0
- config.py +271 -0
- core.py +986 -0
- main.py +381 -0
- requirements.txt +32 -0
.gitignore
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
env/
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.pyc
|
| 4 |
+
*.pyo
|
| 5 |
+
*.pyd
|
| 6 |
+
*.sqlite3
|
| 7 |
+
*.log
|
| 8 |
+
*.env
|
| 9 |
+
.DS_Store
|
| 10 |
+
app.py
|
| 11 |
+
exports/
|
| 12 |
+
backups/
|
README.md
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🔬 Advanced AI Reasoning Research System
|
| 2 |
+
|
| 3 |
+
[](https://www.python.org/downloads/)
|
| 4 |
+
[](https://opensource.org/licenses/MIT)
|
| 5 |
+
[](https://github.com/your-username/ai-reasoning-system)
|
| 6 |
+
|
| 7 |
+
An open-source research platform that implements cutting-edge AI reasoning methodologies including **Tree of Thoughts**, **Constitutional AI**, and **multi-agent debate patterns**. Features a modern web interface, real-time streaming, and comprehensive analytics.
|
| 8 |
+
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
## 🎯 What This Project Does
|
| 12 |
+
|
| 13 |
+
- **Multi-Strategy Reasoning**: Apply different reasoning approaches to the same problem
|
| 14 |
+
- **Self-Critique System**: AI reviews and improves its own responses
|
| 15 |
+
- **Real-time Analytics**: Track reasoning depth, confidence, and performance metrics
|
| 16 |
+
- **Export & Documentation**: Save conversations as PDF, Markdown, or JSON
|
| 17 |
+
- **Production Ready**: Caching, rate limiting, error handling, and automatic backups
|
| 18 |
+
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
## 🚀 Quick Start (2 Minutes)
|
| 22 |
+
|
| 23 |
+
### Prerequisites
|
| 24 |
+
|
| 25 |
+
- Python **3.8+**
|
| 26 |
+
- Groq API key (free at [console.groq.com](https://console.groq.com))
|
| 27 |
+
|
| 28 |
+
### Installation
|
| 29 |
+
|
| 30 |
+
```bash
|
| 31 |
+
# Clone repository
|
| 32 |
+
git clone https://github.com/your-username/ai-reasoning-system.git
|
| 33 |
+
cd ai-reasoning-system
|
| 34 |
+
|
| 35 |
+
# Create virtual environment
|
| 36 |
+
python -m venv venv
|
| 37 |
+
source venv/bin/activate # Windows: venv\Scripts\activate
|
| 38 |
+
|
| 39 |
+
# Install dependencies
|
| 40 |
+
pip install -r requirements.txt
|
| 41 |
+
|
| 42 |
+
# Configure API key
|
| 43 |
+
echo "GROQ_API_KEY=your_key_here" > .env
|
| 44 |
+
|
| 45 |
+
# Launch system
|
| 46 |
+
python main.py
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
Open your browser to `http://localhost:7860` and start exploring!
|
| 50 |
+
|
| 51 |
+
---
|
| 52 |
+
|
| 53 |
+
## 📊 Reasoning Strategies
|
| 54 |
+
|
| 55 |
+
| Method | Description | Best For |
|
| 56 |
+
|--------|-------------|----------|
|
| 57 |
+
| **Tree of Thoughts** | Explores multiple reasoning paths systematically | Complex problems with multiple solutions |
|
| 58 |
+
| **Chain of Thought** | Step-by-step transparent reasoning | Mathematical problems, logic puzzles |
|
| 59 |
+
| **Self-Consistency** | Generates multiple answers and finds consensus | Factual questions, reliability important |
|
| 60 |
+
| **Reflexion** | Self-critique and iterative improvement | Creative writing, analysis tasks |
|
| 61 |
+
| **Multi-Agent Debate** | Presents multiple perspectives | Ethical dilemmas, policy questions |
|
| 62 |
+
| **Analogical Reasoning** | Finds similar problems and adapts solutions | Novel problems, innovation tasks |
|
| 63 |
+
|
| 64 |
+
---
|
| 65 |
+
|
| 66 |
+
## 🎥 Demo Features
|
| 67 |
+
|
| 68 |
+
### Real-time Interface
|
| 69 |
+
|
| 70 |
+
- **Streaming Responses**: Watch reasoning unfold in real-time
|
| 71 |
+
- **Live Metrics**: See inference time, tokens/second, reasoning depth
|
| 72 |
+
- **Interactive Controls**: Switch models, adjust temperature, enable critique
|
| 73 |
+
- **Modern Design**: Clean, responsive interface with dark theme
|
| 74 |
+
|
| 75 |
+
### Analytics Dashboard
|
| 76 |
+
|
| 77 |
+
- Session performance metrics
|
| 78 |
+
- Model usage distribution
|
| 79 |
+
- Cache hit rates
|
| 80 |
+
- Error tracking and retry statistics
|
| 81 |
+
|
| 82 |
+
### Export Options
|
| 83 |
+
|
| 84 |
+
- **PDF**: Professional reports with formatting
|
| 85 |
+
- **Markdown**: GitHub-friendly documentation
|
| 86 |
+
- **JSON**: Machine-readable data
|
| 87 |
+
- **Plain Text**: Simple conversation logs
|
| 88 |
+
|
| 89 |
+
---
|
| 90 |
+
|
| 91 |
+
## 🔧 Configuration
|
| 92 |
+
|
| 93 |
+
Key settings in `config.py`:
|
| 94 |
+
|
| 95 |
+
```python
|
| 96 |
+
MAX_HISTORY_LENGTH = 10 # Messages in context
|
| 97 |
+
CACHE_SIZE = 100 # Cached responses
|
| 98 |
+
RATE_LIMIT_REQUESTS = 50 # Per minute
|
| 99 |
+
DEFAULT_TEMPERATURE = 0.7 # Creativity level
|
| 100 |
+
MAX_TOKENS = 4000 # Response length
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
---
|
| 104 |
+
|
| 105 |
+
## 🏗️ Architecture
|
| 106 |
+
|
| 107 |
+
```
|
| 108 |
+
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
|
| 109 |
+
│ Gradio UI │ │ Core Engine │ │ Groq API │
|
| 110 |
+
│ │ │ │ │ │
|
| 111 |
+
│ • Chat Interface│◄──►│ • Reasoning │◄──►│ • LLM Models │
|
| 112 |
+
│ • Controls │ │ • Caching │ │ • Streaming │
|
| 113 |
+
│ • Metrics │ │ • Rate Limiting │ │ • Token Count │
|
| 114 |
+
│ • Export │ │ • Error Handling│ │ │
|
| 115 |
+
└─────────────────┘ └─────────────────┘ └─────────────────┘
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
---
|
| 119 |
+
|
| 120 |
+
## 📈 Performance
|
| 121 |
+
|
| 122 |
+
- **Cold Start**: ~2 seconds
|
| 123 |
+
- **Time to First Token**: 0.3–1.2 seconds
|
| 124 |
+
- **Throughput**: Up to 100 tokens/second
|
| 125 |
+
- **Memory Usage**: ~100MB base + conversation history
|
| 126 |
+
- **Concurrent Users**: Limited by Groq rate limits (50 req/min)
|
| 127 |
+
|
| 128 |
+
---
|
| 129 |
+
|
| 130 |
+
## 🧪 Example Use Cases
|
| 131 |
+
|
| 132 |
+
### Research Analysis
|
| 133 |
+
|
| 134 |
+
```
|
| 135 |
+
User: "Analyze the impact of remote work on productivity"
|
| 136 |
+
System: Uses Tree of Thoughts to explore economic, psychological, and technological factors
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
### Code Review
|
| 140 |
+
|
| 141 |
+
```
|
| 142 |
+
User: "Review this Python function for errors"
|
| 143 |
+
System: Applies Chain of Thought to identify bugs, suggest improvements
|
| 144 |
+
```
|
| 145 |
+
|
| 146 |
+
### Creative Writing
|
| 147 |
+
|
| 148 |
+
```
|
| 149 |
+
User: "Write a story about AI consciousness"
|
| 150 |
+
System: Uses Reflexion to draft, critique, and refine the narrative
|
| 151 |
+
```
|
| 152 |
+
|
| 153 |
+
### Decision Making
|
| 154 |
+
|
| 155 |
+
```
|
| 156 |
+
User: "Should we implement a four-day work week?"
|
| 157 |
+
System: Multi-Agent Debate presents management and employee perspectives
|
| 158 |
+
```
|
| 159 |
+
|
| 160 |
+
---
|
| 161 |
+
|
| 162 |
+
## 📚 Research Foundation
|
| 163 |
+
|
| 164 |
+
Built on seminal papers:
|
| 165 |
+
|
| 166 |
+
- **Tree of Thoughts** (Yao et al., 2023) – Systematic exploration
|
| 167 |
+
- **Constitutional AI** (Bai et al., 2022) – Self-critique mechanisms
|
| 168 |
+
- **Chain of Thought** (Wei et al., 2022) – Transparent reasoning
|
| 169 |
+
- **Reflexion** (Shinn et al., 2023) – Iterative improvement
|
| 170 |
+
- **Self-Consistency** (Wang et al., 2022) – Consensus building
|
| 171 |
+
|
| 172 |
+
---
|
| 173 |
+
|
| 174 |
+
## 🔍 Project Structure
|
| 175 |
+
|
| 176 |
+
```
|
| 177 |
+
ai-reasoning-system/
|
| 178 |
+
├── main.py # Gradio interface and event handlers
|
| 179 |
+
├── core.py # Business logic and reasoning engine
|
| 180 |
+
├── config.py # Configuration and constants
|
| 181 |
+
├── requirements.txt # Dependencies
|
| 182 |
+
├── README.md # Project documentation
|
| 183 |
+
├── .env # API keys (created by user)
|
| 184 |
+
├── exports/ # Exported conversations
|
| 185 |
+
├── backups/ # Automatic backups
|
| 186 |
+
└── reasoning_system.log # Application logs
|
| 187 |
+
```
|
| 188 |
+
|
| 189 |
+
---
|
| 190 |
+
|
| 191 |
+
## 🧪 Development
|
| 192 |
+
|
| 193 |
+
### Running Tests
|
| 194 |
+
|
| 195 |
+
```bash
|
| 196 |
+
# Install test dependencies
|
| 197 |
+
pip install pytest pytest-cov
|
| 198 |
+
|
| 199 |
+
# Run tests
|
| 200 |
+
pytest tests/ -v --cov=core
|
| 201 |
+
```
|
| 202 |
+
|
| 203 |
+
### Adding New Reasoning Mode
|
| 204 |
+
|
| 205 |
+
1. Add enum value in `ReasoningMode`
|
| 206 |
+
2. Add system prompt in `PromptEngine.SYSTEM_PROMPTS`
|
| 207 |
+
3. Add reasoning template in `PromptEngine.REASONING_PROMPTS`
|
| 208 |
+
4. Update UI choices in `main.py`
|
| 209 |
+
|
| 210 |
+
### Custom Models
|
| 211 |
+
|
| 212 |
+
Add to `ModelConfig` enum:
|
| 213 |
+
|
| 214 |
+
```python
|
| 215 |
+
CUSTOM_MODEL = ("custom-model-id", parameters, context_length, "Description")
|
| 216 |
+
```
|
| 217 |
+
|
| 218 |
+
---
|
| 219 |
+
|
| 220 |
+
## 🔧 Troubleshooting
|
| 221 |
+
|
| 222 |
+
| Issue | Solution |
|
| 223 |
+
|-------|----------|
|
| 224 |
+
| API Key Error | Check `.env` file format: `GROQ_API_KEY=gsk_...` |
|
| 225 |
+
| Rate Limit Hit | Wait 60 seconds or reduce request frequency |
|
| 226 |
+
| Memory Issues | Reduce `MAX_CONVERSATION_STORAGE` in config |
|
| 227 |
+
| PDF Export Fails | Install reportlab: `pip install reportlab` |
|
| 228 |
+
| Port Already in Use | Change port: `python main.py --port 7861` |
|
| 229 |
+
|
| 230 |
+
---
|
| 231 |
+
|
| 232 |
+
## 📄 License
|
| 233 |
+
|
| 234 |
+
This project is licensed under the **MIT License** - see the [LICENSE](LICENSE) file for details.
|
| 235 |
+
|
| 236 |
+
---
|
| 237 |
+
|
| 238 |
+
## 🎓 Academic Use
|
| 239 |
+
|
| 240 |
+
Perfect for:
|
| 241 |
+
|
| 242 |
+
- Final year projects
|
| 243 |
+
- Research demonstrations
|
| 244 |
+
- AI methodology studies
|
| 245 |
+
- Human-AI interaction experiments
|
| 246 |
+
|
| 247 |
+
### Citation
|
| 248 |
+
|
| 249 |
+
```bibtex
|
| 250 |
+
@software{ai_reasoning_system_2025,
|
| 251 |
+
title = {Advanced AI Reasoning Research System},
|
| 252 |
+
year = {2025},
|
| 253 |
+
url = {https://github.com/your-username/ai-reasoning-system}
|
| 254 |
+
}
|
| 255 |
+
```
|
| 256 |
+
|
| 257 |
+
---
|
| 258 |
+
|
| 259 |
+
## 🤝 Contributing
|
| 260 |
+
|
| 261 |
+
1. Fork the repository
|
| 262 |
+
2. Create feature branch: `git checkout -b feature-name`
|
| 263 |
+
3. Commit changes: `git commit -m "Add feature"`
|
| 264 |
+
4. Push to branch: `git push origin feature-name`
|
| 265 |
+
5. Submit Pull Request
|
| 266 |
+
|
| 267 |
+
---
|
| 268 |
+
|
| 269 |
+
## 📞 Support
|
| 270 |
+
|
| 271 |
+
- Create an [issue](https://github.com/your-username/ai-reasoning-system/issues) for bugs or features
|
| 272 |
+
- Check existing issues before creating new ones
|
| 273 |
+
- Include system details and error logs
|
| 274 |
+
|
| 275 |
+
---
|
| 276 |
+
|
| 277 |
+
<div align="center">
|
| 278 |
+
|
| 279 |
+
### ⭐ Star this repo if you find it helpful!
|
| 280 |
+
|
| 281 |
+
Made with ❤️ by the AI Research Community
|
| 282 |
+
|
| 283 |
+
[Report Bug](https://github.com/your-username/ai-reasoning-system/issues) · [Request Feature](https://github.com/your-username/ai-reasoning-system/issues) · [Documentation](https://github.com/your-username/ai-reasoning-system/wiki)
|
| 284 |
+
|
| 285 |
+
</div>
|
config.py
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from enum import Enum
|
| 4 |
+
from logging.handlers import RotatingFileHandler
|
| 5 |
+
|
| 6 |
+
def setup_logging():
|
| 7 |
+
"""Setup advanced logging with rotation"""
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
logger.setLevel(logging.INFO)
|
| 10 |
+
|
| 11 |
+
# Prevent duplicate handlers
|
| 12 |
+
if logger.handlers:
|
| 13 |
+
return logger
|
| 14 |
+
|
| 15 |
+
console_handler = logging.StreamHandler()
|
| 16 |
+
console_handler.setLevel(logging.INFO)
|
| 17 |
+
console_format = logging.Formatter(
|
| 18 |
+
'%(asctime)s | %(levelname)-8s | %(message)s',
|
| 19 |
+
datefmt='%H:%M:%S'
|
| 20 |
+
)
|
| 21 |
+
console_handler.setFormatter(console_format)
|
| 22 |
+
|
| 23 |
+
file_handler = RotatingFileHandler(
|
| 24 |
+
'reasoning_system.log',
|
| 25 |
+
maxBytes=10*1024*1024,
|
| 26 |
+
backupCount=5,
|
| 27 |
+
encoding='utf-8'
|
| 28 |
+
)
|
| 29 |
+
file_handler.setLevel(logging.DEBUG)
|
| 30 |
+
file_format = logging.Formatter(
|
| 31 |
+
'%(asctime)s | %(levelname)-8s | %(name)s:%(lineno)d | %(message)s'
|
| 32 |
+
)
|
| 33 |
+
file_handler.setFormatter(file_format)
|
| 34 |
+
|
| 35 |
+
logger.addHandler(console_handler)
|
| 36 |
+
logger.addHandler(file_handler)
|
| 37 |
+
return logger
|
| 38 |
+
|
| 39 |
+
logger = setup_logging()
|
| 40 |
+
|
| 41 |
+
class AppConfig:
|
| 42 |
+
"""Centralized application configuration"""
|
| 43 |
+
MAX_HISTORY_LENGTH: int = 10
|
| 44 |
+
MAX_CONVERSATION_STORAGE: int = 1000
|
| 45 |
+
DEFAULT_TEMPERATURE: float = 0.7
|
| 46 |
+
MIN_TEMPERATURE: float = 0.0
|
| 47 |
+
MAX_TEMPERATURE: float = 2.0
|
| 48 |
+
DEFAULT_MAX_TOKENS: int = 4000
|
| 49 |
+
MIN_TOKENS: int = 100
|
| 50 |
+
MAX_TOKENS: int = 32000
|
| 51 |
+
REQUEST_TIMEOUT: int = 60
|
| 52 |
+
MAX_RETRIES: int = 3
|
| 53 |
+
RETRY_DELAY: float = 1.0
|
| 54 |
+
CACHE_SIZE: int = 100
|
| 55 |
+
CACHE_TTL: int = 3600
|
| 56 |
+
RATE_LIMIT_REQUESTS: int = 50
|
| 57 |
+
RATE_LIMIT_WINDOW: int = 60
|
| 58 |
+
EXPORT_DIR: Path = Path("exports")
|
| 59 |
+
BACKUP_DIR: Path = Path("backups")
|
| 60 |
+
MAX_EXPORT_SIZE_MB: int = 50
|
| 61 |
+
THEME_PRIMARY: str = "purple"
|
| 62 |
+
THEME_SECONDARY: str = "blue"
|
| 63 |
+
AUTO_SAVE_INTERVAL: int = 300
|
| 64 |
+
ENABLE_ANALYTICS: bool = True
|
| 65 |
+
ANALYTICS_BATCH_SIZE: int = 10
|
| 66 |
+
|
| 67 |
+
@classmethod
|
| 68 |
+
def validate(cls) -> bool:
|
| 69 |
+
try:
|
| 70 |
+
assert cls.MIN_TEMPERATURE <= cls.DEFAULT_TEMPERATURE <= cls.MAX_TEMPERATURE
|
| 71 |
+
assert cls.MIN_TOKENS <= cls.DEFAULT_MAX_TOKENS <= cls.MAX_TOKENS
|
| 72 |
+
assert cls.MAX_HISTORY_LENGTH > 0
|
| 73 |
+
return True
|
| 74 |
+
except AssertionError as e:
|
| 75 |
+
logger.error(f"Configuration validation failed: {e}")
|
| 76 |
+
return False
|
| 77 |
+
|
| 78 |
+
@classmethod
|
| 79 |
+
def create_directories(cls) -> None:
|
| 80 |
+
cls.EXPORT_DIR.mkdir(exist_ok=True)
|
| 81 |
+
cls.BACKUP_DIR.mkdir(exist_ok=True)
|
| 82 |
+
logger.info("Application directories initialized")
|
| 83 |
+
|
| 84 |
+
AppConfig.create_directories()
|
| 85 |
+
AppConfig.validate()
|
| 86 |
+
|
| 87 |
+
class ReasoningMode(Enum):
|
| 88 |
+
"""Research-aligned reasoning methodologies"""
|
| 89 |
+
TREE_OF_THOUGHTS = "Tree of Thoughts (ToT)"
|
| 90 |
+
CHAIN_OF_THOUGHT = "Chain of Thought (CoT)"
|
| 91 |
+
SELF_CONSISTENCY = "Self-Consistency Sampling"
|
| 92 |
+
REFLEXION = "Reflexion + Self-Correction"
|
| 93 |
+
DEBATE = "Multi-Agent Debate"
|
| 94 |
+
ANALOGICAL = "Analogical Reasoning"
|
| 95 |
+
|
| 96 |
+
class ModelConfig(Enum):
|
| 97 |
+
"""Available models with specifications"""
|
| 98 |
+
# Original Models
|
| 99 |
+
LLAMA_70B = ("llama-3.3-70b-versatile", 70, 8000, "Best overall")
|
| 100 |
+
DEEPSEEK_70B = ("deepseek-r1-distill-llama-70b", 70, 8000, "Optimized reasoning")
|
| 101 |
+
MIXTRAL_8X7B = ("mixtral-8x7b-32768", 47, 32768, "Long context")
|
| 102 |
+
LLAMA_70B_V31 = ("llama-3.1-70b-versatile", 70, 8000, "Stable")
|
| 103 |
+
GEMMA_9B = ("gemma2-9b-it", 9, 8192, "Fast")
|
| 104 |
+
|
| 105 |
+
# Meta / Llama
|
| 106 |
+
LLAMA_3_1_8B_INSTANT = ("llama-3.1-8b-instant", 8, 131072, "Fast responses")
|
| 107 |
+
LLAMA_4_MAVERICK_17B = ("meta-llama/llama-4-maverick-17b-128k", 17, 131072, "Llama 4 experimental")
|
| 108 |
+
LLAMA_4_SCOUT_17B = ("meta-llama/llama-4-scout-17b-16e-instruct", 17, 16384, "Llama 4 scout model")
|
| 109 |
+
LLAMA_GUARD_4_12B = ("meta-llama/llama-guard-4-12b", 12, 8192, "Safety/Guard model")
|
| 110 |
+
LLAMA_PROMPT_GUARD_2_22M = ("meta-llama/llama-prompt-guard-2-22m", 0, 8192, "Prompt safety (22M)")
|
| 111 |
+
LLAMA_PROMPT_GUARD_2_86M = ("meta-llama/llama-prompt-guard-2-86m", 0, 8192, "Prompt safety (86M)")
|
| 112 |
+
|
| 113 |
+
# Moonshot AI
|
| 114 |
+
KIMI_K2_INSTRUCT_DEPRECATED = ("moonshotai/kimi-k2-instruct", 0, 200000, "Long context (Deprecated)")
|
| 115 |
+
KIMI_K2_INSTRUCT_0905 = ("moonshotai/kimi-k2-instruct-0905", 0, 200000, "Long context")
|
| 116 |
+
|
| 117 |
+
# OpenAI
|
| 118 |
+
GPT_OSS_120B = ("openai/gpt-oss-120b", 120, 8192, "OpenAI open source model")
|
| 119 |
+
GPT_OSS_20B = ("openai/gpt-oss-20b", 20, 8192, "OpenAI open source model")
|
| 120 |
+
|
| 121 |
+
# Qwen
|
| 122 |
+
QWEN3_32B = ("qwen/qwen3-32b", 32, 32768, "Qwen 3 model")
|
| 123 |
+
|
| 124 |
+
# Groq
|
| 125 |
+
GROQ_COMPOUND = ("groq/compound", 0, 8192, "Groq compound model")
|
| 126 |
+
GROQ_COMPOUND_MINI = ("groq/compound-mini", 0, 8192, "Groq mini compound model")
|
| 127 |
+
|
| 128 |
+
def __init__(self, model_id: str, params_b: int, max_context: int, description: str):
|
| 129 |
+
self.model_id = model_id
|
| 130 |
+
self.params_b = params_b
|
| 131 |
+
self.max_context = max_context
|
| 132 |
+
self.description = description
|
| 133 |
+
|
| 134 |
+
CUSTOM_CSS = """
|
| 135 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&family=JetBrains+Mono:wght@400;500;600&display=swap');
|
| 136 |
+
|
| 137 |
+
:root {
|
| 138 |
+
--primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 139 |
+
--success-gradient: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
|
| 140 |
+
--shadow-lg: 0 10px 40px rgba(0,0,0,0.15);
|
| 141 |
+
--border-radius: 16px;
|
| 142 |
+
--transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
.research-header {
|
| 146 |
+
background: var(--primary-gradient);
|
| 147 |
+
padding: 3rem 2.5rem;
|
| 148 |
+
border-radius: var(--border-radius);
|
| 149 |
+
color: white;
|
| 150 |
+
margin-bottom: 2rem;
|
| 151 |
+
box-shadow: var(--shadow-lg);
|
| 152 |
+
animation: slideDown 0.6s ease-out;
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
.research-header h1 {
|
| 156 |
+
font-size: 2.5rem;
|
| 157 |
+
font-weight: 800;
|
| 158 |
+
margin-bottom: 1rem;
|
| 159 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
.badge {
|
| 163 |
+
background: rgba(255,255,255,0.25);
|
| 164 |
+
backdrop-filter: blur(10px);
|
| 165 |
+
color: white;
|
| 166 |
+
padding: 0.5rem 1.2rem;
|
| 167 |
+
border-radius: 25px;
|
| 168 |
+
font-size: 0.9rem;
|
| 169 |
+
margin: 0.3rem;
|
| 170 |
+
display: inline-block;
|
| 171 |
+
transition: var(--transition);
|
| 172 |
+
border: 1px solid rgba(255,255,255,0.2);
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
.badge:hover {
|
| 176 |
+
transform: translateY(-2px);
|
| 177 |
+
background: rgba(255,255,255,0.35);
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
.metrics-card {
|
| 181 |
+
background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
|
| 182 |
+
border-left: 5px solid #667eea;
|
| 183 |
+
padding: 1.8rem;
|
| 184 |
+
border-radius: var(--border-radius);
|
| 185 |
+
margin: 1rem 0;
|
| 186 |
+
font-family: 'JetBrains Mono', monospace;
|
| 187 |
+
transition: var(--transition);
|
| 188 |
+
color: #2c3e50 !important;
|
| 189 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
.metrics-card strong {
|
| 193 |
+
color: #1a1a1a !important;
|
| 194 |
+
font-weight: 600;
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
.metrics-card:hover {
|
| 198 |
+
transform: translateX(5px);
|
| 199 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.12);
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
.analytics-panel {
|
| 203 |
+
background: var(--success-gradient);
|
| 204 |
+
color: white;
|
| 205 |
+
padding: 2rem;
|
| 206 |
+
border-radius: var(--border-radius);
|
| 207 |
+
animation: fadeIn 0.5s ease-out;
|
| 208 |
+
box-shadow: var(--shadow-lg);
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
.analytics-panel h3 {
|
| 212 |
+
color: white !important;
|
| 213 |
+
margin-bottom: 1rem;
|
| 214 |
+
font-size: 1.5rem;
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
.analytics-panel p {
|
| 218 |
+
color: rgba(255,255,255,0.95) !important;
|
| 219 |
+
line-height: 1.6;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
.analytics-panel strong {
|
| 223 |
+
color: white !important;
|
| 224 |
+
font-weight: 600;
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
.status-active {
|
| 228 |
+
color: #10b981 !important;
|
| 229 |
+
font-weight: bold;
|
| 230 |
+
animation: pulse 2s infinite;
|
| 231 |
+
text-shadow: 0 0 10px rgba(16, 185, 129, 0.5);
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
@keyframes slideDown {
|
| 235 |
+
from { opacity: 0; transform: translateY(-30px); }
|
| 236 |
+
to { opacity: 1; transform: translateY(0); }
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
@keyframes fadeIn {
|
| 240 |
+
from { opacity: 0; transform: scale(0.95); }
|
| 241 |
+
to { opacity: 1; transform: scale(1); }
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
@keyframes pulse {
|
| 245 |
+
0%, 100% { opacity: 1; }
|
| 246 |
+
50% { opacity: 0.7; }
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
.gradio-container {
|
| 250 |
+
font-family: 'Inter', sans-serif !important;
|
| 251 |
+
max-width: 1600px !important;
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
.gr-button {
|
| 255 |
+
transition: var(--transition) !important;
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
.gr-button:hover {
|
| 259 |
+
transform: translateY(-2px) !important;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
.gr-markdown {
|
| 263 |
+
color: #2c3e50 !important;
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
.gr-markdown strong {
|
| 267 |
+
color: #1a1a1a !important;
|
| 268 |
+
}
|
| 269 |
+
"""
|
| 270 |
+
|
| 271 |
+
logger.info("Enhanced configuration initialized")
|
core.py
ADDED
|
@@ -0,0 +1,986 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import json
|
| 4 |
+
import hashlib
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
from typing import List, Dict, Generator, Optional, Any, Tuple
|
| 7 |
+
from dataclasses import dataclass, field, asdict
|
| 8 |
+
from functools import wraps, lru_cache
|
| 9 |
+
from contextlib import contextmanager
|
| 10 |
+
from collections import deque, defaultdict
|
| 11 |
+
import threading
|
| 12 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 13 |
+
|
| 14 |
+
from dotenv import load_dotenv
|
| 15 |
+
from groq import Groq
|
| 16 |
+
|
| 17 |
+
from config import logger, AppConfig, ReasoningMode, ModelConfig
|
| 18 |
+
|
| 19 |
+
class ResponseCache:
|
| 20 |
+
"""Thread-safe LRU cache for API responses"""
|
| 21 |
+
def __init__(self, maxsize: int = 100, ttl: int = 3600):
|
| 22 |
+
self.cache: Dict[str, Tuple[Any, float]] = {}
|
| 23 |
+
self.maxsize = maxsize
|
| 24 |
+
self.ttl = ttl
|
| 25 |
+
self.lock = threading.Lock()
|
| 26 |
+
self.hits = 0
|
| 27 |
+
self.misses = 0
|
| 28 |
+
|
| 29 |
+
def get(self, key: str) -> Optional[Any]:
|
| 30 |
+
"""Get cached value if not expired"""
|
| 31 |
+
with self.lock:
|
| 32 |
+
if key in self.cache:
|
| 33 |
+
value, timestamp = self.cache[key]
|
| 34 |
+
if time.time() - timestamp < self.ttl:
|
| 35 |
+
self.hits += 1
|
| 36 |
+
logger.debug(f"Cache hit for key: {key[:20]}...")
|
| 37 |
+
return value
|
| 38 |
+
else:
|
| 39 |
+
del self.cache[key]
|
| 40 |
+
self.misses += 1
|
| 41 |
+
return None
|
| 42 |
+
|
| 43 |
+
def set(self, key: str, value: Any) -> None:
|
| 44 |
+
"""Set cached value with timestamp"""
|
| 45 |
+
with self.lock:
|
| 46 |
+
if len(self.cache) >= self.maxsize:
|
| 47 |
+
oldest_key = min(self.cache.keys(), key=lambda k: self.cache[k][1])
|
| 48 |
+
del self.cache[oldest_key]
|
| 49 |
+
self.cache[key] = (value, time.time())
|
| 50 |
+
logger.debug(f"Cached response for key: {key[:20]}...")
|
| 51 |
+
|
| 52 |
+
def clear(self) -> None:
|
| 53 |
+
"""Clear cache"""
|
| 54 |
+
with self.lock:
|
| 55 |
+
self.cache.clear()
|
| 56 |
+
logger.info("Cache cleared")
|
| 57 |
+
|
| 58 |
+
def get_stats(self) -> Dict[str, int]:
|
| 59 |
+
"""Get cache statistics"""
|
| 60 |
+
with self.lock:
|
| 61 |
+
total = self.hits + self.misses
|
| 62 |
+
hit_rate = (self.hits / total * 100) if total > 0 else 0
|
| 63 |
+
return {
|
| 64 |
+
"hits": self.hits,
|
| 65 |
+
"misses": self.misses,
|
| 66 |
+
"hit_rate": round(hit_rate, 2),
|
| 67 |
+
"size": len(self.cache)
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
class RateLimiter:
|
| 71 |
+
"""Token bucket rate limiter"""
|
| 72 |
+
def __init__(self, max_requests: int = 50, window: int = 60):
|
| 73 |
+
self.max_requests = max_requests
|
| 74 |
+
self.window = window
|
| 75 |
+
self.requests = deque()
|
| 76 |
+
self.lock = threading.Lock()
|
| 77 |
+
|
| 78 |
+
def is_allowed(self) -> Tuple[bool, Optional[float]]:
|
| 79 |
+
"""Check if request is allowed"""
|
| 80 |
+
with self.lock:
|
| 81 |
+
now = time.time()
|
| 82 |
+
while self.requests and self.requests[0] < now - self.window:
|
| 83 |
+
self.requests.popleft()
|
| 84 |
+
|
| 85 |
+
if len(self.requests) < self.max_requests:
|
| 86 |
+
self.requests.append(now)
|
| 87 |
+
return True, None
|
| 88 |
+
else:
|
| 89 |
+
wait_time = self.window - (now - self.requests[0])
|
| 90 |
+
return False, wait_time
|
| 91 |
+
|
| 92 |
+
def reset(self) -> None:
|
| 93 |
+
"""Reset rate limiter"""
|
| 94 |
+
with self.lock:
|
| 95 |
+
self.requests.clear()
|
| 96 |
+
|
| 97 |
+
@dataclass
|
| 98 |
+
class ConversationMetrics:
|
| 99 |
+
"""Enhanced metrics with advanced tracking"""
|
| 100 |
+
reasoning_depth: int = 0
|
| 101 |
+
self_corrections: int = 0
|
| 102 |
+
confidence_score: float = 0.0
|
| 103 |
+
inference_time: float = 0.0
|
| 104 |
+
tokens_used: int = 0
|
| 105 |
+
tokens_per_second: float = 0.0
|
| 106 |
+
reasoning_paths_explored: int = 0
|
| 107 |
+
total_conversations: int = 0
|
| 108 |
+
avg_response_time: float = 0.0
|
| 109 |
+
cache_hits: int = 0
|
| 110 |
+
cache_misses: int = 0
|
| 111 |
+
error_count: int = 0
|
| 112 |
+
retry_count: int = 0
|
| 113 |
+
last_updated: str = field(default_factory=lambda: datetime.now().strftime("%H:%M:%S"))
|
| 114 |
+
session_start: str = field(default_factory=lambda: datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
| 115 |
+
model_switches: int = 0
|
| 116 |
+
mode_switches: int = 0
|
| 117 |
+
peak_tokens: int = 0
|
| 118 |
+
total_latency: float = 0.0
|
| 119 |
+
|
| 120 |
+
def update_confidence(self) -> None:
|
| 121 |
+
"""Calculate confidence based on multiple factors"""
|
| 122 |
+
depth_score = min(30, self.reasoning_depth * 5)
|
| 123 |
+
correction_score = min(20, self.self_corrections * 10)
|
| 124 |
+
speed_score = min(25, 25 / max(1, self.avg_response_time))
|
| 125 |
+
consistency_score = 25
|
| 126 |
+
self.confidence_score = min(95.0, depth_score + correction_score + speed_score + consistency_score)
|
| 127 |
+
|
| 128 |
+
def update_tokens_per_second(self, tokens: int, time_taken: float) -> None:
|
| 129 |
+
"""Calculate tokens per second"""
|
| 130 |
+
if time_taken > 0:
|
| 131 |
+
self.tokens_per_second = tokens / time_taken
|
| 132 |
+
|
| 133 |
+
def reset(self) -> None:
|
| 134 |
+
"""Reset metrics for new session"""
|
| 135 |
+
self.__init__()
|
| 136 |
+
|
| 137 |
+
def to_dict(self) -> Dict[str, Any]:
|
| 138 |
+
"""Convert to dictionary"""
|
| 139 |
+
return asdict(self)
|
| 140 |
+
|
| 141 |
+
@dataclass
|
| 142 |
+
class ConversationEntry:
|
| 143 |
+
"""Enhanced conversation entry with metadata"""
|
| 144 |
+
timestamp: str
|
| 145 |
+
user_message: str
|
| 146 |
+
ai_response: str
|
| 147 |
+
model: str
|
| 148 |
+
reasoning_mode: str
|
| 149 |
+
inference_time: float
|
| 150 |
+
tokens: int
|
| 151 |
+
feedback: str = ""
|
| 152 |
+
tags: List[str] = field(default_factory=list)
|
| 153 |
+
rating: Optional[int] = None
|
| 154 |
+
session_id: str = ""
|
| 155 |
+
conversation_id: str = ""
|
| 156 |
+
parent_id: Optional[str] = None
|
| 157 |
+
temperature: float = 0.7
|
| 158 |
+
max_tokens: int = 4000
|
| 159 |
+
cache_hit: bool = False
|
| 160 |
+
error_occurred: bool = False
|
| 161 |
+
retry_count: int = 0
|
| 162 |
+
tokens_per_second: float = 0.0
|
| 163 |
+
|
| 164 |
+
def __post_init__(self):
|
| 165 |
+
"""Generate unique IDs"""
|
| 166 |
+
if not self.conversation_id:
|
| 167 |
+
self.conversation_id = self._generate_id()
|
| 168 |
+
|
| 169 |
+
def _generate_id(self) -> str:
|
| 170 |
+
"""Generate unique conversation ID"""
|
| 171 |
+
content = f"{self.timestamp}{self.user_message}"
|
| 172 |
+
return hashlib.md5(content.encode()).hexdigest()[:12]
|
| 173 |
+
|
| 174 |
+
def to_dict(self) -> Dict[str, Any]:
|
| 175 |
+
"""Convert to dictionary with sanitization"""
|
| 176 |
+
return asdict(self)
|
| 177 |
+
|
| 178 |
+
@classmethod
|
| 179 |
+
def from_dict(cls, data: Dict[str, Any]) -> 'ConversationEntry':
|
| 180 |
+
"""Create instance from dictionary"""
|
| 181 |
+
return cls(**data)
|
| 182 |
+
|
| 183 |
+
def add_tag(self, tag: str) -> None:
|
| 184 |
+
"""Add tag to conversation"""
|
| 185 |
+
if tag not in self.tags:
|
| 186 |
+
self.tags.append(tag)
|
| 187 |
+
|
| 188 |
+
def set_rating(self, rating: int) -> None:
|
| 189 |
+
"""Set user rating (1-5)"""
|
| 190 |
+
if 1 <= rating <= 5:
|
| 191 |
+
self.rating = rating
|
| 192 |
+
|
| 193 |
+
def error_handler(func):
|
| 194 |
+
"""Enhanced error handling decorator with retries"""
|
| 195 |
+
@wraps(func)
|
| 196 |
+
def wrapper(*args, **kwargs):
|
| 197 |
+
max_retries = AppConfig.MAX_RETRIES
|
| 198 |
+
retry_delay = AppConfig.RETRY_DELAY
|
| 199 |
+
|
| 200 |
+
for attempt in range(max_retries):
|
| 201 |
+
try:
|
| 202 |
+
return func(*args, **kwargs)
|
| 203 |
+
except Exception as e:
|
| 204 |
+
logger.error(f"Error in {func.__name__} (attempt {attempt+1}/{max_retries}): {str(e)}")
|
| 205 |
+
|
| 206 |
+
if attempt < max_retries - 1:
|
| 207 |
+
logger.info(f"Retrying in {retry_delay}s...")
|
| 208 |
+
time.sleep(retry_delay)
|
| 209 |
+
retry_delay *= 2
|
| 210 |
+
else:
|
| 211 |
+
error_msg = f"System Error: {str(e)}\n\n"
|
| 212 |
+
|
| 213 |
+
if "api" in str(e).lower() or "key" in str(e).lower():
|
| 214 |
+
error_msg += "Please verify your GROQ_API_KEY in the .env file."
|
| 215 |
+
elif "rate" in str(e).lower() or "limit" in str(e).lower():
|
| 216 |
+
error_msg += "Rate limit exceeded. Please wait a moment and try again."
|
| 217 |
+
elif "timeout" in str(e).lower():
|
| 218 |
+
error_msg += "Request timed out. Please try again."
|
| 219 |
+
else:
|
| 220 |
+
error_msg += "Please try again or contact support if the issue persists."
|
| 221 |
+
|
| 222 |
+
return error_msg
|
| 223 |
+
return wrapper
|
| 224 |
+
|
| 225 |
+
@contextmanager
|
| 226 |
+
def timer(operation: str = "Operation"):
|
| 227 |
+
"""Enhanced context manager for timing operations"""
|
| 228 |
+
start = time.time()
|
| 229 |
+
logger.info(f"Starting: {operation}")
|
| 230 |
+
try:
|
| 231 |
+
yield
|
| 232 |
+
finally:
|
| 233 |
+
duration = time.time() - start
|
| 234 |
+
logger.info(f"Completed: {operation} in {duration:.3f}s")
|
| 235 |
+
|
| 236 |
+
def validate_input(text: str, max_length: int = 10000) -> Tuple[bool, Optional[str]]:
|
| 237 |
+
"""Validate user input"""
|
| 238 |
+
if not text or not text.strip():
|
| 239 |
+
return False, "Input cannot be empty"
|
| 240 |
+
|
| 241 |
+
if len(text) > max_length:
|
| 242 |
+
return False, f"Input too long (max {max_length} characters)"
|
| 243 |
+
|
| 244 |
+
suspicious_patterns = ["<script", "javascript:", "onerror=", "onclick="]
|
| 245 |
+
text_lower = text.lower()
|
| 246 |
+
for pattern in suspicious_patterns:
|
| 247 |
+
if pattern in text_lower:
|
| 248 |
+
return False, "Input contains potentially unsafe content"
|
| 249 |
+
|
| 250 |
+
return True, None
|
| 251 |
+
|
| 252 |
+
class GroqClientManager:
|
| 253 |
+
"""Enhanced singleton manager for Groq client"""
|
| 254 |
+
_instance: Optional[Groq] = None
|
| 255 |
+
_lock = threading.Lock()
|
| 256 |
+
_initialized = False
|
| 257 |
+
_health_check_time: Optional[float] = None
|
| 258 |
+
_health_check_interval = 300
|
| 259 |
+
|
| 260 |
+
@classmethod
|
| 261 |
+
def get_client(cls) -> Groq:
|
| 262 |
+
"""Get or create Groq client instance with health check"""
|
| 263 |
+
if cls._instance is None:
|
| 264 |
+
with cls._lock:
|
| 265 |
+
if cls._instance is None:
|
| 266 |
+
cls._initialize_client()
|
| 267 |
+
|
| 268 |
+
if cls._should_health_check():
|
| 269 |
+
cls._perform_health_check()
|
| 270 |
+
|
| 271 |
+
return cls._instance
|
| 272 |
+
|
| 273 |
+
@classmethod
|
| 274 |
+
def _initialize_client(cls) -> None:
|
| 275 |
+
"""Initialize Groq client"""
|
| 276 |
+
load_dotenv()
|
| 277 |
+
api_key = os.environ.get("GROQ_API_KEY")
|
| 278 |
+
|
| 279 |
+
if not api_key:
|
| 280 |
+
logger.error("GROQ_API_KEY not found in environment")
|
| 281 |
+
raise ValueError("GROQ_API_KEY not found. Please set it in your .env file.")
|
| 282 |
+
|
| 283 |
+
try:
|
| 284 |
+
cls._instance = Groq(api_key=api_key, timeout=AppConfig.REQUEST_TIMEOUT)
|
| 285 |
+
cls._initialized = True
|
| 286 |
+
cls._health_check_time = time.time()
|
| 287 |
+
logger.info("Groq client initialized successfully")
|
| 288 |
+
except Exception as e:
|
| 289 |
+
logger.error(f"Failed to initialize Groq client: {e}")
|
| 290 |
+
raise
|
| 291 |
+
|
| 292 |
+
@classmethod
|
| 293 |
+
def _should_health_check(cls) -> bool:
|
| 294 |
+
"""Check if health check is needed"""
|
| 295 |
+
if not cls._health_check_time:
|
| 296 |
+
return True
|
| 297 |
+
return time.time() - cls._health_check_time > cls._health_check_interval
|
| 298 |
+
|
| 299 |
+
@classmethod
|
| 300 |
+
def _perform_health_check(cls) -> None:
|
| 301 |
+
"""Perform health check on client"""
|
| 302 |
+
try:
|
| 303 |
+
if cls._instance:
|
| 304 |
+
cls._health_check_time = time.time()
|
| 305 |
+
logger.debug("Health check passed")
|
| 306 |
+
except Exception as e:
|
| 307 |
+
logger.warning(f"Health check failed: {e}")
|
| 308 |
+
cls._instance = None
|
| 309 |
+
cls._initialized = False
|
| 310 |
+
|
| 311 |
+
class PromptEngine:
|
| 312 |
+
"""Enhanced centralized prompt management"""
|
| 313 |
+
|
| 314 |
+
SYSTEM_PROMPTS = {
|
| 315 |
+
ReasoningMode.TREE_OF_THOUGHTS: """You are an advanced reasoning system using Tree of Thoughts methodology.
|
| 316 |
+
Explore multiple reasoning paths systematically before converging on the best solution.
|
| 317 |
+
Always show your thought process explicitly.""",
|
| 318 |
+
|
| 319 |
+
ReasoningMode.CHAIN_OF_THOUGHT: """You are a systematic problem solver using Chain of Thought reasoning.
|
| 320 |
+
Break down complex problems into clear, logical steps with explicit reasoning.""",
|
| 321 |
+
|
| 322 |
+
ReasoningMode.SELF_CONSISTENCY: """You are a consistency-focused reasoning system.
|
| 323 |
+
Generate multiple independent solutions and identify the most consistent answer.""",
|
| 324 |
+
|
| 325 |
+
ReasoningMode.REFLEXION: """You are a self-reflective AI system.
|
| 326 |
+
Solve problems, critique your own reasoning, and refine your solutions iteratively.""",
|
| 327 |
+
|
| 328 |
+
ReasoningMode.DEBATE: """You are a multi-agent debate system.
|
| 329 |
+
Present multiple perspectives and synthesize the strongest arguments.""",
|
| 330 |
+
|
| 331 |
+
ReasoningMode.ANALOGICAL: """You are an analogical reasoning system.
|
| 332 |
+
Find similar problems and apply their solutions."""
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
TEMPLATES = {
|
| 336 |
+
"Code Review": {
|
| 337 |
+
"prompt": "Analyze the following code for bugs, performance issues, and best practices:\n\n{query}",
|
| 338 |
+
"context": "code_analysis"
|
| 339 |
+
},
|
| 340 |
+
"Research Summary": {
|
| 341 |
+
"prompt": "Provide a comprehensive research summary on:\n\n{query}\n\nInclude key findings, methodologies, and implications.",
|
| 342 |
+
"context": "research"
|
| 343 |
+
},
|
| 344 |
+
"Problem Solving": {
|
| 345 |
+
"prompt": "Solve this problem step-by-step with detailed explanations:\n\n{query}",
|
| 346 |
+
"context": "problem_solving"
|
| 347 |
+
},
|
| 348 |
+
"Creative Writing": {
|
| 349 |
+
"prompt": "Generate creative content based on:\n\n{query}\n\nBe imaginative and engaging.",
|
| 350 |
+
"context": "creative"
|
| 351 |
+
},
|
| 352 |
+
"Data Analysis": {
|
| 353 |
+
"prompt": "Analyze this data/scenario and provide insights:\n\n{query}",
|
| 354 |
+
"context": "analysis"
|
| 355 |
+
},
|
| 356 |
+
"Debugging": {
|
| 357 |
+
"prompt": "Debug this code/issue systematically:\n\n{query}",
|
| 358 |
+
"context": "debugging"
|
| 359 |
+
},
|
| 360 |
+
"Custom": {
|
| 361 |
+
"prompt": "{query}",
|
| 362 |
+
"context": "general"
|
| 363 |
+
}
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
REASONING_PROMPTS = {
|
| 367 |
+
ReasoningMode.TREE_OF_THOUGHTS: """
|
| 368 |
+
**Tree of Thoughts Analysis**
|
| 369 |
+
|
| 370 |
+
Problem: {query}
|
| 371 |
+
|
| 372 |
+
**Exploration Phase:**
|
| 373 |
+
PATH A (Analytical): [Examine from first principles]
|
| 374 |
+
PATH B (Alternative): [Consider different angle]
|
| 375 |
+
PATH C (Synthesis): [Integrate insights]
|
| 376 |
+
|
| 377 |
+
**Evaluation Phase:**
|
| 378 |
+
- Assess each path's validity
|
| 379 |
+
- Identify strongest reasoning chain
|
| 380 |
+
- Converge on optimal solution
|
| 381 |
+
|
| 382 |
+
**Final Solution:** [Most robust answer with justification]""",
|
| 383 |
+
|
| 384 |
+
ReasoningMode.CHAIN_OF_THOUGHT: """
|
| 385 |
+
**Step-by-Step Reasoning**
|
| 386 |
+
|
| 387 |
+
Problem: {query}
|
| 388 |
+
|
| 389 |
+
Step 1: Understand the question
|
| 390 |
+
Step 2: Identify key components
|
| 391 |
+
Step 3: Apply relevant logic/principles
|
| 392 |
+
Step 4: Derive solution
|
| 393 |
+
Step 5: Validate and verify
|
| 394 |
+
|
| 395 |
+
Final Answer: [Clear, justified conclusion]""",
|
| 396 |
+
|
| 397 |
+
ReasoningMode.SELF_CONSISTENCY: """
|
| 398 |
+
**Multi-Path Consistency Check**
|
| 399 |
+
|
| 400 |
+
Problem: {query}
|
| 401 |
+
|
| 402 |
+
**Attempt 1:** [First independent solution]
|
| 403 |
+
**Attempt 2:** [Alternative approach]
|
| 404 |
+
**Attempt 3:** [Third perspective]
|
| 405 |
+
|
| 406 |
+
**Consensus:** [Most consistent answer across attempts]""",
|
| 407 |
+
|
| 408 |
+
ReasoningMode.REFLEXION: """
|
| 409 |
+
**Reflexion with Self-Correction**
|
| 410 |
+
|
| 411 |
+
Problem: {query}
|
| 412 |
+
|
| 413 |
+
**Initial Solution:** [First attempt]
|
| 414 |
+
|
| 415 |
+
**Self-Critique:**
|
| 416 |
+
- Assumptions made?
|
| 417 |
+
- Logical flaws?
|
| 418 |
+
- Missing elements?
|
| 419 |
+
|
| 420 |
+
**Refined Solution:** [Improved answer based on reflection]""",
|
| 421 |
+
|
| 422 |
+
ReasoningMode.DEBATE: """
|
| 423 |
+
**Multi-Agent Debate**
|
| 424 |
+
|
| 425 |
+
Problem: {query}
|
| 426 |
+
|
| 427 |
+
**Position A:** [Strongest case for one approach]
|
| 428 |
+
**Position B:** [Critical examination]
|
| 429 |
+
**Synthesis:** [Balanced conclusion]""",
|
| 430 |
+
|
| 431 |
+
ReasoningMode.ANALOGICAL: """
|
| 432 |
+
**Analogical Reasoning**
|
| 433 |
+
|
| 434 |
+
Problem: {query}
|
| 435 |
+
|
| 436 |
+
**Similar Problems:** [Identify analogous situations]
|
| 437 |
+
**Solution Transfer:** [Adapt known solutions]
|
| 438 |
+
**Final Answer:** [Solution derived from analogy]"""
|
| 439 |
+
}
|
| 440 |
+
|
| 441 |
+
@classmethod
|
| 442 |
+
def build_prompt(cls, query: str, mode: ReasoningMode, template: str) -> str:
|
| 443 |
+
"""Build enhanced reasoning prompt"""
|
| 444 |
+
template_data = cls.TEMPLATES.get(template, cls.TEMPLATES["Custom"])
|
| 445 |
+
formatted_query = template_data["prompt"].format(query=query)
|
| 446 |
+
return cls.REASONING_PROMPTS[mode].format(query=formatted_query)
|
| 447 |
+
|
| 448 |
+
@classmethod
|
| 449 |
+
def build_critique_prompt(cls) -> str:
|
| 450 |
+
"""Build validation prompt for self-critique"""
|
| 451 |
+
return """
|
| 452 |
+
**Validation Check:**
|
| 453 |
+
Review the previous response for:
|
| 454 |
+
1. Factual accuracy
|
| 455 |
+
2. Logical consistency
|
| 456 |
+
3. Completeness
|
| 457 |
+
4. Potential biases or errors
|
| 458 |
+
|
| 459 |
+
Provide brief validation or corrections if needed."""
|
| 460 |
+
|
| 461 |
+
@classmethod
|
| 462 |
+
def get_template_context(cls, template: str) -> str:
|
| 463 |
+
"""Get context for template"""
|
| 464 |
+
return cls.TEMPLATES.get(template, {}).get("context", "general")
|
| 465 |
+
|
| 466 |
+
class ConversationExporter:
|
| 467 |
+
"""Enhanced conversation export with multiple formats including PDF"""
|
| 468 |
+
|
| 469 |
+
@staticmethod
|
| 470 |
+
def to_json(entries: List[ConversationEntry], pretty: bool = True) -> str:
|
| 471 |
+
"""Export to JSON format"""
|
| 472 |
+
data = [entry.to_dict() for entry in entries]
|
| 473 |
+
indent = 2 if pretty else None
|
| 474 |
+
return json.dumps(data, indent=indent, ensure_ascii=False)
|
| 475 |
+
|
| 476 |
+
@staticmethod
|
| 477 |
+
def to_markdown(entries: List[ConversationEntry], include_metadata: bool = True) -> str:
|
| 478 |
+
"""Export to Markdown format"""
|
| 479 |
+
md = "# Conversation History\n\n"
|
| 480 |
+
md += f"*Exported on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*\n\n"
|
| 481 |
+
md += "---\n\n"
|
| 482 |
+
|
| 483 |
+
for i, entry in enumerate(entries, 1):
|
| 484 |
+
md += f"## Conversation {i}\n\n"
|
| 485 |
+
md += f"**Timestamp:** {entry.timestamp} \n"
|
| 486 |
+
md += f"**Model:** {entry.model} \n"
|
| 487 |
+
md += f"**Mode:** {entry.reasoning_mode} \n"
|
| 488 |
+
md += f"**Performance:** {entry.inference_time:.2f}s | {entry.tokens} tokens\n\n"
|
| 489 |
+
md += f"### User\n\n{entry.user_message}\n\n"
|
| 490 |
+
md += f"### Assistant\n\n{entry.ai_response}\n\n"
|
| 491 |
+
md += "---\n\n"
|
| 492 |
+
|
| 493 |
+
return md
|
| 494 |
+
|
| 495 |
+
@staticmethod
|
| 496 |
+
def to_text(entries: List[ConversationEntry]) -> str:
|
| 497 |
+
"""Export to plain text format"""
|
| 498 |
+
txt = "="*70 + "\n"
|
| 499 |
+
txt += "CONVERSATION HISTORY\n"
|
| 500 |
+
txt += f"Exported: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
|
| 501 |
+
txt += "="*70 + "\n\n"
|
| 502 |
+
|
| 503 |
+
for i, entry in enumerate(entries, 1):
|
| 504 |
+
txt += f"Conversation {i}\n"
|
| 505 |
+
txt += f"Time: {entry.timestamp}\n"
|
| 506 |
+
txt += f"Model: {entry.model} | Mode: {entry.reasoning_mode}\n"
|
| 507 |
+
txt += f"Performance: {entry.inference_time:.2f}s | {entry.tokens} tokens\n"
|
| 508 |
+
txt += "\n"
|
| 509 |
+
txt += f"USER:\n{entry.user_message}\n\n"
|
| 510 |
+
txt += f"ASSISTANT:\n{entry.ai_response}\n"
|
| 511 |
+
txt += "\n" + "-"*70 + "\n\n"
|
| 512 |
+
|
| 513 |
+
return txt
|
| 514 |
+
|
| 515 |
+
@staticmethod
|
| 516 |
+
def to_pdf(entries: List[ConversationEntry], filename: str) -> str:
|
| 517 |
+
"""Export to PDF format"""
|
| 518 |
+
try:
|
| 519 |
+
from reportlab.lib.pagesizes import letter
|
| 520 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 521 |
+
from reportlab.lib.units import inch
|
| 522 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
|
| 523 |
+
from reportlab.lib.enums import TA_LEFT, TA_CENTER
|
| 524 |
+
from reportlab.lib.colors import HexColor
|
| 525 |
+
|
| 526 |
+
doc = SimpleDocTemplate(filename, pagesize=letter)
|
| 527 |
+
story = []
|
| 528 |
+
styles = getSampleStyleSheet()
|
| 529 |
+
|
| 530 |
+
title_style = ParagraphStyle(
|
| 531 |
+
'CustomTitle',
|
| 532 |
+
parent=styles['Heading1'],
|
| 533 |
+
fontSize=24,
|
| 534 |
+
textColor=HexColor('#667eea'),
|
| 535 |
+
spaceAfter=30,
|
| 536 |
+
alignment=TA_CENTER
|
| 537 |
+
)
|
| 538 |
+
|
| 539 |
+
heading_style = ParagraphStyle(
|
| 540 |
+
'CustomHeading',
|
| 541 |
+
parent=styles['Heading2'],
|
| 542 |
+
fontSize=14,
|
| 543 |
+
textColor=HexColor('#764ba2'),
|
| 544 |
+
spaceAfter=12,
|
| 545 |
+
spaceBefore=12
|
| 546 |
+
)
|
| 547 |
+
|
| 548 |
+
user_style = ParagraphStyle(
|
| 549 |
+
'UserStyle',
|
| 550 |
+
parent=styles['Normal'],
|
| 551 |
+
fontSize=11,
|
| 552 |
+
textColor=HexColor('#2c3e50'),
|
| 553 |
+
leftIndent=20,
|
| 554 |
+
spaceAfter=10
|
| 555 |
+
)
|
| 556 |
+
|
| 557 |
+
ai_style = ParagraphStyle(
|
| 558 |
+
'AIStyle',
|
| 559 |
+
parent=styles['Normal'],
|
| 560 |
+
fontSize=11,
|
| 561 |
+
textColor=HexColor('#34495e'),
|
| 562 |
+
leftIndent=20,
|
| 563 |
+
spaceAfter=10
|
| 564 |
+
)
|
| 565 |
+
|
| 566 |
+
meta_style = ParagraphStyle(
|
| 567 |
+
'MetaStyle',
|
| 568 |
+
parent=styles['Normal'],
|
| 569 |
+
fontSize=9,
|
| 570 |
+
textColor=HexColor('#7f8c8d'),
|
| 571 |
+
spaceAfter=6
|
| 572 |
+
)
|
| 573 |
+
|
| 574 |
+
story.append(Paragraph("AI Reasoning Chat History", title_style))
|
| 575 |
+
story.append(Paragraph(
|
| 576 |
+
f"Exported on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
|
| 577 |
+
meta_style
|
| 578 |
+
))
|
| 579 |
+
story.append(Spacer(1, 0.3*inch))
|
| 580 |
+
|
| 581 |
+
for i, entry in enumerate(entries, 1):
|
| 582 |
+
story.append(Paragraph(f"Conversation {i}", heading_style))
|
| 583 |
+
|
| 584 |
+
meta_text = f"<b>Time:</b> {entry.timestamp} | <b>Model:</b> {entry.model} | <b>Mode:</b> {entry.reasoning_mode}"
|
| 585 |
+
story.append(Paragraph(meta_text, meta_style))
|
| 586 |
+
|
| 587 |
+
perf_text = f"<b>Performance:</b> {entry.inference_time:.2f}s | {entry.tokens} tokens | {entry.tokens_per_second:.1f} tok/s"
|
| 588 |
+
story.append(Paragraph(perf_text, meta_style))
|
| 589 |
+
story.append(Spacer(1, 0.1*inch))
|
| 590 |
+
|
| 591 |
+
story.append(Paragraph("<b>User:</b>", user_style))
|
| 592 |
+
user_msg = entry.user_message.replace('<', '<').replace('>', '>').replace('\n', '<br/>')
|
| 593 |
+
if len(user_msg) > 3000:
|
| 594 |
+
user_msg = user_msg[:3000] + "... (truncated)"
|
| 595 |
+
story.append(Paragraph(user_msg, user_style))
|
| 596 |
+
story.append(Spacer(1, 0.15*inch))
|
| 597 |
+
|
| 598 |
+
story.append(Paragraph("<b>Assistant:</b>", ai_style))
|
| 599 |
+
ai_resp = entry.ai_response.replace('<', '<').replace('>', '>').replace('\n', '<br/>')
|
| 600 |
+
if len(ai_resp) > 5000:
|
| 601 |
+
ai_resp = ai_resp[:5000] + "... (truncated)"
|
| 602 |
+
story.append(Paragraph(ai_resp, ai_style))
|
| 603 |
+
|
| 604 |
+
if i < len(entries):
|
| 605 |
+
story.append(PageBreak())
|
| 606 |
+
|
| 607 |
+
doc.build(story)
|
| 608 |
+
logger.info(f"PDF exported to {filename}")
|
| 609 |
+
return filename
|
| 610 |
+
|
| 611 |
+
except ImportError:
|
| 612 |
+
error_msg = "reportlab library not installed. Run: pip install reportlab"
|
| 613 |
+
logger.error(error_msg)
|
| 614 |
+
return ""
|
| 615 |
+
except Exception as e:
|
| 616 |
+
logger.error(f"PDF export failed: {e}")
|
| 617 |
+
return ""
|
| 618 |
+
|
| 619 |
+
@classmethod
|
| 620 |
+
def export(cls, entries: List[ConversationEntry], format_type: str,
|
| 621 |
+
include_metadata: bool = True) -> Tuple[str, str]:
|
| 622 |
+
"""Export conversation and return content and filename"""
|
| 623 |
+
|
| 624 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 625 |
+
|
| 626 |
+
if format_type == "pdf":
|
| 627 |
+
ext = "pdf"
|
| 628 |
+
filename = AppConfig.EXPORT_DIR / f"conversation_{timestamp}.{ext}"
|
| 629 |
+
result = cls.to_pdf(entries, str(filename))
|
| 630 |
+
if result:
|
| 631 |
+
return "PDF exported successfully! Check the exports folder.", str(filename)
|
| 632 |
+
else:
|
| 633 |
+
return "PDF export failed. Install reportlab: pip install reportlab", ""
|
| 634 |
+
|
| 635 |
+
exporters = {
|
| 636 |
+
"json": lambda: cls.to_json(entries),
|
| 637 |
+
"markdown": lambda: cls.to_markdown(entries, include_metadata),
|
| 638 |
+
"txt": lambda: cls.to_text(entries)
|
| 639 |
+
}
|
| 640 |
+
|
| 641 |
+
if format_type not in exporters:
|
| 642 |
+
format_type = "markdown"
|
| 643 |
+
|
| 644 |
+
content = exporters[format_type]()
|
| 645 |
+
ext = "md" if format_type == "markdown" else format_type
|
| 646 |
+
filename = AppConfig.EXPORT_DIR / f"conversation_{timestamp}.{ext}"
|
| 647 |
+
|
| 648 |
+
try:
|
| 649 |
+
with open(filename, 'w', encoding='utf-8') as f:
|
| 650 |
+
f.write(content)
|
| 651 |
+
logger.info(f"Conversation exported to {filename}")
|
| 652 |
+
return content, str(filename)
|
| 653 |
+
except Exception as e:
|
| 654 |
+
logger.error(f"Failed to export conversation: {e}")
|
| 655 |
+
return f"Error: {str(e)}", ""
|
| 656 |
+
|
| 657 |
+
@staticmethod
|
| 658 |
+
def create_backup(entries: List[ConversationEntry]) -> str:
|
| 659 |
+
"""Create automatic backup"""
|
| 660 |
+
if not entries:
|
| 661 |
+
return ""
|
| 662 |
+
|
| 663 |
+
try:
|
| 664 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 665 |
+
filename = AppConfig.BACKUP_DIR / f"backup_{timestamp}.json"
|
| 666 |
+
|
| 667 |
+
data = [entry.to_dict() for entry in entries]
|
| 668 |
+
with open(filename, 'w', encoding='utf-8') as f:
|
| 669 |
+
json.dump(data, f, indent=2, ensure_ascii=False)
|
| 670 |
+
|
| 671 |
+
logger.info(f"Backup created: {filename}")
|
| 672 |
+
return str(filename)
|
| 673 |
+
except Exception as e:
|
| 674 |
+
logger.error(f"Backup failed: {e}")
|
| 675 |
+
return ""
|
| 676 |
+
|
| 677 |
+
class AdvancedReasoner:
|
| 678 |
+
"""Enhanced reasoning engine with caching, rate limiting, and advanced features"""
|
| 679 |
+
|
| 680 |
+
def __init__(self):
|
| 681 |
+
self.client = GroqClientManager.get_client()
|
| 682 |
+
self.metrics = ConversationMetrics()
|
| 683 |
+
self.conversation_history: List[ConversationEntry] = []
|
| 684 |
+
self.response_times: List[float] = []
|
| 685 |
+
self.prompt_engine = PromptEngine()
|
| 686 |
+
self.exporter = ConversationExporter()
|
| 687 |
+
|
| 688 |
+
self.cache = ResponseCache(maxsize=AppConfig.CACHE_SIZE, ttl=AppConfig.CACHE_TTL)
|
| 689 |
+
self.rate_limiter = RateLimiter(
|
| 690 |
+
max_requests=AppConfig.RATE_LIMIT_REQUESTS,
|
| 691 |
+
window=AppConfig.RATE_LIMIT_WINDOW
|
| 692 |
+
)
|
| 693 |
+
self.session_id = hashlib.md5(str(time.time()).encode()).hexdigest()[:12]
|
| 694 |
+
self.executor = ThreadPoolExecutor(max_workers=3)
|
| 695 |
+
|
| 696 |
+
self.model_usage: Dict[str, int] = defaultdict(int)
|
| 697 |
+
self.mode_usage: Dict[str, int] = defaultdict(int)
|
| 698 |
+
self.error_log: List[Dict[str, Any]] = []
|
| 699 |
+
|
| 700 |
+
logger.info(f"AdvancedReasoner initialized with session ID: {self.session_id}")
|
| 701 |
+
|
| 702 |
+
def _generate_cache_key(self, query: str, model: str, mode: str,
|
| 703 |
+
temp: float, template: str) -> str:
|
| 704 |
+
"""Generate cache key for request"""
|
| 705 |
+
content = f"{query}|{model}|{mode}|{temp:.2f}|{template}"
|
| 706 |
+
return hashlib.sha256(content.encode()).hexdigest()
|
| 707 |
+
|
| 708 |
+
def _calculate_reasoning_depth(self, response: str) -> int:
|
| 709 |
+
"""Calculate reasoning depth from response"""
|
| 710 |
+
indicators = {
|
| 711 |
+
"Step": 3, "PATH": 4, "Attempt": 3, "Phase": 3,
|
| 712 |
+
"Analysis": 2, "Consider": 1, "Therefore": 2,
|
| 713 |
+
"Conclusion": 2, "Evidence": 2, "Reasoning": 1
|
| 714 |
+
}
|
| 715 |
+
|
| 716 |
+
depth = 0
|
| 717 |
+
for indicator, weight in indicators.items():
|
| 718 |
+
depth += response.count(indicator) * weight
|
| 719 |
+
|
| 720 |
+
return min(depth, 100)
|
| 721 |
+
|
| 722 |
+
def _build_messages(
|
| 723 |
+
self,
|
| 724 |
+
query: str,
|
| 725 |
+
history: List[Dict],
|
| 726 |
+
mode: ReasoningMode,
|
| 727 |
+
template: str
|
| 728 |
+
) -> List[Dict[str, str]]:
|
| 729 |
+
"""Build message list for API call"""
|
| 730 |
+
messages = [
|
| 731 |
+
{"role": "system", "content": self.prompt_engine.SYSTEM_PROMPTS[mode]}
|
| 732 |
+
]
|
| 733 |
+
|
| 734 |
+
recent_history = history[-AppConfig.MAX_HISTORY_LENGTH:] if history else []
|
| 735 |
+
for msg in recent_history:
|
| 736 |
+
clean_msg = {
|
| 737 |
+
"role": msg.get("role"),
|
| 738 |
+
"content": msg.get("content", "")
|
| 739 |
+
}
|
| 740 |
+
messages.append(clean_msg)
|
| 741 |
+
|
| 742 |
+
enhanced_query = self.prompt_engine.build_prompt(query, mode, template)
|
| 743 |
+
messages.append({"role": "user", "content": enhanced_query})
|
| 744 |
+
|
| 745 |
+
return messages
|
| 746 |
+
|
| 747 |
+
def _log_error(self, error: Exception, context: Dict[str, Any]) -> None:
|
| 748 |
+
"""Log error with context"""
|
| 749 |
+
error_entry = {
|
| 750 |
+
"timestamp": datetime.now().isoformat(),
|
| 751 |
+
"error": str(error),
|
| 752 |
+
"type": type(error).__name__,
|
| 753 |
+
"context": context
|
| 754 |
+
}
|
| 755 |
+
self.error_log.append(error_entry)
|
| 756 |
+
self.metrics.error_count += 1
|
| 757 |
+
logger.error(f"Error logged: {error_entry}")
|
| 758 |
+
|
| 759 |
+
@error_handler
|
| 760 |
+
def generate_response(
|
| 761 |
+
self,
|
| 762 |
+
query: str,
|
| 763 |
+
history: List[Dict],
|
| 764 |
+
model: str,
|
| 765 |
+
reasoning_mode: ReasoningMode,
|
| 766 |
+
enable_critique: bool,
|
| 767 |
+
temperature: float,
|
| 768 |
+
max_tokens: int,
|
| 769 |
+
prompt_template: str = "Custom",
|
| 770 |
+
use_cache: bool = True
|
| 771 |
+
) -> Generator[str, None, None]:
|
| 772 |
+
"""Generate response with advanced features"""
|
| 773 |
+
|
| 774 |
+
is_valid, error_msg = validate_input(query)
|
| 775 |
+
if not is_valid:
|
| 776 |
+
yield f"Validation Error: {error_msg}"
|
| 777 |
+
return
|
| 778 |
+
|
| 779 |
+
allowed, wait_time = self.rate_limiter.is_allowed()
|
| 780 |
+
if not allowed:
|
| 781 |
+
yield f"Rate Limit: Please wait {wait_time:.1f} seconds."
|
| 782 |
+
return
|
| 783 |
+
|
| 784 |
+
cache_key = self._generate_cache_key(query, model, reasoning_mode.value, temperature, prompt_template)
|
| 785 |
+
if use_cache:
|
| 786 |
+
cached_response = self.cache.get(cache_key)
|
| 787 |
+
if cached_response:
|
| 788 |
+
self.metrics.cache_hits += 1
|
| 789 |
+
logger.info("Returning cached response")
|
| 790 |
+
yield cached_response
|
| 791 |
+
return
|
| 792 |
+
|
| 793 |
+
self.metrics.cache_misses += 1
|
| 794 |
+
|
| 795 |
+
with timer(f"Response generation for {model}"):
|
| 796 |
+
start_time = time.time()
|
| 797 |
+
messages = self._build_messages(query, history, reasoning_mode, prompt_template)
|
| 798 |
+
|
| 799 |
+
full_response = ""
|
| 800 |
+
token_count = 0
|
| 801 |
+
|
| 802 |
+
try:
|
| 803 |
+
stream = self.client.chat.completions.create(
|
| 804 |
+
messages=messages,
|
| 805 |
+
model=model,
|
| 806 |
+
temperature=temperature,
|
| 807 |
+
max_tokens=max_tokens,
|
| 808 |
+
stream=True,
|
| 809 |
+
)
|
| 810 |
+
|
| 811 |
+
for chunk in stream:
|
| 812 |
+
if chunk.choices[0].delta.content:
|
| 813 |
+
content = chunk.choices[0].delta.content
|
| 814 |
+
full_response += content
|
| 815 |
+
token_count += 1
|
| 816 |
+
self.metrics.tokens_used += 1
|
| 817 |
+
yield full_response
|
| 818 |
+
|
| 819 |
+
except Exception as e:
|
| 820 |
+
self._log_error(e, {
|
| 821 |
+
"query": query[:100],
|
| 822 |
+
"model": model,
|
| 823 |
+
"mode": reasoning_mode.value
|
| 824 |
+
})
|
| 825 |
+
raise
|
| 826 |
+
|
| 827 |
+
inference_time = time.time() - start_time
|
| 828 |
+
self.metrics.reasoning_depth = self._calculate_reasoning_depth(full_response)
|
| 829 |
+
self.metrics.update_tokens_per_second(token_count, inference_time)
|
| 830 |
+
self.metrics.peak_tokens = max(self.metrics.peak_tokens, token_count)
|
| 831 |
+
|
| 832 |
+
if enable_critique and len(full_response) > 150:
|
| 833 |
+
messages.append({"role": "assistant", "content": full_response})
|
| 834 |
+
messages.append({
|
| 835 |
+
"role": "user",
|
| 836 |
+
"content": self.prompt_engine.build_critique_prompt()
|
| 837 |
+
})
|
| 838 |
+
|
| 839 |
+
full_response += "\n\n---\n### Validation & Self-Critique\n"
|
| 840 |
+
|
| 841 |
+
try:
|
| 842 |
+
critique_stream = self.client.chat.completions.create(
|
| 843 |
+
messages=messages,
|
| 844 |
+
model=model,
|
| 845 |
+
temperature=temperature * 0.7,
|
| 846 |
+
max_tokens=max_tokens // 3,
|
| 847 |
+
stream=True,
|
| 848 |
+
)
|
| 849 |
+
|
| 850 |
+
for chunk in critique_stream:
|
| 851 |
+
if chunk.choices[0].delta.content:
|
| 852 |
+
content = chunk.choices[0].delta.content
|
| 853 |
+
full_response += content
|
| 854 |
+
token_count += 1
|
| 855 |
+
yield full_response
|
| 856 |
+
|
| 857 |
+
self.metrics.self_corrections += 1
|
| 858 |
+
|
| 859 |
+
except Exception as e:
|
| 860 |
+
logger.warning(f"Critique phase failed: {e}")
|
| 861 |
+
|
| 862 |
+
final_inference_time = time.time() - start_time
|
| 863 |
+
self.metrics.inference_time = final_inference_time
|
| 864 |
+
self.metrics.total_latency += final_inference_time
|
| 865 |
+
self.response_times.append(final_inference_time)
|
| 866 |
+
self.metrics.avg_response_time = sum(self.response_times) / len(self.response_times)
|
| 867 |
+
self.metrics.last_updated = datetime.now().strftime("%H:%M:%S")
|
| 868 |
+
self.metrics.update_confidence()
|
| 869 |
+
self.metrics.total_conversations += 1
|
| 870 |
+
|
| 871 |
+
self.model_usage[model] += 1
|
| 872 |
+
self.mode_usage[reasoning_mode.value] += 1
|
| 873 |
+
|
| 874 |
+
tokens_per_sec = token_count / final_inference_time if final_inference_time > 0 else 0
|
| 875 |
+
entry = ConversationEntry(
|
| 876 |
+
timestamp=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 877 |
+
user_message=query,
|
| 878 |
+
ai_response=full_response,
|
| 879 |
+
model=model,
|
| 880 |
+
reasoning_mode=reasoning_mode.value,
|
| 881 |
+
inference_time=final_inference_time,
|
| 882 |
+
tokens=token_count,
|
| 883 |
+
session_id=self.session_id,
|
| 884 |
+
temperature=temperature,
|
| 885 |
+
max_tokens=max_tokens,
|
| 886 |
+
cache_hit=False,
|
| 887 |
+
tokens_per_second=tokens_per_sec
|
| 888 |
+
)
|
| 889 |
+
|
| 890 |
+
self.conversation_history.append(entry)
|
| 891 |
+
|
| 892 |
+
if use_cache:
|
| 893 |
+
self.cache.set(cache_key, full_response)
|
| 894 |
+
|
| 895 |
+
if len(self.conversation_history) % 10 == 0:
|
| 896 |
+
try:
|
| 897 |
+
self.exporter.create_backup(self.conversation_history)
|
| 898 |
+
except Exception as e:
|
| 899 |
+
logger.warning(f"Auto-backup failed: {e}")
|
| 900 |
+
|
| 901 |
+
if len(self.conversation_history) > AppConfig.MAX_CONVERSATION_STORAGE:
|
| 902 |
+
self.conversation_history = self.conversation_history[-AppConfig.MAX_CONVERSATION_STORAGE:]
|
| 903 |
+
logger.info(f"Trimmed history to {AppConfig.MAX_CONVERSATION_STORAGE} entries")
|
| 904 |
+
|
| 905 |
+
yield full_response
|
| 906 |
+
|
| 907 |
+
def export_conversation(self, format_type: str, include_metadata: bool = True) -> Tuple[str, str]:
|
| 908 |
+
"""Export conversation history"""
|
| 909 |
+
if not self.conversation_history:
|
| 910 |
+
return "No conversations to export.", ""
|
| 911 |
+
|
| 912 |
+
try:
|
| 913 |
+
return self.exporter.export(self.conversation_history, format_type, include_metadata)
|
| 914 |
+
except Exception as e:
|
| 915 |
+
logger.error(f"Export failed: {e}")
|
| 916 |
+
return f"Export failed: {str(e)}", ""
|
| 917 |
+
|
| 918 |
+
def export_current_chat_pdf(self) -> Optional[str]:
|
| 919 |
+
"""Export current chat as PDF - for quick download button"""
|
| 920 |
+
if not self.conversation_history:
|
| 921 |
+
return None
|
| 922 |
+
|
| 923 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 924 |
+
filename = AppConfig.EXPORT_DIR / f"chat_{timestamp}.pdf"
|
| 925 |
+
|
| 926 |
+
result = self.exporter.to_pdf(self.conversation_history, str(filename))
|
| 927 |
+
return result if result else None
|
| 928 |
+
|
| 929 |
+
def search_conversations(self, keyword: str) -> List[Tuple[int, ConversationEntry]]:
|
| 930 |
+
"""Search through conversation history"""
|
| 931 |
+
keyword_lower = keyword.lower()
|
| 932 |
+
return [
|
| 933 |
+
(i, entry) for i, entry in enumerate(self.conversation_history)
|
| 934 |
+
if keyword_lower in entry.user_message.lower()
|
| 935 |
+
or keyword_lower in entry.ai_response.lower()
|
| 936 |
+
]
|
| 937 |
+
|
| 938 |
+
def get_analytics(self) -> Optional[Dict[str, Any]]:
|
| 939 |
+
"""Generate analytics data"""
|
| 940 |
+
if not self.conversation_history:
|
| 941 |
+
return None
|
| 942 |
+
|
| 943 |
+
models = [e.model for e in self.conversation_history]
|
| 944 |
+
modes = [e.reasoning_mode for e in self.conversation_history]
|
| 945 |
+
total_time = sum(e.inference_time for e in self.conversation_history)
|
| 946 |
+
total_tokens = sum(e.tokens for e in self.conversation_history)
|
| 947 |
+
|
| 948 |
+
return {
|
| 949 |
+
"session_id": self.session_id,
|
| 950 |
+
"total_conversations": len(self.conversation_history),
|
| 951 |
+
"total_tokens": total_tokens,
|
| 952 |
+
"total_time": total_time,
|
| 953 |
+
"avg_inference_time": self.metrics.avg_response_time,
|
| 954 |
+
"peak_tokens": self.metrics.peak_tokens,
|
| 955 |
+
"most_used_model": max(set(models), key=models.count),
|
| 956 |
+
"most_used_mode": max(set(modes), key=modes.count),
|
| 957 |
+
"cache_hits": self.metrics.cache_hits,
|
| 958 |
+
"cache_misses": self.metrics.cache_misses,
|
| 959 |
+
"error_count": self.metrics.error_count
|
| 960 |
+
}
|
| 961 |
+
|
| 962 |
+
def clear_history(self) -> None:
|
| 963 |
+
"""Clear conversation history and reset metrics"""
|
| 964 |
+
if self.conversation_history:
|
| 965 |
+
try:
|
| 966 |
+
self.exporter.create_backup(self.conversation_history)
|
| 967 |
+
except Exception as e:
|
| 968 |
+
logger.warning(f"Failed to backup before clearing: {e}")
|
| 969 |
+
|
| 970 |
+
self.conversation_history.clear()
|
| 971 |
+
self.response_times.clear()
|
| 972 |
+
self.metrics.reset()
|
| 973 |
+
self.cache.clear()
|
| 974 |
+
self.rate_limiter.reset()
|
| 975 |
+
self.model_usage.clear()
|
| 976 |
+
self.mode_usage.clear()
|
| 977 |
+
|
| 978 |
+
logger.info("History cleared and metrics reset")
|
| 979 |
+
|
| 980 |
+
def __del__(self):
|
| 981 |
+
"""Cleanup on deletion"""
|
| 982 |
+
try:
|
| 983 |
+
self.executor.shutdown(wait=False)
|
| 984 |
+
logger.info("AdvancedReasoner cleanup completed")
|
| 985 |
+
except:
|
| 986 |
+
pass
|
main.py
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from config import logger, CUSTOM_CSS, ReasoningMode, AppConfig, ModelConfig
|
| 3 |
+
from core import AdvancedReasoner, PromptEngine
|
| 4 |
+
|
| 5 |
+
# Initialize system
|
| 6 |
+
reasoner = AdvancedReasoner()
|
| 7 |
+
|
| 8 |
+
def get_metrics_html() -> str:
|
| 9 |
+
"""Generate enhanced metrics HTML"""
|
| 10 |
+
m = reasoner.metrics
|
| 11 |
+
cache_stats = reasoner.cache.get_stats()
|
| 12 |
+
status = '<span class="status-active">Active</span>' if m.tokens_used > 0 else 'Ready'
|
| 13 |
+
|
| 14 |
+
return f"""<div class="metrics-card">
|
| 15 |
+
<strong>Inference:</strong> {m.inference_time:.2f}s<br>
|
| 16 |
+
<strong>Avg Time:</strong> {m.avg_response_time:.2f}s<br>
|
| 17 |
+
<strong>Speed:</strong> {m.tokens_per_second:.1f} tok/s<br>
|
| 18 |
+
<strong>Reasoning:</strong> {m.reasoning_depth} steps<br>
|
| 19 |
+
<strong>Corrections:</strong> {m.self_corrections}<br>
|
| 20 |
+
<strong>Confidence:</strong> {m.confidence_score:.1f}%<br>
|
| 21 |
+
<strong>Total:</strong> {m.total_conversations}<br>
|
| 22 |
+
<strong>Tokens:</strong> {m.tokens_used:,}<br>
|
| 23 |
+
<strong>Peak:</strong> {m.peak_tokens}<br>
|
| 24 |
+
<strong>Cache:</strong> {cache_stats['hit_rate']}% hit rate<br>
|
| 25 |
+
<strong>Status:</strong> {status}<br>
|
| 26 |
+
<strong>Session:</strong> {reasoner.session_id[:8]}...
|
| 27 |
+
</div>"""
|
| 28 |
+
|
| 29 |
+
def get_empty_analytics_html() -> str:
|
| 30 |
+
"""Generate empty analytics HTML"""
|
| 31 |
+
return """<div class="analytics-panel">
|
| 32 |
+
<h3>No data yet</h3>
|
| 33 |
+
<p>Start a conversation to see analytics!</p>
|
| 34 |
+
</div>"""
|
| 35 |
+
|
| 36 |
+
def create_ui() -> gr.Blocks:
|
| 37 |
+
"""Create enhanced Gradio interface"""
|
| 38 |
+
|
| 39 |
+
with gr.Blocks(
|
| 40 |
+
theme=gr.themes.Soft(
|
| 41 |
+
primary_hue=AppConfig.THEME_PRIMARY,
|
| 42 |
+
secondary_hue=AppConfig.THEME_SECONDARY,
|
| 43 |
+
font=gr.themes.GoogleFont("Inter")
|
| 44 |
+
),
|
| 45 |
+
css=CUSTOM_CSS,
|
| 46 |
+
title="Advanced AI Reasoning System Pro"
|
| 47 |
+
) as demo:
|
| 48 |
+
|
| 49 |
+
gr.HTML("""
|
| 50 |
+
<div class="research-header">
|
| 51 |
+
<h1>Advanced AI Reasoning System Pro</h1>
|
| 52 |
+
<p><strong>Enhanced Implementation:</strong> Tree of Thoughts + Constitutional AI + Multi-Agent Validation + Caching + Rate Limiting</p>
|
| 53 |
+
<div style="margin-top: 1rem;">
|
| 54 |
+
<span class="badge">Yao et al. 2023 - Tree of Thoughts</span>
|
| 55 |
+
<span class="badge">Bai et al. 2022 - Constitutional AI</span>
|
| 56 |
+
<span class="badge">Enhanced with 6 Reasoning Modes</span>
|
| 57 |
+
<span class="badge">Performance Optimized</span>
|
| 58 |
+
</div>
|
| 59 |
+
</div>
|
| 60 |
+
""")
|
| 61 |
+
|
| 62 |
+
with gr.Tabs():
|
| 63 |
+
# Main Chat Tab
|
| 64 |
+
with gr.Tab("Reasoning Workspace"):
|
| 65 |
+
with gr.Row():
|
| 66 |
+
with gr.Column(scale=3):
|
| 67 |
+
chatbot = gr.Chatbot(
|
| 68 |
+
label="Reasoning Workspace",
|
| 69 |
+
height=550,
|
| 70 |
+
show_copy_button=True,
|
| 71 |
+
type="messages",
|
| 72 |
+
avatar_images=(
|
| 73 |
+
"https://api.dicebear.com/7.x/avataaars/svg?seed=User",
|
| 74 |
+
"https://api.dicebear.com/7.x/bottts/svg?seed=AI"
|
| 75 |
+
)
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
msg = gr.Textbox(
|
| 79 |
+
placeholder="Enter your complex problem or research question... (Max 10,000 characters)",
|
| 80 |
+
label="Query Input",
|
| 81 |
+
lines=3,
|
| 82 |
+
max_lines=10
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
with gr.Row():
|
| 86 |
+
submit_btn = gr.Button("Process", variant="primary", scale=2)
|
| 87 |
+
clear_btn = gr.Button("Clear", scale=1)
|
| 88 |
+
pdf_btn = gr.Button("Download PDF", scale=1)
|
| 89 |
+
|
| 90 |
+
with gr.Column(scale=1):
|
| 91 |
+
gr.Markdown("### Configuration")
|
| 92 |
+
|
| 93 |
+
reasoning_mode = gr.Radio(
|
| 94 |
+
choices=[mode.value for mode in ReasoningMode],
|
| 95 |
+
value=ReasoningMode.TREE_OF_THOUGHTS.value,
|
| 96 |
+
label="Reasoning Method",
|
| 97 |
+
info="Select the reasoning strategy"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
prompt_template = gr.Dropdown(
|
| 101 |
+
choices=list(PromptEngine.TEMPLATES.keys()),
|
| 102 |
+
value="Custom",
|
| 103 |
+
label="Prompt Template",
|
| 104 |
+
info="Pre-built prompt templates"
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
enable_critique = gr.Checkbox(
|
| 108 |
+
label="Enable Self-Critique",
|
| 109 |
+
value=True,
|
| 110 |
+
info="Add validation phase"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
use_cache = gr.Checkbox(
|
| 114 |
+
label="Use Cache",
|
| 115 |
+
value=True,
|
| 116 |
+
info="Cache responses for speed"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
model = gr.Dropdown(
|
| 120 |
+
choices=[m.model_id for m in ModelConfig],
|
| 121 |
+
value=ModelConfig.LLAMA_70B.model_id,
|
| 122 |
+
label="Model",
|
| 123 |
+
info="Select AI model"
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 127 |
+
temperature = gr.Slider(
|
| 128 |
+
AppConfig.MIN_TEMPERATURE,
|
| 129 |
+
AppConfig.MAX_TEMPERATURE,
|
| 130 |
+
value=AppConfig.DEFAULT_TEMPERATURE,
|
| 131 |
+
step=0.1,
|
| 132 |
+
label="Temperature",
|
| 133 |
+
info="Higher = more creative"
|
| 134 |
+
)
|
| 135 |
+
max_tokens = gr.Slider(
|
| 136 |
+
AppConfig.MIN_TOKENS,
|
| 137 |
+
8000,
|
| 138 |
+
value=AppConfig.DEFAULT_MAX_TOKENS,
|
| 139 |
+
step=500,
|
| 140 |
+
label="Max Tokens",
|
| 141 |
+
info="Maximum response length"
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
gr.Markdown("### Live Metrics")
|
| 145 |
+
metrics_display = gr.Markdown(value=get_metrics_html())
|
| 146 |
+
|
| 147 |
+
with gr.Accordion("Info", open=False):
|
| 148 |
+
gr.Markdown(f"""
|
| 149 |
+
**Session ID:** `{reasoner.session_id}`
|
| 150 |
+
**Cache Size:** {AppConfig.CACHE_SIZE}
|
| 151 |
+
**Rate Limit:** {AppConfig.RATE_LIMIT_REQUESTS} req/{AppConfig.RATE_LIMIT_WINDOW}s
|
| 152 |
+
**Max History:** {AppConfig.MAX_HISTORY_LENGTH} messages
|
| 153 |
+
""")
|
| 154 |
+
|
| 155 |
+
# Export Tab
|
| 156 |
+
with gr.Tab("Export & History"):
|
| 157 |
+
gr.Markdown("### Export Conversation History")
|
| 158 |
+
|
| 159 |
+
with gr.Row():
|
| 160 |
+
export_format = gr.Radio(
|
| 161 |
+
choices=["json", "markdown", "txt", "pdf"],
|
| 162 |
+
value="markdown",
|
| 163 |
+
label="Export Format"
|
| 164 |
+
)
|
| 165 |
+
include_meta = gr.Checkbox(
|
| 166 |
+
label="Include Metadata",
|
| 167 |
+
value=True
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
export_btn = gr.Button("Export Now", variant="primary")
|
| 171 |
+
export_output = gr.Code(label="Exported Data", language="markdown", lines=20)
|
| 172 |
+
download_file = gr.File(label="Download File")
|
| 173 |
+
|
| 174 |
+
gr.Markdown("---")
|
| 175 |
+
gr.Markdown("### Search Conversations")
|
| 176 |
+
|
| 177 |
+
with gr.Row():
|
| 178 |
+
search_input = gr.Textbox(
|
| 179 |
+
placeholder="Enter keyword to search...",
|
| 180 |
+
scale=3,
|
| 181 |
+
label="Search Query"
|
| 182 |
+
)
|
| 183 |
+
search_btn = gr.Button("Search", scale=1)
|
| 184 |
+
|
| 185 |
+
search_results = gr.Markdown("No results yet. Enter a keyword and click Search.")
|
| 186 |
+
|
| 187 |
+
gr.Markdown("---")
|
| 188 |
+
gr.Markdown("### Conversation History")
|
| 189 |
+
history_stats = gr.Markdown("Loading...")
|
| 190 |
+
|
| 191 |
+
# Analytics Tab
|
| 192 |
+
with gr.Tab("Analytics & Insights"):
|
| 193 |
+
refresh_btn = gr.Button("Refresh Analytics", variant="primary", size="lg")
|
| 194 |
+
|
| 195 |
+
with gr.Row():
|
| 196 |
+
with gr.Column():
|
| 197 |
+
gr.Markdown("### Performance Metrics")
|
| 198 |
+
analytics_display = gr.Markdown(get_empty_analytics_html())
|
| 199 |
+
|
| 200 |
+
with gr.Column():
|
| 201 |
+
gr.Markdown("### Cache Statistics")
|
| 202 |
+
cache_display = gr.Markdown("No cache data yet.")
|
| 203 |
+
|
| 204 |
+
gr.Markdown("---")
|
| 205 |
+
gr.Markdown("### Usage Distribution")
|
| 206 |
+
|
| 207 |
+
with gr.Row():
|
| 208 |
+
model_dist = gr.Markdown("**Model Usage:** No data")
|
| 209 |
+
mode_dist = gr.Markdown("**Mode Usage:** No data")
|
| 210 |
+
|
| 211 |
+
# Settings Tab
|
| 212 |
+
with gr.Tab("Settings"):
|
| 213 |
+
gr.Markdown("### Application Settings")
|
| 214 |
+
|
| 215 |
+
gr.Markdown(f"""
|
| 216 |
+
**Current Configuration:**
|
| 217 |
+
|
| 218 |
+
| Setting | Value |
|
| 219 |
+
|---------|-------|
|
| 220 |
+
| Max History Length | {AppConfig.MAX_HISTORY_LENGTH} |
|
| 221 |
+
| Max Conversation Storage | {AppConfig.MAX_CONVERSATION_STORAGE} |
|
| 222 |
+
| Cache Size | {AppConfig.CACHE_SIZE} |
|
| 223 |
+
| Cache TTL | {AppConfig.CACHE_TTL}s |
|
| 224 |
+
| Rate Limit | {AppConfig.RATE_LIMIT_REQUESTS} requests per {AppConfig.RATE_LIMIT_WINDOW}s |
|
| 225 |
+
| Request Timeout | {AppConfig.REQUEST_TIMEOUT}s |
|
| 226 |
+
| Max Retries | {AppConfig.MAX_RETRIES} |
|
| 227 |
+
| Export Directory | `{AppConfig.EXPORT_DIR}` |
|
| 228 |
+
| Backup Directory | `{AppConfig.BACKUP_DIR}` |
|
| 229 |
+
""")
|
| 230 |
+
|
| 231 |
+
clear_cache_btn = gr.Button("Clear Cache", variant="stop")
|
| 232 |
+
cache_status = gr.Markdown("")
|
| 233 |
+
|
| 234 |
+
# Define pdf_file_output BEFORE event handlers
|
| 235 |
+
pdf_file_output = gr.File(visible=False)
|
| 236 |
+
|
| 237 |
+
# Event handlers
|
| 238 |
+
def process_message(message, history, mode, critique, model_name, temp, tokens, template, cache):
|
| 239 |
+
if not message.strip():
|
| 240 |
+
return history, get_metrics_html()
|
| 241 |
+
|
| 242 |
+
history = history or []
|
| 243 |
+
mode_enum = ReasoningMode(mode)
|
| 244 |
+
|
| 245 |
+
history.append({"role": "user", "content": message})
|
| 246 |
+
yield history, get_metrics_html()
|
| 247 |
+
|
| 248 |
+
history.append({"role": "assistant", "content": ""})
|
| 249 |
+
|
| 250 |
+
for response in reasoner.generate_response(
|
| 251 |
+
message, history[:-1], model_name, mode_enum,
|
| 252 |
+
critique, temp, tokens, template, cache
|
| 253 |
+
):
|
| 254 |
+
history[-1]["content"] = response
|
| 255 |
+
yield history, get_metrics_html()
|
| 256 |
+
|
| 257 |
+
def reset_chat():
|
| 258 |
+
reasoner.clear_history()
|
| 259 |
+
return [], get_metrics_html()
|
| 260 |
+
|
| 261 |
+
def export_conv(format_type, include_metadata):
|
| 262 |
+
content, filename = reasoner.export_conversation(format_type, include_metadata)
|
| 263 |
+
return content, filename
|
| 264 |
+
|
| 265 |
+
def download_chat_pdf():
|
| 266 |
+
"""Download current chat as PDF"""
|
| 267 |
+
pdf_file = reasoner.export_current_chat_pdf()
|
| 268 |
+
if pdf_file:
|
| 269 |
+
return pdf_file
|
| 270 |
+
return None
|
| 271 |
+
|
| 272 |
+
def search_conv(keyword):
|
| 273 |
+
if not keyword.strip():
|
| 274 |
+
return "Please enter a search keyword."
|
| 275 |
+
|
| 276 |
+
results = reasoner.search_conversations(keyword)
|
| 277 |
+
if not results:
|
| 278 |
+
return f"No results found for '{keyword}'."
|
| 279 |
+
|
| 280 |
+
output = f"### Found {len(results)} result(s) for '{keyword}'\n\n"
|
| 281 |
+
for idx, entry in results[:10]:
|
| 282 |
+
output += f"**{idx + 1}.** {entry.timestamp} | {entry.model}\n"
|
| 283 |
+
output += f"**User:** {entry.user_message[:100]}...\n\n"
|
| 284 |
+
|
| 285 |
+
if len(results) > 10:
|
| 286 |
+
output += f"\n*Showing first 10 of {len(results)} results*"
|
| 287 |
+
|
| 288 |
+
return output
|
| 289 |
+
|
| 290 |
+
def refresh_analytics():
|
| 291 |
+
analytics = reasoner.get_analytics()
|
| 292 |
+
if not analytics:
|
| 293 |
+
return get_empty_analytics_html(), "No cache data.", "No data", "No data"
|
| 294 |
+
|
| 295 |
+
analytics_html = f"""<div class="analytics-panel">
|
| 296 |
+
<h3>Session Analytics</h3>
|
| 297 |
+
<p><strong>Session ID:</strong> {analytics['session_id']}</p>
|
| 298 |
+
<p><strong>Total Conversations:</strong> {analytics['total_conversations']}</p>
|
| 299 |
+
<p><strong>Total Tokens:</strong> {analytics['total_tokens']:,}</p>
|
| 300 |
+
<p><strong>Total Time:</strong> {analytics['total_time']:.1f}s</p>
|
| 301 |
+
<p><strong>Avg Time:</strong> {analytics['avg_inference_time']:.2f}s</p>
|
| 302 |
+
<p><strong>Peak Tokens:</strong> {analytics['peak_tokens']}</p>
|
| 303 |
+
<p><strong>Most Used Model:</strong> {analytics['most_used_model']}</p>
|
| 304 |
+
<p><strong>Most Used Mode:</strong> {analytics['most_used_mode']}</p>
|
| 305 |
+
<p><strong>Errors:</strong> {analytics['error_count']}</p>
|
| 306 |
+
</div>"""
|
| 307 |
+
|
| 308 |
+
cache_html = f"""**Cache Performance:**
|
| 309 |
+
- Hits: {analytics['cache_hits']}
|
| 310 |
+
- Misses: {analytics['cache_misses']}
|
| 311 |
+
- Total: {analytics['cache_hits'] + analytics['cache_misses']}
|
| 312 |
+
"""
|
| 313 |
+
|
| 314 |
+
model_dist_html = f"**Model Usage:** {analytics['most_used_model']}"
|
| 315 |
+
mode_dist_html = f"**Mode Usage:** {analytics['most_used_mode']}"
|
| 316 |
+
|
| 317 |
+
return analytics_html, cache_html, model_dist_html, mode_dist_html
|
| 318 |
+
|
| 319 |
+
def update_history_stats():
|
| 320 |
+
count = len(reasoner.conversation_history)
|
| 321 |
+
if count == 0:
|
| 322 |
+
return "No conversations yet."
|
| 323 |
+
|
| 324 |
+
return f"""**Total Conversations:** {count}
|
| 325 |
+
**Session:** {reasoner.session_id[:8]}..."""
|
| 326 |
+
|
| 327 |
+
def clear_cache_action():
|
| 328 |
+
reasoner.cache.clear()
|
| 329 |
+
return "Cache cleared successfully!"
|
| 330 |
+
|
| 331 |
+
# Connect events
|
| 332 |
+
submit_btn.click(
|
| 333 |
+
process_message,
|
| 334 |
+
[msg, chatbot, reasoning_mode, enable_critique, model, temperature, max_tokens, prompt_template, use_cache],
|
| 335 |
+
[chatbot, metrics_display]
|
| 336 |
+
).then(lambda: "", None, msg)
|
| 337 |
+
|
| 338 |
+
msg.submit(
|
| 339 |
+
process_message,
|
| 340 |
+
[msg, chatbot, reasoning_mode, enable_critique, model, temperature, max_tokens, prompt_template, use_cache],
|
| 341 |
+
[chatbot, metrics_display]
|
| 342 |
+
).then(lambda: "", None, msg)
|
| 343 |
+
|
| 344 |
+
clear_btn.click(reset_chat, None, [chatbot, metrics_display])
|
| 345 |
+
|
| 346 |
+
# PDF Download button
|
| 347 |
+
pdf_btn.click(download_chat_pdf, None, pdf_file_output)
|
| 348 |
+
|
| 349 |
+
export_btn.click(export_conv, [export_format, include_meta], [export_output, download_file])
|
| 350 |
+
search_btn.click(search_conv, search_input, search_results)
|
| 351 |
+
refresh_btn.click(
|
| 352 |
+
refresh_analytics,
|
| 353 |
+
None,
|
| 354 |
+
[analytics_display, cache_display, model_dist, mode_dist]
|
| 355 |
+
)
|
| 356 |
+
clear_cache_btn.click(clear_cache_action, None, cache_status)
|
| 357 |
+
|
| 358 |
+
# Update history stats on load
|
| 359 |
+
demo.load(update_history_stats, None, history_stats)
|
| 360 |
+
|
| 361 |
+
return demo
|
| 362 |
+
|
| 363 |
+
if __name__ == "__main__":
|
| 364 |
+
try:
|
| 365 |
+
logger.info("="*60)
|
| 366 |
+
logger.info("Starting Advanced AI Reasoning System Pro...")
|
| 367 |
+
logger.info(f"Session ID: {reasoner.session_id}")
|
| 368 |
+
logger.info("="*60)
|
| 369 |
+
|
| 370 |
+
demo = create_ui()
|
| 371 |
+
demo.launch(
|
| 372 |
+
share=False,
|
| 373 |
+
server_name="0.0.0.0",
|
| 374 |
+
server_port=7860,
|
| 375 |
+
show_error=True,
|
| 376 |
+
show_api=False,
|
| 377 |
+
favicon_path=None
|
| 378 |
+
)
|
| 379 |
+
except Exception as e:
|
| 380 |
+
logger.critical(f"Failed to start application: {e}", exc_info=True)
|
| 381 |
+
raise
|
requirements.txt
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Core Framework
|
| 2 |
+
gradio==5.48.0
|
| 3 |
+
groq==0.32.0
|
| 4 |
+
python-dotenv==1.1.1
|
| 5 |
+
|
| 6 |
+
# Async & Performance
|
| 7 |
+
aiohttp==3.11.11
|
| 8 |
+
aiofiles==24.1.0
|
| 9 |
+
httpx==0.28.1
|
| 10 |
+
orjson==3.11.3
|
| 11 |
+
|
| 12 |
+
# Type Support
|
| 13 |
+
typing-extensions==4.15.0
|
| 14 |
+
|
| 15 |
+
# Data & Imaging
|
| 16 |
+
numpy==2.2.6
|
| 17 |
+
pandas==2.3.3
|
| 18 |
+
pillow==11.3.0
|
| 19 |
+
|
| 20 |
+
# Production Server
|
| 21 |
+
uvicorn[standard]==0.37.0
|
| 22 |
+
gunicorn==23.0.0
|
| 23 |
+
|
| 24 |
+
# Monitoring & Logging
|
| 25 |
+
tqdm==4.67.1
|
| 26 |
+
|
| 27 |
+
# Optional: Rate Limiting & Security
|
| 28 |
+
slowapi==0.1.9
|
| 29 |
+
python-multipart==0.0.19
|
| 30 |
+
tenacity
|
| 31 |
+
tiktoken
|
| 32 |
+
reportlab
|