Update simple_dialin.py
Browse files- simple_dialin.py +28 -108
simple_dialin.py
CHANGED
@@ -7,61 +7,39 @@ import argparse
|
|
7 |
import asyncio
|
8 |
import os
|
9 |
import sys
|
10 |
-
|
11 |
-
from call_connection_manager import CallConfigManager, SessionManager
|
12 |
-
from dotenv import load_dotenv
|
13 |
from loguru import logger
|
14 |
|
15 |
-
from
|
16 |
-
from pipecat.adapters.schemas.tools_schema import ToolsSchema
|
17 |
from pipecat.audio.vad.silero import SileroVADAnalyzer
|
18 |
-
from pipecat.frames.frames import EndTaskFrame
|
19 |
from pipecat.pipeline.pipeline import Pipeline
|
20 |
from pipecat.pipeline.runner import PipelineRunner
|
21 |
from pipecat.pipeline.task import PipelineParams, PipelineTask
|
22 |
from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
|
23 |
-
from pipecat.processors.frame_processor import FrameDirection
|
24 |
from pipecat.services.cartesia.tts import CartesiaTTSService
|
25 |
-
from pipecat.services.llm_service import FunctionCallParams
|
26 |
from pipecat.services.openai.llm import OpenAILLMService
|
27 |
from pipecat.transports.services.daily import DailyDialinSettings, DailyParams, DailyTransport
|
28 |
|
29 |
-
load_dotenv(override=True)
|
30 |
-
|
31 |
logger.remove(0)
|
32 |
logger.add(sys.stderr, level="DEBUG")
|
33 |
|
34 |
-
|
35 |
-
daily_api_url = os.getenv("DAILY_API_URL", "https://api.daily.co/v1")
|
36 |
-
|
37 |
-
|
38 |
-
async def main(
|
39 |
-
room_url: str,
|
40 |
-
token: str,
|
41 |
-
body: dict,
|
42 |
-
):
|
43 |
-
# ------------ CONFIGURATION AND SETUP ------------
|
44 |
-
|
45 |
-
# Create a config manager using the provided body
|
46 |
call_config_manager = CallConfigManager.from_json_string(body) if body else CallConfigManager()
|
47 |
-
|
48 |
-
|
|
|
49 |
test_mode = call_config_manager.is_test_mode()
|
50 |
-
|
51 |
-
# Get dialin settings if present
|
52 |
dialin_settings = call_config_manager.get_dialin_settings()
|
53 |
|
54 |
-
|
55 |
-
|
56 |
|
57 |
# ------------ TRANSPORT SETUP ------------
|
58 |
-
|
59 |
-
# Set up transport parameters
|
60 |
if test_mode:
|
61 |
logger.info("Running in test mode")
|
62 |
transport_params = DailyParams(
|
63 |
-
api_url=
|
64 |
-
api_key=
|
65 |
audio_in_enabled=True,
|
66 |
audio_out_enabled=True,
|
67 |
video_out_enabled=False,
|
@@ -73,8 +51,8 @@ async def main(
|
|
73 |
call_id=dialin_settings.get("call_id"), call_domain=dialin_settings.get("call_domain")
|
74 |
)
|
75 |
transport_params = DailyParams(
|
76 |
-
api_url=
|
77 |
-
api_key=
|
78 |
dialin_settings=daily_dialin_settings,
|
79 |
audio_in_enabled=True,
|
80 |
audio_out_enabled=True,
|
@@ -83,82 +61,33 @@ async def main(
|
|
83 |
transcription_enabled=True,
|
84 |
)
|
85 |
|
86 |
-
|
87 |
-
transport = DailyTransport(
|
88 |
-
room_url,
|
89 |
-
token,
|
90 |
-
"Simple Dial-in Bot",
|
91 |
-
transport_params,
|
92 |
-
)
|
93 |
-
|
94 |
-
# Initialize TTS
|
95 |
tts = CartesiaTTSService(
|
96 |
-
api_key=os.
|
97 |
-
voice_id="b7d50908-b17c-442d-ad8d-810c63997ed9",
|
98 |
-
)
|
99 |
-
|
100 |
-
# ------------ FUNCTION DEFINITIONS ------------
|
101 |
-
|
102 |
-
async def terminate_call(params: FunctionCallParams):
|
103 |
-
"""Function the bot can call to terminate the call upon completion of a voicemail message."""
|
104 |
-
if session_manager:
|
105 |
-
# Mark that the call was terminated by the bot
|
106 |
-
session_manager.call_flow_state.set_call_terminated()
|
107 |
-
|
108 |
-
# Then end the call
|
109 |
-
await params.llm.queue_frame(EndTaskFrame(), FrameDirection.UPSTREAM)
|
110 |
-
|
111 |
-
# Define function schemas for tools
|
112 |
-
terminate_call_function = FunctionSchema(
|
113 |
-
name="terminate_call",
|
114 |
-
description="Call this function to terminate the call.",
|
115 |
-
properties={},
|
116 |
-
required=[],
|
117 |
)
|
118 |
-
|
119 |
-
# Create tools schema
|
120 |
-
tools = ToolsSchema(standard_tools=[terminate_call_function])
|
121 |
|
122 |
# ------------ LLM AND CONTEXT SETUP ------------
|
123 |
-
|
124 |
-
# Set up the system instruction for the LLM
|
125 |
-
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. """
|
126 |
-
|
127 |
-
# Initialize LLM
|
128 |
-
llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"))
|
129 |
-
|
130 |
-
# Register functions with the LLM
|
131 |
-
llm.register_function("terminate_call", terminate_call)
|
132 |
-
|
133 |
-
# Create system message and initialize messages list
|
134 |
messages = [call_config_manager.create_system_message(system_instruction)]
|
135 |
-
|
136 |
-
# Initialize LLM context and aggregator
|
137 |
-
context = OpenAILLMContext(messages, tools)
|
138 |
context_aggregator = llm.create_context_aggregator(context)
|
139 |
|
140 |
# ------------ PIPELINE SETUP ------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
-
# Build pipeline
|
143 |
-
pipeline = Pipeline(
|
144 |
-
[
|
145 |
-
transport.input(), # Transport user input
|
146 |
-
context_aggregator.user(), # User responses
|
147 |
-
llm, # LLM
|
148 |
-
tts, # TTS
|
149 |
-
transport.output(), # Transport bot output
|
150 |
-
context_aggregator.assistant(), # Assistant spoken responses
|
151 |
-
]
|
152 |
-
)
|
153 |
-
|
154 |
-
# Create pipeline task
|
155 |
task = PipelineTask(pipeline, params=PipelineParams(allow_interruptions=True))
|
156 |
|
157 |
-
# ------------ EVENT HANDLERS ------------
|
158 |
-
|
159 |
@transport.event_handler("on_first_participant_joined")
|
160 |
async def on_first_participant_joined(transport, participant):
|
161 |
-
logger.debug(f"First participant joined: {participant['id']}")
|
162 |
await transport.capture_participant_transcription(participant["id"])
|
163 |
await task.queue_frames([context_aggregator.user().get_context_frame()])
|
164 |
|
@@ -168,25 +97,16 @@ async def main(
|
|
168 |
await task.cancel()
|
169 |
|
170 |
# ------------ RUN PIPELINE ------------
|
171 |
-
|
172 |
-
if test_mode:
|
173 |
-
logger.debug("Running in test mode (can be tested in Daily Prebuilt)")
|
174 |
-
|
175 |
runner = PipelineRunner()
|
176 |
await runner.run(task)
|
177 |
|
178 |
-
|
179 |
if __name__ == "__main__":
|
180 |
-
parser = argparse.ArgumentParser(description="Simple
|
181 |
parser.add_argument("-u", "--url", type=str, help="Room URL")
|
182 |
parser.add_argument("-t", "--token", type=str, help="Room Token")
|
183 |
parser.add_argument("-b", "--body", type=str, help="JSON configuration string")
|
184 |
-
|
185 |
args = parser.parse_args()
|
186 |
-
|
187 |
-
# Log the arguments for debugging
|
188 |
logger.info(f"Room URL: {args.url}")
|
189 |
logger.info(f"Token: {args.token}")
|
190 |
logger.info(f"Body provided: {bool(args.body)}")
|
191 |
-
|
192 |
-
asyncio.run(main(args.url, args.token, args.body))
|
|
|
7 |
import asyncio
|
8 |
import os
|
9 |
import sys
|
|
|
|
|
|
|
10 |
from loguru import logger
|
11 |
|
12 |
+
from call_connection_manager import CallConfigManager
|
|
|
13 |
from pipecat.audio.vad.silero import SileroVADAnalyzer
|
14 |
+
from pipecat.frames.frames import EndTaskFrame, LLMMessagesFrame
|
15 |
from pipecat.pipeline.pipeline import Pipeline
|
16 |
from pipecat.pipeline.runner import PipelineRunner
|
17 |
from pipecat.pipeline.task import PipelineParams, PipelineTask
|
18 |
from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
|
|
|
19 |
from pipecat.services.cartesia.tts import CartesiaTTSService
|
|
|
20 |
from pipecat.services.openai.llm import OpenAILLMService
|
21 |
from pipecat.transports.services.daily import DailyDialinSettings, DailyParams, DailyTransport
|
22 |
|
|
|
|
|
23 |
logger.remove(0)
|
24 |
logger.add(sys.stderr, level="DEBUG")
|
25 |
|
26 |
+
async def main(room_url: str, token: str, body: dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
call_config_manager = CallConfigManager.from_json_string(body) if body else CallConfigManager()
|
28 |
+
caller_info = call_config_manager.get_caller_info()
|
29 |
+
caller_number = caller_info["caller_number"]
|
30 |
+
dialed_number = caller_info["dialed_number"]
|
31 |
test_mode = call_config_manager.is_test_mode()
|
|
|
|
|
32 |
dialin_settings = call_config_manager.get_dialin_settings()
|
33 |
|
34 |
+
logger.info(f"Caller number: {caller_number}")
|
35 |
+
logger.info(f"Dialed number: {dialed_number}")
|
36 |
|
37 |
# ------------ TRANSPORT SETUP ------------
|
|
|
|
|
38 |
if test_mode:
|
39 |
logger.info("Running in test mode")
|
40 |
transport_params = DailyParams(
|
41 |
+
api_url=os.environ.get("DAILY_API_URL", "https://api.daily.co/v1"),
|
42 |
+
api_key=os.environ.get("HF_DAILY_API_KEY", ""),
|
43 |
audio_in_enabled=True,
|
44 |
audio_out_enabled=True,
|
45 |
video_out_enabled=False,
|
|
|
51 |
call_id=dialin_settings.get("call_id"), call_domain=dialin_settings.get("call_domain")
|
52 |
)
|
53 |
transport_params = DailyParams(
|
54 |
+
api_url=os.environ.get("DAILY_API_URL", "https://api.daily.co/v1"),
|
55 |
+
api_key=os.environ.get("HF_DAILY_API_KEY", ""),
|
56 |
dialin_settings=daily_dialin_settings,
|
57 |
audio_in_enabled=True,
|
58 |
audio_out_enabled=True,
|
|
|
61 |
transcription_enabled=True,
|
62 |
)
|
63 |
|
64 |
+
transport = DailyTransport(room_url, token, "Simple Dialin Bot", transport_params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
tts = CartesiaTTSService(
|
66 |
+
api_key=os.environ.get("HF_CARTESIA_API_KEY", ""),
|
67 |
+
voice_id="b7d50908-b17c-442d-ad8d-810c63997ed9",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
)
|
69 |
+
llm = OpenAILLMService(api_key=os.environ.get("HF_OPENAI_API_KEY"))
|
|
|
|
|
70 |
|
71 |
# ------------ LLM AND CONTEXT SETUP ------------
|
72 |
+
system_instruction = """You are a friendly, helpful robot. Greet the user and ask how you can assist them."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
messages = [call_config_manager.create_system_message(system_instruction)]
|
74 |
+
context = OpenAILLMContext(messages)
|
|
|
|
|
75 |
context_aggregator = llm.create_context_aggregator(context)
|
76 |
|
77 |
# ------------ PIPELINE SETUP ------------
|
78 |
+
pipeline = Pipeline([
|
79 |
+
transport.input(),
|
80 |
+
context_aggregator.user(),
|
81 |
+
llm,
|
82 |
+
tts,
|
83 |
+
transport.output(),
|
84 |
+
context_aggregator.assistant(),
|
85 |
+
])
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
task = PipelineTask(pipeline, params=PipelineParams(allow_interruptions=True))
|
88 |
|
|
|
|
|
89 |
@transport.event_handler("on_first_participant_joined")
|
90 |
async def on_first_participant_joined(transport, participant):
|
|
|
91 |
await transport.capture_participant_transcription(participant["id"])
|
92 |
await task.queue_frames([context_aggregator.user().get_context_frame()])
|
93 |
|
|
|
97 |
await task.cancel()
|
98 |
|
99 |
# ------------ RUN PIPELINE ------------
|
|
|
|
|
|
|
|
|
100 |
runner = PipelineRunner()
|
101 |
await runner.run(task)
|
102 |
|
|
|
103 |
if __name__ == "__main__":
|
104 |
+
parser = argparse.ArgumentParser(description="Pipecat Simple Dialin Bot")
|
105 |
parser.add_argument("-u", "--url", type=str, help="Room URL")
|
106 |
parser.add_argument("-t", "--token", type=str, help="Room Token")
|
107 |
parser.add_argument("-b", "--body", type=str, help="JSON configuration string")
|
|
|
108 |
args = parser.parse_args()
|
|
|
|
|
109 |
logger.info(f"Room URL: {args.url}")
|
110 |
logger.info(f"Token: {args.token}")
|
111 |
logger.info(f"Body provided: {bool(args.body)}")
|
112 |
+
asyncio.run(main(args.url, args.token, args.body))
|
|