Add response_time_ms and response_time_sec fields to start and turn API responses

This commit is contained in:
2026-08-31 10:55:53 +05:30
parent 15e8b2dfec
commit 80eaefadb2
2 changed files with 20 additions and 8 deletions
@@ -1,6 +1,7 @@
import asyncio
import base64
import json
import time
from uuid import uuid4
from typing import Optional
@@ -44,6 +45,7 @@ async def start_voice_onboarding(
request: StartVoiceOnboardingRequest,
db: Session = Depends(get_db),
):
start_time = time.time()
try:
onboarding_service = OnboardingService()
voice_service = VoiceService()
@@ -92,6 +94,7 @@ async def start_voice_onboarding(
stage="greeting",
)
elapsed = time.time() - start_time
return StartVoiceOnboardingResponse(
success=True,
session_id=session.session_id,
@@ -102,6 +105,8 @@ async def start_voice_onboarding(
stage=session.stage,
greeting_text=greeting_text,
audio_url=f"/v1/voice-onboarding/audio/{audio_id}",
response_time_ms=round(elapsed * 1000, 2),
response_time_sec=round(elapsed, 3),
)
except Exception as err:
import traceback
@@ -120,6 +125,7 @@ async def voice_turn(
text_fallback: Optional[str] = Form(None),
db: Session = Depends(get_db),
):
start_time = time.time()
onboarding_service = OnboardingService()
voice_service = VoiceService()
ai_service = AIOnboardingService()
@@ -161,17 +167,16 @@ async def voice_turn(
)
# If STILL empty, user spoke nothing or silence was recorded.
# Do NOT substitute fake default text or auto-fill clinical fields!
if not patient_transcript or not patient_transcript.strip():
lang = session.language.lower()
if lang.startswith("ta"):
no_speech_msg = "மன்னிக்கவும், நீங்க பேசுறது கேட்கலை. திரும்பவும் என்ன பிரச்சனைன்னு சொல்ல முடியுமா?"
no_speech_msg = "உங்க குரல் கேட்கலைங்க, தயவுசெய்து மைக்கில பேசுங்க. உங்க உடம்பு பிரச்சனையை சொல்லுங்க."
elif lang.startswith("hi"):
no_speech_msg = "क्षमा करें, मुझे आपकी आवाज़ सुनाई नहीं दी। कृपया अपनी स्वास्थ्य समस्या के बारे में बोलें।"
no_speech_msg = "आपकी आवाज़ सुनाई नहीं दी। कृपया अपने माइक में बोलें और अपनी समस्या बताएं।"
elif lang.startswith("te"):
no_speech_msg = "క్షమించండి, మీ ధ్వని వినిపించలేదు. దయచేసి మీ ఆరోగ్య సమస్య గురించి మాట్లాడండి."
no_speech_msg = "మీ స్వరం వినబడలేదు. దయచేసి మైక్‌లో మాట్లాడి మీ ఆరోగ్య సమస్యను చెప్పండి."
elif lang.startswith("kn"):
no_speech_msg = "ಕ್ಷಮಿಸಿ, ನಿಮ್ಮ ಧ್ವನಿ ಕೇಳಿಸಲಿಲ್ಲ. ದಯವಿಟ್ಟು ನಿಮ್ಮ ಆರೋಗ್ಯದ ತೊಂದರೆ ಬಗ್ಗೆ ಮಾತನಾಡಿ."
no_speech_msg = "ನಿಮ್ಮ ಧ್ವನಿ ಕೇಳಿಸಲಿಲ್ಲ. ದಯವಿಟ್ಟು ಮೈಕ್‌ನಲ್ಲಿ ಮಾತನಾಡಿ ನಿಮ್ಮ ಆರೋಗ್ಯ ಸಮಸ್ಯೆಯನ್ನು ತಿಳಿಸಿ."
else:
no_speech_msg = "I didn't hear anything. Please speak into your microphone and describe your health symptoms."
@@ -180,6 +185,7 @@ async def voice_turn(
language_code=session.language,
)
elapsed = time.time() - start_time
return VoiceTurnResponse(
success=True,
session_id=session_id,
@@ -188,10 +194,10 @@ async def voice_turn(
stage=session.stage,
completed=False,
audio_url=f"/v1/voice-onboarding/audio/{audio_id}",
response_time_ms=round(elapsed * 1000, 2),
response_time_sec=round(elapsed, 3),
)
# 3. Save patient message
onboarding_service.save_message(
db=db,
@@ -272,6 +278,7 @@ async def voice_turn(
voice_service.synthesize_to_file(audio_id, ai_reply_text, session.language)
)
elapsed = time.time() - start_time
return VoiceTurnResponse(
success=True,
session_id=session_id,
@@ -282,6 +289,8 @@ async def voice_turn(
audio_url=f"/v1/voice-onboarding/audio/{audio_id}",
audio_base64=None,
suggested_specialty=sug_spec,
response_time_ms=round(elapsed * 1000, 2),
response_time_sec=round(elapsed, 3),
)
except Exception as err:
import traceback
@@ -350,7 +359,6 @@ async def complete_voice_onboarding(
# Check if there is any conversation to summarize
has_details = completion_service.is_complete(session, state)
messages = (
db.query(OnboardingMessage)
.filter(OnboardingMessage.session_id == session_id)
@@ -19,6 +19,8 @@ class StartVoiceOnboardingResponse(BaseModel):
stage: str
greeting_text: str
audio_url: str
response_time_ms: Optional[float] = None
response_time_sec: Optional[float] = None
class VoiceTurnResponse(BaseModel):
@@ -31,3 +33,5 @@ class VoiceTurnResponse(BaseModel):
audio_url: str
audio_base64: Optional[str] = None
suggested_specialty: Optional[dict] = None
response_time_ms: Optional[float] = None
response_time_sec: Optional[float] = None