Update call_transfer.py
Browse files- call_transfer.py +3 -56
call_transfer.py
CHANGED
|
@@ -8,11 +8,9 @@ import asyncio
|
|
| 8 |
import os
|
| 9 |
import sys
|
| 10 |
import time
|
| 11 |
-
import json
|
| 12 |
-
|
| 13 |
-
from call_connection_manager import CallConfigManager, SessionManager
|
| 14 |
from loguru import logger
|
| 15 |
|
|
|
|
| 16 |
from pipecat.adapters.schemas.function_schema import FunctionSchema
|
| 17 |
from pipecat.adapters.schemas.tools_schema import ToolsSchema
|
| 18 |
from pipecat.audio.vad.silero import SileroVADAnalyzer
|
|
@@ -42,52 +40,6 @@ logger.add(sys.stderr, level="DEBUG")
|
|
| 42 |
daily_api_key = os.environ.get("HF_DAILY_API_KEY", "")
|
| 43 |
daily_api_url = os.environ.get("DAILY_API_URL", "https://api.daily.co/v1")
|
| 44 |
|
| 45 |
-
class SilenceDetectorProcessor(FrameProcessor):
|
| 46 |
-
"""Detects prolonged silence and triggers a TTS prompt after 10 seconds."""
|
| 47 |
-
def __init__(self, session_manager, call_config_manager, tts_service, task):
|
| 48 |
-
super().__init__()
|
| 49 |
-
self.session_manager = session_manager
|
| 50 |
-
self.call_config_manager = call_config_manager
|
| 51 |
-
self.tts_service = tts_service
|
| 52 |
-
self.task = task
|
| 53 |
-
self.last_speech_time = time.time()
|
| 54 |
-
self.silence_prompt_count = 0
|
| 55 |
-
self.max_prompts = 3
|
| 56 |
-
self.silence_threshold = 10 # 10 seconds
|
| 57 |
-
|
| 58 |
-
async def process_frame(self, frame: Frame, direction: FrameDirection):
|
| 59 |
-
await super().process_frame(frame, direction)
|
| 60 |
-
|
| 61 |
-
if isinstance(frame, UserStartedSpeakingFrame):
|
| 62 |
-
self.last_speech_time = time.time()
|
| 63 |
-
self.session_manager.call_flow_state.reset_silence_prompts()
|
| 64 |
-
self.silence_prompt_count = 0
|
| 65 |
-
elif isinstance(frame, UserStoppedSpeakingFrame):
|
| 66 |
-
self.last_speech_time = time.time()
|
| 67 |
-
|
| 68 |
-
# Check for prolonged silence
|
| 69 |
-
if time.time() - self.last_speech_time >= self.silence_threshold:
|
| 70 |
-
if self.silence_prompt_count < self.max_prompts:
|
| 71 |
-
# Increment prompt count and log silence event
|
| 72 |
-
self.silence_prompt_count += 1
|
| 73 |
-
self.session_manager.call_flow_state.increment_silence_prompts()
|
| 74 |
-
logger.info(f"Silence detected for {self.silence_threshold}s, sending prompt #{self.silence_prompt_count}")
|
| 75 |
-
|
| 76 |
-
# Send TTS prompt
|
| 77 |
-
prompt = "Hello, are you still there? How can I assist you?"
|
| 78 |
-
message = self.call_config_manager.create_system_message(prompt)
|
| 79 |
-
await self.task.queue_frames([LLMMessagesFrame([message])])
|
| 80 |
-
self.last_speech_time = time.time() # Reset silence timer
|
| 81 |
-
else:
|
| 82 |
-
# Terminate call after max prompts
|
| 83 |
-
logger.info("Max silence prompts reached, terminating call")
|
| 84 |
-
farewell = "Thank you for calling. Goodbye."
|
| 85 |
-
message = self.call_config_manager.create_system_message(farewell)
|
| 86 |
-
await self.task.queue_frames([LLMMessagesFrame([message])])
|
| 87 |
-
await self.task.queue_frame(EndTaskFrame(), FrameDirection.UPSTREAM)
|
| 88 |
-
|
| 89 |
-
await self.push_frame(frame, direction)
|
| 90 |
-
|
| 91 |
class TranscriptionModifierProcessor(FrameProcessor):
|
| 92 |
"""Processor that modifies transcription frames before they reach the context aggregator."""
|
| 93 |
def __init__(self, operator_session_id_ref):
|
|
@@ -126,7 +78,6 @@ async def main(room_url: str, token: str, body: dict):
|
|
| 126 |
dialed_number = caller_info["dialed_number"]
|
| 127 |
customer_name = call_config_manager.get_customer_name(caller_number) if caller_number else None
|
| 128 |
operator_dialout_settings = call_config_manager.get_dialout_settings_for_caller(caller_number)
|
| 129 |
-
call_start_time = time.time() # Track call start time
|
| 130 |
|
| 131 |
logger.info(f"Caller number: {caller_number}")
|
| 132 |
logger.info(f"Dialed number: {dialed_number}")
|
|
@@ -200,18 +151,17 @@ async def main(room_url: str, token: str, body: dict):
|
|
| 200 |
|
| 201 |
messages = [call_config_manager.create_system_message(system_instruction)]
|
| 202 |
llm = OpenAILLMService(api_key=os.environ.get("HF_OPENAI_API_KEY"))
|
| 203 |
-
llm.register_function("terminate_call", lambda params: terminate_call(task, params
|
| 204 |
llm.register_function("dial_operator", dial_operator)
|
| 205 |
context = OpenAILLMContext(messages, tools)
|
| 206 |
context_aggregator = llm.create_context_aggregator(context)
|
| 207 |
|
| 208 |
# ------------ FUNCTION DEFINITIONS ------------
|
| 209 |
-
async def terminate_call(task: PipelineTask, params: FunctionCallParams
|
| 210 |
content = "The user wants to end the conversation, thank them for chatting."
|
| 211 |
message = call_config_manager.create_system_message(content)
|
| 212 |
messages.append(message)
|
| 213 |
await task.queue_frames([LLMMessagesFrame(messages)])
|
| 214 |
-
await call_config_manager.log_call_summary(call_start_time, session_manager, caller_number, dialed_number, customer_name)
|
| 215 |
await params.llm.queue_frame(EndTaskFrame(), FrameDirection.UPSTREAM)
|
| 216 |
|
| 217 |
async def dial_operator(params: FunctionCallParams):
|
|
@@ -257,7 +207,6 @@ async def main(room_url: str, token: str, body: dict):
|
|
| 257 |
# ------------ PIPELINE SETUP ------------
|
| 258 |
summary_finished = SummaryFinished(session_manager.call_flow_state)
|
| 259 |
transcription_modifier = TranscriptionModifierProcessor(session_manager.get_session_id_ref("operator"))
|
| 260 |
-
silence_detector = SilenceDetectorProcessor(session_manager, call_config_manager, tts, task)
|
| 261 |
|
| 262 |
async def should_speak(self) -> bool:
|
| 263 |
return (not session_manager.call_flow_state.operator_connected or
|
|
@@ -265,7 +214,6 @@ async def main(room_url: str, token: str, body: dict):
|
|
| 265 |
|
| 266 |
pipeline = Pipeline([
|
| 267 |
transport.input(),
|
| 268 |
-
silence_detector, # Add silence detection
|
| 269 |
transcription_modifier,
|
| 270 |
context_aggregator.user(),
|
| 271 |
FunctionFilter(should_speak),
|
|
@@ -323,7 +271,6 @@ async def main(room_url: str, token: str, body: dict):
|
|
| 323 |
logger.debug(f"Participant left: {participant}, reason: {reason}")
|
| 324 |
if not (session_manager.get_session_id("operator") and
|
| 325 |
participant["id"] == session_manager.get_session_id("operator")):
|
| 326 |
-
await call_config_manager.log_call_summary(call_start_time, session_manager, caller_number, dialed_number, customer_name)
|
| 327 |
await task.cancel()
|
| 328 |
return
|
| 329 |
logger.debug("Operator left the call")
|
|
|
|
| 8 |
import os
|
| 9 |
import sys
|
| 10 |
import time
|
|
|
|
|
|
|
|
|
|
| 11 |
from loguru import logger
|
| 12 |
|
| 13 |
+
from call_connection_manager import CallConfigManager, SessionManager
|
| 14 |
from pipecat.adapters.schemas.function_schema import FunctionSchema
|
| 15 |
from pipecat.adapters.schemas.tools_schema import ToolsSchema
|
| 16 |
from pipecat.audio.vad.silero import SileroVADAnalyzer
|
|
|
|
| 40 |
daily_api_key = os.environ.get("HF_DAILY_API_KEY", "")
|
| 41 |
daily_api_url = os.environ.get("DAILY_API_URL", "https://api.daily.co/v1")
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
class TranscriptionModifierProcessor(FrameProcessor):
|
| 44 |
"""Processor that modifies transcription frames before they reach the context aggregator."""
|
| 45 |
def __init__(self, operator_session_id_ref):
|
|
|
|
| 78 |
dialed_number = caller_info["dialed_number"]
|
| 79 |
customer_name = call_config_manager.get_customer_name(caller_number) if caller_number else None
|
| 80 |
operator_dialout_settings = call_config_manager.get_dialout_settings_for_caller(caller_number)
|
|
|
|
| 81 |
|
| 82 |
logger.info(f"Caller number: {caller_number}")
|
| 83 |
logger.info(f"Dialed number: {dialed_number}")
|
|
|
|
| 151 |
|
| 152 |
messages = [call_config_manager.create_system_message(system_instruction)]
|
| 153 |
llm = OpenAILLMService(api_key=os.environ.get("HF_OPENAI_API_KEY"))
|
| 154 |
+
llm.register_function("terminate_call", lambda params: terminate_call(task, params))
|
| 155 |
llm.register_function("dial_operator", dial_operator)
|
| 156 |
context = OpenAILLMContext(messages, tools)
|
| 157 |
context_aggregator = llm.create_context_aggregator(context)
|
| 158 |
|
| 159 |
# ------------ FUNCTION DEFINITIONS ------------
|
| 160 |
+
async def terminate_call(task: PipelineTask, params: FunctionCallParams):
|
| 161 |
content = "The user wants to end the conversation, thank them for chatting."
|
| 162 |
message = call_config_manager.create_system_message(content)
|
| 163 |
messages.append(message)
|
| 164 |
await task.queue_frames([LLMMessagesFrame(messages)])
|
|
|
|
| 165 |
await params.llm.queue_frame(EndTaskFrame(), FrameDirection.UPSTREAM)
|
| 166 |
|
| 167 |
async def dial_operator(params: FunctionCallParams):
|
|
|
|
| 207 |
# ------------ PIPELINE SETUP ------------
|
| 208 |
summary_finished = SummaryFinished(session_manager.call_flow_state)
|
| 209 |
transcription_modifier = TranscriptionModifierProcessor(session_manager.get_session_id_ref("operator"))
|
|
|
|
| 210 |
|
| 211 |
async def should_speak(self) -> bool:
|
| 212 |
return (not session_manager.call_flow_state.operator_connected or
|
|
|
|
| 214 |
|
| 215 |
pipeline = Pipeline([
|
| 216 |
transport.input(),
|
|
|
|
| 217 |
transcription_modifier,
|
| 218 |
context_aggregator.user(),
|
| 219 |
FunctionFilter(should_speak),
|
|
|
|
| 271 |
logger.debug(f"Participant left: {participant}, reason: {reason}")
|
| 272 |
if not (session_manager.get_session_id("operator") and
|
| 273 |
participant["id"] == session_manager.get_session_id("operator")):
|
|
|
|
| 274 |
await task.cancel()
|
| 275 |
return
|
| 276 |
logger.debug("Operator left the call")
|