Menu
CoddyTech

まとめ:Weather Desk

CoddyのAI Engineeringジャーニー「ツールとエージェント」セクションの一部。レッスン 5/22。

challenge icon

チャレンジ

中級

2つのツールを使用して回答するウェザーデスクを作成します。スターターコードには get_weather と get_air_quality が定義されており、質問を読み込みます。

  1. 必須の文字列プロパティ city をそれぞれ1つ持つ2つのツールを定義します。説明 Get the current weather for a city を持つ get_weather と、説明 Get the air quality index for a city を持つ get_air_quality です。
  2. 両方のツール、model="claude-opus-5-5"、および max_tokens=300 を使用して質問を送信します。
  3. 停止理由が "tool_use" の場合:Tool: 、ツールの名前、スペース、およびその入力(input)を出力し、対応する関数を実行し、アシスタントのターンと tool_result を送信して、再度モデルを呼び出します。それ以外の場合は Tool: none を出力します。
  4. 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オンラインコンパイラ