@tool を追加するだけ:カスタムツールを作成する
あなたは AgentsCafé で働き始めました。このカフェでは、各テーブルの顧客が複数のドリンクを注文できます。
バリスタは現在、T5_Latte_20250812_0915 のような注文コードを手書きしていますが、誤字や書式の不統一が原因で、注文の混乱や誤配達が頻繁に起きています。
この問題を解決するために、generate_order_id というシンプルなツールを作成しましょう。このツールはテーブル ID とドリンク名を受け取り、タイムスタンプを付加することで、すべての注文コードを明確かつ一貫した形式に統一します。
注意:datetime ライブラリはすでにインポートされています。
この演習はコースの一部です
Hugging Face smolagents で学ぶ AI エージェント
演習の手順
smolagentsライブラリからtoolデコレータをインポートしてください。@toolデコレータを使って、generate_order_id関数をツールとして登録してください。- テーブル ID、ドリンク名、現在のタイムスタンプを組み合わせた、整形済みの注文 ID 文字列を返してください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# 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 ____