|
|
|
|
|
|
|
|
|
|
|
import argparse |
|
import asyncio |
|
import os |
|
import sys |
|
|
|
from call_connection_manager import CallConfigManager |
|
from dotenv import load_dotenv |
|
from loguru import logger |
|
|
|
from pipecat.adapters.schemas.function_schema import FunctionSchema |
|
from pipecat.adapters.schemas.tools_schema import ToolsSchema |
|
from pipecat.audio.vad.silero import SileroVADAnalyzer |
|
from pipecat.frames.frames import EndTaskFrame |
|
from pipecat.pipeline.pipeline import Pipeline |
|
from pipecat.pipeline.runner import PipelineRunner |
|
from pipecat.pipeline.task import PipelineParams, PipelineTask |
|
from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext |
|
from pipecat.processors.frame_processor import FrameDirection |
|
from pipecat.services.cartesia.tts import CartesiaTTSService |
|
from pipecat.services.llm_service import FunctionCallParams |
|
from pipecat.services.openai.llm import OpenAILLMService |
|
from pipecat.transports.services.daily import DailyParams, DailyTransport |
|
|
|
load_dotenv(override=True) |
|
|
|
logger.remove(0) |
|
logger.add(sys.stderr, level="DEBUG") |
|
|
|
daily_api_key = os.getenv("DAILY_API_KEY", "") |
|
daily_api_url = os.getenv("DAILY_API_URL", "https://api.daily.co/v1") |
|
|
|
|
|
async def main( |
|
room_url: str, |
|
token: str, |
|
body: dict, |
|
): |
|
|
|
|
|
|
|
call_config_manager = CallConfigManager.from_json_string(body) if body else CallConfigManager() |
|
|
|
|
|
dialout_settings = call_config_manager.get_dialout_settings() |
|
test_mode = call_config_manager.is_test_mode() |
|
|
|
|
|
|
|
transport_params = DailyParams( |
|
api_url=daily_api_url, |
|
api_key=daily_api_key, |
|
audio_in_enabled=True, |
|
audio_out_enabled=True, |
|
video_out_enabled=False, |
|
vad_analyzer=SileroVADAnalyzer(), |
|
transcription_enabled=True, |
|
) |
|
|
|
|
|
transport = DailyTransport( |
|
room_url, |
|
token, |
|
"Simple Dial-out Bot", |
|
transport_params, |
|
) |
|
|
|
|
|
tts = CartesiaTTSService( |
|
api_key=os.getenv("CARTESIA_API_KEY", ""), |
|
voice_id="b7d50908-b17c-442d-ad8d-810c63997ed9", |
|
) |
|
|
|
|
|
|
|
async def terminate_call(params: FunctionCallParams): |
|
"""Function the bot can call to terminate the call upon completion of a voicemail message.""" |
|
await params.llm.queue_frame(EndTaskFrame(), FrameDirection.UPSTREAM) |
|
|
|
|
|
terminate_call_function = FunctionSchema( |
|
name="terminate_call", |
|
description="Call this function to terminate the call.", |
|
properties={}, |
|
required=[], |
|
) |
|
|
|
|
|
tools = ToolsSchema(standard_tools=[terminate_call_function]) |
|
|
|
|
|
|
|
|
|
system_instruction = """You are Chatbot, a friendly, helpful robot. Your goal is to demonstrate your capabilities in a succinct way. Your output will be converted to audio so don't include special characters in your answers. Respond to what the user said in a creative and helpful way, but keep your responses brief. Start by introducing yourself. If the user ends the conversation, **IMMEDIATELY** call the `terminate_call` function. """ |
|
|
|
|
|
llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY")) |
|
|
|
|
|
llm.register_function("terminate_call", terminate_call) |
|
|
|
|
|
messages = [call_config_manager.create_system_message(system_instruction)] |
|
|
|
|
|
context = OpenAILLMContext(messages, tools) |
|
context_aggregator = llm.create_context_aggregator(context) |
|
|
|
|
|
|
|
|
|
pipeline = Pipeline( |
|
[ |
|
transport.input(), |
|
context_aggregator.user(), |
|
llm, |
|
tts, |
|
transport.output(), |
|
context_aggregator.assistant(), |
|
] |
|
) |
|
|
|
|
|
task = PipelineTask(pipeline, params=PipelineParams(allow_interruptions=True)) |
|
|
|
|
|
|
|
@transport.event_handler("on_joined") |
|
async def on_joined(transport, data): |
|
|
|
if not test_mode and dialout_settings: |
|
logger.debug("Dialout settings detected; starting dialout") |
|
await call_config_manager.start_dialout(transport, dialout_settings) |
|
|
|
@transport.event_handler("on_dialout_connected") |
|
async def on_dialout_connected(transport, data): |
|
logger.debug(f"Dial-out connected: {data}") |
|
|
|
@transport.event_handler("on_dialout_answered") |
|
async def on_dialout_answered(transport, data): |
|
logger.debug(f"Dial-out answered: {data}") |
|
|
|
await transport.capture_participant_transcription(data["sessionId"]) |
|
|
|
|
|
@transport.event_handler("on_first_participant_joined") |
|
async def on_first_participant_joined(transport, participant): |
|
if test_mode: |
|
logger.debug(f"First participant joined: {participant['id']}") |
|
await transport.capture_participant_transcription(participant["id"]) |
|
|
|
|
|
@transport.event_handler("on_participant_left") |
|
async def on_participant_left(transport, participant, reason): |
|
logger.debug(f"Participant left: {participant}, reason: {reason}") |
|
await task.cancel() |
|
|
|
|
|
|
|
if test_mode: |
|
logger.debug("Running in test mode (can be tested in Daily Prebuilt)") |
|
|
|
runner = PipelineRunner() |
|
await runner.run(task) |
|
|
|
|
|
if __name__ == "__main__": |
|
parser = argparse.ArgumentParser(description="Simple Dial-out Bot") |
|
parser.add_argument("-u", "--url", type=str, help="Room URL") |
|
parser.add_argument("-t", "--token", type=str, help="Room Token") |
|
parser.add_argument("-b", "--body", type=str, help="JSON configuration string") |
|
|
|
args = parser.parse_args() |
|
|
|
|
|
logger.info(f"Room URL: {args.url}") |
|
logger.info(f"Token: {args.token}") |
|
logger.info(f"Body provided: {bool(args.body)}") |
|
|
|
asyncio.run(main(args.url, args.token, args.body)) |
|
|