-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathfood_ordering_direct_functions.py
More file actions
356 lines (288 loc) · 10.7 KB
/
food_ordering_direct_functions.py
File metadata and controls
356 lines (288 loc) · 10.7 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#
# Copyright (c) 2024-2026, Daily
#
# SPDX-License-Identifier: BSD 2-Clause License
#
"""A dynamic food ordering flow example using Direct Functions.
This example demonstrates a food ordering system using dynamic flows with
direct functions where conversation paths are determined at runtime.
Direct functions combine the function definition and handler in a single function.
The flow handles:
1. Initial greeting and food type selection (pizza or sushi)
2. Order details collection based on food type
3. Order confirmation and revision
4. Order completion
Multi-LLM Support:
Set LLM_PROVIDER environment variable to choose your LLM provider.
Supported: openai (default), anthropic, google, aws
Requirements:
- CARTESIA_API_KEY (for TTS)
- DEEPGRAM_API_KEY (for STT)
- DAILY_API_KEY (for transport)
- LLM API key (varies by provider - see env.example)
"""
import os
from datetime import datetime, timedelta
from dotenv import load_dotenv
from loguru import logger
from pipecat.audio.vad.silero import SileroVADAnalyzer
from pipecat.pipeline.pipeline import Pipeline
from pipecat.pipeline.runner import PipelineRunner
from pipecat.pipeline.task import PipelineParams, PipelineTask
from pipecat.processors.aggregators.llm_context import LLMContext
from pipecat.processors.aggregators.llm_response_universal import (
LLMContextAggregatorPair,
LLMUserAggregatorParams,
)
from pipecat.runner.types import RunnerArguments
from pipecat.runner.utils import create_transport
from pipecat.services.cartesia.tts import CartesiaTTSService
from pipecat.services.deepgram.stt import DeepgramSTTService
from pipecat.transports.base_transport import BaseTransport, TransportParams
from pipecat.transports.daily.transport import DailyParams
from pipecat.transports.websocket.fastapi import FastAPIWebsocketParams
from utils import create_llm
from pipecat_flows import FlowManager, FlowResult, NodeConfig
load_dotenv(override=True)
transport_params = {
"daily": lambda: DailyParams(
audio_in_enabled=True,
audio_out_enabled=True,
),
"twilio": lambda: FastAPIWebsocketParams(
audio_in_enabled=True,
audio_out_enabled=True,
),
"webrtc": lambda: TransportParams(
audio_in_enabled=True,
audio_out_enabled=True,
),
}
# Type definitions
class PizzaOrderResult(FlowResult):
size: str
type: str
price: float
class SushiOrderResult(FlowResult):
count: int
type: str
price: float
class DeliveryEstimateResult(FlowResult):
time: str
# Pre-action handlers
async def check_kitchen_status(action: dict, flow_manager: FlowManager) -> None:
"""Check if kitchen is open and log status."""
logger.info("Checking kitchen status")
# Direct Functions for Initial Node
async def choose_pizza(flow_manager: FlowManager) -> tuple[None, NodeConfig]:
"""
User wants to order pizza. Let's get that order started.
"""
return None, create_pizza_node()
async def choose_sushi(flow_manager: FlowManager) -> tuple[None, NodeConfig]:
"""
User wants to order sushi. Let's get that order started.
"""
return None, create_sushi_node()
# Direct Functions for Pizza Node
async def select_pizza_order(
flow_manager: FlowManager, size: str, pizza_type: str
) -> tuple[PizzaOrderResult, NodeConfig]:
"""
Record the pizza order details.
Args:
size (str): Size of the pizza. Must be one of "small", "medium", or "large".
pizza_type (str): Type of pizza. Must be one of "pepperoni", "cheese", "supreme", or "vegetarian".
"""
# Simple pricing
base_price = {"small": 10.00, "medium": 15.00, "large": 20.00}
price = base_price[size]
result = PizzaOrderResult(size=size, type=pizza_type, price=price)
# Store order details in flow state
flow_manager.state["order"] = {
"type": "pizza",
"size": size,
"pizza_type": pizza_type,
"price": price,
}
return result, create_confirmation_node()
# Direct Functions for Sushi Node
async def select_sushi_order(
flow_manager: FlowManager, count: int, roll_type: str
) -> tuple[SushiOrderResult, NodeConfig]:
"""
Record the sushi order details.
Args:
count (int): Number of sushi rolls to order. Must be between 1 and 10.
roll_type (str): Type of sushi roll. Must be one of "california", "spicy tuna", "rainbow", or "dragon".
"""
# Simple pricing: $8 per roll
price = count * 8.00
result = SushiOrderResult(count=count, type=roll_type, price=price)
# Store order details in flow state
flow_manager.state["order"] = {
"type": "sushi",
"count": count,
"roll_type": roll_type,
"price": price,
}
return result, create_confirmation_node()
# Direct Functions for Confirmation Node
async def complete_order(flow_manager: FlowManager) -> tuple[None, NodeConfig]:
"""
User confirms the order is correct.
"""
return None, create_end_node()
async def revise_order(flow_manager: FlowManager) -> tuple[None, NodeConfig]:
"""
User wants to make changes to their order.
"""
return None, create_initial_node()
# Node creation functions
def create_initial_node() -> NodeConfig:
"""Create the initial node for food type selection."""
return NodeConfig(
name="initial",
role_messages=[
{
"role": "system",
"content": "You are an order-taking assistant. You must ALWAYS use the available functions to progress the conversation. This is a phone conversation and your responses will be converted to audio. Keep the conversation friendly, casual, and polite. Avoid outputting special characters and emojis.",
}
],
task_messages=[
{
"role": "system",
"content": "For this step, ask the user if they want pizza or sushi, and wait for them to use a function to choose. Start off by greeting them. Be friendly and casual; you're taking an order for food over the phone.",
}
],
pre_actions=[
{
"type": "function",
"handler": check_kitchen_status,
},
],
functions=[choose_pizza, choose_sushi],
)
def create_pizza_node() -> NodeConfig:
"""Create the pizza ordering node."""
return NodeConfig(
name="choose_pizza",
task_messages=[
{
"role": "system",
"content": """You are handling a pizza order. Use the available functions:
- Use select_pizza_order when the user specifies both size AND type
Pricing:
- Small: $10
- Medium: $15
- Large: $20
Remember to be friendly and casual.""",
}
],
functions=[select_pizza_order],
)
def create_sushi_node() -> NodeConfig:
"""Create the sushi ordering node."""
return NodeConfig(
name="choose_sushi",
task_messages=[
{
"role": "system",
"content": """You are handling a sushi order. Use the available functions:
- Use select_sushi_order when the user specifies both count AND type
Pricing:
- $8 per roll
Remember to be friendly and casual.""",
}
],
functions=[select_sushi_order],
)
def create_confirmation_node() -> NodeConfig:
"""Create the order confirmation node."""
return NodeConfig(
name="confirm",
task_messages=[
{
"role": "system",
"content": """Read back the complete order details to the user and ask if they want anything else or if they want to make changes. Use the available functions:
- Use complete_order when the user confirms that the order is correct and no changes are needed
- Use revise_order if they want to change something
Be friendly and clear when reading back the order details.""",
}
],
functions=[complete_order, revise_order],
)
def create_end_node() -> NodeConfig:
"""Create the final node."""
return NodeConfig(
name="end",
task_messages=[
{
"role": "system",
"content": "Thank the user for their order and end the conversation politely and concisely.",
}
],
post_actions=[{"type": "end_conversation"}],
)
async def run_bot(transport: BaseTransport, runner_args: RunnerArguments):
"""Run the food ordering bot with direct functions."""
stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY"))
tts = CartesiaTTSService(
api_key=os.getenv("CARTESIA_API_KEY"),
voice_id="820a3788-2b37-4d21-847a-b65d8a68c99a", # Salesman
)
# LLM service is created using the create_llm function from utils.py
# Default is OpenAI; can be changed by setting LLM_PROVIDER environment variable
llm = create_llm()
context = LLMContext()
context_aggregator = LLMContextAggregatorPair(
context,
user_params=LLMUserAggregatorParams(vad_analyzer=SileroVADAnalyzer()),
)
pipeline = Pipeline(
[
transport.input(),
stt,
context_aggregator.user(),
llm,
tts,
transport.output(),
context_aggregator.assistant(),
]
)
task = PipelineTask(pipeline, params=PipelineParams(allow_interruptions=True))
# Define "global" functions available at every node
async def get_delivery_estimate(
flow_manager: FlowManager,
) -> tuple[DeliveryEstimateResult, None]:
"""Provide delivery estimate information."""
delivery_time = datetime.now() + timedelta(minutes=30)
return DeliveryEstimateResult(
time=f"{delivery_time}",
), None
# Initialize flow manager
flow_manager = FlowManager(
task=task,
llm=llm,
context_aggregator=context_aggregator,
transport=transport,
global_functions=[get_delivery_estimate],
)
@transport.event_handler("on_client_connected")
async def on_client_connected(transport, client):
logger.info("Client connected")
# Kick off the conversation with the initial node
await flow_manager.initialize(create_initial_node())
@transport.event_handler("on_client_disconnected")
async def on_client_disconnected(transport, client):
logger.info(f"Client disconnected")
await task.cancel()
runner = PipelineRunner(handle_sigint=runner_args.handle_sigint)
await runner.run(task)
async def bot(runner_args: RunnerArguments):
"""Main bot entry point compatible with Pipecat Cloud."""
transport = await create_transport(runner_args, transport_params)
await run_bot(transport, runner_args)
if __name__ == "__main__":
from pipecat.runner.run import main
main()