文本 API

Chat 对话、Legacy 补全与 Responses 对话,支持 SSE 流式输出

文本接口提供 Chat 对话、Legacy 文本补全与 Responses 对话三种能力,均支持非流式 JSON 与 SSE 流式输出。

能力概述

云擎数智 网关提供三条文本类接口,按场景选择:

接口适用场景端点类型
POST /v1/chat/completions通用对话、工具调用、多模态输入(图片等)、流式 SSEopenai
POST /v1/completionsLegacy 文本补全(prompt → 续写)openai
POST /v1/responses多轮 Responses 对话、推理、结构化输出、工具调用openai-response

选型建议

  • 大多数新项目优先使用 Chat Completions/v1/chat/completions)。
  • 仅需单段 prompt 续写、对接旧版 SDK 时使用 Completions/v1/completions)。
  • 需要 previous_response_id 多轮上下文、Responses 结构化输出时使用 Responses/v1/responses)。

前置条件model 须为 模型 API 返回列表中、supported_endpoint_types 含对应端点类型的模型。

通用约定:鉴权、Base URL、Content-Type 等见 通用约定。错误响应格式见 通用约定

接口一览

方法路径说明
POST/v1/chat/completionsChat 对话(支持 SSE 流式)
POST/v1/completions文本补全
POST/v1/responsesResponses 对话

POST /v1/chat/completions

Chat 对话,支持非流式 JSON 与 SSE 流式。

鉴权:Bearer Token

Content-Typeapplication/json

请求体

{
  "model": "gpt-4o",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "Hello, who are you?"
    }
  ],
  "temperature": 0.7,
  "max_tokens": 1024,
  "stream": false
}

请求字段

字段类型必填说明
modelstring模型 ID
messagesarray对话消息列表
messages[].rolestringsystem / user / assistant / tool
messages[].contentstring / array文本或多模态内容
streambooleantrue 时返回 SSE 流,默认 false
temperaturenumber采样温度,0–2
top_pnumber核采样,0–1
max_tokensinteger最大输出 Token 数
max_completion_tokensinteger最大补全 Token 数
stopstring / array停止序列
toolsarray工具定义列表
tool_choicestring / object工具选择策略
response_formatobject结构化输出格式
stream_optionsobject流式选项,如 { "include_usage": true }

多模态消息示例(图片输入):

{
  "model": "gpt-4o",
  "messages": [
    {
      "role": "user",
      "content": [
        { "type": "text", "text": "What's in this image?" },
        {
          "type": "image_url",
          "image_url": { "url": "https://example.com/image.jpg" }
        }
      ]
    }
  ]
}

请求示例(curl)

非流式:

curl https://www.yunsell.com/v1/chat/completions \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Hello, who are you?"}
    ],
    "temperature": 0.7,
    "max_tokens": 1024,
    "stream": false
  }'

流式(SSE):

curl -N https://www.yunsell.com/v1/chat/completions \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "Hello"}],
    "stream": true
  }'

响应体(非流式)

HTTP 200Content-Type: application/json

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1712697600,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! I'm an AI assistant. How can I help you today?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 28,
    "completion_tokens": 15,
    "total_tokens": 43
  }
}

响应字段

字段类型说明
idstring响应 ID,如 chatcmpl-xxx
objectstring固定为 chat.completion
createdintegerUnix 时间戳(秒)
modelstring实际使用的模型
choicesarray生成结果列表,长度等于请求中的 n(默认 1)
choices[].indexinteger结果索引,从 0 开始
choices[].message.rolestring固定为 assistant
choices[].message.contentstring / null回复文本;工具调用时可能为 null
choices[].message.tool_callsarray工具调用列表(启用 tools 时)
choices[].message.tool_calls[].idstring工具调用 ID
choices[].message.tool_calls[].typestring固定为 function
choices[].message.tool_calls[].function.namestring函数名
choices[].message.tool_calls[].function.argumentsstring函数参数 JSON 字符串
choices[].message.reasoning_contentstring推理过程文本(推理模型)
choices[].finish_reasonstring结束原因,见下表
usageobjectToken 用量,见 公共响应对象

