VisionForge now supports two AI providers for the chatbot functionality:
- Gemini (Google's Generative AI) - Original provider
- Claude (Anthropic's Claude AI) - New addition
Users can switch between providers using a simple environment variable configuration.
- Purpose: Implements Claude AI integration mirroring Gemini's functionality
- Key Features:
- Chat with conversation history
- Workflow modification suggestions
- File upload support (images, PDFs, text)
- Architecture generation from files
- Improvement suggestions
Model Used: claude-3-5-sonnet-20241022 (Latest Claude Sonnet)
Key Differences from Gemini:
- Uses Anthropic SDK (
anthropicpackage) - File handling: Base64 encoding for images/PDFs instead of File API
- Message format: Direct user/assistant roles (no conversion needed)
- Response format:
response.content[0].textinstead ofresponse.text
- Purpose: Provider selection and instantiation
- Methods:
create_service(): Returns appropriate service based onAI_PROVIDERenv varget_provider_name(): Returns human-readable provider name
Error Handling:
- Validates
AI_PROVIDERvalue (must be 'gemini' or 'claude') - Propagates API key errors from individual services
Added:
anthropic>=0.39.0
# AI Provider Configuration
AI_PROVIDER=gemini # or 'claude'
# Gemini AI Configuration
GEMINI_API_KEY=your-key-here
# Claude AI Configuration
ANTHROPIC_API_KEY=your-key-hereSame structure with placeholder values.
Changes:
- Replaced direct
GeminiChatServiceimport withAIServiceFactory - Updated
chat_message()endpoint:- Uses factory to create service
- Handles file uploads differently per provider:
- Gemini: Uploads to Gemini File API → passes
gemini_fileparam - Claude: Reads file content locally → passes
file_contentparam
- Gemini: Uploads to Gemini File API → passes
- Provider-agnostic error messages
- Updated
get_suggestions()endpoint:- Uses factory instead of direct service instantiation
- Generic error messages
Additions:
- Provider selection guide
- Separate setup instructions for Gemini and Claude
- API key acquisition for both providers
- Provider comparison section
- Troubleshooting for provider switching
- Updated security and privacy sections
-
Get API Key:
- Visit: https://aistudio.google.com/app/apikey
- Create free API key
-
Configure
.env:AI_PROVIDER=gemini GEMINI_API_KEY=AIzaSy...
-
Restart server:
python manage.py runserver
-
Get API Key:
- Visit: https://console.anthropic.com/
- Create account and generate API key
-
Configure
.env:AI_PROVIDER=claude ANTHROPIC_API_KEY=sk-ant-...
-
Install package:
pip install anthropic
-
Restart server:
python manage.py runserver
Simply update AI_PROVIDER in .env and restart the server. No code changes needed!
User Request
↓
chat_views.py
↓
AIServiceFactory.create_service()
↓
├─→ GeminiChatService (if AI_PROVIDER=gemini)
└─→ ClaudeChatService (if AI_PROVIDER=claude)
↓
AI Provider API
↓
Response → Frontend
Both services implement the same interface:
class ChatServiceInterface:
def chat(message, history, modification_mode, workflow_state, **kwargs)
def generate_suggestions(workflow_state)
def _format_workflow_context(workflow_state)
def _build_system_prompt(modification_mode, workflow_state)
def _extract_modifications(response_text)Gemini Approach:
- Save uploaded file to temp location
- Upload to Gemini File API using
genai.upload_file() - Pass file object to model
- Clean up temp file
Claude Approach:
- Read file content directly from Django's UploadedFile
- Encode images/PDFs as base64
- Include in message content array
- No temp file needed
Both services use identical regex pattern to extract JSON modifications:
json_pattern = r'```json\s*(\{.*?\})\s*```'This ensures consistent modification format regardless of provider.
{
"message": "Add a Conv2D layer",
"history": [{"role": "user", "content": "..."}],
"modificationMode": true,
"workflowState": {"nodes": [...], "edges": [...]}
}{
"response": "AI response text...",
"modifications": [
{
"action": "add_node",
"details": {...},
"explanation": "..."
}
]
}Frontend compatibility: No changes needed! The response format is identical.
Invalid Provider:
ValueError: Invalid AI_PROVIDER: 'gpt4'. Must be 'gemini' or 'claude'.
Missing API Key (Gemini):
ValueError: GEMINI_API_KEY environment variable is not set
Missing API Key (Claude):
ValueError: ANTHROPIC_API_KEY environment variable is not set
Both services handle:
- API communication failures
- Rate limiting
- Invalid file uploads
- Malformed responses
Errors are logged and returned as user-friendly messages.
- Gemini provider works with chat
- Gemini provider works with file uploads
- Gemini provider works with suggestions
- Claude provider works with chat (requires API key)
- Claude provider works with file uploads (requires API key)
- Claude provider works with suggestions (requires API key)
- Provider switching works
- Error handling for missing API keys
- Error handling for invalid provider
- Documentation updated
| Feature | Gemini | Claude |
|---|---|---|
| Model | gemini-2.0-flash | claude-3-5-sonnet-20241022 |
| Speed | Very Fast | Fast |
| Free Tier | ✅ Yes | ❌ No |
| Image Support | ✅ Yes | ✅ Yes |
| PDF Support | ✅ Yes | ✅ Yes |
| Max Tokens | 8192 | 4096 (configurable) |
| Reasoning | Good | Excellent |
| Code Understanding | Good | Excellent |
| Rate Limit (Free) | 15 RPM | N/A |
- OpenAI GPT-4 support
- Provider-specific features:
- Gemini: Grounding with Google Search
- Claude: Extended context (200k tokens)
- Provider fallback: If one fails, try another
- Cost tracking: Monitor API usage per provider
- A/B testing: Compare response quality
- Provider-specific prompts: Optimize for each model's strengths
# Add new provider:
# 1. Create service class: NewProviderChatService
# 2. Update AIServiceFactory.create_service()
# 3. Add env vars: NEW_PROVIDER_API_KEY
# 4. Update documentation-
API Keys:
- Stored in
.env(git-ignored) - Never exposed to frontend
- Validated at service initialization
- Stored in
-
Data Privacy:
- Workflow data sent to external APIs
- User should review provider privacy policies
- No sensitive data should be in workflows
-
Rate Limiting:
- Implement request throttling in production
- Monitor costs (especially for Claude)
- Consider caching common responses
Cause: Missing or invalid AI_PROVIDER or API key
Solution:
- Check
.envfile hasAI_PROVIDER=geminiorAI_PROVIDER=claude - Verify corresponding API key is set
- Restart Django server
Cause: Server not restarted after .env change
Solution: Always restart Django after changing environment variables
Cause: Unsupported file type or size Solution:
- Check file is image (PNG, JPG, WEBP, GIF) or PDF
- Ensure file is under 10MB
- Review error logs for details
This implementation provides:
- ✅ Flexibility: Easy provider switching via config
- ✅ Consistency: Same API interface for both providers
- ✅ Maintainability: Factory pattern for easy extension
- ✅ Reliability: Comprehensive error handling
- ✅ Documentation: Complete setup and usage guides
No frontend changes required - the implementation is completely transparent to the client.
Users can now choose the AI provider that best fits their needs, budget, and preferences!