本文へ移動
すべてのドキュメント

ガイド

ツール呼び出し

どちらのモデルも、OpenAI形式でツールを呼び出します。関数を記述すると、モデルがそのひとつを要求するので、それを実行して結果を返します。

2026年10月26日 確認

ループ

  1. toolsを送ります。それぞれ、名前、説明、JSON Schemaのパラメータを持つ関数です。
  2. モデルがツールを使いたいとき、返答のfinish_reasonはtool_callsになり、message.tool_callsに一覧が入ります。引数はJSON文字列です。
  3. それぞれを実行し、tool_call_idと結果を付けたtoolメッセージを追加します。
  4. 会話をもう一度送ります。finish_reasonがstopになるまで繰り返します。
Python
import json
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://nokeep.ai/v1",
    api_key=os.environ["NOKEEP_API_KEY"],
)

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Today's weather in a city",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

def get_weather(city):
    return {"city": city, "high_c": 21, "sky": "clear"}

messages = [{"role": "user", "content": "Should I bring a jacket in Lisbon today?"}]
while True:
    reply = client.chat.completions.create(model="deepseek-v4.1-flash-uncensored", messages=messages, tools=tools)
    message = reply.choices[0].message
    messages.append(message)
    if not message.tool_calls:
        print(message.content)
        break
    for call in message.tool_calls:
        result = get_weather(**json.loads(call.function.arguments))
        messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})

tool_choice

モデル受け付ける値
deepseek-v4.1-flash-uncensoredauto、none、required、または関数名の指定。parallel_tool_callsも使えます。
glm-5.3-flash-uncensoredautoまたはnone。ツールはモデルが自分で選びます。それ以外は400 unsupported_tool_choiceになります。

知っておくと良いこと

  • toolメッセージには、それが答えるtool_call_idが必要です。なければ400 invalid_messagesで拒否されます。
  • ツールのスキーマは毎回のリクエストに含まれ、入力トークンとして数えられます。
  • モデル自身はツールを実行しません。実行するのはあなたのコードなので、ツールが何に触れられるかはそちらで決められます。