finish_reason 取值

说明
stop自然结束或命中 stop 序列
length达到 max_tokens 上限
tool_calls模型发起工具调用
content_filter内容被安全策略拦截

工具调用响应示例

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1712697600,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call_abc123",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{\"location\":\"Beijing\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ],
  "usage": {
    "prompt_tokens": 50,
    "completion_tokens": 20,
    "total_tokens": 70
  }
}

响应体(流式)

HTTP 200Content-Type: text/event-stream

请求体设置 "stream": true 后,服务端以 SSE 推送,每条消息格式:

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1712697600,"model":"gpt-4o","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1712697600,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1712697600,"model":"gpt-4o","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":28,"completion_tokens":15,"total_tokens":43}}

data: [DONE]

流式 chunk 字段

字段类型说明
idstring与最终响应相同的 ID
objectstring固定为 chat.completion.chunk
createdintegerUnix 时间戳(秒)
modelstring使用的模型
system_fingerprintstring系统指纹(部分模型返回)
choices[].indexinteger结果索引
choices[].delta.rolestring首条 chunk 可能含 assistant
choices[].delta.contentstring增量文本片段
choices[].delta.reasoning_contentstring增量推理文本(推理模型)
choices[].delta.tool_callsarray增量工具调用片段
choices[].finish_reasonstring / null末条 chunk 为结束原因,其余为 null
usageobject仅末条 chunk 出现(需 stream_options.include_usage=true

流式结束标志:最后一行 data: [DONE]


POST /v1/completions

Legacy 文本补全接口。

鉴权:Bearer Token

Content-Typeapplication/json

请求体

{
  "model": "gpt-4o",
  "prompt": "Say this is a test",
  "max_tokens": 100,
  "temperature": 0.7,
  "stream": false
}

请求字段

字段类型必填说明
modelstring模型 ID
promptstring / array提示文本
max_tokensinteger最大输出 Token 数
temperaturenumber采样温度
top_pnumber核采样
ninteger生成数量,默认 1
streamboolean是否流式
stopstring / array停止序列

请求示例(curl)

非流式:

curl https://www.yunsell.com/v1/completions \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "prompt": "Say this is a test",
    "max_tokens": 100,
    "temperature": 0.7,
    "stream": false
  }'

流式(SSE):

curl -N https://www.yunsell.com/v1/completions \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "prompt": "Say this is a test",
    "stream": true
  }'

响应体(非流式)

HTTP 200

{
  "id": "cmpl-abc123",
  "object": "text_completion",
  "created": 1712697600,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "text": "This is indeed a test.",
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 6,
    "total_tokens": 11
  }
}

响应字段

字段类型说明
idstring响应 ID,如 cmpl-xxx
objectstring固定为 text_completion
createdintegerUnix 时间戳(秒)
modelstring使用的模型
choicesarray生成结果列表
choices[].indexinteger结果索引
choices[].textstring补全文本
choices[].finish_reasonstringstop / length / content_filter
usageobjectToken 用量,见 公共响应对象

响应体(流式)

data: {"id":"cmpl-abc123","object":"text_completion","created":1712697600,"choices":[{"index":0,"text":"This","finish_reason":null}]}

data: {"id":"cmpl-abc123","object":"text_completion","created":1712697600,"choices":[{"index":0,"text":" is","finish_reason":null}]}

data: [DONE]

流式 chunk 字段

字段类型说明
idstring响应 ID
objectstring固定为 text_completion
createdintegerUnix 时间戳(秒)
choices[].indexinteger结果索引
choices[].textstring增量文本片段
choices[].finish_reasonstring / null末条 chunk 为结束原因

POST /v1/responses

Responses 对话接口,支持多轮对话、工具调用、推理等。

鉴权:Bearer Token

Content-Typeapplication/json

请求体

{
  "model": "gpt-4o",
  "input": [
    {
      "role": "user",
      "content": [
        {
          "type": "input_text",
          "text": "Explain quantum computing in one sentence."
        }
      ]
    }
  ],
  "temperature": 0.7,
  "max_output_tokens": 1024,
  "stream": false
}

请求字段

