Menu
CoddyTech

ツール結果の連携

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

多くのリクエストでは、2つのツールを連続して使用する必要があり、2つ目のツールは1つ目のツールが返した結果を必要とします。What is the price of the kettle with tax? と尋ねられたとき、get_price ツールと add_tax ツールを持つモデルは、まだ add_tax を呼び出すことができません。価格が得られていないためです。モデルはまず get_price を呼び出し、結果に含まれる 30 dollars を読み取ってから、その数値を使って add_tax を呼び出します。

get_price {'item': 'kettle'}
add_tax {'price': 30}

ループのおかげで、事前に把握していなくてもこれが可能になります:stop_reason が "tool_use" である間、各結果を送信し返して処理を続けます。1ラウンドしか処理しないループは get_price の後に停止し、2回目の呼び出しが行われることはありません。

チェーンを機能させるには2つのポイントがあります。モデルは結果を次の呼び出しにコピーするため、結果の数値は明確に記述する必要があります(内部コードではなく 30 dollars など)。また、数値を受け取る引数は、モデルが関数で計算処理できる値を渡せるように、"type": "number" または "integer" にする必要があります。

challenge icon

チャレンジ

難しい

電車の案内アシスタントはユーロでの運賃を検索できます。モデルが2つのツールを連鎖して呼び出せるように、それを変換するツールを追加してください。

  1. TOOLS に convert_euros という名前のツールを追加します。説明は Convert an amount in euros to another currency. とし、2つの必須プロパティを含めます:amount(数値、ユーロでの金額)および currency(文字列、通貨コード:USD、JPY、またはGBP)。
  2. convert_euros(amount, currency) を作成します。これは <amount> euros is <converted> <currency> を返します。ここで、変換後の値は amount * RATES[currency] を小数点以下2桁で計算したものです。
  3. それを FUNCTIONS に追加します。

例えば、入力 What does the train to Lyon cost in dollars? に対して、プログラムは以下を出力します:

Step 1: get_fare {'destination': 'Lyon'}
Step 2: convert_euros {'amount': 48, 'currency': 'USD'}
Answer: Here is what I found:
- Lyon: 48 euros
- 48 euros is 52.32 USD

自分で試してみよう

import coddy_ai as anthropic  # 本番環境: import anthropic

client = anthropic.Anthropic()

FARES = {"Lyon": 48, "Nice": 95, "Lille": 35, "Bordeaux": 70}
RATES = {"USD": 1.09, "JPY": 162.0, "GBP": 0.86}

TOOLS = [
    {
        "name": "get_fare",
        "description": "Get the price of a train ticket from Paris to a destination, in euros.",
        "input_schema": {
            "type": "object",
            "properties": {"destination": {"type": "string", "description": "The destination city"}},
            "required": ["destination"],
        },
    },
    # ここに convert_euros ツールを追加してください
]


def get_fare(destination):
    return f"{destination}: {FARES[destination]} euros"


# ここに convert_euros(amount, currency) を作成してください

FUNCTIONS = {"get_fare": get_fare}

request = input()
messages = [{"role": "user", "content": request}]
response = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=TOOLS, messages=messages)
step = 0
while response.stop_reason == "tool_use":
    messages.append({"role": "assistant", "content": response.content})
    results = []
    for block in response.content:
        if block.type == "tool_use":
            step += 1
            print(f"Step {step}: {block.name} {block.input}")
            output = FUNCTIONS[block.name](**block.input)
            results.append({"type": "tool_result", "tool_use_id": block.id, "content": output})
    messages.append({"role": "user", "content": results})
    response = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=TOOLS, messages=messages)

for block in response.content:
    if block.type == "text":
        print("Answer: " + block.text)
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

ツールとエージェントのすべてのレッスン

自分で練習してみよう: Pythonオンラインコンパイラ