関数呼び出し(ツール使用とも呼ばれます)を使うと、モデルは直接答える代わりに、関数の実行をあなたのコードに依頼できます。天気 API を調べる、データベースに問い合わせる、計算をするといった処理を行い、その結果を使って回答を仕上げます。モデル自身が何かを実行することはありません。モデルが返すのは特定の引数で関数を呼び出すよう求めるリクエストだけで、実際に実行するかどうかはあなたのコードが決めます。
リクエストの形
利用できる関数はそれぞれ JSON Schema で記述します。名前、説明、引数の形を指定します。
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name, e.g. Tokyo"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["city"],
},
},
}]
モデルはこの関数の説明とパラメーターの説明をもとに、いつどのように関数を呼び出すかを判断します。型シグネチャを書くだけでなく、他の人に関数を説明するつもりで書いてください。
完全なマルチターンの例
ツール呼び出しで会話は終わりません。モデルがいったん止まり、あなたが関数を実行し、その結果を新しいメッセージで送り返すことで、モデルは続きを生成できます。
import json
from openai import OpenAI
client = OpenAI(base_url="https://aidiscount.ai/v1", api_key=os.environ["AIDISCOUNT_API_KEY"])
def get_weather(city, unit="celsius"):
return {"city": city, "temperature": 18, "unit": unit, "condition": "cloudy"}
messages = [{"role": "user", "content": "What's the weather in Tokyo right now?"}]
resp = client.chat.completions.create(model="gpt-6-luna", messages=messages, tools=tools)
msg = resp.choices[0].message
messages.append(msg)
if msg.tool_calls:
for call in msg.tool_calls:
args = json.loads(call.function.arguments)
result = get_weather(**args)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result),
})
final = client.chat.completions.create(model="gpt-6-luna", messages=messages, tools=tools)
print(final.choices[0].message.content)
この例で押さえるべき点は次のとおりです。ツールの結果より前に、アシスタントのツール呼び出しメッセージを追加すること。各 tool_call_id を、それが応答する呼び出しと一致させること。content は文字列として送ること(オブジェクトは json.dumps でエンコードします)。
1 ターンで複数のツール呼び出し
モデルは、たとえば 3 つの都市の天気を調べるときのように、複数のツール呼び出しを一度に要求することがあります。msg.tool_calls をループし、呼び出しごとにそれぞれの tool_call_id を持つ "role": "tool" メッセージを 1 つずつ追加してから、再びモデルを呼び出します。
Anthropic Messages 形式
形が異なります。各ツールのスキーマは function.parameters ではなく input_schema に入れます。ツール呼び出しは tool_use コンテンツブロックとして届き、結果は user メッセージ内の tool_result コンテンツブロックとして送り返します。
import anthropic
client = anthropic.Anthropic(base_url="https://aidiscount.ai", api_key=os.environ["AIDISCOUNT_API_KEY"])
tools = [{
"name": "get_weather",
"description": "Get the current weather for a city",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
}]
messages = [{"role": "user", "content": "What's the weather in Tokyo right now?"}]
resp = client.messages.create(model="claude-opus-4-8", max_tokens=1024, messages=messages, tools=tools)
messages.append({"role": "assistant", "content": resp.content})
tool_use = next(b for b in resp.content if b.type == "tool_use")
result = get_weather(**tool_use.input)
messages.append({
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": tool_use.id, "content": json.dumps(result)}],
})
final = client.messages.create(model="claude-opus-4-8", max_tokens=1024, messages=messages, tools=tools)
print(final.content[0].text)
ツール使用の強制と無効化
デフォルトでは、ツールを呼び出すかどうかはモデルが判断します。特定のツールを強制するには、tool_choice={"type": "function", "function": {"name": "get_weather"}}(OpenAI 形式)または tool_choice={"type": "tool", "name": "get_weather"}(Anthropic 形式)を渡します。定義は残したまま、1 回のリクエストでだけツールを呼ばせないようにするには、tool_choice="none"(OpenAI 形式)または tool_choice={"type": "none"}(Anthropic 形式)を設定します。
よくある間違い
- ツールの結果を誤ったロールで返す。OpenAI 形式では
"role": "tool"が必要です。通常の user メッセージや assistant メッセージとしては結果を受け付けません。 - アシスタントのツール呼び出しメッセージを送り返し忘れる。モデルが結果と結びつけるには、自分のツール呼び出しが履歴に含まれている必要があります。
contentに JSON 文字列ではなく Python オブジェクトをそのまま送る。必ずjson.dumpsでシリアライズしてください。- 関数を実行する前に引数を検証しない。ツール呼び出しの引数は、モデルから来た信頼できない入力として扱ってください。特にデータベースクエリやシェルコマンドで使う前には、型と範囲を検証しましょう。
モデルの選び方
AIDiscount の現行モデルのほとんどはツール呼び出しに対応しています。マーケットプレイスのモデルページでは、機能の欄に記載されています。ツール対応モデルの例と現在の価格:
| モデル | 入力 / 100万トークン | 出力 / 100万トークン | 公式価格との比較 |
|---|---|---|---|
GPT-6 Lunagpt-6-luna | $0.05 | $0.30 | 40%オフ |
Claude Opus 4.8claude-opus-4-8 | $0.375 | $1.875 | 92.5%オフ |
DeepSeek V4 Prodeepseek-v4-pro | $0.3857 | $1.1571 | 41.6%オフ |
Kimi K3kimi-k3 | $1.714 | $8.571 | 42.9%オフ |
GLM-5.3glm-5.3 | $0.6857 | $2.40 | 45.5%オフ |
無料でアカウント作成API ドキュメントを読む