字段类型必填说明
modelstring模型 ID
inputstring / array输入内容;可为字符串或消息数组
instructionsstring系统指令
temperaturenumber采样温度
max_output_tokensinteger最大输出 Token 数
streamboolean是否流式
toolsarray工具定义
tool_choicestring / object工具选择策略
previous_response_idstring上一轮响应 ID,用于多轮对话
storeboolean是否存储对话
metadataobject自定义元数据

请求示例(curl)

非流式:

curl https://www.yunsell.com/v1/responses \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "input": [
      {
        "role": "user",
        "content": [
          {"type": "input_text", "text": "Explain quantum computing in one sentence."}
        ]
      }
    ],
    "temperature": 0.7,
    "max_output_tokens": 1024,
    "stream": false
  }'

流式(SSE):

curl -N https://www.yunsell.com/v1/responses \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "input": "Hello",
    "stream": true
  }'

响应体(非流式)

HTTP 200

{
  "id": "resp_abc123",
  "object": "response",
  "created_at": 1712697600,
  "status": "completed",
  "model": "gpt-4o",
  "output": [
    {
      "type": "message",
      "id": "msg_abc123",
      "status": "completed",
      "role": "assistant",
      "content": [
        {
          "type": "output_text",
          "text": "Quantum computing uses quantum bits that can exist in superposition to perform certain calculations exponentially faster than classical computers.",
          "annotations": []
        }
      ]
    }
  ],
  "usage": {
    "input_tokens": 15,
    "output_tokens": 28,
    "total_tokens": 43
  },
  "temperature": 0.7,
  "top_p": 1.0
}

响应字段

字段类型说明
idstring响应 ID,如 resp_xxx
objectstring固定为 response
created_atintegerUnix 时间戳(秒)
statusstring响应状态,见下表
modelstring使用的模型
outputarray输出项列表
output[].typestring输出类型,见下表
output[].idstring输出项 ID
output[].statusstring输出项状态
output[].rolestring角色(message 类型时为 assistant
output[].contentarray内容块列表
output[].content[].typestring内容类型,如 output_text
output[].content[].textstring文本内容
output[].content[].annotationsarray注释/引用列表
output[].call_idstring工具调用 ID(function_call 类型)
output[].namestring函数名(function_call 类型)
output[].argumentsstring / object函数参数(function_call 类型)
output[].qualitystring图像质量(image_generation_call 类型)
output[].sizestring图像尺寸(image_generation_call 类型)
instructionsstring系统指令(回显)
temperaturenumber采样温度(回显)
top_pnumber核采样(回显)
max_output_tokensinteger最大输出 Token(回显)
previous_response_idstring上一轮响应 ID
parallel_tool_callsboolean是否并行工具调用
storeboolean是否存储对话
metadataobject自定义元数据
incomplete_details.reasonstring未完成原因(status=incomplete 时)
errorobject错误信息(status=failed 时)
usageobjectToken 用量,见 公共响应对象

status 取值

说明
completed生成完成
in_progress生成中
failed生成失败
incomplete因 Token 上限等原因未完成

output[].type 取值

说明
message助手文本消息
function_call函数/工具调用
image_generation_call图像生成调用
web_search_call联网搜索调用

响应体(流式)

HTTP 200Content-Type: text/event-stream

data: {"type":"response.created","response":{"id":"resp_abc123","object":"response","status":"in_progress"}}

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

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

data: {"type":"response.completed","response":{"id":"resp_abc123","status":"completed","usage":{"input_tokens":15,"output_tokens":28,"total_tokens":43}}}

data: [DONE]

流式事件字段

字段类型说明
typestring事件类型,见下表
responseobject完整或部分响应对象
deltastring增量文本
output_indexinteger输出项索引
content_indexinteger内容块索引
itemobject新增/完成的输出项
item_idstring输出项 ID
partobject推理摘要片段

常见 type 取值

说明
response.created响应创建
response.output_text.delta文本增量
response.output_item.added新增输出项
response.output_item.done输出项完成
response.function_call_arguments.delta函数参数增量
response.function_call_arguments.done函数参数完成
response.completed响应完成