A professional-grade FastAPI application that provides AI-powered PC build recommendations through WebSocket connections. This service supports multiple concurrent users, includes comprehensive error handling, and provides real-time streaming responses.
- Real-time WebSocket Communication: Stream AI responses in real-time
- Concurrent User Support: Handle multiple users simultaneously
- Comprehensive Error Handling: Robust error handling with detailed logging
- Rate Limiting: Built-in rate limiting and connection management
- Search Integration: DuckDuckGo search for current PC part pricing and availability
- Memory Management: Conversation memory per session
- Health Monitoring: Health checks and statistics endpoints
- Production Ready: Docker support with proper logging and monitoring
The application is structured with a modular, professional architecture:
├── main.py # FastAPI application entry point
├── config.py # Configuration management
├── models.py # Pydantic models for validation
├── exceptions.py # Custom exception classes
├── logger.py # Structured logging setup
├── connection_manager.py # WebSocket connection management
├── agent_service.py # AI agent service
├── callback_handler.py # WebSocket callback handler
├── search_tool.py # Search functionality
├── requirements.txt # Python dependencies
├── Dockerfile # Docker container configuration
├── docker-compose.yml # Docker Compose setup
└── .env.example # Environment variables example
- Python 3.11+
- Google AI API key (for Gemini)
-
Clone and setup:
git clone <repository> cd pc-build-assistant python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -r requirements.txt
-
Environment configuration:
cp .env.example .env # Edit .env and add your GOOGLE_API_KEY -
Run the application:
python main.py
-
Build and run:
docker-compose up --build
-
Production deployment:
docker-compose --profile production up -d
GET /- API information and available endpointsGET /health- Health check with system statisticsGET /stats- Detailed application statisticsGET /client- Simple HTML client for testing (development only)
WS /ws- Main WebSocket endpoint for PC build assistance
- Connect: Client connects to
/ws - Authentication: Server responds with session ID and connection status
- Query: Client sends JSON message with user query
- Streaming: Server streams real-time updates during processing
- Response: Server sends final recommendation
Client to Server:
{
"query": "I want to build a gaming PC for $1500",
"session_id": "optional_session_id"
}Server to Client:
{
"type": "log|token|final_output|error|heartbeat|connection_status",
"content": "message content",
"timestamp": "2024-01-01T12:00:00Z",
"metadata": {}
}connection_status: Connection established/status updateslog: Processing status updatestoken: Real-time AI response tokensfinal_output: Complete recommendationerror: Error messagesheartbeat: Keep-alive messages
All configuration is handled through environment variables:
GOOGLE_API_KEY: Google AI API key for Gemini
SEARCH_RATE_LIMIT_DELAY: Delay between searches (default: 1.0s)MAX_SEARCH_RESULTS: Maximum search results per query (default: 5)AGENT_MAX_ITERATIONS: Maximum agent iterations (default: 10)AGENT_TEMPERATURE: AI response creativity (default: 0.7)LLM_MODEL: Gemini model to use (default: gemini-2.0-flash-exp)WEBSOCKET_TIMEOUT: Connection timeout (default: 300s)MAX_CONCURRENT_CONNECTIONS: Maximum concurrent connections (default: 100)LOG_LEVEL: Logging level (default: INFO)
The application includes comprehensive error handling:
- Input Validation: Pydantic models validate all inputs
- Connection Management: Automatic cleanup of stale connections
- Rate Limiting: Prevents overwhelming the search API
- Timeout Handling: Prevents long-running requests
- Graceful Degradation: Continues functioning when external services fail
All logs are structured JSON with:
- Timestamp
- Log level
- Component name
- Session ID (when available)