深空 API文档
文档导航

Responses

提交文本或结构化输入,读取模型生成的输出项。

POST /v1/responses

请求头使用 Authorization: Bearer <API Key>。使用令牌页创建的本站 Key。提交 JSON 时带上 Content-Type: application/json。

本页目录

请求示例#

在本机环境中设置 XKRELAY_API_KEY。

在 Bash 或 Zsh 中运行。将 YOUR_MODEL_ID 换成分组内可用的完整模型 ID。

printf 'Authorization: Bearer %s\n' "$XKRELAY_API_KEY" |
  curl -sS --fail-with-body --max-time 60 https://xkrelay.com/v1/responses \
    -H @- -H 'Content-Type: application/json' \
    -d '{"model":"YOUR_MODEL_ID","input":"只回复 OK"}'

Python 3.10+。另设置 XKRELAY_MODEL。保存为 request.py,运行 python3 request.py。

import json
import os
from urllib.request import Request, urlopen

body = {"model": os.environ["XKRELAY_MODEL"], "input": "只回复 OK"}
request = Request("https://xkrelay.com/v1/responses", method="POST",
                  data=json.dumps(body).encode(), headers={
    "Authorization": "Bearer " + os.environ["XKRELAY_API_KEY"],
    "Content-Type": "application/json",
})
with urlopen(request, timeout=60) as response:
    result = json.load(response)
if result["status"] != "completed":
    raise SystemExit("Responses 状态:" + result["status"])
for item in result["output"]:
    if item["type"] == "message":
        for part in item["content"]:
            if part["type"] == "output_text":
                print(part["text"])

Node.js 22+。另设置 XKRELAY_MODEL。保存为 request.mjs,运行 node request.mjs。

const response = await fetch('https://xkrelay.com/v1/responses', {
  method: 'POST',
  headers: {
    Authorization: 'Bearer ' + process.env.XKRELAY_API_KEY,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ model: process.env.XKRELAY_MODEL, input: '只回复 OK' }),
  signal: AbortSignal.timeout(60_000),
});
if (!response.ok) throw new Error('HTTP ' + response.status);
const result = await response.json();
if (result.status !== 'completed') throw new Error('Responses 状态:' + result.status);
for (const item of result.output) {
  if (item.type !== 'message') continue;
  for (const part of item.content) {
    if (part.type === 'output_text') console.log(part.text);
  }
}

响应示例#

HTTP 200
{
  "id": "resp_example",
  "object": "response",
  "status": "completed",
  "model": "YOUR_MODEL_ID",
  "output": [{
    "type": "message",
    "role": "assistant",
    "content": [{
      "type": "output_text",
      "text": "OK",
      "annotations": []
    }]
  }],
  "usage": {
    "input_tokens": 11,
    "output_tokens": 1,
    "total_tokens": 12
  }
}

普通文本响应中,遍历 output 的消息项,再取内容块中 type 为 output_text 的 text。推理和工具调用也可能出现在 output 中,读取时按 type 区分。

检查 status 确认请求是否完成。usage 给出用量,实际扣费见调用日志。

请求参数#

参数说明
modelstring · 必填分组内支持 Responses 的完整模型 ID。
inputstring / array · 必填简单文本使用字符串。传入多条消息或结构化内容时使用输入项数组。
instructionsstring · 可选提供本次请求的系统或开发者指令。
max_output_tokensinteger · 可选输出 Token 上限,包含模型的推理 Token,按模型支持的范围填写。
streamboolean · 可选设为 true 时返回 SSE 事件。

流式响应#

请求加入 "stream": true。按事件类型处理数据。response.output_text.delta 的 delta 是文本增量,下面节选一个增量和一个完成事件。

event: response.output_text.delta
data: {"type":"response.output_text.delta","output_index":0,"content_index":0,"delta":"OK"}

event: response.completed
data: {"type":"response.completed","response":{"id":"resp_example","status":"completed"}}

收到 response.completed 时确认完成。response.incomplete 表示输出未完成,可查看 incomplete_details。收到 response.failed 或错误帧时读取错误信息。连接在终态前关闭时,将请求标记为未完成。事件字段见 OpenAI 流式响应指南。