Just Add @tool: Writing a Custom Tool
You've just started working at a AgentsCafé, where customers at each table can place multiple drink orders.
Baristas currently hand-write order codes like T5_Latte_20250812_0915
, but typos and inconsistent formatting often lead to mix-ups and wrong deliveries.
To fix this, you'll build a simple tool called generate_order_id
that takes a table ID, drink name, and appends a timestamp, ensuring every order code is clear and consistent.
Note: The datetime
library has been already imported.
This exercise is part of the course
AI Agents with Hugging Face smolagents
Exercise instructions
- Import the
tool
decorator from thesmolagents
library. - Use the
@tool
decorator to register yourgenerate_order_id
function as a tool. - Return the formatted order ID string that combines the table ID, drink name, and current timestamp.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import the tool decorator
from smolagents import ____
# Create a tool with the @tool decorator
____
def generate_order_id(table_id: str, drink_name: str) -> str:
"""
Generates a unique order ID for a café order.
Args:
table_id: The table's identifier (e.g. "T5")
drink_name: Name of the drink (e.g. "Latte")
Returns:
A string in the format "{table_id}_{drink_name}_{YYYYMMDD_HHMM}"
"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M")
order_id = f"{table_id}_{drink_name}_{timestamp}"
# Return the order ID
return ____