Adding items to the order

When a customer says I would like the salmon, your tool needs to find the matching item, add it to the order, and return a friendly confirmation. The key insight is that speech is messy. People say partial names, mispronounce words, and skip articles. Your tool has to forgive all of that.

restaurant_agent.py
python
order_items = []

@function_tool()
async def add_item_to_order(item_name: str) -> str:
    """Adds an item to the customer order by name or partial match."""
    item_found = None
    # Exact match first
    for items in MENU.values():
        for item in items:
            if item["name"].lower() == item_name.lower():
                item_found = item
                break
        if item_found:
            break
    # Fall back to partial match
    if not item_found:
        for items in MENU.values():
            for item in items:
                if item_name.lower() in item["name"].lower():
                    item_found = item
                    break
            if item_found:
                break
    if item_found:
        order_items.append(item_found)
        return f"Added {item_found['name']} for ${item_found['price']:.2f} to your order."
    return f"I could not find {item_name} on the menu. Could you say the exact item name?"

Exact match first, partial match as a fallback. The return string is already a speakable sentence, so the LLM can pass it straight through to TTS without rewording.

Quiz: Quiz

Loading practice…