中国站
帮助中心 > 人工智能 > 大模型服务 > API参考 > TEXT > Chat Completions API文档

Chat Completions API文档

  1. POST https://api.yisu.com/v1/ai/{ENDPOINT_PATH}/chat/completions
功能概述:为给定的聊天对话创建模型响应

请求参数

字段 含义 层级 格式 必需 备注
messages 构成当前对话的消息列表 1 arrary 根据使用的模型不同,支持不同类型的消息内容(模态),包括文本、图像和音频
Developer message 开发者提供的指令,模型应始终遵循,无论用户发送何种消息 2 object 对于o1及更新版本的模型,开发者消息替代了之前的系统消息
content 开发者消息的内容 3 string 或者 arrary
Text content 开发者消息的内容 4 string
Array of content parts 由特定类型定义的内容块数组 4 array 对于开发者消息,仅支持文本类型(text)
text 文本内容 5 string
type 内容块的类型 5 string
role 消息作者的角色 3 string 此处为developer
name 参与者的可选名称。用于向模型提供信息以区分相同角色的不同参与者 3 string
System message 开发者提供的指令,无论用户发送什么消息,模型都应遵循这些指令 2 object 在 o1 及更高版本的模型中,请改用开发者消息 (developer messages) 实现此功能
content 系统消息的内容 3 string 或者 arrary
Text content 系统消息的内容 4 string
Array of content parts 由特定类型定义的内容块数组 4 array 对于系统消息,仅支持文本类型(text)
Text content part 文本内容 5 object
text 文本内容 6 string
type 内容块的类型 6 string
role 消息作者的角色 3 string 此处为system
name 参与者的可选名称。用于向模型提供信息以区分相同角色的不同参与者 3 string
User message 终端用户发送的消息,包含提示词或额外的上下文信息 2 object
content 用户消息的内容 3 string 或者 arrary
Text content 用户消息的内容 4 string
Array of content parts 由特定类型定义的内容块数组 4 array 支持的内容类型根据用于生成响应的模型而有所不同,可包含文本、图像或音频输入
Text content part 文本内容 5 object
text 文本内容 6 string
type 内容块的类型 6 string
Image content part 5 object
image_url 6 object
url 图像的URL或base64编码的图像数据 7 stirng
detail 指定图像的细节级别 7 stirng 默认为auto
type 内容块的类型 6 string
role 消息作者的角色 3 string 此处为user
name 参与者的可选名称。用于向模型提供信息以区分相同角色的不同参与者 3 string
Assistant message 模型为响应用户消息而发送的消息 2 object
role 消息作者的角色 3 string 此处为助手 assistant
audio 模型先前音频响应的相关数据 3 object或者null
id 模型先前音频响应的唯一标识符 4 string
content 助手消息的内容 3 string 或者 arrary 除非指定了 tool_callsfunction_call,否则为必需字段
Text content 助手消息的文本内容 4 string
Array of content parts 具有定义类型的内容块数组 4 array 可包含一个或多个文本类型 (text) 内容块,或恰好一个拒绝类型 (refusal) 内容块
Text content part 5 object
text 文本内容 6 string
type 内容块的类型 6 string
Refusal content part 5 object
refusal 模型生成的拒绝消息 6 string
type 内容块的类型 6 string
function_call 已弃用,由 tool_calls 替代 3 object或者null 包含模型生成的应调用函数的名称和参数
name 要调用的函数名称 4 string
arguments 调用函数时使用的参数,由模型以JSON格式生成。 4 string 注意:模型生成的JSON不一定有效,可能会产生函数模式中未定义的参数。调用函数前请验证参数
name 参与者的可选名称。用于区分相同角色的不同参与者 3 string
refusal 助手的拒绝消息 3 string或者null
tool_calls 模型生成的工具调用(如函数调用) 3 array
Function tool call 对模型创建的函数工具的调用 4 object
function 模型调用的函数 5 object
arguments 调用函数时所需的参数,由模型以JSON格式生成 6 string 请注意,模型生成的内容并不总是有效的JSON,而且可能会虚构出函数 schema 中未定义的参数。因此,在调用函数之前,请在代码中对这些参数进行验证
name 要调用的函数名称 6 string
id toolcall的ID 5 string
type tool的类型,目前只支持function 5 string
Tool message 2 object
content 工具消息的内容 3 string或者array
Text content 工具消息的文本内容 4 string
Array of content parts 具有特定类型的内容块数组。对于工具消息,仅支持文本类型 (text) 4 array
Text content part 5 object
text 文本内容 6 string
type 内容块类型 6 string
role 消息作者的角色,此处为tool 3 string
tool_call_id 该消息所响应的工具调用ID 3 string
model 用于生成响应的模型ID,例如 gpt-4oo3 1 string
frequency_penalty 介于 -2.0 和 2.0 之间的数值,默认是0 1 number或者null 正值会根据标记在当前文本中的现有频率进行惩罚,从而降低模型逐字重复相同内容的可能性
logit_bias 调整指定标记在生成结果中出现的概率,默认为null 1 map - 接收一个JSON对象,该对象将标记(通过分词器中的标记ID指定)映射到-100至100之间的偏差值
- 数学原理:该偏差值会在模型生成logits后、采样前被加入
- 具体效果因模型而异
max_completion_tokens 生成结果中允许的最大标记数上限(包括可见输出标记和推理标记) 1 integer或者null
n 为每条输入消息生成的聊天补全选项数量,默认为1 1 integer或者null 注意:费用将基于所有选项生成的标记总数计算
parallel_tool_calls 是否在工具使用时启用并行函数调用功能,默认为true 1 boolean
presence_penalty 介于 -2.0 和 2.0 之间的数值,默认为0 1 number或者null 正值会根据标记是否已在当前文本中出现进行惩罚
response_format 指定模型输出格式的对象 1 object
seed (已弃用) 1 integer或者null
stop 最新推理模型 o3 和 o4-mini 不支持此参数,默认为null 1 string/array/null 最多可设置 4 个停止序列
store 是否存储本次聊天补全请求的输出 1 boolean或者null
stream 若设为 true,模型响应数据将通过服务器发送事件(SSE)流式传输 1 boolean或者null
temperature 采样温度值(范围 0-2),默认为1 1 number或者null
tool_choice 控制模型是否(以及如何)调用工具 1 string或者object
tools 模型可调用的工具列表 1 array 当前仅支持将函数作为工具使用
top_p 温度采样的替代方案——核采样(nucleus sampling) 1 number或者null

