跳到主要内容

计算消息中的 Token 数量。Token 计数 API 可用于计算消息中的 Token 数量,包括工具、图像和文档,而无需创建消息。

授权

发起 REST API 请求时,必须在请求头中包含 AccessToken 以及 Content-Type 头。您可以使用以下格式进行授权:

--header 'Authorization: Bearer <your_token_here>'
--header 'Content-Type: application/json'

注意:请将 your_token_here 替换为您的实际 AccessToken。它包含允许服务器验证您的身份和权限的信息。 您可以在此处创建 API 密钥。

请求体

字段类型必填描述
modelstring用于生成的模型标识符。必须与后端提供的模型之一匹配。
messagesarray组成对话历史的消息对象数组。每个消息对象应具有 role(例如 "user"、"assistant")和 content
systemstring, object可选的系统级指令,用于引导模型行为。可以以纯字符串或结构化对象形式提供。
tool_choiceobject指定模型应如何选择工具。可以强制特定工具调用或允许模型自动决定。
toolsobject模型可调用的可用工具/函数定义。通常包括名称、描述和参数的 JSON schema。

消息配置

字段类型必填描述
rolestring消息发送者的角色(例如 "user"、"assistant")
contentstring消息的内容

工具

字段类型必填描述
namestring工具名称
descriptionstring工具描述(强烈推荐)
input_schemadict工具输入形状的 JSON Schema,模型将在 tool_use 输出内容块中生成该形状。

工具示例

[
{
"name": "get_weather",
"description": "Get the current weather information for a given city.",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The name of the city, e.g. Singapore."
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use."
},
"include_forecast": {
"type": "boolean",
"description": "Whether to include a short-term weather forecast."
}
},
"required": ["city"]
}
}
]

Tool Choice 配置

字段类型必填描述
typestring模型应如何使用提供的工具。模型可以使用特定工具、任何可用工具、自行决定或不使用工具。(autoanytoolnone
namestring仅在 type 为 tool 时使用

请求示例

{
"model": "minimax/minimax-m2.5",
"messages": [
{
"role": "user",
"content": "What is the weather in Singapore today?"
}
],
"system": "You are a helpful assistant that can use tools when needed."
"tools": [
{
"name": "get_weather",
"description": "Get the current weather information for a given city.",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The name of the city, e.g. Singapore."
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["city"]
}
}
],
"tool_choice": {
"type": "auto"
}
}

响应

成功响应

字段类型描述
input_tokensint模型实际处理的输入 Token 数。