Skip to content

Latest commit

 

History

History
121 lines (90 loc) · 3.6 KB

File metadata and controls

121 lines (90 loc) · 3.6 KB

Ted API Refactored Structure

This document explains the refactored structure of the Ted API codebase, which has been organized into clean, modular components.

📁 File Structure

Core Application

  • main.py - Clean entry point with app setup, middleware, and route registration
  • src/models.py - Pydantic models for API requests/responses

Business Logic

  • src/chat.py - Chat service with Ted instance management and streaming logic
  • src/transcription.py - Audio transcription service with OpenAI integration and hallucination detection

API Routes

  • src/api/__init__.py - API package initialization
  • src/api/chat_routes.py - Chat-related endpoints (/chat, /chat/stream, /chatlog)
  • src/api/transcription_routes.py - Audio transcription endpoint (/transcribe_audio)

Existing Modules (unchanged)

  • src/boot.py - Service initialization
  • src/config.py - Configuration management
  • src/ted.py - Ted agent implementation
  • src/memory.py - Memory management
  • src/llm.py - LLM client wrapper
  • src/utils.py - Utility functions

🔄 What Changed

Before Refactoring

  • Single monolithic main.py file (506 lines)
  • Mixed concerns: API endpoints, business logic, models, validation
  • Difficult to maintain and test

After Refactoring

  • Clean separation of concerns
  • Modular, testable components
  • main.py reduced to ~60 lines of setup code
  • Easy to extend with new features

📍 API Endpoints

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

🎯 Key Improvements

1. Modular Architecture

  • Each module has a single responsibility
  • Easy to test individual components
  • Clean import structure

2. Enhanced Transcription

  • Comprehensive hallucination detection
  • Optimized prompts based on research
  • Automatic fallback from GPT-4o to Whisper-1
  • Duration-based validation

3. Clean API Structure

  • Router-based organization
  • Type-safe Pydantic models
  • Consistent error handling

4. Maintainability

  • Clear docstrings throughout
  • Logical code organization
  • Easy to add new features

🚀 Running the Application

The application runs exactly the same as before:

# Development
python main.py

# Production
uvicorn main:app --host 0.0.0.0 --port 8000

🧪 Testing

Individual 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")

📦 Dependencies

No new dependencies were added. The refactoring only reorganizes existing functionality.

🔧 Future Enhancements

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

💡 Benefits

  1. Maintainability: Easy to understand and modify individual components
  2. Testability: Each module can be tested independently
  3. Scalability: Easy to add new features without touching existing code
  4. Team Development: Multiple developers can work on different modules
  5. Code Reuse: Business logic is now reusable across different contexts