The Speech Similarity API processes audio data, transcribes it using OpenAI Whisper, and calculates vector similarity between transcribed text and reference documents using OpenAI embeddings.
Base URL: http://localhost:8000/api/v1
Authentication: None (configure as needed)
Content-Type: application/json
Calculate similarity between stored session transcription and reference text.
Description: This endpoint performs the similarity calculation pipeline:
- Retrieves session data from database (with pre-transcribed speech text)
- Generates embeddings for both stored transcribed text and reference text
- Calculates cosine similarity between embeddings
Parameters:
session_id(path, integer, required): ID of the session containing audio data
Request Body:
{
"reference_text": "This is a sample reference document that will be used for similarity comparison with the transcribed audio content."
}Request Schema:
reference_text(string, required): Reference document text for comparison- Min length: 1 character
- Max length: 50,000 characters
- Cannot be empty or just whitespace
Response (200 OK):
{
"session_id": 123,
"session_data": {
"id": 123,
"speech": "Sample transcribed speech text",
"questions": {"q1": "What is the main topic?"},
"created_by": "user123",
"generated_by": "speech-api-v1",
"created_at": "2023-10-31T10:30:00Z",
"audio": "UklGRnoGAABXQVZFZm10..."
},
"transcribed_text": "This is the transcribed text from the audio file using OpenAI Whisper",
"similarity_score": 0.85,
"processing_time_ms": 2500,
"timestamp": "2023-10-31T10:35:00Z"
}Response Schema:
session_id: Session identifiersession_data: Complete session record from databasetranscribed_text: Text transcribed from audio using Whispersimilarity_score: Cosine similarity score (0.0-1.0)processing_time_ms: Total processing time in millisecondstimestamp: Response generation timestamp
Error Responses:
404: Session not found422: Invalid audio data or request500: Internal server error502: External service error (OpenAI API issues)503: Service unavailable
Update session by transcribing new audio data.
Description: Replace the session's speech content by transcribing new base64-encoded audio using OpenAI Whisper. The transcribed text is stored in the session, replacing any previous speech content.
Parameters:
session_id(path, integer, required): ID of the session to update
Request Body:
{
"audio": "UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmGgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
}Request Schema:
audio(string, required): Base64 encoded audio file- Supported formats: WAV, MP3, FLAC, M4A, OGG, WebM
- Must be valid base64 data
- Minimum size: 44 bytes (WAV header minimum)
- Maximum size: 25MB (configurable)
Response (200 OK):
{
"session_id": 123,
"message": "Audio transcribed and session updated successfully",
"updated_at": "2023-10-31T10:35:00Z"
}Error Responses:
404: Session not found422: Invalid audio data500: Internal server error503: Database service unavailable
Comprehensive health check of the service and its dependencies.
Description: Checks the health of all critical dependencies including database connection (Supabase) and external service availability.
Response (200 OK):
{
"status": "healthy",
"timestamp": "2023-10-31T10:35:00Z",
"version": "1.0.0",
"dependencies": {
"supabase": "connected",
"openai": "available"
}
}Response Schema:
status: Service health status ("healthy" or "unhealthy")timestamp: Health check timestampversion: API versiondependencies: Status of external dependencies
Error Responses:
503: Service is unhealthy
Readiness check for load balancers and orchestration systems.
Description: Determines if the service can handle requests. Used by load balancers to decide if the service should receive traffic.
Response (200 OK):
{
"ready": true,
"timestamp": "2023-10-31T10:35:00Z",
"checks": {
"database": "ready",
"external_services": "ready"
}
}Error Responses:
503: Service is not ready
Retrieve comprehensive performance metrics and statistics.
Parameters:
reset(query, boolean, optional): Reset metrics after retrieval (default: false)
Response (200 OK):
{
"timestamp": "2023-10-31T10:35:00Z",
"uptime_seconds": 3600.5,
"requests": {
"total": 150,
"by_method": {"GET": 100, "POST": 50},
"by_status": {"200": 140, "404": 8, "500": 2},
"by_endpoint": {"/api/v1/similarity/123": 45, "/api/v1/health": 100}
},
"response_times": {
"total_time": 125.5,
"count": 150,
"average": 0.837,
"min": 0.001,
"max": 5.234
},
"errors": {
"total": 10,
"by_type": {"ValidationError": 8, "DatabaseError": 2}
},
"system": {
"cpu": {"usage_percent": 15.2, "count": 4},
"memory": {
"total_bytes": 8589934592,
"available_bytes": 4294967296,
"used_bytes": 4294967296,
"usage_percent": 50.0
},
"disk": {
"total_bytes": 1000000000000,
"free_bytes": 500000000000,
"used_bytes": 500000000000,
"usage_percent": 50.0
},
"network": {
"bytes_sent": 1048576,
"bytes_recv": 2097152,
"packets_sent": 1000,
"packets_recv": 1500
}
}
}Retrieve comprehensive system health information including resource usage.
Response (200 OK):
{
"timestamp": "2023-10-31T10:35:00Z",
"status": "healthy",
"version": "1.0.0",
"uptime_seconds": 3600.5,
"dependencies": {
"supabase": {
"status": "connected",
"response_time": 0.045,
"last_check": "2023-10-31T10:35:00Z"
},
"openai": {
"status": "available",
"response_time": 0.123,
"last_check": "2023-10-31T10:35:00Z"
}
},
"system_resources": {
"cpu": {"usage_percent": 15.2, "count": 4},
"memory": {"usage_percent": 50.0, "available_bytes": 4294967296},
"disk": {"usage_percent": 50.0, "free_bytes": 500000000000},
"network": {
"bytes_sent": 1048576,
"bytes_recv": 2097152
}
},
"performance_summary": {
"total_requests": 150,
"error_rate": 6.67,
"average_response_time": 0.837,
"requests_per_minute": 2.5
}
}Reset all performance metrics to zero.
Response (200 OK):
{
"message": "Performance metrics reset successfully",
"timestamp": "2023-10-31T10:35:00Z",
"previous_stats": {
"total_requests": 150,
"total_errors": 10
}
}All endpoints return errors in a consistent format:
{
"error_code": "SESSION_NOT_FOUND",
"message": "Session with ID 123 was not found",
"details": {"session_id": 123, "table": "sessions"},
"timestamp": "2023-10-31T10:35:00Z",
"request_id": "req_abc123def456"
}Error Schema:
error_code: Machine-readable error codemessage: Human-readable error messagedetails: Additional error details (optional)timestamp: Error occurrence timestamprequest_id: Unique request identifier for tracking
SESSION_NOT_FOUND: Requested session does not existSESSION_VALIDATION_ERROR: Session data validation failedAUDIO_VALIDATION_ERROR: Audio data format or encoding invalidAUDIO_PROCESSING_ERROR: Audio processing failedTRANSCRIPTION_ERROR: Speech-to-text transcription failedEMBEDDING_ERROR: Text embedding generation failedSIMILARITY_ERROR: Similarity calculation failedDATABASE_ERROR: Database operation failedDATABASE_CONNECTION_ERROR: Database connection failedEXTERNAL_SERVICE_ERROR: External API (OpenAI) error
-
Max audio size: 25MB (configurable via
MAX_AUDIO_SIZE_MB) -
Reference text limit: 50,000 characters
-
Supported audio formats: WAV, MP3, FLAC, M4A, OGG, WebM
-
Audio encoding: Base64
Start the server:
uvicorn app.main:app --reloadAPI Documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI JSON: http://localhost:8000/openapi.json
Environment Setup:
Copy .env.example to .env and configure:
SUPABASE_POOLER_CONNECTION_STRING- PostgreSQL connection string for Supabase session poolerOPENAI_API_KEY- OpenAI API key for Whisper and embeddingsOPENAI_ORGANIZATION- Optional: only needed if you have an OpenAI organization- Other optional settings
Sessions stored in the database contain:
{
"id": 123,
"speech": "Optional transcribed speech text",
"questions": {"q1": "What is the main topic?"},
"created_by": "user123",
"generated_by": "speech-api-v1",
"created_at": "2023-10-31T10:30:00Z",
"audio": "base64_encoded_audio_data"
}The audio field contains the base64-encoded audio data that gets processed by the similarity endpoint.