跳到主要内容

基于对话从大语言模型生成响应。

授权

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

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

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

请求体

字段类型必填描述
modelstring用于生成的模型标识符。必须与后端提供的模型之一匹配。
max_tokensinteger响应中生成的最大 Token 数。(最小值:1)
messagesarray组成对话历史的消息对象数组。每个消息对象应具有 role(例如 "user"、"assistant")和 content
metadataobject描述请求元数据的对象。
stop_sequencesarray一个或多个序列,API 遇到这些序列时将停止生成更多 Token。返回的文本不包含停止序列。
streambool是否在生成时以流式方式返回响应(默认:false)
systemstring, object可选的系统级指令,用于引导模型行为。可以以纯字符串或结构化对象形式提供。
temperaturenumber, null控制生成中的随机性。较高值(如 0.8)产生更多样的输出,较低值(如 0.2)使输出更确定。取值范围:0<=x<=2
tool_choiceobject指定模型应如何选择工具。可以强制特定工具调用或允许模型自动决定。
toolsobject模型可调用的可用工具/函数定义。通常包括名称、描述和参数的 JSON schema。
top_kinteger, null生成响应时保留的最高概率 Token 数量,用于 top-k 采样,有助于控制输出的随机性。取值范围:0 <= x <= 100
top_pnumber, null生成响应时使用的核采样概率,有助于在生成的响应中平衡随机性和连贯性。取值范围:0<=x<=1

消息配置

字段类型必填描述
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",
"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"
}
}

响应

成功响应

字段类型描述
idstring消息响应的唯一标识符。通常以 msg_ 为前缀,由服务器生成。
typestring对象类型。Anthropic Messages API 响应始终为 "message"。
rolestring消息作者的角色。模型生成的响应始终为 "assistant"。
modelstring用于生成响应的模型名称
contentarray模型生成的响应选择数组。每个选择包括生成的消息、完成原因和选择的索引。
stop_reasonstring模型停止生成的原因。
usageobject包含请求和响应 Token 使用信息的对象,包括提示词中的 Token 数、补全中的 Token 数和使用的总 Token 数。