This document explains the refactored structure of the Ted API codebase, which has been organized into clean, modular components.
main.py- Clean entry point with app setup, middleware, and route registrationsrc/models.py- Pydantic models for API requests/responses
src/chat.py- Chat service with Ted instance management and streaming logicsrc/transcription.py- Audio transcription service with OpenAI integration and hallucination detection
src/api/__init__.py- API package initializationsrc/api/chat_routes.py- Chat-related endpoints (/chat,/chat/stream,/chatlog)src/api/transcription_routes.py- Audio transcription endpoint (/transcribe_audio)
src/boot.py- Service initializationsrc/config.py- Configuration managementsrc/ted.py- Ted agent implementationsrc/memory.py- Memory managementsrc/llm.py- LLM client wrappersrc/utils.py- Utility functions
- Single monolithic
main.pyfile (506 lines) - Mixed concerns: API endpoints, business logic, models, validation
- Difficult to maintain and test
- Clean separation of concerns
- Modular, testable components
main.pyreduced to ~60 lines of setup code- Easy to extend with new features
All original endpoints are preserved:
| Endpoint | Method | Description |
|---|---|---|
/health |
GET | Health check |
/chat |
POST | Simple chat endpoint |
/chat/stream |
POST | Streaming chat with SSE |
/chatlog |
GET | Get chat history |
/transcribe_audio |
POST | Audio transcription |
- Each module has a single responsibility
- Easy to test individual components
- Clean import structure
- Comprehensive hallucination detection
- Optimized prompts based on research
- Automatic fallback from GPT-4o to Whisper-1
- Duration-based validation
- Router-based organization
- Type-safe Pydantic models
- Consistent error handling
- Clear docstrings throughout
- Logical code organization
- Easy to add new features
The application runs exactly the same as before:
# Development
python main.py
# Production
uvicorn main:app --host 0.0.0.0 --port 8000Individual modules can now be tested in isolation:
# Test transcription logic
from src.transcription import is_transcription_valid
assert is_transcription_valid("Hello world", "prompt", 2.0) == True
# Test chat service
from src.chat import get_ted_for_user
ted = get_ted_for_user("test_user")No new dependencies were added. The refactoring only reorganizes existing functionality.
The modular structure makes it easy to:
- Add new API endpoints
- Implement additional transcription providers
- Add middleware for authentication/rate limiting
- Create comprehensive test suites
- Add monitoring and metrics
- Maintainability: Easy to understand and modify individual components
- Testability: Each module can be tested independently
- Scalability: Easy to add new features without touching existing code
- Team Development: Multiple developers can work on different modules
- Code Reuse: Business logic is now reusable across different contexts