A Basic LLM chatbot featuring a React frontend and FastAPI backend, providing a unified interface for multiple LLM providers (Gemini, OpenAI, Anthropic).
- Implemented through the
AIProviderabstract base class - Allows switching between different LLM providers seamlessly
- Each provider implements the same interface for consistency
AIProviderFactorycreates appropriate provider instances- Providers can be switched via environment variables
- Simplifies adding new providers
- Install dependencies:
cd backend
pip install -r requirements.txt- Set up environment variables:
# Create .env file
cp .env.example .env
# Add your API keys
GEMINI_API_KEY=your_key_here
OPENAI_API_KEY=your_key_here
ANTHROPIC_API_KEY=your_key_here- Run the development server:
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000- Install dependencies:
cd frontend
npm install- Run the development server:
npm run devThe frontend will be available at http://localhost:3000
Build and run both services using Docker Compose:
docker-compose upThis will:
- Build and start both frontend and backend services
- Mount code directories for live reloading
- Start the frontend on port 3000 and backend on port 8000
POST /api/chat
Request body:
{
"message": "Your message here"
}Response:
{
"response": "AI generated response",
"timestamp": "2024-02-10T12:00:00Z",
"provider": "gemini"
}To implement a new AI provider:
- Create a new service class inheriting from
AIProvider:
from .base import AIProvider
class NewProvider(AIProvider):
async def generate_response(self, message: str) -> str:
# Implementation here
pass
async def get_model_info(self) -> Dict[str, Any]:
# Implementation here
pass- Add the provider to the factory:
class AIProviderFactory:
_providers = {
"new_provider": NewProvider,
# ... other providers
}# General settings
DEBUG=1
ENVIRONMENT=development
# Provider selection
AI_PROVIDER=gemini # Options: gemini, openai, anthropic
# API Keys
GEMINI_API_KEY=your_key
OPENAI_API_KEY=your_key
ANTHROPIC_API_KEY=your_keyThe service implements comprehensive error handling:
- API-specific errors (rate limits, quotas)
- Network errors
- Invalid input validation
- Service unavailability
Example error response:
{
"detail": "Error message here",
"error_code": "PROVIDER_ERROR",
"timestamp": "2024-02-10T12:00:00Z"
}Built by @abdelilah.khossan@gmail.com