返回结果

返回一个对话完成对象(chat completion object),如果请求启用了流式传输,则返回对话完成块对象(chat completion chunk objects)的流式序列

示例请求与响应结果

Default请求

示例代码

curl请求
  1. curl https://api.yisu.com/v1/ai/{ENDPOINT_PATH}/chat/completions \
  2. -H "Content-Type: application/json" \
  3. -H "Authorization: Bearer {YOUR_ACCESS_KEY}" \
  4. -d '{
  5. "model": "gpt-5",
  6. "messages": [
  7. {
  8. "role": "developer",
  9. "content": "You are a helpful assistant."
  10. },
  11. {
  12. "role": "user",
  13. "content": "Hello!"
  14. }
  15. ]
  16. }'

Default响应结果

  1. {
  2. "id": "chatcmpl-B9MBs8CjcvOU2jLn4n570S5qMJKcT",
  3. "object": "chat.completion",
  4. "created": 1741569952,
  5. "model": "gpt-4.1-2025-04-14",
  6. "choices": [
  7. {
  8. "index": 0,
  9. "message": {
  10. "role": "assistant",
  11. "content": "Hello! How can I assist you today?",
  12. "refusal": null,
  13. "annotations": []
  14. },
  15. "logprobs": null,
  16. "finish_reason": "stop"
  17. }
  18. ],
  19. "usage": {
  20. "prompt_tokens": 19,
  21. "completion_tokens": 10,
  22. "total_tokens": 29,
  23. "prompt_tokens_details": {
  24. "cached_tokens": 0,
  25. "audio_tokens": 0
  26. },
  27. "completion_tokens_details": {
  28. "reasoning_tokens": 0,
  29. "audio_tokens": 0,
  30. "accepted_prediction_tokens": 0,
  31. "rejected_prediction_tokens": 0
  32. }
  33. },
  34. "service_tier": "default"
  35. }

Streaming请求

  1. curl https://api.yisu.com/v1/ai/{ENDPOINT_PATH}/chat/completions \
  2. -H "Content-Type: application/json" \
  3. -H "Authorization: Bearer {YOUR_ACCESS_KEY}" \
  4. -d '{
  5. "model": "gpt-5",
  6. "messages": [
  7. {
  8. "role": "developer",
  9. "content": "You are a helpful assistant."
  10. },
  11. {
  12. "role": "user",
  13. "content": "Hello!"
  14. }
  15. ],
  16. "stream": true
  17. }'

Streaming响应结果

  1. {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}
  2. {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"content":"Hello"},"logprobs":null,"finish_reason":null}]}
  3. ....
  4. {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}