Repository navigation
Expand file tree
/
Copy pathdatabase.py
More file actions
708 lines (653 loc) · 24.3 KB
/
Copy pathdatabase.py
File metadata and controls
708 lines (653 loc) · 24.3 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
from __future__ import annotations
from collections import defaultdict
from typing import Literal
from pydantic import BaseModel
COMMON_INSTRUCTIONS = (
# Outcome — what a great interaction looks like.
"You are Mac, a quick and friendly McDonald’s drive-thru attendant, and a customer has just "
"pulled up to the speaker. A great interaction ends with their complete, correct order in the "
"ordering system — every item they asked for, at the right size, with nothing they didn’t ask "
"for — reached in as few, as natural exchanges as possible. \n"
"\n\n"
# Voice & personality — keep it short and human.
"Your output is synthesized directly to speech, so produce a natural verbatim transcript, not "
"polished text. Start responses with real reactions (oh, hmm, ah) and fillers (um, uh, like) "
'rather than "Absolutely" or "Certainly", and let mid-sentence fillers (like, you know, I '
"mean) fall where they naturally would. Use informal phrasing: yeah, gonna, kinda, gotcha, "
"lemme. Keep replies short, upbeat, and snappy, and ask about one thing at a time so you never "
"overwhelm the customer. Confirm choices warmly ('Alright, one Big Mac Combo!'), and when "
"something’s missing or unavailable, say so with empathy and offer the closest option ('Ah, "
"we’re out of Sweet Tea right now — can I get you a Coke instead?'). \n"
"\n\n"
# How to work — infer intent, acknowledge before acting, stop when you have enough.
"Assume the customer wants food even if they don’t open with a clear request, and guide them "
"toward it. Treat each transcript as a rough draft of what was said — it may contain "
"speech-to-text errors, so don’t mention the transcript or repeat its mistakes. When you can "
"reasonably infer intent and it’s safe to, just go with it; when the input is genuinely "
"ambiguous or nonsensical, ask the customer to repeat. \n"
"Before a tool call that takes a moment, give a brief spoken acknowledgment first ('lemme get "
"that added') so there’s no dead air. After each step, ask yourself whether you now have "
"everything needed to complete the customer’s request: if you do, act; if a required detail "
"is still missing, ask for just that one detail. \n"
"\n\n"
# Hard constraints — these are invariants, not judgment calls.
"Constraints that always hold:\n"
"- Stick strictly to the defined menu. Never invent items or sizes. If what the customer wants "
"isn’t *exactly* on the menu, say you don’t have it and offer the closest match (a hamburger "
"isn’t a cheeseburger). \n"
"- Any add, change, or removal must go through a tool call — actually call it, never pretend. "
"When a customer swaps an item, remove the old one before adding the new so the order has no "
"duplicates. \n"
"- Only add items the customer explicitly asked for; never add anything on their behalf. \n"
"- A stated quantity is explicit intent: if the customer asks for two of something, add it "
"twice right away — no need to confirm the count first. If no quantity is stated, assume "
"one and add the item without confirming the count. \n"
"- Don’t assume unstated details other than quantity — especially the drink in a combo. If a "
"required detail is missing, ask before calling the tool. \n"
"- Ask about size only for items that actually have more than one size; if an item has a single "
"size, don’t mention size at all. For a 'large meal', make both the fries and drink large "
"without re-confirming, unless the customer specifies different sizes. \n"
"- If a tool returns an error, tell the customer and ask them to try again. \n"
)
ItemSize = Literal["S", "M", "L"]
ItemCategory = Literal["drink", "combo_meal", "happy_meal", "regular", "sauce"]
class MenuItem(BaseModel):
id: str
name: str
calories: int
price: float
available: bool
size: ItemSize | None = None
voice_alias: str | None = None
category: ItemCategory
class FakeDB:
async def list_drinks(self) -> list[MenuItem]:
drink_data = [
{
"id": "coca_cola",
"name": "Coca-Cola®",
"sizes": {
"S": {"calories": 200, "price": 1.49},
"M": {"calories": 270, "price": 1.69},
"L": {"calories": 380, "price": 1.89},
},
},
{
"id": "sprite",
"name": "Sprite®",
"sizes": {
"S": {"calories": 190, "price": 1.49},
"M": {"calories": 250, "price": 1.69},
"L": {"calories": 350, "price": 1.89},
},
},
{
"id": "diet_coke",
"name": "Diet Coke®",
"sizes": {
"S": {"calories": 0, "price": 1.49},
"M": {"calories": 0, "price": 1.69},
"L": {"calories": 0, "price": 1.89},
},
},
{
"id": "dr_pepper",
"name": "Dr Pepper®",
"sizes": {
"S": {"calories": 200, "price": 1.49},
"M": {"calories": 270, "price": 1.69},
"L": {"calories": 380, "price": 1.89},
},
},
{
"id": "fanta_orange",
"name": "Fanta® Orange",
"sizes": {
"S": {"calories": 210, "price": 1.49},
"M": {"calories": 280, "price": 1.69},
"L": {"calories": 390, "price": 1.89},
},
},
{
"id": "hi_c_orange_lavaburst",
"name": "Hi-C® Orange Lavaburst®",
"sizes": {
"S": {"calories": 210, "price": 1.49},
"M": {"calories": 280, "price": 1.69},
"L": {"calories": 390, "price": 1.89},
},
},
{
"id": "sweet_tea",
"name": "Sweet Tea",
"sizes": {
"S": {"calories": 140, "price": 1.39},
"M": {"calories": 180, "price": 1.59},
"L": {"calories": 220, "price": 1.79},
},
"available": False,
},
{
"id": "unsweetened_iced_tea",
"name": "Unsweetened Iced Tea",
"sizes": {
"S": {"calories": 0, "price": 1.39},
"M": {"calories": 0, "price": 1.59},
"L": {"calories": 0, "price": 1.79},
},
},
{
"id": "minute_maid_orange_juice",
"name": "Minute Maid® Premium Orange Juice",
"sizes": {
"S": {"calories": 190, "price": 2.59},
"M": {"calories": 240, "price": 2.79},
"L": {"calories": 300, "price": 2.99},
},
},
{
"id": "milk",
"name": "Milk",
"calories": 100,
"price": 1.29,
},
{
"id": "chocolate_milk",
"name": "Chocolate Milk",
"calories": 150,
"price": 1.39,
},
{
"id": "dasani_water",
"name": "DASANI® Water",
"calories": 0,
"price": 1.59,
},
]
items = []
for item in drink_data:
if sizes := item.get("sizes", {}):
for size, size_details in sizes.items():
items.append(
MenuItem(
id=item["id"],
name=item["name"],
calories=size_details["calories"],
price=size_details["price"],
size=size,
available=True,
category="drink",
)
)
else:
items.append(
MenuItem(
id=item["id"],
name=item["name"],
calories=item["calories"],
price=item["price"],
available=True,
category="drink",
)
)
return items
async def list_combo_meals(self) -> list[MenuItem]:
raw_meals = [
{
"id": "combo_big_mac",
"name": "Big Mac® Combo",
"alias": "1",
"calories": 970,
"price": 9.49,
},
{
"id": "combo_quarter_pounder_2a",
"name": "Quarter Pounder® with Cheese Combo",
"alias": "2a",
"calories": 840,
"price": 9.89,
},
{
"id": "combo_quarter_pounder_2b",
"name": "Quarter Pounder® with Cheese & Bacon Combo",
"alias": "2b",
"calories": 950,
"price": 10.39,
},
{
"id": "combo_quarter_pounder_2c",
"name": "Quarter Pounder® Deluxe Combo",
"alias": "2c",
"calories": 950,
"price": 10.39,
},
{
"id": "combo_double_quarter",
"name": "Double Quarter Pounder® with Cheese Combo",
"alias": "3",
"calories": 1060,
"price": 10.29,
},
{
"id": "combo_mccrispy_4a",
"name": "McCrispy™ Original Combo",
"alias": "4a",
"calories": 790,
"price": 8.99,
},
{
"id": "combo_mccrispy_4b",
"name": "McCrispy™ Spicy Combo",
"alias": "4b",
"calories": 850,
"price": 8.99,
},
{
"id": "combo_mccrispy_4c",
"name": "McCrispy™ Deluxe Combo",
"alias": "4c",
"calories": 880,
"price": 9.89,
},
{
"id": "combo_mccrispy_4d",
"name": "McCrispy™ Spicy Deluxe Combo",
"alias": "4d",
"calories": 860,
"price": 9.99,
},
{
"id": "combo_chicken_mcnuggets_10pc",
"name": "10 pc. Chicken McNuggets® Combo",
"alias": "5",
"calories": 740,
"price": 9.49,
},
{
"id": "combo_filet_o_fish",
"name": "Filet-O-Fish® Combo",
"alias": "6",
"calories": 700,
"price": 7.89,
},
{
"id": "combo_cheeseburgers_2pc",
"name": "2 Cheeseburgers Combo",
"alias": "7",
"calories": 920,
"price": 7.89,
},
]
meals = []
for item in raw_meals:
meals.append(
MenuItem(
id=item["id"],
name=item["name"],
calories=item["calories"],
price=item["price"],
voice_alias=item["alias"],
category="combo_meal",
available=True,
)
)
return meals
async def list_happy_meals(self) -> list[MenuItem]:
raw_happy_meals = [
{
"id": "happy_meal_4pc_mcnuggets",
"name": "4 pc. Chicken McNuggets® Happy Meal",
"calories": 430,
"price": 5.99,
},
{
"id": "happy_meal_6pc_mcnuggets",
"name": "6 pc. Chicken McNuggets® Happy Meal",
"calories": 530,
"price": 6.99,
},
{
"id": "happy_meal_hamburger",
"name": "Hamburger Happy Meal",
"calories": 510,
"price": 5.59,
},
]
meals = []
for item in raw_happy_meals:
meals.append(
MenuItem(
id=item["id"],
name=item["name"],
calories=item["calories"],
price=item["price"],
available=True,
category="happy_meal",
)
)
return meals
async def list_regulars(self) -> list[MenuItem]:
raw_items = [
{
"id": "big_mac",
"name": "Big Mac®",
"calories": 590,
"price": 5.89,
},
{
"id": "quarter_pounder_cheese",
"name": "Quarter Pounder® with Cheese",
"calories": 520,
"price": 6.29,
},
{
"id": "quarter_pounder_bacon",
"name": "Quarter Pounder® with Cheese & Bacon",
"calories": 590,
"price": 6.79,
},
{
"id": "quarter_pounder_deluxe",
"name": "Quarter Pounder® Deluxe",
"calories": 530,
"price": 6.39,
},
{
"id": "double_quarter_pounder",
"name": "Double Quarter Pounder® with Cheese",
"calories": 740,
"price": 7.49,
},
{
"id": "mccrispy_original",
"name": "McCrispy™ Original",
"calories": 470,
"price": 5.69,
},
{
"id": "mccrispy_spicy",
"name": "McCrispy™ Spicy",
"calories": 500,
"price": 5.69,
},
{
"id": "mccrispy_deluxe",
"name": "McCrispy™ Deluxe",
"calories": 530,
"price": 6.39,
},
{
"id": "mccrispy_spicy_deluxe",
"name": "McCrispy™ Spicy Deluxe",
"calories": 530,
"price": 6.59,
},
{
"id": "mcnuggets_10pc",
"name": "10 pc. Chicken McNuggets®",
"calories": 410,
"price": 6.79,
},
{
"id": "filet_o_fish",
"name": "Filet-O-Fish®",
"calories": 390,
"price": 5.89,
},
{
"id": "hamburger",
"name": "Hamburger",
"calories": 300,
"price": 2,
},
{
"id": "cheeseburger",
"name": "Cheeseburger",
"calories": 600,
"price": 2.58,
},
{
"id": "fries",
"name": "Fries",
"sizes": {
"S": {"calories": 230, "price": 1.89},
"M": {"calories": 350, "price": 3.99},
"L": {"calories": 521, "price": 4.75},
},
},
{
"id": "sweet_sundae",
"name": "Sundae",
"calories": 330,
"price": 3.69,
},
{
"id": "sweet_mcflurry_oreo",
"name": "McFlurry® (Oreo)",
"calories": 480,
"price": 4.89,
},
{
"id": "shake_vanilla",
"name": "Vanilla Shake",
"sizes": {
"S": {"calories": 510, "price": 2.79},
"M": {"calories": 610, "price": 3.59},
"L": {"calories": 820, "price": 3.89},
},
},
{
"id": "shake_chocolate",
"name": "Chocolate Shake",
"sizes": {
"S": {"calories": 520, "price": 2.79},
"M": {"calories": 620, "price": 3.59},
"L": {"calories": 830, "price": 3.89},
},
},
{
"id": "shake_strawberry",
"name": "Strawberry Shake",
"sizes": {
"S": {"calories": 530, "price": 2.79},
"M": {"calories": 620, "price": 3.59},
"L": {"calories": 840, "price": 3.89},
},
},
{
"id": "sweet_cone",
"name": "Cone",
"calories": 200,
"price": 3.19,
},
]
items = []
for item in raw_items:
if sizes := item.get("sizes", {}):
for size, size_details in sizes.items():
items.append(
MenuItem(
id=item["id"],
name=item["name"],
calories=size_details["calories"],
price=size_details["price"],
size=size,
available=True,
category="regular",
)
)
else:
items.append(
MenuItem(
id=item["id"],
name=item["name"],
calories=item["calories"],
price=item["price"],
available=True,
category="regular",
)
)
return items
async def list_sauces(self) -> list[MenuItem]:
raw_items = [
{
"id": "jalapeno_ranch",
"name": "Jalapeño Ranch",
"calories": 70,
"price": 0.25,
},
{
"id": "garlic_sauce",
"name": "Garlic Sauce",
"calories": 45,
"price": 0.25,
},
{
"id": "mayonnaise",
"name": "Mayonnaise",
"calories": 90,
"price": 0.20,
},
{
"id": "frietsaus",
"name": "Frietsaus",
"calories": 100,
"price": 0.20,
},
{
"id": "curry_suace",
"name": "Curry sauce",
"calories": 60,
"price": 0.20,
},
{
"id": "ketchup",
"name": "Ketchup",
"calories": 20,
"price": 0.10,
},
{
"id": "barbecue_sauce",
"name": "Barbecue Sauce",
"calories": 45,
"price": 0.20,
},
{
"id": "sweet_and_sour_sauce",
"name": "Sweet-and-sour sauce",
"calories": 50,
"price": 0.40,
},
{
"id": "honey_mustard_dressing",
"name": "Honey mustard dressing",
"calories": 60,
"price": 0.20,
},
]
sauces = []
for item in raw_items:
sauces.append(
MenuItem(
id=item["id"],
name=item["name"],
calories=item["calories"],
price=item["price"],
available=True,
category="sauce",
)
)
return sauces
# The code below is optimized for ease of use instead of efficiency.
def map_by_sizes(
items: list[MenuItem],
) -> tuple[dict[str, dict[ItemSize, MenuItem]], list[MenuItem]]:
result = defaultdict(dict)
leftovers = [item for item in items if not item.size]
[result[item.id].update({item.size: item}) for item in items if item.size]
return dict(result), leftovers
def find_items_by_id(
items: list[MenuItem], item_id: str, size: ItemSize | None = None
) -> list[MenuItem]:
return [item for item in items if item.id == item_id and (size is None or item.size == size)]
def menu_instructions(category: ItemCategory, *, items: list[MenuItem]) -> str:
if category == "drink":
return _drink_menu_instructions(items)
elif category == "combo_meal":
return _combo_menu_instructions(items)
elif category == "happy_meal":
return _happy_menu_instructions(items)
elif category == "sauce":
return _sauce_menu_instructions(items)
elif category == "regular":
return _regular_menu_instructions(items)
def _drink_menu_instructions(items: list[MenuItem]) -> str:
available_sizes, leftovers = map_by_sizes(items)
menu_lines = []
for _, size_map in available_sizes.items():
first_item = next(iter(size_map.values()))
menu_lines.append(f" - {first_item.name} (id:{first_item.id}):")
for item in size_map.values():
line = f" - Size {item.size}: {item.calories} Cal, ${item.price:.2f}"
if not item.available:
line += " UNAVAILABLE"
menu_lines.append(line)
for item in leftovers:
# explicitely saying there is no `size` for this item, otherwise the LLM seems to hallucinate quite often
line = f" - {item.name}: {item.calories} Cal, ${item.price:.2f} (id:{item.id}) - Not size-selectable`"
if not item.available:
line += " UNAVAILABLE"
menu_lines.append(line)
return "# Drinks:\n" + "\n".join(menu_lines)
def _combo_menu_instructions(items: list[MenuItem]) -> str:
menu_lines = []
for item in items:
line = f" **{item.voice_alias}**. {item.name}: {item.calories} Cal, ${item.price:.2f} (id:{item.id})"
if not item.available:
line += " UNAVAILABLE"
menu_lines.append(line)
instructions = (
"# Combo Meals:\n"
"The user can select a combo meal by saying its voice alias (e.g., '1', '2a', '4c'). Use the alias to identify which combo they chose.\n"
"But don't mention the voice alias to the user if not needed."
)
return instructions + "\n".join(menu_lines)
def _happy_menu_instructions(items: list[MenuItem]) -> str:
menu_lines = []
for item in items:
line = f" - {item.name}: {item.calories} Cal, ${item.price:.2f} (id:{item.id})"
if not item.available:
line += " UNAVAILABLE"
menu_lines.append(line)
return (
"# Happy Meals:\n" + "\n".join(menu_lines) + "\n\nRecommended drinks with the Happy Meal:\n"
" - Milk chocolate/white\n"
" - DASANI Water\n"
" - Or any other small drink."
)
def _sauce_menu_instructions(items: list[MenuItem]) -> str:
menu_lines = []
for item in items:
line = f" - {item.name}: {item.calories} Cal, ${item.price:.2f} (id:{item.id})"
if not item.available:
line += " UNAVAILABLE"
menu_lines.append(line)
return "# Sauces:\n" + "\n".join(menu_lines)
# regular/a la carte
def _regular_menu_instructions(items: list[MenuItem]) -> str:
available_sizes, leftovers = map_by_sizes(items)
menu_lines = []
for _, size_map in available_sizes.items():
first_item = next(iter(size_map.values()))
menu_lines.append(f" - {first_item.name} (id:{first_item.id}):")
for item in size_map.values():
line = f" - Size {item.size}: {item.calories} Cal, ${item.price:.2f}"
if not item.available:
line += " UNAVAILABLE"
menu_lines.append(line)
for item in leftovers:
line = f" - {item.name}: {item.calories} Cal, ${item.price:.2f} (id:{item.id}) - Not size-selectable"
if not item.available:
line += " UNAVAILABLE"
menu_lines.append(line)
return "# Regular items/À la carte:\n" + "\n".join(menu_lines)