产品文档

Responses API 协议指南

更新于 2026-12-03

Tengri 大模型完整兼容 OpenAI Responses API(POST /v1/responses 与 POST /v1/responses/input_tokens),支持直接通过新版 OpenAI SDK 的 client.responses.create() 进行多模态交互、结构化输出、函数调用与事件驱动流式输出。

端点与基址

Base URL:https://cloud.heimori.cn/v1(同步挂载于 DashScope 兼容基址 https://cloud.heimori.cn/compatible-mode/v1)。

方法路径说明是否计费
POST/v1/responses创建模型响应(支持非流式 JSON 与事件流 SSE)按 token 计费
POST/v1/responses/input_tokens估算输入文本与系统指令的 token 数不计费
POST/compatible-mode/v1/responsesDashScope 兼容前缀响应接口按 token 计费
POST/compatible-mode/v1/responses/input_tokensDashScope 兼容前缀输入 token 估算不计费

鉴权与请求头

请求头必填示例与说明
Authorization条件必填Bearer sk-heimori-...(与 api-key 二选一)
api-key条件必填sk-heimori-...(兼容型鉴权头,未传 Authorization 时生效)
Content-Type必填application/json

POST /v1/responses 请求参数

参数类型必填说明
modelstring必填目标模型名称(如 tengri-2、tengri-2-max),可用列表见 GET /v1/models
inputstring | object[]必填单轮纯文本提问可直接传字符串;多轮对话、多模态内容或工具回传请传入 InputItem 数组
instructionsstring可选系统级指令,等效于 Chat Completions 的 system 消息,优先级高于 input 中的普通消息
streamboolean可选设为 true 时以 text/event-stream SSE 推送 Responses 生命周期事件,末尾输出 data: [DONE]默认值false
max_output_tokensinteger可选限制单次响应的最大输出 token 数(≥ 1);达到上限时响应 status 为 incomplete 且 incomplete_details.reason 为 max_output_tokens
temperaturenumber可选采样温度(0.0 ~ 2.0),建议与 top_p 二选一调节
top_pnumber可选核采样概率阈值(0.0 ~ 1.0)
toolsobject[]可选函数工具定义数组;与 Chat Completions 不同,字段扁平展开:{ "type": "function", "name": "...", "description"?: "...", "parameters"?: {...}, "strict"?: boolean }
tool_choicestring | object可选工具选择策略,或传 { "type": "function", "name": "fn_name" } 强制指定函数(仅传入 tools 时默认 auto)可选值auto默认nonerequired
parallel_tool_callsboolean可选是否允许单轮输出多个 function_call 条目默认值true
textobject可选输出格式约束对象 { "format": { "type": ... } };json_schema 另需传 name、schema 与可选 strict可选值text默认json_objectjson_schema
userstring可选调用方终端用户唯一标识
metadataRecord<string, string>可选自定义字符串键值元数据

input 条目与多模态内容块类型

当 input 为数组时,支持混合传入以下条目类型与内容分段:

条目 / 内容块 typeJSON 结构说明
message(或省略 type){ "role": "user", "content": "..." }标准对话消息条目,content 支持字符串或内容块数组可选 roleuserassistantsystemdeveloper
input_text{ "type": "input_text", "text": "请提取关键信息" }用户输入文本内容块(放在 message.content[] 内)
output_text{ "type": "output_text", "text": "历史助手回复" }多轮对话回传助手历史消息时的文本块
input_image{ "type": "input_image", "image_url": "https://... 或 data:image/jpeg;base64,...", "detail"?: "auto" }图片输入块,支持公网 HTTPS URL 或 Base64 Data URI图片格式JPEGPNGWebPGIF可选值auto默认lowhigh
input_file{ "type": "input_file", "filename": "report.pdf", "file_data": "data:application/pdf;base64,..." }文档附件输入块(PDF 等 Base64 Data URI,请求体总上限 200 MB)
function_call{ "type": "function_call", "call_id": "call_...", "name": "get_weather", "arguments": "{\"city\":\"呼和浩特\"}" }多轮工具调用中回传上一轮模型发出的函数调用条目
function_call_output{ "type": "function_call_output", "call_id": "call_...", "output": "{\"temp\":18}" }回传本地函数执行结果,call_id 须与对应的 function_call.call_id 一致

POST /v1/responses/input_tokens 输入 Token 估算

在发起长上下文调用前预估输入 token 数。本接口不计费且在账户余额为 0 时仍可调用(按 input 文本与 instructions 系统指令估算字符折算 token 数)。

参数类型必填说明
modelstring必填模型名(缺省返回 400 missing_model)
inputstring | object[]可选待估算的文本或 Responses 消息条目数组
instructionsstring可选可选系统指令文本,将一并计入估算结果

响应结构:

JSON
{
  "object": "response.input_tokens",
  "input_tokens": 28
}

非流式响应字段(object: "response")

