-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
276 lines (250 loc) · 9.47 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from fasthtml.common import (
fast_app,
Script,
Main,
Div,
Button,
H1,
Span,
serve,
Response,
)
from datetime import datetime
import requests
import os
import json
ULTRAVOX_API_KEY = os.environ.get('ULTRAVOX_API_KEY', "<your ultravox api key>")
# Ultravox calls out to your tool over HTTP, so in debug mode, we need to expose the tool to the internet. A simple way to do that is with ngrok. Install ngrok, then run `ngrok http 5001` to expose your local server to the internet. Update this URL with the ngrok URL.
TOOL_URL = "https://<your-path>.ngrok.app"
app, rt = fast_app(pico=False, hdrs=(Script(src="https://cdn.tailwindcss.com"),))
def fixie_request(method, path, **kwargs):
u = "https://api.ultravox.ai/api"
return requests.request(
method, u + path, headers={"X-API-Key": ULTRAVOX_API_KEY}, **kwargs
)
# We're going to define a single tool to handle navigating between stages
# This tool will be used to navigate between the GREETING, SCHEDULE, and RESCHEDULE stages
NAVIGATE_STAGE_TOOL = {
"temporaryTool": {
"modelToolName": "navigateStage",
"description": "After determining if a new stage is necessary, call this tool to navigate to the next appropriate stage.",
# Dynamic parameters are params that you need the LLM to provider
"dynamicParameters": [
{
"name": "stageName",
"location": "PARAMETER_LOCATION_BODY",
"schema": {
"description": "The stage to navigate to",
"type": "string",
"enum": ["GREETING", "SCHEDULE", "RESCHEDULE"],
},
"required": True,
}
],
# This is for passing in the call ID to every tool request, useful for logging, keeping state, etc
"automaticParameters": [
{
"name": "call_id",
"location": "PARAMETER_LOCATION_BODY",
"knownValue": "KNOWN_PARAM_CALL_ID",
}
],
"http": {
"baseUrlPattern": f"{TOOL_URL}/navigateStage",
"httpMethod": "POST",
},
}
}
# We're going to define a tool to handle scheduling events
# We could wrap this around cal.com or some other scheduling service (or build our own)
SCHEDULE_EVENT_TOOL = {
"temporaryTool": {
"modelToolName": "scheduleEvent",
"description": "Schedule an event with the user after collecting the necessary information",
"dynamicParameters": [
{
"name": "attendeeName",
"location": "PARAMETER_LOCATION_BODY",
"schema": {
"description": "The user's name",
"type": "string",
},
"required": True,
},
{
"name": "attendeeEmail",
"location": "PARAMETER_LOCATION_BODY",
"schema": {
"description": "The user's name",
"type": "string",
},
"required": True,
},
{
"name": "startTime",
"location": "PARAMETER_LOCATION_BODY",
"schema": {
"description": "The date and start time of the event, expressed as UTC date string (e.g., 2024-08-13T09:00:00Z)",
"type": "string",
},
"required": True,
},
{
"name": "lengthInMinutes",
"location": "PARAMETER_LOCATION_BODY",
"schema": {
"description": "The number of minutes of the meeting",
"type": "number",
},
"required": True,
},
],
"automaticParameters": [
{
"name": "call_id",
"location": "PARAMETER_LOCATION_BODY",
"knownValue": "KNOWN_PARAM_CALL_ID",
}
],
"http": {
"baseUrlPattern": f"{TOOL_URL}/scheduleEvent",
"httpMethod": "POST",
},
}
}
STAGES = {
"GREETING": {
"systemPrompt": f"You're a helpful scheduling assistant. Today is {datetime.now().strftime('%B %d, %Y')}. You need to politely figure out if the user is trying to schedule a new appointment or reschedule an existing one. Once you know, you must immediately call the navigateStage tool. Don't mention anything about stages to the user.",
"selectedTools": [NAVIGATE_STAGE_TOOL],
},
"SCHEDULE": {
"systemPrompt": f"You're a helpful scheduling assistant. Today is {datetime.now().strftime('%B %d, %Y')}. You need to collect the necessary information to schedule an event. Once you have the information, you must call the scheduleEvent tool. There is no need to confirm availability. Don't mention anything about stages to the user.",
"selectedTools": [NAVIGATE_STAGE_TOOL, SCHEDULE_EVENT_TOOL],
},
"RESCHEDULE": {
"systemPrompt": "You're a rescheduling assistant.",
"selectedTools": [NAVIGATE_STAGE_TOOL],
},
}
# This is the script that will be loaded when the page is loaded
# We import the Ultravox JS SDK and create a new session
js_on_load = """
import { UltravoxSession } from 'https://esm.sh/ultravox-client';
const debugMessages = new Set(["debug"]);
window.UVSession = new UltravoxSession({ experimentalMessages: debugMessages });
"""
# This is the client side JS that will be run when the call is started
# It will join the call and set up event listeners for status, transcripts, and debug messages
def client_js(callDetails):
return f"""
async function joinCall() {{
const callStatus = await window.UVSession.joinCall("{callDetails.get('joinUrl')}");
console.log(callStatus);
}}
window.UVSession.addEventListener('status', (e) => {{
let statusDiv = htmx.find("#call-status")
statusDiv.innerText = e.target._status;
}});
window.UVSession.addEventListener('transcripts', (e) => {{
let transcripts = e.target._transcripts;
transcript = htmx.find("#transcript");
transcript.innerText = transcripts.filter(t => t && t.speaker !== "user").map(t => t ? t.text : "").join("\\n");
}});
window.UVSession.addEventListener('experimental_message', (msg) => {{
console.log('Debug: ', JSON.stringify(msg));
}});
joinCall();
htmx.on("#end-call", "click", async (e) => {{
try {{
await UVSession.leaveCall();
}} catch (error) {{
console.error("Error leaving call:", error);
}}
}})
"""
# This just makes the button look nice-ish
TW_BUTTON = "bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mt-4"
def layout(*args, **kwargs):
return Main(
# HTML Navigation
Div(
Div(*args, **kwargs, cls="mx-auto max-w-3xl"),
cls="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8",
)
)
# This is the main route that will be hit when the page is loaded
# It will return a button that will start the call
@rt("/")
def get():
button = Button("Start call", hx_post="/start", hx_target="#call-mgmt", hx_swap="outerHTML", cls=TW_BUTTON)
return layout(
Script(js_on_load, type="module"),
H1("Ultravox Stages Example", cls="text-xl font-bold mt-8"),
Div(
Div(
"Status: ",
Span("Waiting", id="call-status", cls="font-bold"),
),
Div(
"Call ID:",
Span("N/A", id="call-id", cls="font-bold"),
),
Div(button),
id="call-mgmt"
),
)
# This route will be hit when the LLM calls the navigateStage tool
# It will return the system prompt and selected tools for the next stage, along with appopriate headers
# Note the X-Ultravox-Response-Type header, which tells Ultravox to expect a new stage
@rt("/navigateStage")
async def post(req):
body = await req.json()
response_body = {
"systemPrompt": STAGES[body.get("stageName")].get("systemPrompt"),
"selectedTools": STAGES[body.get("stageName")].get("selectedTools"),
}
return Response(
status_code=200,
headers={"X-Ultravox-Response-Type": "new-stage"},
content=json.dumps(response_body),
media_type="application/json",
)
@rt("/scheduleEvent")
def post(body):
print("POST to /scheduleEvent")
print(body)
return Response(
status_code=200,
content="Event successfully created",
)
@rt("/start")
async def post():
d = {
"systemPrompt": STAGES["GREETING"]["systemPrompt"],
"voice": "Mark",
"selectedTools": STAGES["GREETING"]["selectedTools"],
}
r = fixie_request("POST", "/calls", json=d)
if r.status_code == 201:
callDetails = r.json()
js = client_js(callDetails)
return Div(
Div(
"Status: ",
Span("Initializing", id="call-status", cls="font-bold"),
),
Div(
"Call ID: ",
Span(callDetails.get("callId"), id="call-id", cls="font-bold"),
),
Button("End call", id="end-call", cls=TW_BUTTON, hx_get="/end", hx_swap="outerHTML"),
Div("", id="transcript"),
Script(code=js),
)
else:
print(r.text)
return r.text
@rt("/end")
def get():
return Button("Restart", cls=TW_BUTTON, hx_get="/", hx_target="body", hx_boost="false")
serve()