まとめ:Weather Desk
CoddyのAI Engineeringジャーニー「ツールとエージェント」セクションの一部。レッスン 5/22。
チャレンジ
中級2つのツールを使用して回答するウェザーデスクを作成します。スターターコードには get_weather と get_air_quality が定義されており、質問を読み込みます。
- 必須の文字列プロパティ
cityをそれぞれ1つ持つ2つのツールを定義します。説明Get the current weather for a cityを持つget_weatherと、説明Get the air quality index for a cityを持つget_air_qualityです。 - 両方のツール、
model="claude-opus-5-5"、およびmax_tokens=300を使用して質問を送信します。 - 停止理由が
"tool_use"の場合:Tool:、ツールの名前、スペース、およびその入力(input)を出力し、対応する関数を実行し、アシスタントのターンとtool_resultを送信して、再度モデルを呼び出します。それ以外の場合はTool: noneを出力します。 Answer:と最後の返答のテキストブロックのテキストを出力します。
例えば、入力 What is the weather in Lisbon? に対して、プログラムは以下を出力します:
Tool: get_weather {'city': 'Lisbon'}
Answer: Here is what I found: Lisbon: 19 C and sunny自分で試してみよう
import coddy_ai as anthropic # 本番環境では: import anthropic
client = anthropic.Anthropic()
WEATHER = {"Lisbon": "19 C and sunny", "Delhi": "31 C and hazy", "Montreal": "-3 C and snowing"}
AIR = {"Lisbon": "index 22, good", "Delhi": "index 168, unhealthy", "Montreal": "index 35, good"}
def get_weather(city):
return f"{city}: {WEATHER.get(city, 'no weather data')}"
def get_air_quality(city):
return f"{city}: {AIR.get(city, 'no air quality data')}"
question = input()
# 以下にコードを記述してください
ツールとエージェントのすべてのレッスン
自分で練習してみよう: Pythonオンラインコンパイラ