字段类型说明
idstring响应唯一 ID(如 resp_20261128001)
objectstring固定为 response
created_atinteger响应创建的 Unix 秒级时间戳
statusstring响应终态:completed 完成、incomplete 输出截断或触发安全过滤、failed 失败可选值completedincompletefailed
incomplete_detailsobject可选;仅 status: "incomplete" 时返回
└reasonstring中断原因可选值max_output_tokenscontent_filter
modelstring请求的模型名称
outputobject[]输出条目数组,含以下两类条目
└message 条目object文本消息输出:{ id, type: "message", status: "completed", role: "assistant", content: [{ type: "output_text", text, annotations: [] }] }
└function_call 条目object函数调用输出:{ id, type: "function_call", status: "completed", call_id, name, arguments }
usageobject本次请求的 token 用量统计
└input_tokensinteger输入 token 消耗
└output_tokensinteger输出 token 消耗(含深度推理过程消耗)
└total_tokensinteger总 token 消耗(input_tokens + output_tokens)
└input_tokens_detailsobject可选;命中提示词缓存时包含 cached_tokens(整数)

流式 SSE 事件规范(stream: true)

开启 stream: true 后,服务端以 Content-Type: text/event-stream 按序推送以下 JSON 事件,末尾以 data: [DONE]\n\n 结束:

事件 type触发时机与关键字段
response.created响应初始化完成,携带 response: { id, object: "response", status: "in_progress", model, output: [] }
response.in_progress响应进入生成阶段(部分模型在 response.created 后发出)
response.output_item.added新增输出条目(output_index + item,类型为 message 或 function_call)
response.content_part.added消息条目内新增内容分段(output_index、content_index、part: { type: "output_text", text: "", annotations: [] })
response.output_text.delta文本正文增量块(output_index、content_index、delta),客户端依次拼接 delta
response.output_text.done当前分段文本生成完成,携带完整聚合文本 text
response.content_part.done当前内容分段完成(携带最终 part)
response.function_call_arguments.delta函数调用参数 JSON 字符串增量块(output_index、delta)
response.function_call_arguments.done函数调用参数生成完成,携带完整 JSON 字符串 arguments
response.output_item.done当前输出条目完成(携带完整 item)
response.completed全部生成正常完成,response 内含完整 output[] 与最终 usage
response.incomplete因达 max_output_tokens 或触发内容安全策略而提前结束,response.incomplete_details.reason 标明原因
response.failed生成阶段失败,携带 response 失败状态详情
error流式传输中途发生服务端错误时下发 { type: "error", error: { type, code, message } }

典型 SSE 帧序列示例:

Text
data: {"type":"response.created","response":{"id":"resp_a1b2c3","object":"response","created_at":1795852900,"status":"in_progress","model":"tengri-2","output":[]}}

data: {"type":"response.output_item.added","output_index":0,"item":{"id":"msg_a1b2c3","type":"message","status":"in_progress","role":"assistant","content":[]}}

data: {"type":"response.content_part.added","output_index":0,"content_index":0,"part":{"type":"output_text","text":"","annotations":[]}}

data: {"type":"response.output_text.delta","output_index":0,"content_index":0,"delta":"ᠲᠠ ᠪᠦᠬᠦᠨ ᠳᠦ ᠮᠡᠨᠳᠦ ᠬᠦᠷᠭᠡᠶ᠎ᠡ᠃"}

data: {"type":"response.output_text.done","output_index":0,"content_index":0,"text":"ᠲᠠ ᠪᠦᠬᠦᠨ ᠳᠦ ᠮᠡᠨᠳᠦ ᠬᠦᠷᠭᠡᠶ᠎ᠡ᠃"}

data: {"type":"response.output_item.done","output_index":0,"item":{"id":"msg_a1b2c3","type":"message","status":"completed","role":"assistant","content":[{"type":"output_text","text":"ᠲᠠ ᠪᠦᠬᠦᠨ ᠳᠦ ᠮᠡᠨᠳᠦ ᠬᠦᠷᠭᠡᠶ᠎ᠡ᠃","annotations":[]}]}}

data: {"type":"response.completed","response":{"id":"resp_a1b2c3","object":"response","created_at":1795852900,"status":"completed","model":"tengri-2","output":[{"id":"msg_a1b2c3","type":"message","status":"completed","role":"assistant","content":[{"type":"output_text","text":"ᠲᠠ ᠪᠦᠬᠦᠨ ᠳᠦ ᠮᠡᠨᠳᠦ ᠬᠦᠷᠭᠡᠶ᠎ᠡ᠃","annotations":[]}]}],"usage":{"input_tokens":14,"output_tokens":18,"total_tokens":32}}}

data: [DONE]

错误响应格式

Responses 端点遵循 OpenAI 统一 JSON 错误体,并在响应头附带 X-Request-Id(完整错误码对照见错误码):

