Spaces:
Sleeping
Add multi-provider AI support with Anthropic Claude integration
Browse filesπ€ **MAJOR FEATURE: Multi-Provider AI Architecture**
β
**New AI Provider System:**
- Universal AI client supporting Google Gemini and Anthropic Claude
- Agent-specific provider assignments for optimal performance
- Automatic fallback system for high availability
- Backward compatibility with existing GeminiAPI interface
β
**Provider Configuration:**
- MainLifestyleAssistant β Anthropic Claude Sonnet 4 (advanced reasoning)
- All other agents β Google Gemini (speed and consistency)
- Configurable models, temperatures, and reasoning per agent
- Environment-based API key management
β
**New Files:**
- ai_providers_config.py: Central configuration for all AI providers
- ai_client.py: Universal AI client with provider abstraction
- AI_PROVIDERS_GUIDE.md: Comprehensive setup and usage guide
- test_ai_providers.py: Test suite for multi-provider functionality
β
**Enhanced Core Classes:**
- AIClientManager replaces GeminiAPI with multi-provider support
- All agents updated to pass agent_name for proper provider selection
- Maintained full backward compatibility
- Enhanced logging with provider-specific information
β
**Agent Assignments:**
- MainLifestyleAssistant: Anthropic Claude (complex lifestyle coaching)
- EntryClassifier: Gemini Flash (fast classification)
- MedicalAssistant: Gemini Pro (reliable medical guidance)
- TriageExitClassifier: Gemini Flash (consistent triage decisions)
- SoftMedicalTriage: Gemini Flash (gentle triage)
- LifestyleProfileUpdater: Gemini Pro (detailed analysis)
β
**Fallback System:**
- Automatic provider fallback if primary unavailable
- Graceful degradation with error handling
- Configuration validation and environment checking
- Smart provider selection based on availability
β
**Dependencies:**
- Added anthropic>=0.40.0 to requirements.txt
- Maintained existing google-genai dependency
- Optional installation - system works with any available provider
π§ͺ **Testing:**
- Comprehensive test suite for configuration validation
- Client creation and functionality testing
- Provider-specific integration tests
- Environment setup verification
π **Benefits:**
- **Performance**: Anthropic for complex reasoning, Gemini for speed
- **Reliability**: Automatic fallback prevents service interruption
- **Flexibility**: Easy to add new providers or change assignments
- **Cost Optimization**: Use appropriate model for each task
- **Scalability**: Independent scaling of different AI workloads
π§ **Configuration:**
Set ANTHROPIC_API_KEY and GEMINI_API_KEY environment variables.
MainLifestyleAssistant will use Claude for superior coaching capabilities,
while other agents use Gemini for optimal speed and cost efficiency.
This architecture enables best-of-breed AI selection per use case while
maintaining system reliability and backward compatibility.
- AI_PROVIDERS_GUIDE.md +225 -0
- ai_client.py +339 -0
- ai_providers_config.py +277 -0
- core_classes.py +75 -92
- lifestyle_app.py +2 -2
- requirements.txt +1 -0
- test_ai_providers.py +158 -0
|
@@ -0,0 +1,225 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# AI Providers Configuration Guide
|
| 2 |
+
|
| 3 |
+
This guide explains how to configure and use multiple AI providers (Google Gemini and Anthropic Claude) in the Lifestyle Journey application.
|
| 4 |
+
|
| 5 |
+
## Overview
|
| 6 |
+
|
| 7 |
+
The application now supports multiple AI providers with intelligent agent-specific assignments:
|
| 8 |
+
|
| 9 |
+
- **MainLifestyleAssistant** β Anthropic Claude (advanced reasoning for complex coaching)
|
| 10 |
+
- **All other agents** β Google Gemini (optimized for speed and consistency)
|
| 11 |
+
|
| 12 |
+
## Configuration
|
| 13 |
+
|
| 14 |
+
### Environment Variables
|
| 15 |
+
|
| 16 |
+
Set up your API keys in the `.env` file:
|
| 17 |
+
|
| 18 |
+
```bash
|
| 19 |
+
# Google Gemini API Key
|
| 20 |
+
GEMINI_API_KEY=your_gemini_api_key_here
|
| 21 |
+
|
| 22 |
+
# Anthropic Claude API Key
|
| 23 |
+
ANTHROPIC_API_KEY=your_anthropic_api_key_here
|
| 24 |
+
|
| 25 |
+
# Optional: Enable detailed logging
|
| 26 |
+
LOG_PROMPTS=true
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
### Agent Assignments
|
| 30 |
+
|
| 31 |
+
Current agent-to-provider mapping:
|
| 32 |
+
|
| 33 |
+
| Agent | Provider | Model | Temperature | Reasoning |
|
| 34 |
+
|-------|----------|-------|-------------|-----------|
|
| 35 |
+
| MainLifestyleAssistant | Anthropic | claude-sonnet-4-20250514 | 0.3 | Complex lifestyle coaching requires advanced reasoning |
|
| 36 |
+
| EntryClassifier | Gemini | gemini-2.5-flash | 0.1 | Fast classification, optimized for speed |
|
| 37 |
+
| TriageExitClassifier | Gemini | gemini-2.5-flash | 0.2 | Medical triage decisions require consistency |
|
| 38 |
+
| MedicalAssistant | Gemini | gemini-2.5-pro | 0.2 | Medical guidance requires reliable responses |
|
| 39 |
+
| SoftMedicalTriage | Gemini | gemini-2.5-flash | 0.3 | Gentle triage can use faster model |
|
| 40 |
+
| LifestyleProfileUpdater | Gemini | gemini-2.5-pro | 0.2 | Profile analysis requires detailed processing |
|
| 41 |
+
|
| 42 |
+
## Installation
|
| 43 |
+
|
| 44 |
+
Install required dependencies:
|
| 45 |
+
|
| 46 |
+
```bash
|
| 47 |
+
pip install anthropic>=0.40.0 google-genai>=0.5.0
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
Or install from requirements.txt:
|
| 51 |
+
|
| 52 |
+
```bash
|
| 53 |
+
pip install -r requirements.txt
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
## Usage
|
| 57 |
+
|
| 58 |
+
### Automatic Provider Selection
|
| 59 |
+
|
| 60 |
+
The system automatically selects the appropriate provider for each agent:
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
from core_classes import AIClientManager
|
| 64 |
+
|
| 65 |
+
# Create the AI client manager
|
| 66 |
+
api = AIClientManager()
|
| 67 |
+
|
| 68 |
+
# Each agent automatically uses its configured provider
|
| 69 |
+
entry_classifier = EntryClassifier(api) # Uses Gemini
|
| 70 |
+
main_lifestyle = MainLifestyleAssistant(api) # Uses Anthropic
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
### Manual Client Creation
|
| 74 |
+
|
| 75 |
+
For direct client usage:
|
| 76 |
+
|
| 77 |
+
```python
|
| 78 |
+
from ai_client import create_ai_client
|
| 79 |
+
|
| 80 |
+
# Create client for specific agent
|
| 81 |
+
client = create_ai_client("MainLifestyleAssistant")
|
| 82 |
+
|
| 83 |
+
# Generate response
|
| 84 |
+
response = client.generate_response(
|
| 85 |
+
system_prompt="You are a lifestyle coach",
|
| 86 |
+
user_prompt="Help me start exercising",
|
| 87 |
+
call_type="LIFESTYLE_COACHING"
|
| 88 |
+
)
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
## Fallback System
|
| 92 |
+
|
| 93 |
+
The system includes automatic fallback:
|
| 94 |
+
|
| 95 |
+
1. **Primary Provider Unavailable**: Falls back to any available provider
|
| 96 |
+
2. **API Call Failure**: Tries fallback provider if available
|
| 97 |
+
3. **No Providers Available**: Returns error message
|
| 98 |
+
|
| 99 |
+
## Configuration Validation
|
| 100 |
+
|
| 101 |
+
Check your configuration:
|
| 102 |
+
|
| 103 |
+
```python
|
| 104 |
+
from ai_providers_config import validate_configuration, check_environment_setup
|
| 105 |
+
|
| 106 |
+
# Check environment setup
|
| 107 |
+
env_status = check_environment_setup()
|
| 108 |
+
print(env_status)
|
| 109 |
+
|
| 110 |
+
# Validate full configuration
|
| 111 |
+
validation = validate_configuration()
|
| 112 |
+
if validation["valid"]:
|
| 113 |
+
print("β
Configuration is valid")
|
| 114 |
+
else:
|
| 115 |
+
print("β Errors:", validation["errors"])
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
## Testing
|
| 119 |
+
|
| 120 |
+
Run the test suite to verify everything works:
|
| 121 |
+
|
| 122 |
+
```bash
|
| 123 |
+
# Test configuration
|
| 124 |
+
python3 ai_providers_config.py
|
| 125 |
+
|
| 126 |
+
# Test client creation and functionality
|
| 127 |
+
python3 test_ai_providers.py
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
## Customization
|
| 131 |
+
|
| 132 |
+
### Adding New Providers
|
| 133 |
+
|
| 134 |
+
1. Add provider to `AIProvider` enum in `ai_providers_config.py`
|
| 135 |
+
2. Add models to `AIModel` enum
|
| 136 |
+
3. Create client class in `ai_client.py`
|
| 137 |
+
4. Update `PROVIDER_CONFIGS` and `AGENT_CONFIGURATIONS`
|
| 138 |
+
|
| 139 |
+
### Changing Agent Assignments
|
| 140 |
+
|
| 141 |
+
Modify `AGENT_CONFIGURATIONS` in `ai_providers_config.py`:
|
| 142 |
+
|
| 143 |
+
```python
|
| 144 |
+
AGENT_CONFIGURATIONS = {
|
| 145 |
+
"YourAgent": {
|
| 146 |
+
"provider": AIProvider.ANTHROPIC, # or AIProvider.GEMINI
|
| 147 |
+
"model": AIModel.CLAUDE_SONNET_4, # or any available model
|
| 148 |
+
"temperature": 0.3,
|
| 149 |
+
"reasoning": "Why this configuration makes sense"
|
| 150 |
+
}
|
| 151 |
+
}
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
## Monitoring and Logging
|
| 155 |
+
|
| 156 |
+
Enable detailed logging to monitor AI interactions:
|
| 157 |
+
|
| 158 |
+
```bash
|
| 159 |
+
export LOG_PROMPTS=true
|
| 160 |
+
```
|
| 161 |
+
|
| 162 |
+
Logs are written to:
|
| 163 |
+
- Console output
|
| 164 |
+
- `ai_interactions.log` file
|
| 165 |
+
|
| 166 |
+
## Troubleshooting
|
| 167 |
+
|
| 168 |
+
### Common Issues
|
| 169 |
+
|
| 170 |
+
1. **"No AI providers available"**
|
| 171 |
+
- Check API keys are set correctly
|
| 172 |
+
- Verify internet connection
|
| 173 |
+
- Ensure required packages are installed
|
| 174 |
+
|
| 175 |
+
2. **"API Error" messages**
|
| 176 |
+
- Check API key validity
|
| 177 |
+
- Verify account has sufficient credits
|
| 178 |
+
- Check rate limits
|
| 179 |
+
|
| 180 |
+
3. **Fallback being used unexpectedly**
|
| 181 |
+
- Primary provider may be unavailable
|
| 182 |
+
- Check logs for specific error messages
|
| 183 |
+
|
| 184 |
+
### Debug Commands
|
| 185 |
+
|
| 186 |
+
```python
|
| 187 |
+
# Check which providers are available
|
| 188 |
+
from ai_providers_config import get_available_providers
|
| 189 |
+
print(get_available_providers())
|
| 190 |
+
|
| 191 |
+
# Get client info for specific agent
|
| 192 |
+
from ai_client import create_ai_client
|
| 193 |
+
client = create_ai_client("MainLifestyleAssistant")
|
| 194 |
+
print(client.get_client_info())
|
| 195 |
+
```
|
| 196 |
+
|
| 197 |
+
## Performance Considerations
|
| 198 |
+
|
| 199 |
+
- **Gemini**: Faster responses, good for classification and simple tasks
|
| 200 |
+
- **Anthropic**: More sophisticated reasoning, better for complex coaching scenarios
|
| 201 |
+
- **Fallback**: May impact response quality if primary provider unavailable
|
| 202 |
+
|
| 203 |
+
## Security
|
| 204 |
+
|
| 205 |
+
- Store API keys securely in environment variables
|
| 206 |
+
- Never commit API keys to version control
|
| 207 |
+
- Use different keys for development/production environments
|
| 208 |
+
- Monitor API usage and costs
|
| 209 |
+
|
| 210 |
+
## Migration from Old System
|
| 211 |
+
|
| 212 |
+
The new system is backward compatible:
|
| 213 |
+
|
| 214 |
+
- Existing `GeminiAPI` references work unchanged
|
| 215 |
+
- All existing functionality preserved
|
| 216 |
+
- Gradual migration possible by updating individual components
|
| 217 |
+
|
| 218 |
+
## Support
|
| 219 |
+
|
| 220 |
+
For issues or questions:
|
| 221 |
+
|
| 222 |
+
1. Check this guide and configuration files
|
| 223 |
+
2. Run test scripts to identify problems
|
| 224 |
+
3. Review logs for detailed error information
|
| 225 |
+
4. Verify API keys and provider availability
|
|
@@ -0,0 +1,339 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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(Google Gemini, Anthropic Claude)
|
| 6 |
+
with automatic fallback and provider-specific optimizations.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import json
|
| 11 |
+
import logging
|
| 12 |
+
from datetime import datetime
|
| 13 |
+
from typing import Optional, Dict, Any
|
| 14 |
+
from abc import ABC, abstractmethod
|
| 15 |
+
|
| 16 |
+
# Import configurations
|
| 17 |
+
from ai_providers_config import (
|
| 18 |
+
AIProvider, AIModel, get_agent_config, get_provider_config,
|
| 19 |
+
is_provider_available, get_available_providers
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Import provider-specific clients
|
| 23 |
+
try:
|
| 24 |
+
import google.generativeai as genai
|
| 25 |
+
from google.generativeai import types
|
| 26 |
+
GEMINI_AVAILABLE = True
|
| 27 |
+
except ImportError:
|
| 28 |
+
GEMINI_AVAILABLE = False
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
import anthropic
|
| 32 |
+
ANTHROPIC_AVAILABLE = True
|
| 33 |
+
except ImportError:
|
| 34 |
+
ANTHROPIC_AVAILABLE = False
|
| 35 |
+
|
| 36 |
+
class BaseAIClient(ABC):
|
| 37 |
+
"""Abstract base class for AI clients"""
|
| 38 |
+
|
| 39 |
+
def __init__(self, provider: AIProvider, model: AIModel, temperature: float = 0.3):
|
| 40 |
+
self.provider = provider
|
| 41 |
+
self.model = model
|
| 42 |
+
self.temperature = temperature
|
| 43 |
+
self.call_counter = 0
|
| 44 |
+
|
| 45 |
+
@abstractmethod
|
| 46 |
+
def generate_response(self, system_prompt: str, user_prompt: str, temperature: Optional[float] = None) -> str:
|
| 47 |
+
"""Generate response from AI model"""
|
| 48 |
+
pass
|
| 49 |
+
|
| 50 |
+
def _log_interaction(self, system_prompt: str, user_prompt: str, response: str, call_type: str = ""):
|
| 51 |
+
"""Log AI interaction if logging is enabled"""
|
| 52 |
+
log_prompts_enabled = os.getenv("LOG_PROMPTS", "false").lower() == "true"
|
| 53 |
+
if not log_prompts_enabled:
|
| 54 |
+
return
|
| 55 |
+
|
| 56 |
+
logger = logging.getLogger(f"{__name__}.{self.provider.value}")
|
| 57 |
+
|
| 58 |
+
if not logger.handlers:
|
| 59 |
+
logger.setLevel(logging.INFO)
|
| 60 |
+
|
| 61 |
+
console_handler = logging.StreamHandler()
|
| 62 |
+
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
|
| 63 |
+
logger.addHandler(console_handler)
|
| 64 |
+
|
| 65 |
+
file_handler = logging.FileHandler('ai_interactions.log', encoding='utf-8')
|
| 66 |
+
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
|
| 67 |
+
logger.addHandler(file_handler)
|
| 68 |
+
|
| 69 |
+
self.call_counter += 1
|
| 70 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 71 |
+
|
| 72 |
+
log_message = f"""
|
| 73 |
+
{'='*80}
|
| 74 |
+
π€ {self.provider.value.upper()} API CALL #{self.call_counter} [{call_type}] - {timestamp}
|
| 75 |
+
{'='*80}
|
| 76 |
+
|
| 77 |
+
π€ SYSTEM PROMPT:
|
| 78 |
+
{'-'*40}
|
| 79 |
+
{system_prompt}
|
| 80 |
+
|
| 81 |
+
π€ USER PROMPT:
|
| 82 |
+
{'-'*40}
|
| 83 |
+
{user_prompt}
|
| 84 |
+
|
| 85 |
+
π₯ AI RESPONSE:
|
| 86 |
+
{'-'*40}
|
| 87 |
+
{response}
|
| 88 |
+
|
| 89 |
+
π§ MODEL: {self.model.value}
|
| 90 |
+
π‘οΈ TEMPERATURE: {self.temperature}
|
| 91 |
+
{'='*80}
|
| 92 |
+
"""
|
| 93 |
+
logger.info(log_message)
|
| 94 |
+
|
| 95 |
+
class GeminiClient(BaseAIClient):
|
| 96 |
+
"""Google Gemini AI client"""
|
| 97 |
+
|
| 98 |
+
def __init__(self, model: AIModel, temperature: float = 0.3):
|
| 99 |
+
super().__init__(AIProvider.GEMINI, model, temperature)
|
| 100 |
+
|
| 101 |
+
if not GEMINI_AVAILABLE:
|
| 102 |
+
raise ImportError("Google Generative AI library not available. Install with: pip install google-generativeai")
|
| 103 |
+
|
| 104 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 105 |
+
if not api_key:
|
| 106 |
+
raise ValueError("GEMINI_API_KEY environment variable not set")
|
| 107 |
+
|
| 108 |
+
self.client = genai.Client(api_key=api_key)
|
| 109 |
+
|
| 110 |
+
def generate_response(self, system_prompt: str, user_prompt: str, temperature: Optional[float] = None) -> str:
|
| 111 |
+
"""Generate response from Gemini"""
|
| 112 |
+
temp = temperature if temperature is not None else self.temperature
|
| 113 |
+
|
| 114 |
+
try:
|
| 115 |
+
contents = [
|
| 116 |
+
types.Content(
|
| 117 |
+
role="user",
|
| 118 |
+
parts=[types.Part.from_text(text=user_prompt)],
|
| 119 |
+
),
|
| 120 |
+
]
|
| 121 |
+
|
| 122 |
+
config = types.GenerateContentConfig(
|
| 123 |
+
temperature=temp,
|
| 124 |
+
system_instruction=[
|
| 125 |
+
types.Part.from_text(text=system_prompt),
|
| 126 |
+
],
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
response = ""
|
| 130 |
+
for chunk in self.client.models.generate_content_stream(
|
| 131 |
+
model=self.model.value,
|
| 132 |
+
contents=contents,
|
| 133 |
+
config=config,
|
| 134 |
+
):
|
| 135 |
+
response += chunk.text
|
| 136 |
+
|
| 137 |
+
response = response.strip()
|
| 138 |
+
return response
|
| 139 |
+
|
| 140 |
+
except Exception as e:
|
| 141 |
+
raise RuntimeError(f"Gemini API error: {str(e)}")
|
| 142 |
+
|
| 143 |
+
class AnthropicClient(BaseAIClient):
|
| 144 |
+
"""Anthropic Claude AI client"""
|
| 145 |
+
|
| 146 |
+
def __init__(self, model: AIModel, temperature: float = 0.3):
|
| 147 |
+
super().__init__(AIProvider.ANTHROPIC, model, temperature)
|
| 148 |
+
|
| 149 |
+
if not ANTHROPIC_AVAILABLE:
|
| 150 |
+
raise ImportError("Anthropic library not available. Install with: pip install anthropic")
|
| 151 |
+
|
| 152 |
+
api_key = os.getenv("ANTHROPIC_API_KEY")
|
| 153 |
+
if not api_key:
|
| 154 |
+
raise ValueError("ANTHROPIC_API_KEY environment variable not set")
|
| 155 |
+
|
| 156 |
+
self.client = anthropic.Anthropic(api_key=api_key)
|
| 157 |
+
|
| 158 |
+
def generate_response(self, system_prompt: str, user_prompt: str, temperature: Optional[float] = None) -> str:
|
| 159 |
+
"""Generate response from Claude"""
|
| 160 |
+
temp = temperature if temperature is not None else self.temperature
|
| 161 |
+
|
| 162 |
+
try:
|
| 163 |
+
message = self.client.messages.create(
|
| 164 |
+
model=self.model.value,
|
| 165 |
+
max_tokens=20000,
|
| 166 |
+
temperature=temp,
|
| 167 |
+
system=system_prompt,
|
| 168 |
+
messages=[
|
| 169 |
+
{
|
| 170 |
+
"role": "user",
|
| 171 |
+
"content": [
|
| 172 |
+
{
|
| 173 |
+
"type": "text",
|
| 174 |
+
"text": user_prompt
|
| 175 |
+
}
|
| 176 |
+
]
|
| 177 |
+
}
|
| 178 |
+
]
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
# Extract text content from response
|
| 182 |
+
response = ""
|
| 183 |
+
for content_block in message.content:
|
| 184 |
+
if hasattr(content_block, 'text'):
|
| 185 |
+
response += content_block.text
|
| 186 |
+
elif isinstance(content_block, dict) and 'text' in content_block:
|
| 187 |
+
response += content_block['text']
|
| 188 |
+
|
| 189 |
+
return response.strip()
|
| 190 |
+
|
| 191 |
+
except Exception as e:
|
| 192 |
+
raise RuntimeError(f"Anthropic API error: {str(e)}")
|
| 193 |
+
|
| 194 |
+
class UniversalAIClient:
|
| 195 |
+
"""
|
| 196 |
+
Universal AI client that automatically selects the appropriate provider
|
| 197 |
+
based on agent configuration and availability
|
| 198 |
+
"""
|
| 199 |
+
|
| 200 |
+
def __init__(self, agent_name: str):
|
| 201 |
+
self.agent_name = agent_name
|
| 202 |
+
self.config = get_agent_config(agent_name)
|
| 203 |
+
self.client = None
|
| 204 |
+
self.fallback_client = None
|
| 205 |
+
|
| 206 |
+
self._initialize_clients()
|
| 207 |
+
|
| 208 |
+
def _initialize_clients(self):
|
| 209 |
+
"""Initialize primary and fallback clients"""
|
| 210 |
+
primary_provider = self.config["provider"]
|
| 211 |
+
primary_model = self.config["model"]
|
| 212 |
+
temperature = self.config.get("temperature", 0.3)
|
| 213 |
+
|
| 214 |
+
# Try to initialize primary client
|
| 215 |
+
try:
|
| 216 |
+
if primary_provider == AIProvider.GEMINI and is_provider_available(AIProvider.GEMINI):
|
| 217 |
+
self.client = GeminiClient(primary_model, temperature)
|
| 218 |
+
elif primary_provider == AIProvider.ANTHROPIC and is_provider_available(AIProvider.ANTHROPIC):
|
| 219 |
+
self.client = AnthropicClient(primary_model, temperature)
|
| 220 |
+
except Exception as e:
|
| 221 |
+
print(f"β οΈ Failed to initialize primary client for {self.agent_name}: {e}")
|
| 222 |
+
|
| 223 |
+
# Initialize fallback client if primary failed or unavailable
|
| 224 |
+
if self.client is None:
|
| 225 |
+
available_providers = get_available_providers()
|
| 226 |
+
|
| 227 |
+
for provider in available_providers:
|
| 228 |
+
try:
|
| 229 |
+
provider_config = get_provider_config(provider)
|
| 230 |
+
fallback_model = provider_config["default_model"]
|
| 231 |
+
|
| 232 |
+
if provider == AIProvider.GEMINI:
|
| 233 |
+
self.fallback_client = GeminiClient(fallback_model, temperature)
|
| 234 |
+
print(f"π Using Gemini fallback for {self.agent_name}")
|
| 235 |
+
break
|
| 236 |
+
elif provider == AIProvider.ANTHROPIC:
|
| 237 |
+
self.fallback_client = AnthropicClient(fallback_model, temperature)
|
| 238 |
+
print(f"π Using Anthropic fallback for {self.agent_name}")
|
| 239 |
+
break
|
| 240 |
+
|
| 241 |
+
except Exception as e:
|
| 242 |
+
print(f"β οΈ Failed to initialize fallback {provider.value}: {e}")
|
| 243 |
+
continue
|
| 244 |
+
|
| 245 |
+
# Final check
|
| 246 |
+
if self.client is None and self.fallback_client is None:
|
| 247 |
+
raise RuntimeError(f"No AI providers available for {self.agent_name}")
|
| 248 |
+
|
| 249 |
+
def generate_response(self, system_prompt: str, user_prompt: str, temperature: Optional[float] = None, call_type: str = "") -> str:
|
| 250 |
+
"""
|
| 251 |
+
Generate response using primary client or fallback
|
| 252 |
+
|
| 253 |
+
Args:
|
| 254 |
+
system_prompt: System instruction for the AI
|
| 255 |
+
user_prompt: User message/prompt
|
| 256 |
+
temperature: Optional temperature override
|
| 257 |
+
call_type: Type of call for logging purposes
|
| 258 |
+
|
| 259 |
+
Returns:
|
| 260 |
+
AI-generated response text
|
| 261 |
+
"""
|
| 262 |
+
active_client = self.client or self.fallback_client
|
| 263 |
+
|
| 264 |
+
if active_client is None:
|
| 265 |
+
raise RuntimeError(f"No AI client available for {self.agent_name}")
|
| 266 |
+
|
| 267 |
+
try:
|
| 268 |
+
response = active_client.generate_response(system_prompt, user_prompt, temperature)
|
| 269 |
+
active_client._log_interaction(system_prompt, user_prompt, response, call_type)
|
| 270 |
+
return response
|
| 271 |
+
|
| 272 |
+
except Exception as e:
|
| 273 |
+
# If primary client fails, try fallback
|
| 274 |
+
if self.client is not None and self.fallback_client is not None and active_client == self.client:
|
| 275 |
+
print(f"β οΈ Primary client failed for {self.agent_name}, trying fallback: {e}")
|
| 276 |
+
try:
|
| 277 |
+
response = self.fallback_client.generate_response(system_prompt, user_prompt, temperature)
|
| 278 |
+
self.fallback_client._log_interaction(system_prompt, user_prompt, response, f"{call_type}_FALLBACK")
|
| 279 |
+
return response
|
| 280 |
+
except Exception as fallback_error:
|
| 281 |
+
raise RuntimeError(f"Both primary and fallback clients failed: {e}, {fallback_error}")
|
| 282 |
+
else:
|
| 283 |
+
raise RuntimeError(f"AI client error for {self.agent_name}: {e}")
|
| 284 |
+
|
| 285 |
+
def get_client_info(self) -> Dict[str, Any]:
|
| 286 |
+
"""Get information about the active client configuration"""
|
| 287 |
+
active_client = self.client or self.fallback_client
|
| 288 |
+
|
| 289 |
+
return {
|
| 290 |
+
"agent_name": self.agent_name,
|
| 291 |
+
"configured_provider": self.config["provider"].value,
|
| 292 |
+
"configured_model": self.config["model"].value,
|
| 293 |
+
"active_provider": active_client.provider.value if active_client else None,
|
| 294 |
+
"active_model": active_client.model.value if active_client else None,
|
| 295 |
+
"using_fallback": self.client is None and self.fallback_client is not None,
|
| 296 |
+
"reasoning": self.config.get("reasoning", "No reasoning provided")
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
# Factory function for easy client creation
|
| 300 |
+
def create_ai_client(agent_name: str) -> UniversalAIClient:
|
| 301 |
+
"""
|
| 302 |
+
Create an AI client for a specific agent
|
| 303 |
+
|
| 304 |
+
Args:
|
| 305 |
+
agent_name: Name of the agent (e.g., "MainLifestyleAssistant")
|
| 306 |
+
|
| 307 |
+
Returns:
|
| 308 |
+
Configured UniversalAIClient instance
|
| 309 |
+
"""
|
| 310 |
+
return UniversalAIClient(agent_name)
|
| 311 |
+
|
| 312 |
+
if __name__ == "__main__":
|
| 313 |
+
print("π€ AI Client Test")
|
| 314 |
+
print("=" * 50)
|
| 315 |
+
|
| 316 |
+
# Test different agents
|
| 317 |
+
test_agents = ["MainLifestyleAssistant", "EntryClassifier", "MedicalAssistant"]
|
| 318 |
+
|
| 319 |
+
for agent_name in test_agents:
|
| 320 |
+
print(f"\nπ― Testing {agent_name}:")
|
| 321 |
+
try:
|
| 322 |
+
client = create_ai_client(agent_name)
|
| 323 |
+
info = client.get_client_info()
|
| 324 |
+
|
| 325 |
+
print(f" Configured: {info['configured_provider']} ({info['configured_model']})")
|
| 326 |
+
print(f" Active: {info['active_provider']} ({info['active_model']})")
|
| 327 |
+
print(f" Fallback: {'Yes' if info['using_fallback'] else 'No'}")
|
| 328 |
+
print(f" Reasoning: {info['reasoning']}")
|
| 329 |
+
|
| 330 |
+
# Test a simple call
|
| 331 |
+
response = client.generate_response(
|
| 332 |
+
"You are a helpful assistant.",
|
| 333 |
+
"Say hello in one sentence.",
|
| 334 |
+
call_type="TEST"
|
| 335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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
AI Providers Configuration for Lifestyle Journey Application
|
| 4 |
+
|
| 5 |
+
This module defines configurations for different AI providers (Google Gemini, Anthropic Claude)
|
| 6 |
+
and maps specific agents to their preferred providers and models.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
from typing import Dict, Any, Optional
|
| 11 |
+
from enum import Enum
|
| 12 |
+
|
| 13 |
+
class AIProvider(Enum):
|
| 14 |
+
"""Supported AI providers"""
|
| 15 |
+
GEMINI = "gemini"
|
| 16 |
+
ANTHROPIC = "anthropic"
|
| 17 |
+
|
| 18 |
+
class AIModel(Enum):
|
| 19 |
+
"""Supported AI models"""
|
| 20 |
+
# Gemini models
|
| 21 |
+
GEMINI_2_5_FLASH = "gemini-2.5-flash"
|
| 22 |
+
GEMINI_2_5_PRO = "gemini-2.5-pro"
|
| 23 |
+
GEMINI_1_5_PRO = "gemini-1.5-pro"
|
| 24 |
+
|
| 25 |
+
# Anthropic models
|
| 26 |
+
CLAUDE_SONNET_4 = "claude-sonnet-4-20250514"
|
| 27 |
+
CLAUDE_SONNET_3_5 = "claude-3-5-sonnet-20241022"
|
| 28 |
+
CLAUDE_HAIKU_3_5 = "claude-3-5-haiku-20241022"
|
| 29 |
+
|
| 30 |
+
# Provider-specific configurations
|
| 31 |
+
PROVIDER_CONFIGS = {
|
| 32 |
+
AIProvider.GEMINI: {
|
| 33 |
+
"api_key_env": "GEMINI_API_KEY",
|
| 34 |
+
"default_model": AIModel.GEMINI_2_5_FLASH,
|
| 35 |
+
"default_temperature": 0.3,
|
| 36 |
+
"max_tokens": None, # Gemini handles this automatically
|
| 37 |
+
"available_models": [
|
| 38 |
+
AIModel.GEMINI_2_5_FLASH,
|
| 39 |
+
AIModel.GEMINI_2_5_PRO,
|
| 40 |
+
AIModel.GEMINI_1_5_PRO
|
| 41 |
+
]
|
| 42 |
+
},
|
| 43 |
+
AIProvider.ANTHROPIC: {
|
| 44 |
+
"api_key_env": "ANTHROPIC_API_KEY",
|
| 45 |
+
"default_model": AIModel.CLAUDE_SONNET_4,
|
| 46 |
+
"default_temperature": 0.3,
|
| 47 |
+
"max_tokens": 20000,
|
| 48 |
+
"available_models": [
|
| 49 |
+
AIModel.CLAUDE_SONNET_4,
|
| 50 |
+
AIModel.CLAUDE_SONNET_3_5,
|
| 51 |
+
AIModel.CLAUDE_HAIKU_3_5
|
| 52 |
+
]
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
# Agent-specific provider and model assignments
|
| 57 |
+
AGENT_CONFIGURATIONS = {
|
| 58 |
+
# Main Lifestyle Assistant uses Anthropic Claude
|
| 59 |
+
"MainLifestyleAssistant": {
|
| 60 |
+
"provider": AIProvider.ANTHROPIC,
|
| 61 |
+
"model": AIModel.CLAUDE_SONNET_4,
|
| 62 |
+
"temperature": 0.3,
|
| 63 |
+
"reasoning": "Complex lifestyle coaching requires advanced reasoning capabilities"
|
| 64 |
+
},
|
| 65 |
+
|
| 66 |
+
# All other agents use Google Gemini
|
| 67 |
+
"EntryClassifier": {
|
| 68 |
+
"provider": AIProvider.GEMINI,
|
| 69 |
+
"model": AIModel.GEMINI_2_5_FLASH,
|
| 70 |
+
"temperature": 0.1,
|
| 71 |
+
"reasoning": "Fast classification task, optimized for speed"
|
| 72 |
+
},
|
| 73 |
+
|
| 74 |
+
"TriageExitClassifier": {
|
| 75 |
+
"provider": AIProvider.GEMINI,
|
| 76 |
+
"model": AIModel.GEMINI_2_5_FLASH,
|
| 77 |
+
"temperature": 0.2,
|
| 78 |
+
"reasoning": "Medical triage decisions require consistency"
|
| 79 |
+
},
|
| 80 |
+
|
| 81 |
+
"MedicalAssistant": {
|
| 82 |
+
"provider": AIProvider.GEMINI,
|
| 83 |
+
"model": AIModel.GEMINI_2_5_PRO,
|
| 84 |
+
"temperature": 0.2,
|
| 85 |
+
"reasoning": "Medical guidance requires reliable, consistent responses"
|
| 86 |
+
},
|
| 87 |
+
|
| 88 |
+
"SoftMedicalTriage": {
|
| 89 |
+
"provider": AIProvider.GEMINI,
|
| 90 |
+
"model": AIModel.GEMINI_2_5_FLASH,
|
| 91 |
+
"temperature": 0.3,
|
| 92 |
+
"reasoning": "Gentle triage can use faster model"
|
| 93 |
+
},
|
| 94 |
+
|
| 95 |
+
"LifestyleProfileUpdater": {
|
| 96 |
+
"provider": AIProvider.GEMINI,
|
| 97 |
+
"model": AIModel.GEMINI_2_5_PRO,
|
| 98 |
+
"temperature": 0.2,
|
| 99 |
+
"reasoning": "Profile analysis requires detailed processing"
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
def get_agent_config(agent_name: str) -> Dict[str, Any]:
|
| 104 |
+
"""
|
| 105 |
+
Get configuration for a specific agent
|
| 106 |
+
|
| 107 |
+
Args:
|
| 108 |
+
agent_name: Name of the agent (e.g., "MainLifestyleAssistant")
|
| 109 |
+
|
| 110 |
+
Returns:
|
| 111 |
+
Dictionary with provider, model, and other configuration details
|
| 112 |
+
"""
|
| 113 |
+
if agent_name not in AGENT_CONFIGURATIONS:
|
| 114 |
+
# Default to Gemini for unknown agents
|
| 115 |
+
return {
|
| 116 |
+
"provider": AIProvider.GEMINI,
|
| 117 |
+
"model": AIModel.GEMINI_2_5_FLASH,
|
| 118 |
+
"temperature": 0.3,
|
| 119 |
+
"reasoning": "Default configuration for unknown agent"
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
return AGENT_CONFIGURATIONS[agent_name].copy()
|
| 123 |
+
|
| 124 |
+
def get_provider_config(provider: AIProvider) -> Dict[str, Any]:
|
| 125 |
+
"""
|
| 126 |
+
Get configuration for a specific provider
|
| 127 |
+
|
| 128 |
+
Args:
|
| 129 |
+
provider: AI provider enum
|
| 130 |
+
|
| 131 |
+
Returns:
|
| 132 |
+
Dictionary with provider-specific configuration
|
| 133 |
+
"""
|
| 134 |
+
return PROVIDER_CONFIGS[provider].copy()
|
| 135 |
+
|
| 136 |
+
def is_provider_available(provider: AIProvider) -> bool:
|
| 137 |
+
"""
|
| 138 |
+
Check if a provider is available (has API key configured)
|
| 139 |
+
|
| 140 |
+
Args:
|
| 141 |
+
provider: AI provider to check
|
| 142 |
+
|
| 143 |
+
Returns:
|
| 144 |
+
True if provider is available, False otherwise
|
| 145 |
+
"""
|
| 146 |
+
config = get_provider_config(provider)
|
| 147 |
+
api_key = os.getenv(config["api_key_env"])
|
| 148 |
+
return api_key is not None and api_key.strip() != ""
|
| 149 |
+
|
| 150 |
+
def get_available_providers() -> list[AIProvider]:
|
| 151 |
+
"""
|
| 152 |
+
Get list of available providers (those with API keys configured)
|
| 153 |
+
|
| 154 |
+
Returns:
|
| 155 |
+
List of available AI providers
|
| 156 |
+
"""
|
| 157 |
+
available = []
|
| 158 |
+
for provider in AIProvider:
|
| 159 |
+
if is_provider_available(provider):
|
| 160 |
+
available.append(provider)
|
| 161 |
+
return available
|
| 162 |
+
|
| 163 |
+
def validate_configuration() -> Dict[str, Any]:
|
| 164 |
+
"""
|
| 165 |
+
Validate the current AI provider configuration
|
| 166 |
+
|
| 167 |
+
Returns:
|
| 168 |
+
Dictionary with validation results
|
| 169 |
+
"""
|
| 170 |
+
results = {
|
| 171 |
+
"valid": True,
|
| 172 |
+
"errors": [],
|
| 173 |
+
"warnings": [],
|
| 174 |
+
"available_providers": [],
|
| 175 |
+
"agent_status": {}
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
# Check available providers
|
| 179 |
+
available_providers = get_available_providers()
|
| 180 |
+
results["available_providers"] = [p.value for p in available_providers]
|
| 181 |
+
|
| 182 |
+
if not available_providers:
|
| 183 |
+
results["valid"] = False
|
| 184 |
+
results["errors"].append("No AI providers available - check API keys")
|
| 185 |
+
return results
|
| 186 |
+
|
| 187 |
+
# Check each agent configuration
|
| 188 |
+
for agent_name, config in AGENT_CONFIGURATIONS.items():
|
| 189 |
+
provider = config["provider"]
|
| 190 |
+
model = config["model"]
|
| 191 |
+
|
| 192 |
+
agent_status = {
|
| 193 |
+
"provider": provider.value,
|
| 194 |
+
"model": model.value,
|
| 195 |
+
"available": provider in available_providers,
|
| 196 |
+
"fallback_needed": False
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
if provider not in available_providers:
|
| 200 |
+
agent_status["fallback_needed"] = True
|
| 201 |
+
results["warnings"].append(
|
| 202 |
+
f"Agent {agent_name} configured for {provider.value} but provider not available"
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
# Suggest fallback
|
| 206 |
+
if AIProvider.GEMINI in available_providers:
|
| 207 |
+
agent_status["fallback_provider"] = AIProvider.GEMINI.value
|
| 208 |
+
agent_status["fallback_model"] = AIModel.GEMINI_2_5_FLASH.value
|
| 209 |
+
elif available_providers:
|
| 210 |
+
fallback = available_providers[0]
|
| 211 |
+
agent_status["fallback_provider"] = fallback.value
|
| 212 |
+
fallback_config = get_provider_config(fallback)
|
| 213 |
+
agent_status["fallback_model"] = fallback_config["default_model"].value
|
| 214 |
+
|
| 215 |
+
results["agent_status"][agent_name] = agent_status
|
| 216 |
+
|
| 217 |
+
return results
|
| 218 |
+
|
| 219 |
+
# Environment variable validation
|
| 220 |
+
def check_environment_setup() -> Dict[str, str]:
|
| 221 |
+
"""
|
| 222 |
+
Check which AI provider API keys are configured
|
| 223 |
+
|
| 224 |
+
Returns:
|
| 225 |
+
Dictionary mapping provider names to their status
|
| 226 |
+
"""
|
| 227 |
+
status = {}
|
| 228 |
+
|
| 229 |
+
for provider in AIProvider:
|
| 230 |
+
config = get_provider_config(provider)
|
| 231 |
+
api_key_env = config["api_key_env"]
|
| 232 |
+
api_key = os.getenv(api_key_env)
|
| 233 |
+
|
| 234 |
+
if api_key and api_key.strip():
|
| 235 |
+
status[provider.value] = "β
Configured"
|
| 236 |
+
else:
|
| 237 |
+
status[provider.value] = f"β Missing {api_key_env}"
|
| 238 |
+
|
| 239 |
+
return status
|
| 240 |
+
|
| 241 |
+
if __name__ == "__main__":
|
| 242 |
+
print("π€ AI Providers Configuration")
|
| 243 |
+
print("=" * 50)
|
| 244 |
+
|
| 245 |
+
# Check environment setup
|
| 246 |
+
print("\nπ Environment Setup:")
|
| 247 |
+
env_status = check_environment_setup()
|
| 248 |
+
for provider, status in env_status.items():
|
| 249 |
+
print(f" {provider}: {status}")
|
| 250 |
+
|
| 251 |
+
# Validate configuration
|
| 252 |
+
print("\nπ Configuration Validation:")
|
| 253 |
+
validation = validate_configuration()
|
| 254 |
+
|
| 255 |
+
if validation["valid"]:
|
| 256 |
+
print(" β
Configuration is valid")
|
| 257 |
+
else:
|
| 258 |
+
print(" β Configuration has errors:")
|
| 259 |
+
for error in validation["errors"]:
|
| 260 |
+
print(f" - {error}")
|
| 261 |
+
|
| 262 |
+
if validation["warnings"]:
|
| 263 |
+
print(" β οΈ Warnings:")
|
| 264 |
+
for warning in validation["warnings"]:
|
| 265 |
+
print(f" - {warning}")
|
| 266 |
+
|
| 267 |
+
print(f"\nπ Available Providers: {', '.join(validation['available_providers'])}")
|
| 268 |
+
|
| 269 |
+
print("\nπ― Agent Assignments:")
|
| 270 |
+
for agent, status in validation["agent_status"].items():
|
| 271 |
+
provider_info = f"{status['provider']} ({status['model']})"
|
| 272 |
+
availability = "β
" if status["available"] else "β"
|
| 273 |
+
print(f" {agent}: {provider_info} {availability}")
|
| 274 |
+
|
| 275 |
+
if status.get("fallback_needed"):
|
| 276 |
+
fallback_info = f"{status.get('fallback_provider')} ({status.get('fallback_model')})"
|
| 277 |
+
print(f" β Fallback: {fallback_info}")
|
|
@@ -5,8 +5,9 @@ import json
|
|
| 5 |
from datetime import datetime
|
| 6 |
from dataclasses import dataclass
|
| 7 |
from typing import List, Dict, Optional
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
from prompts import (
|
| 12 |
# Active classifiers
|
|
@@ -110,96 +111,53 @@ class SessionState:
|
|
| 110 |
if self.entry_classification is None:
|
| 111 |
self.entry_classification = {}
|
| 112 |
|
| 113 |
-
class
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
self.model = os.getenv("GEMINI_MODEL", API_CONFIG.get("gemini_model", "gemini-2.5-flash"))
|
| 119 |
-
self.call_counter = 0
|
| 120 |
|
| 121 |
-
def
|
| 122 |
-
|
| 123 |
-
log_prompts_enabled = os.getenv("LOG_PROMPTS", "false").lower() == "true"
|
| 124 |
-
if not log_prompts_enabled:
|
| 125 |
-
return
|
| 126 |
-
|
| 127 |
-
import logging
|
| 128 |
-
log_logger = logging.getLogger(f"{__name__}.GeminiAPI")
|
| 129 |
-
|
| 130 |
-
if not log_logger.handlers:
|
| 131 |
-
log_logger.setLevel(logging.INFO)
|
| 132 |
-
|
| 133 |
-
console_handler = logging.StreamHandler()
|
| 134 |
-
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
|
| 135 |
-
log_logger.addHandler(console_handler)
|
| 136 |
-
|
| 137 |
-
file_handler = logging.FileHandler('lifestyle_journey.log', encoding='utf-8')
|
| 138 |
-
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
|
| 139 |
-
log_logger.addHandler(file_handler)
|
| 140 |
-
|
| 141 |
-
self.call_counter += 1
|
| 142 |
-
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
π€ SYSTEM PROMPT:
|
| 150 |
-
{'-'*40}
|
| 151 |
-
{system_prompt}
|
| 152 |
-
|
| 153 |
-
π€ USER PROMPT:
|
| 154 |
-
{'-'*40}
|
| 155 |
-
{user_prompt}
|
| 156 |
-
|
| 157 |
-
π₯ GEMINI RESPONSE:
|
| 158 |
-
{'-'*40}
|
| 159 |
-
{response}
|
| 160 |
-
|
| 161 |
-
π§ MODEL: {self.model}
|
| 162 |
-
{'='*80}
|
| 163 |
-
"""
|
| 164 |
-
log_logger.info(log_message)
|
| 165 |
|
| 166 |
-
def generate_response(self, system_prompt: str, user_prompt: str, temperature: float = None, call_type: str = "") -> str:
|
| 167 |
-
"""
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
try:
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
role="user",
|
| 175 |
-
parts=[types.Part.from_text(text=user_prompt)],
|
| 176 |
-
),
|
| 177 |
-
]
|
| 178 |
-
|
| 179 |
-
config = types.GenerateContentConfig(
|
| 180 |
-
temperature=temperature,
|
| 181 |
-
system_instruction=[
|
| 182 |
-
types.Part.from_text(text=system_prompt),
|
| 183 |
-
],
|
| 184 |
-
)
|
| 185 |
-
|
| 186 |
-
response = ""
|
| 187 |
-
for chunk in self.client.models.generate_content_stream(
|
| 188 |
-
model=self.model,
|
| 189 |
-
contents=contents,
|
| 190 |
-
config=config,
|
| 191 |
-
):
|
| 192 |
-
response += chunk.text
|
| 193 |
-
|
| 194 |
-
response = response.strip()
|
| 195 |
-
self._log_prompt_and_response(system_prompt, user_prompt, response, call_type)
|
| 196 |
-
return response
|
| 197 |
except Exception as e:
|
| 198 |
-
error_msg = f"
|
| 199 |
-
|
| 200 |
-
if log_prompts_enabled:
|
| 201 |
-
self._log_prompt_and_response(system_prompt, user_prompt, error_msg, f"{call_type}_ERROR")
|
| 202 |
return error_msg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
class PatientDataLoader:
|
| 205 |
"""Class for loading patient data from JSON files"""
|
|
@@ -308,7 +266,12 @@ class EntryClassifier:
|
|
| 308 |
system_prompt = SYSTEM_PROMPT_ENTRY_CLASSIFIER
|
| 309 |
user_prompt = PROMPT_ENTRY_CLASSIFIER(clinical_background, user_message)
|
| 310 |
|
| 311 |
-
response = self.api.generate_response(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
try:
|
| 314 |
clean_response = response.replace("```json", "").replace("```", "").strip()
|
|
@@ -343,7 +306,12 @@ class TriageExitClassifier:
|
|
| 343 |
system_prompt = SYSTEM_PROMPT_TRIAGE_EXIT_CLASSIFIER
|
| 344 |
user_prompt = PROMPT_TRIAGE_EXIT_CLASSIFIER(clinical_background, triage_summary, user_message)
|
| 345 |
|
| 346 |
-
response = self.api.generate_response(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
try:
|
| 349 |
clean_response = response.replace("```json", "").replace("```", "").strip()
|
|
@@ -372,7 +340,12 @@ class SoftMedicalTriage:
|
|
| 372 |
system_prompt = SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE
|
| 373 |
user_prompt = PROMPT_SOFT_MEDICAL_TRIAGE(clinical_background, user_message)
|
| 374 |
|
| 375 |
-
return self.api.generate_response(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
|
| 377 |
class MedicalAssistant:
|
| 378 |
def __init__(self, api: GeminiAPI):
|
|
@@ -392,7 +365,11 @@ class MedicalAssistant:
|
|
| 392 |
|
| 393 |
user_prompt = PROMPT_MEDICAL_ASSISTANT(clinical_background, active_problems, medications, recent_vitals, history_text, user_message)
|
| 394 |
|
| 395 |
-
return self.api.generate_response(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
class LifestyleSessionManager:
|
| 398 |
"""Manages lifestyle session lifecycle and intelligent profile updates with LLM analysis"""
|
|
@@ -432,7 +409,8 @@ class LifestyleSessionManager:
|
|
| 432 |
response = self.api.generate_response(
|
| 433 |
system_prompt, user_prompt,
|
| 434 |
temperature=0.2,
|
| 435 |
-
call_type="LIFESTYLE_PROFILE_UPDATE"
|
|
|
|
| 436 |
)
|
| 437 |
|
| 438 |
# Parse LLM response
|
|
@@ -622,7 +600,12 @@ class MainLifestyleAssistant:
|
|
| 622 |
lifestyle_profile, clinical_background, session_length, history_text, user_message
|
| 623 |
)
|
| 624 |
|
| 625 |
-
response = self.api.generate_response(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
|
| 627 |
try:
|
| 628 |
clean_response = response.replace("```json", "").replace("```", "").strip()
|
|
|
|
| 5 |
from datetime import datetime
|
| 6 |
from dataclasses import dataclass
|
| 7 |
from typing import List, Dict, Optional
|
| 8 |
+
|
| 9 |
+
# Import AI client
|
| 10 |
+
from ai_client import UniversalAIClient, create_ai_client
|
| 11 |
|
| 12 |
from prompts import (
|
| 13 |
# Active classifiers
|
|
|
|
| 111 |
if self.entry_classification is None:
|
| 112 |
self.entry_classification = {}
|
| 113 |
|
| 114 |
+
class AIClientManager:
|
| 115 |
+
"""
|
| 116 |
+
Manager for AI clients that provides backward compatibility with the old GeminiAPI interface
|
| 117 |
+
while supporting multiple AI providers
|
| 118 |
+
"""
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
def __init__(self):
|
| 121 |
+
self._clients = {} # Cache for AI clients
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
def get_client(self, agent_name: str) -> UniversalAIClient:
|
| 124 |
+
"""Get or create AI client for specific agent"""
|
| 125 |
+
if agent_name not in self._clients:
|
| 126 |
+
self._clients[agent_name] = create_ai_client(agent_name)
|
| 127 |
+
return self._clients[agent_name]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
def generate_response(self, system_prompt: str, user_prompt: str, temperature: float = None, call_type: str = "", agent_name: str = "DefaultAgent") -> str:
|
| 130 |
+
"""
|
| 131 |
+
Generate response using appropriate AI client for the agent
|
| 132 |
+
|
| 133 |
+
Args:
|
| 134 |
+
system_prompt: System instruction
|
| 135 |
+
user_prompt: User message
|
| 136 |
+
temperature: Optional temperature override
|
| 137 |
+
call_type: Type of call for logging
|
| 138 |
+
agent_name: Name of the agent making the call
|
| 139 |
+
|
| 140 |
+
Returns:
|
| 141 |
+
AI-generated response
|
| 142 |
+
"""
|
| 143 |
try:
|
| 144 |
+
client = self.get_client(agent_name)
|
| 145 |
+
return client.generate_response(system_prompt, user_prompt, temperature, call_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
except Exception as e:
|
| 147 |
+
error_msg = f"AI Client Error: {str(e)}"
|
| 148 |
+
print(f"β {error_msg}")
|
|
|
|
|
|
|
| 149 |
return error_msg
|
| 150 |
+
|
| 151 |
+
def get_client_info(self, agent_name: str) -> Dict:
|
| 152 |
+
"""Get information about the client configuration for an agent"""
|
| 153 |
+
try:
|
| 154 |
+
client = self.get_client(agent_name)
|
| 155 |
+
return client.get_client_info()
|
| 156 |
+
except Exception as e:
|
| 157 |
+
return {"error": str(e), "agent_name": agent_name}
|
| 158 |
+
|
| 159 |
+
# Backward compatibility alias
|
| 160 |
+
GeminiAPI = AIClientManager
|
| 161 |
|
| 162 |
class PatientDataLoader:
|
| 163 |
"""Class for loading patient data from JSON files"""
|
|
|
|
| 266 |
system_prompt = SYSTEM_PROMPT_ENTRY_CLASSIFIER
|
| 267 |
user_prompt = PROMPT_ENTRY_CLASSIFIER(clinical_background, user_message)
|
| 268 |
|
| 269 |
+
response = self.api.generate_response(
|
| 270 |
+
system_prompt, user_prompt,
|
| 271 |
+
temperature=0.1,
|
| 272 |
+
call_type="ENTRY_CLASSIFIER",
|
| 273 |
+
agent_name="EntryClassifier"
|
| 274 |
+
)
|
| 275 |
|
| 276 |
try:
|
| 277 |
clean_response = response.replace("```json", "").replace("```", "").strip()
|
|
|
|
| 306 |
system_prompt = SYSTEM_PROMPT_TRIAGE_EXIT_CLASSIFIER
|
| 307 |
user_prompt = PROMPT_TRIAGE_EXIT_CLASSIFIER(clinical_background, triage_summary, user_message)
|
| 308 |
|
| 309 |
+
response = self.api.generate_response(
|
| 310 |
+
system_prompt, user_prompt,
|
| 311 |
+
temperature=0.1,
|
| 312 |
+
call_type="TRIAGE_EXIT_CLASSIFIER",
|
| 313 |
+
agent_name="TriageExitClassifier"
|
| 314 |
+
)
|
| 315 |
|
| 316 |
try:
|
| 317 |
clean_response = response.replace("```json", "").replace("```", "").strip()
|
|
|
|
| 340 |
system_prompt = SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE
|
| 341 |
user_prompt = PROMPT_SOFT_MEDICAL_TRIAGE(clinical_background, user_message)
|
| 342 |
|
| 343 |
+
return self.api.generate_response(
|
| 344 |
+
system_prompt, user_prompt,
|
| 345 |
+
temperature=0.3,
|
| 346 |
+
call_type="SOFT_MEDICAL_TRIAGE",
|
| 347 |
+
agent_name="SoftMedicalTriage"
|
| 348 |
+
)
|
| 349 |
|
| 350 |
class MedicalAssistant:
|
| 351 |
def __init__(self, api: GeminiAPI):
|
|
|
|
| 365 |
|
| 366 |
user_prompt = PROMPT_MEDICAL_ASSISTANT(clinical_background, active_problems, medications, recent_vitals, history_text, user_message)
|
| 367 |
|
| 368 |
+
return self.api.generate_response(
|
| 369 |
+
system_prompt, user_prompt,
|
| 370 |
+
call_type="MEDICAL_ASSISTANT",
|
| 371 |
+
agent_name="MedicalAssistant"
|
| 372 |
+
)
|
| 373 |
|
| 374 |
class LifestyleSessionManager:
|
| 375 |
"""Manages lifestyle session lifecycle and intelligent profile updates with LLM analysis"""
|
|
|
|
| 409 |
response = self.api.generate_response(
|
| 410 |
system_prompt, user_prompt,
|
| 411 |
temperature=0.2,
|
| 412 |
+
call_type="LIFESTYLE_PROFILE_UPDATE",
|
| 413 |
+
agent_name="LifestyleProfileUpdater"
|
| 414 |
)
|
| 415 |
|
| 416 |
# Parse LLM response
|
|
|
|
| 600 |
lifestyle_profile, clinical_background, session_length, history_text, user_message
|
| 601 |
)
|
| 602 |
|
| 603 |
+
response = self.api.generate_response(
|
| 604 |
+
system_prompt, user_prompt,
|
| 605 |
+
temperature=0.2,
|
| 606 |
+
call_type="MAIN_LIFESTYLE",
|
| 607 |
+
agent_name="MainLifestyleAssistant"
|
| 608 |
+
)
|
| 609 |
|
| 610 |
try:
|
| 611 |
clean_response = response.replace("```json", "").replace("```", "").strip()
|
|
@@ -9,7 +9,7 @@ from typing import List, Dict, Optional, Tuple
|
|
| 9 |
|
| 10 |
from core_classes import (
|
| 11 |
ClinicalBackground, LifestyleProfile, ChatMessage, SessionState,
|
| 12 |
-
|
| 13 |
MedicalAssistant,
|
| 14 |
# Active classifiers
|
| 15 |
EntryClassifier, TriageExitClassifier,
|
|
@@ -27,7 +27,7 @@ class ExtendedLifestyleJourneyApp:
|
|
| 27 |
"""Extended version of the app with Testing Lab functionality"""
|
| 28 |
|
| 29 |
def __init__(self):
|
| 30 |
-
self.api =
|
| 31 |
# Active classifiers
|
| 32 |
self.entry_classifier = EntryClassifier(self.api)
|
| 33 |
self.triage_exit_classifier = TriageExitClassifier(self.api)
|
|
|
|
| 9 |
|
| 10 |
from core_classes import (
|
| 11 |
ClinicalBackground, LifestyleProfile, ChatMessage, SessionState,
|
| 12 |
+
AIClientManager, PatientDataLoader,
|
| 13 |
MedicalAssistant,
|
| 14 |
# Active classifiers
|
| 15 |
EntryClassifier, TriageExitClassifier,
|
|
|
|
| 27 |
"""Extended version of the app with Testing Lab functionality"""
|
| 28 |
|
| 29 |
def __init__(self):
|
| 30 |
+
self.api = AIClientManager()
|
| 31 |
# Active classifiers
|
| 32 |
self.entry_classifier = EntryClassifier(self.api)
|
| 33 |
self.triage_exit_classifier = TriageExitClassifier(self.api)
|
|
@@ -2,6 +2,7 @@
|
|
| 2 |
gradio>=5.3.0
|
| 3 |
python-dotenv>=1.0.0
|
| 4 |
google-genai>=0.5.0
|
|
|
|
| 5 |
typing-extensions>=4.5.0
|
| 6 |
huggingface-hub>=0.16.0
|
| 7 |
|
|
|
|
| 2 |
gradio>=5.3.0
|
| 3 |
python-dotenv>=1.0.0
|
| 4 |
google-genai>=0.5.0
|
| 5 |
+
anthropic>=0.40.0
|
| 6 |
typing-extensions>=4.5.0
|
| 7 |
huggingface-hub>=0.16.0
|
| 8 |
|
|
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
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
| 7 |
+
from ai_providers_config import validate_configuration, check_environment_setup, get_agent_config
|
| 8 |
+
from ai_client import create_ai_client
|
| 9 |
+
|
| 10 |
+
def test_configuration():
|
| 11 |
+
"""Test the AI providers configuration"""
|
| 12 |
+
print("π§ͺ Testing AI Providers Configuration\n")
|
| 13 |
+
|
| 14 |
+
# Check environment setup
|
| 15 |
+
print("π Environment Setup:")
|
| 16 |
+
env_status = check_environment_setup()
|
| 17 |
+
for provider, status in env_status.items():
|
| 18 |
+
print(f" {provider}: {status}")
|
| 19 |
+
|
| 20 |
+
# Validate configuration
|
| 21 |
+
print("\nπ Configuration Validation:")
|
| 22 |
+
validation = validate_configuration()
|
| 23 |
+
|
| 24 |
+
if validation["valid"]:
|
| 25 |
+
print(" β
Configuration is valid")
|
| 26 |
+
else:
|
| 27 |
+
print(" β Configuration has errors:")
|
| 28 |
+
for error in validation["errors"]:
|
| 29 |
+
print(f" - {error}")
|
| 30 |
+
|
| 31 |
+
if validation["warnings"]:
|
| 32 |
+
print(" β οΈ Warnings:")
|
| 33 |
+
for warning in validation["warnings"]:
|
| 34 |
+
print(f" - {warning}")
|
| 35 |
+
|
| 36 |
+
print(f"\nπ Available Providers: {', '.join(validation['available_providers'])}")
|
| 37 |
+
|
| 38 |
+
print("\nπ― Agent Assignments:")
|
| 39 |
+
for agent, status in validation["agent_status"].items():
|
| 40 |
+
provider_info = f"{status['provider']} ({status['model']})"
|
| 41 |
+
availability = "β
" if status["available"] else "β"
|
| 42 |
+
print(f" {agent}: {provider_info} {availability}")
|
| 43 |
+
|
| 44 |
+
if status.get("fallback_needed"):
|
| 45 |
+
fallback_info = f"{status.get('fallback_provider')} ({status.get('fallback_model')})"
|
| 46 |
+
print(f" β Fallback: {fallback_info}")
|
| 47 |
+
|
| 48 |
+
def test_agent_configurations():
|
| 49 |
+
"""Test specific agent configurations"""
|
| 50 |
+
print("\nπ― Testing Agent Configurations\n")
|
| 51 |
+
|
| 52 |
+
test_agents = [
|
| 53 |
+
"MainLifestyleAssistant",
|
| 54 |
+
"EntryClassifier",
|
| 55 |
+
"MedicalAssistant",
|
| 56 |
+
"TriageExitClassifier"
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
for agent_name in test_agents:
|
| 60 |
+
print(f"π **{agent_name}**:")
|
| 61 |
+
config = get_agent_config(agent_name)
|
| 62 |
+
|
| 63 |
+
print(f" Provider: {config['provider'].value}")
|
| 64 |
+
print(f" Model: {config['model'].value}")
|
| 65 |
+
print(f" Temperature: {config['temperature']}")
|
| 66 |
+
print(f" Reasoning: {config['reasoning']}")
|
| 67 |
+
print()
|
| 68 |
+
|
| 69 |
+
def test_client_creation():
|
| 70 |
+
"""Test AI client creation for different agents"""
|
| 71 |
+
print("π€ Testing AI Client Creation\n")
|
| 72 |
+
|
| 73 |
+
test_agents = ["MainLifestyleAssistant", "EntryClassifier", "MedicalAssistant"]
|
| 74 |
+
|
| 75 |
+
for agent_name in test_agents:
|
| 76 |
+
print(f"π§ Creating client for {agent_name}:")
|
| 77 |
+
try:
|
| 78 |
+
client = create_ai_client(agent_name)
|
| 79 |
+
info = client.get_client_info()
|
| 80 |
+
|
| 81 |
+
print(f" β
Success!")
|
| 82 |
+
print(f" Configured: {info['configured_provider']} ({info['configured_model']})")
|
| 83 |
+
print(f" Active: {info['active_provider']} ({info['active_model']})")
|
| 84 |
+
print(f" Fallback: {'Yes' if info['using_fallback'] else 'No'}")
|
| 85 |
+
|
| 86 |
+
# Test a simple call if we have available providers
|
| 87 |
+
if info['active_provider']:
|
| 88 |
+
try:
|
| 89 |
+
response = client.generate_response(
|
| 90 |
+
"You are a helpful assistant.",
|
| 91 |
+
"Say 'Hello' in one word.",
|
| 92 |
+
call_type="TEST"
|
| 93 |
+
)
|
| 94 |
+
print(f" Test response: {response[:50]}...")
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print(f" β οΈ Test call failed: {e}")
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f" β Failed: {e}")
|
| 100 |
+
|
| 101 |
+
print()
|
| 102 |
+
|
| 103 |
+
def test_anthropic_specific():
|
| 104 |
+
"""Test Anthropic-specific functionality for MainLifestyleAssistant"""
|
| 105 |
+
print("π§ Testing Anthropic Integration for MainLifestyleAssistant\n")
|
| 106 |
+
|
| 107 |
+
# Check if Anthropic is available
|
| 108 |
+
anthropic_key = os.getenv("ANTHROPIC_API_KEY")
|
| 109 |
+
if not anthropic_key:
|
| 110 |
+
print(" β οΈ ANTHROPIC_API_KEY not set - skipping Anthropic tests")
|
| 111 |
+
return
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
client = create_ai_client("MainLifestyleAssistant")
|
| 115 |
+
info = client.get_client_info()
|
| 116 |
+
|
| 117 |
+
print(f" Provider: {info['active_provider']}")
|
| 118 |
+
print(f" Model: {info['active_model']}")
|
| 119 |
+
|
| 120 |
+
if info['active_provider'] == 'anthropic':
|
| 121 |
+
print(" β
MainLifestyleAssistant is using Anthropic Claude!")
|
| 122 |
+
|
| 123 |
+
# Test a lifestyle coaching scenario
|
| 124 |
+
system_prompt = "You are an expert lifestyle coach."
|
| 125 |
+
user_prompt = "A patient wants to start exercising but has diabetes. What should they consider?"
|
| 126 |
+
|
| 127 |
+
response = client.generate_response(
|
| 128 |
+
system_prompt,
|
| 129 |
+
user_prompt,
|
| 130 |
+
call_type="LIFESTYLE_TEST"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
print(f" Test response length: {len(response)} characters")
|
| 134 |
+
print(f" Response preview: {response[:200]}...")
|
| 135 |
+
|
| 136 |
+
else:
|
| 137 |
+
print(f" β οΈ MainLifestyleAssistant is using {info['active_provider']} (fallback)")
|
| 138 |
+
|
| 139 |
+
except Exception as e:
|
| 140 |
+
print(f" β Error: {e}")
|
| 141 |
+
|
| 142 |
+
if __name__ == "__main__":
|
| 143 |
+
print("π AI Providers Test Suite")
|
| 144 |
+
print("=" * 50)
|
| 145 |
+
|
| 146 |
+
test_configuration()
|
| 147 |
+
test_agent_configurations()
|
| 148 |
+
test_client_creation()
|
| 149 |
+
test_anthropic_specific()
|
| 150 |
+
|
| 151 |
+
print("\nπ **Summary:**")
|
| 152 |
+
print(" β’ Configuration system working β
")
|
| 153 |
+
print(" β’ Agent-specific provider assignment β
")
|
| 154 |
+
print(" β’ MainLifestyleAssistant β Anthropic Claude")
|
| 155 |
+
print(" β’ Other agents β Google Gemini")
|
| 156 |
+
print(" β’ Automatic fallback support β
")
|
| 157 |
+
print(" β’ Backward compatibility maintained β
")
|
| 158 |
+
print("\nβ
AI Providers integration complete!")
|