Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlesne committed Dec 5, 2024
2 parents 8c9e70d + f6c6148 commit 41c60f2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
8 changes: 2 additions & 6 deletions app/helpers/call_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def on_new_call(
phone_number: str,
wss_url: str,
) -> bool:
logger.debug("Incoming call handler caller ID: %s", phone_number)
logger.debug("Incoming call handler")

streaming_options = MediaStreamingOptions(
audio_channel_type=MediaStreamingAudioChannelType.UNMIXED,
Expand All @@ -72,11 +72,7 @@ async def on_new_call(
incoming_call_context=incoming_context,
media_streaming=streaming_options,
)
logger.info(
"Answered call with %s (%s)",
phone_number,
answer_call_result.call_connection_id,
)
logger.info("Answered call (%s)", answer_call_result.call_connection_id)
return True

except ClientAuthenticationError:
Expand Down
30 changes: 20 additions & 10 deletions app/helpers/call_llm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
from collections.abc import Awaitable, Callable
from datetime import UTC, datetime, timedelta
from functools import wraps

import aiojobs
Expand Down Expand Up @@ -574,18 +575,18 @@ async def _in_audio( # noqa: PLR0913
timeout_callback: Callable[[], Awaitable[None]],
) -> None:
clear_tts_task: asyncio.Task | None = None
flush_task: asyncio.Task | None = None
silence_task: asyncio.Task | None = None
vad = Vad(
# Aggressiveness mode (0, 1, 2, or 3)
# Sets the VAD operating mode. A more aggressive (higher mode) VAD is more restrictive in reporting speech. Put in other words the probability of being speech when the VAD returns 1 is increased with increasing mode. As a consequence also the missed detection rate goes up.
mode=3,
)

async def _flush_callback() -> None:
async def _silence_callback() -> None:
"""
Flush the audio buffer if no audio is detected for a while.
Flush the audio buffer if no audio is detected for a while and trigger the timeout if required.
"""
# Wait for the timeout
# Wait and flush the audio buffer
nonlocal clear_tts_task
timeout_ms = await vad_silence_timeout_ms()
await asyncio.sleep(timeout_ms / 1000)
Expand All @@ -595,10 +596,19 @@ async def _flush_callback() -> None:
logger.debug("Flushing audio buffer after %i ms", timeout_ms)
await response_callback()

# Wait for the timeout, if any
# Wait for silence and trigger timeout
timeout_sec = await phone_silence_timeout_sec()
while call.in_progress:
timeout_start = datetime.now(UTC)
await asyncio.sleep(timeout_sec)
if (
call.messages[-1].created_at + timedelta(seconds=timeout_sec)
> timeout_start
):
logger.debug(
"Message sent in the meantime, canceling this silence timeout"
)
continue
logger.info("Silence triggered after %i sec", timeout_sec)
await timeout_callback()

Expand Down Expand Up @@ -650,17 +660,17 @@ async def _clear_tts_callback() -> None:
):
in_empty = True
# Start timeout if not already started
if not flush_task:
flush_task = asyncio.create_task(_flush_callback())
if not silence_task:
silence_task = asyncio.create_task(_silence_callback())

if in_empty:
# Continue to the next audio packet
continue

# Voice detected, cancel the timeout if any
if flush_task:
flush_task.cancel()
flush_task = None
if silence_task:
silence_task.cancel()
silence_task = None

# Start the TTS clear task
if not clear_tts_task:
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/config_models/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def _messages(
role="system",
),
]
self.logger.debug("Messages: %s", messages)
# self.logger.debug("Messages: %s", messages)
return messages

@cached_property
Expand Down

0 comments on commit 41c60f2

Please sign in to comment.