fix(voice): harden noisy streaming turns
This commit is contained in:
@@ -930,6 +930,19 @@ def _voice_topic_prompt(language: str, caller_texts: list[str]) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
def _voice_summary_slot_prompt(language: str, context_summary: str | dict[str, Any] | None) -> str | None:
|
||||
summary = load_context_summary(context_summary)
|
||||
intent = str(summary.get("active_intent") or "").strip()
|
||||
facts = summary.get("confirmed_facts") if isinstance(summary.get("confirmed_facts"), dict) else {}
|
||||
city = str(facts.get("city") or "").strip()
|
||||
branch_hint = str(facts.get("branch_hint") or "").strip()
|
||||
if intent == "schedule" and city and not branch_hint:
|
||||
if language == "kz":
|
||||
return f"{city} qalasy boiynsha qaysy filial nemese mekenjai qyzyqtyratynyn aitnyz."
|
||||
return f"По городу {city} уточните, пожалуйста, филиал или адрес."
|
||||
return None
|
||||
|
||||
|
||||
def _voice_generic_prompt(language: str) -> str:
|
||||
if language == "kz":
|
||||
return "Jyldam komektesu ushin eki ush sozben ne kerek ekenin aitnyz: jumys uaqyty, otinish statusy, tarif nemese operator."
|
||||
@@ -1850,6 +1863,26 @@ def _voice_decision(
|
||||
decision["metadata"] = v2_metadata
|
||||
return decision
|
||||
|
||||
summary_slot_prompt = _voice_summary_slot_prompt(language, context_summary)
|
||||
if not kb_results and summary_slot_prompt:
|
||||
decision = {
|
||||
"language": language,
|
||||
"intent": "clarification",
|
||||
"reply_text": summary_slot_prompt,
|
||||
"confidence": 0.72,
|
||||
"needs_handoff": False,
|
||||
"handoff_reason": None,
|
||||
"case_action": "keep_open",
|
||||
"kb_refs": [],
|
||||
"summary_text": "AI продолжил активный сценарий из conversation summary и уточнил недостающий слот.",
|
||||
"model": "voice_policy_context_summary",
|
||||
"latency_ms": 1,
|
||||
}
|
||||
if v2_metadata:
|
||||
decision["reply_text"] = _voice_compact_reply_text(decision["reply_text"], language=language)
|
||||
decision["metadata"] = {**v2_metadata, "reply_phase": "final"}
|
||||
return decision
|
||||
|
||||
llm_decision = _voice_llm_decision(
|
||||
language=language,
|
||||
customer=customer,
|
||||
|
||||
@@ -5,6 +5,7 @@ import audioop
|
||||
import contextlib
|
||||
import hashlib
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
@@ -186,6 +187,10 @@ class AudioSocketMediaRuntime:
|
||||
self._immediate_ack_min_ms = 700
|
||||
self._v2_ack_post_gap_seconds = 0.10
|
||||
self._partial_poll_interval_seconds = 0.20
|
||||
self._streaming_asr_partial_poll_enabled = (
|
||||
str(os.getenv("AI_VOICE_V2_STREAMING_ASR_PARTIAL_POLL_ENABLED", "0")).strip().lower()
|
||||
in {"1", "true", "yes", "on"}
|
||||
)
|
||||
self._stable_partial_hold_seconds = 0.40
|
||||
self._barge_in_trigger_ms = 220
|
||||
self._streaming_asr_reopen_backoff_seconds = 2.0
|
||||
@@ -197,7 +202,10 @@ class AudioSocketMediaRuntime:
|
||||
|
||||
@staticmethod
|
||||
def _normalize_intent_text(text: str) -> str:
|
||||
return " ".join(str(text or "").strip().lower().split())
|
||||
compact = str(text or "").strip().lower()
|
||||
for char in ",.!?;:…\"'()[]{}":
|
||||
compact = compact.replace(char, " ")
|
||||
return " ".join(compact.split())
|
||||
|
||||
@classmethod
|
||||
def _is_low_signal_transcript(cls, text: str | None) -> bool:
|
||||
@@ -220,9 +228,12 @@ class AudioSocketMediaRuntime:
|
||||
"привет",
|
||||
"слышу",
|
||||
"слышно",
|
||||
"твой",
|
||||
"угу",
|
||||
"хорошо",
|
||||
"ясно",
|
||||
"давай",
|
||||
"поргай что это",
|
||||
}
|
||||
|
||||
def _detect_early_intent(self, text: str) -> str:
|
||||
@@ -270,7 +281,7 @@ class AudioSocketMediaRuntime:
|
||||
return "Сейчас сориентирую."
|
||||
if ack_kind == "clarify":
|
||||
return "Сейчас уточню."
|
||||
return "Сейчас подскажу."
|
||||
return "Секунду."
|
||||
|
||||
@staticmethod
|
||||
def _should_use_emotive_ack(registration: MediaRegistration, language: str | None) -> bool:
|
||||
@@ -323,11 +334,10 @@ class AudioSocketMediaRuntime:
|
||||
"Ага, сейчас сориентирую.",
|
||||
)
|
||||
return (
|
||||
"Угу, сейчас подскажу.",
|
||||
"Мхм, секунду.",
|
||||
"Ага, сейчас подскажу.",
|
||||
"Секунду.",
|
||||
"Хм, секунду.",
|
||||
"Хорошо, сейчас подскажу.",
|
||||
"Хорошо, секунду.",
|
||||
)
|
||||
|
||||
def _select_ack_payload(
|
||||
@@ -623,6 +633,36 @@ class AudioSocketMediaRuntime:
|
||||
intent = self._detect_early_intent(transcript_text)
|
||||
self._update_stable_partial_intent(actor, intent)
|
||||
|
||||
async def _run_streaming_partial_poll(self, actor: MediaActor, utterance_generation: int) -> None:
|
||||
try:
|
||||
if utterance_generation != actor.utterance_generation:
|
||||
return
|
||||
await self._poll_streaming_partial(actor)
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"audiosocket.streaming_asr_partial_failed session_id=%s generation=%s error=%s",
|
||||
actor.registration.voice_session_id,
|
||||
utterance_generation,
|
||||
str(exc)[:500],
|
||||
)
|
||||
|
||||
def _maybe_schedule_streaming_partial_poll(self, actor: MediaActor) -> None:
|
||||
if not self._streaming_asr_partial_poll_enabled:
|
||||
return
|
||||
if actor.closed or not actor.asr_streaming_enabled or not actor.asr_stream_id:
|
||||
return
|
||||
if time.monotonic() < actor.asr_poll_due_monotonic:
|
||||
return
|
||||
task = actor.partial_asr_task
|
||||
if task is not None and not task.done():
|
||||
return
|
||||
actor.partial_asr_task = asyncio.create_task(
|
||||
self._run_streaming_partial_poll(actor, actor.utterance_generation),
|
||||
name=f"streaming-asr-partial-{actor.registration.voice_session_id}",
|
||||
)
|
||||
|
||||
async def _finalize_streaming_transcription(self, actor: MediaActor) -> ASRTranscription:
|
||||
if not actor.asr_streaming_enabled or not actor.asr_stream_id:
|
||||
raise StreamingASRUnavailable("Streaming ASR stream is not active")
|
||||
@@ -756,10 +796,11 @@ class AudioSocketMediaRuntime:
|
||||
language: str | None,
|
||||
metadata: dict[str, Any],
|
||||
ack_source: str,
|
||||
ack_kind: str | None = None,
|
||||
) -> None:
|
||||
if actor.closed or actor.early_ack_started:
|
||||
return
|
||||
ack_kind = self._ack_kind_for_intent(actor.partial_intent or "unknown")
|
||||
ack_kind = ack_kind or self._ack_kind_for_intent(actor.partial_intent or "unknown")
|
||||
ack_text, style_hints, ack_variant = self._select_ack_payload(
|
||||
actor,
|
||||
language=language,
|
||||
@@ -774,7 +815,7 @@ class AudioSocketMediaRuntime:
|
||||
metadata={
|
||||
**metadata,
|
||||
"partial_transcript": actor.partial_transcript,
|
||||
"early_intent": actor.partial_intent,
|
||||
"early_intent": metadata.get("early_intent") or actor.partial_intent,
|
||||
"ack_kind": ack_kind,
|
||||
"ack_variant": ack_variant,
|
||||
"voice_style": "emotive_ack" if style_hints else "neutral_ack",
|
||||
@@ -1127,6 +1168,7 @@ class AudioSocketMediaRuntime:
|
||||
stream_input_active = actor.input_active or actor.barge_in_pending
|
||||
if actor.asr_streaming_enabled and actor.asr_stream_id and stream_input_active:
|
||||
self._queue_streaming_asr_pcm(actor, pcm_frame)
|
||||
self._maybe_schedule_streaming_partial_poll(actor)
|
||||
elif actor.registration.voice_v2_partial_asr:
|
||||
self._maybe_schedule_partial_asr(actor)
|
||||
if vad_result.utterance_pcm:
|
||||
@@ -1183,14 +1225,7 @@ class AudioSocketMediaRuntime:
|
||||
"early_intent": partial_intent,
|
||||
}
|
||||
if self._should_use_voice_v2(actor.registration) and not actor.early_ack_started:
|
||||
if actor.asr_streaming_enabled:
|
||||
with contextlib.suppress(StreamingASRUnavailable):
|
||||
await self._poll_streaming_partial(actor)
|
||||
partial_transcript = str(actor.partial_transcript or "").strip()
|
||||
partial_intent = str(actor.stable_partial_intent or actor.partial_intent or "").strip() or self._detect_early_intent(partial_transcript)
|
||||
base_metadata["partial_transcript"] = partial_transcript
|
||||
base_metadata["early_intent"] = partial_intent
|
||||
else:
|
||||
if not actor.asr_streaming_enabled:
|
||||
partial_task = actor.partial_asr_task
|
||||
if partial_task is not None and not partial_task.done():
|
||||
with contextlib.suppress(asyncio.TimeoutError, asyncio.CancelledError):
|
||||
@@ -1212,6 +1247,7 @@ class AudioSocketMediaRuntime:
|
||||
language=actor.registration.language,
|
||||
metadata=base_metadata,
|
||||
ack_source="immediate_turn_close",
|
||||
ack_kind="unknown",
|
||||
)
|
||||
|
||||
if actor.asr_streaming_enabled and not actor.asr_streaming_failed:
|
||||
|
||||
@@ -33,6 +33,7 @@ _LOW_SIGNAL_TEXTS = {
|
||||
_CITY_ALIASES = {
|
||||
"алма ата": "Алма-Ата",
|
||||
"алмат": "Алмата",
|
||||
"матта": "Алмата",
|
||||
"астан": "Астана",
|
||||
"актау": "Актау",
|
||||
"актоб": "Актобе",
|
||||
|
||||
Reference in New Issue
Block a user