Menu
CoddyTech

ツールの説明の記述

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

モデルがPython関数を直接見ることはありません。ツールを呼び出すかどうか、そしてどのツールを呼び出すかは、ツールのnameとdescriptionだけに基づいて判断します。Useful helper.のような説明では判断材料が得られないため、ツールを使わずに回答してしまいます。

vague = {"name": "helper", "description": "Useful helper.", "input_schema": schema}
clear = {
    "name": "get_tides",
    "description": "Get today's high and low tide times for a harbour. Use it for any question about tides.",
    "input_schema": schema,
}

When is high tide in Dover?と尋ねられると、モデルは単独でvagueと回答し(停止理由 end_turn)、clearを呼び出します(停止理由 tool_use)。

良い説明は3つのことを伝えます。ツールが何を行うか(または何を返すか)、いつ使用すべきか、そして引数に関してモデルが知っておくべきこと(単位、形式、制限など)です。各引数にも独自のdescriptionを指定してください。代役のシステムはリクエストと説明の間で単語を照合します。実際のモデルは意味を読み取りますが、説明に何も記載されていない場合は同様に失敗します。

challenge icon

チャレンジ

中級

図書館のアシスタントは説明に何も書かれていないため、lookup ツールを全く使用しません。説明を修正してください。名前、スキーマ、ループはそのまま維持してください。

  1. Looks things up. を1文または2文の説明に置き換えます。
  2. このツールが図書館分館の開館時間を取得することを記述します。
  3. 誰かが分館が開く時間や閉じる時間を尋ねたときに使用することを記述します。

ループは各ツール呼び出しと回答を出力します。例えば、入力が When does the Riverside branch open? の場合、プログラムは次のように出力します:

Tool: lookup {'branch_name': 'Riverside'}
Answer: Here is what I found: Riverside: 9 am to 6 pm

自分で試してみよう

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

client = anthropic.Anthropic()

HOURS = {"Riverside": "9 am to 6 pm", "Hill": "10 am to 4 pm", "Oak": "8 am to 8 pm"}

# モデルがこのツールをいつ使用すべきかを理解できるように、descriptionを書き換えます
TOOLS = [
    {
        "name": "lookup",
        "description": "Looks things up.",
        "input_schema": {
            "type": "object",
            "properties": {"branch_name": {"type": "string", "description": "The branch name, such as Riverside"}},
            "required": ["branch_name"],
        },
    }
]


def lookup(branch_name):
    return f"{branch_name}: {HOURS.get(branch_name, 'no such branch')}"


request = input()
messages = [{"role": "user", "content": request}]
response = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=TOOLS, messages=messages)
while response.stop_reason == "tool_use":
    messages.append({"role": "assistant", "content": response.content})
    results = []
    for block in response.content:
        if block.type == "tool_use":
            print(f"Tool: {block.name} {block.input}")
            results.append({"type": "tool_result", "tool_use_id": block.id, "content": lookup(**block.input)})
    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オンラインコンパイラ