计算消息中的 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 密钥。
请求体
| 字段 | 类型 | 必填 | 描述 |
|---|---|---|---|
model | string | 是 | 用于生成的模型标识符。必须与后端提供的模型之一匹配。 |
messages | array | 是 | 组成对话历史的消息对象数组。每个消息对象应具有 role(例如 "user"、"assistant")和 content。 |
system | string, object | 否 | 可选的系统级指令,用于引导模型行为。可以以纯字符串或结构化对象形式提供。 |
tool_choice | object | 否 | 指定模型应如何选择工具。可以强制特定工具调用或允许模型自动决定。 |
tools | object | 否 | 模型可调用的可用工具/函数定义。通常包括名称、描述和参数的 JSON schema。 |
消息配置
| 字段 | 类型 | 必填 | 描述 |
|---|---|---|---|
role | string | 是 | 消息发送者的角色(例如 "user"、"assistant") |
content | string | 是 | 消息的内容 |
工具
| 字段 | 类型 | 必填 | 描述 |
|---|---|---|---|
name | string | 是 | 工具名称 |
description | string | 否 | 工具描述(强烈推荐) |
input_schema | dict | 是 | 工具输入形状的 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 配置
| 字段 | 类型 | 必填 | 描述 |
|---|---|---|---|
type | string | 是 | 模型应如何使用提供的工具。模型可以使用特定工具、任何可用工具、自行决定或不使用工具。(auto、any、tool、none) |
name | string | 是 | 仅在 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_tokens | int | 模型实际处理的输入 Token 数。 |