JSON
{
  "error": {
    "message": "Missing required parameter: model",
    "type": "invalid_request_error",
    "param": null,
    "code": "missing_model"
  }
}

代码示例

1. cURL:非流式响应与 Token 估算

Bash / cURL
# 估算输入 token(不计费)
curl -X POST "https://cloud.heimori.cn/v1/responses/input_tokens" \
  -H "Authorization: Bearer sk-heimori-..." \
  -H "Content-Type: application/json" \
  -d '{
    "model": "tengri-2",
    "instructions": "使用传统蒙古文作答,并附中文释义。",
    "input": "请写一句草原迎客祝福语。"
  }'

# 发起非流式响应
curl -X POST "https://cloud.heimori.cn/v1/responses" \
  -H "Authorization: Bearer sk-heimori-..." \
  -H "Content-Type: application/json" \
  -d '{
    "model": "tengri-2",
    "instructions": "使用传统蒙古文作答,并附中文释义。",
    "input": "请写一句草原迎客祝福语。",
    "max_output_tokens": 512,
    "temperature": 0.6
  }'

响应示例:

JSON
{
  "id": "resp_202611280091",
  "object": "response",
  "created_at": 1795852910,
  "status": "completed",
  "model": "tengri-2",
  "output": [
    {
      "id": "msg_202611280091",
      "type": "message",
      "status": "completed",
      "role": "assistant",
      "content": [
        {
          "type": "output_text",
          "text": "ᠲᠠᠯ᠎ᠠ ᠨᠤᠲᠤᠭ ᠲᠤ ᠮᠣᠷᠢᠯᠠᠨ ᠢᠷᠡᠭᠰᠡᠨ ᠳᠦ ᠲᠠᠨᠢ ᠪᠠᠶᠠᠷ ᠬᠦᠷᠭᠡᠶ᠎ᠡ᠃\n中文释义:热烈欢迎您莅临草原故乡。",
          "annotations": []
        }
      ]
    }
  ],
  "usage": {
    "input_tokens": 28,
    "output_tokens": 41,
    "total_tokens": 69
  }
}

2. Python(OpenAI SDK):流式输出与函数调用两轮闭环

Python
import json
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["XM_API_KEY"],
    base_url="https://cloud.heimori.cn/v1",
)

# 1. 流式消费 response.output_text.delta
stream = client.responses.create(
    model="tengri-2",
    instructions="简明作答。",
    input="传统蒙古文的书写方向是什么?",
    stream=True,
)
for event in stream:
    if event.type == "response.output_text.delta":
        print(event.delta, end="", flush=True)
    elif event.type == "response.completed":
        print(f"\n[usage] {event.response.usage}")

# 2. 函数调用闭环(function_call -> function_call_output)
tools = [
    {
        "type": "function",
        "name": "lookup_festival_date",
        "description": "查询指定地区那达慕大会举办日期",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    }
]

conversation = [{"role": "user", "content": "查一下锡林浩特今年那达慕是什么时候?"}]
r1 = client.responses.create(model="tengri-2", input=conversation, tools=tools)

for item in r1.output:
    if item.type == "function_call":
        args = json.loads(item.arguments)
        result = {"city": args["city"], "date": "2026-07-18"}
        conversation.append(
            {
                "type": "function_call",
                "call_id": item.call_id,
                "name": item.name,
                "arguments": item.arguments,
            }
        )
        conversation.append(
            {
                "type": "function_call_output",
                "call_id": item.call_id,
                "output": json.dumps(result, ensure_ascii=False),
            }
        )

r2 = client.responses.create(model="tengri-2", input=conversation, tools=tools)
print(r2.output_text)

3. Node.js(OpenAI SDK):结构化输出与多模态图片理解

JavaScript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.XM_API_KEY,
  baseURL: "https://cloud.heimori.cn/v1",
});

// 1. JSON Schema 结构化输出
const structured = await client.responses.create({
  model: "tengri-2-pro",
  input: "分析文本并提取语种与主题:ᠰᠠᠶᠢᠨ ᠪᠠᠶᠢᠨ᠎ᠠ ᠤᠤ,今天天气真好。",
  text: {
    format: {
      type: "json_schema",
      name: "text_analysis",
      strict: true,
      schema: {
        type: "object",
        properties: {
          languages: { type: "array", items: { type: "string" } },
          topic: { type: "string" },
        },
        required: ["languages", "topic"],
        additionalProperties: false,
      },
    },
  },
});
console.log("结构化输出:", structured.output_text);

// 2. 多模态图片理解(input_image)
const vision = await client.responses.create({
  model: "tengri-2-max",
  input: [
    {
      role: "user",
      content: [
        { type: "input_text", text: "识别图片中的文字并翻译为中文。" },
        { type: "input_image", image_url: "https://example.com/signboard.jpg", detail: "high" },
      ],
    },
  ],
});
console.log("图片识别:", vision.output_text);