基于对话从大语言模型生成响应。
授权
发起 REST API 请求时,必须在请求头中包含 AccessToken 以及 Content-Type 头。您可以使用以下格式进行授权:
--header 'Authorization: Bearer <your_token_here>'
--header 'Content-Type: application/json'
注意:请将 your_token_here 替换为您的实际 AccessToken。它包含允许服务器验证您的身份和权限的信息。
您可以在此处创建 API 密钥。
请求体
| 字段 | 类型 | 必填 | 描述 |
|---|---|---|---|
model | string | 是 | 用于生成的模型标识符。必须与后端提供的模型之一匹配。 |
max_tokens | integer | 否 | 响应中生成的最大 Token 数。(最小值:1) |
messages | array | 是 | 组成对话历史的消息对象数组。每个消息对象应具有 role(例如 "user"、"assistant")和 content。 |
metadata | object | 否 | 描述请求元数据的对象。 |
stop_sequences | array | 否 | 一个或多个序列,API 遇到这些序列时将停止生成更多 Token。返回的文本不包含停止序列。 |
stream | bool | 否 | 是否在生成时以流式方式返回响应(默认:false) |
system | string, object | 否 | 可选的系统级指令,用于引导模型行为。可以以纯字符串或结构化对象形式提供。 |
temperature | number, null | 否 | 控制生成中的随机性。较高值(如 0.8)产生更多样的输出,较低值(如 0.2)使输出更确定。取值范围:0<=x<=2 |
tool_choice | object | 否 | 指定模型应如何选择工具。可以强制特定工具调用或允许模型自动决定。 |
tools | object | 否 | 模型可调用的可用工具/函数定义。通常包括名称、描述和参数的 JSON schema。 |
top_k | integer, null | 否 | 生成响应时保留的最高概率 Token 数量,用于 top-k 采样,有助于控制输出的随机性。取值范围:0 <= x <= 100 |
top_p | number, null | 否 | 生成响应时使用的核采样概率,有助于在生成的响应中平衡随机性和连贯性。取值范围:0<=x<=1 |
消息配置
| 字段 | 类型 | 必填 | 描述 |
|---|---|---|---|
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",
"max_tokens": 128,
"messages": [
{
"role": "user",
"content": "What is the weather in Singapore today?"
}
],
"metadata": {
"request_id": "test-123"
},
"stop_sequences": ["\n\n"],
"system": "You are a helpful assistant that can use tools when needed."
"stream": false,
"temperature": 0.7,
"top_k": 50,
"top_p": 0.9,
"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"
}
}
响应
成功响应
| 字段 | 类型 | 描述 |
|---|---|---|
id | string | 消息响应的唯一标识符。通常以 msg_ 为前缀,由服务器生成。 |
type | string | 对象类型。Anthropic Messages API 响应始终为 "message"。 |
role | string | 消息作者的角色。模型生成的响应始终为 "assistant"。 |
model | string | 用于生成响应的模型名称 |
content | array | 模型生成的响应选择数组。每个选择包括生成的消息、完成原因和选择的索引。 |
stop_reason | string | 模型停止生成的原因。 |
usage | object | 包含请求和响应 Token 使用信息的对象,包括提示词中的 Token 数、补全中的 Token 数和使用的总 Token 数。 |