# OpenAI/聊天(Chat)

# POST 创建聊天补全

POST /v1/chat/completions

给定一个提示,该模型将返回一个或多个预测的完成,并且还可以返回每个位置的替代标记的概率。

为提供的提示和参数创建完成

Body 请求参数

{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "Hello!"
    }
  ]
}

# 请求参数

以下是将提供的内容转换为Markdown表格的示例:

名称 位置 类型 必选 说明
Content-Type header string none
Accept header string none
Authorization header string none
body body object none
» model body string 要使用的模型的 ID。有关哪些模型可与聊天 API 一起使用的详细信息,请参阅模型端点兼容性表。
» messages body [object] 至今为止对话所包含的消息列表。Python 代码示例。
»» role body string none
»» content body string none
» temperature body integer 使用什么采样温度,介于 0 和 2 之间。较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使输出更加集中和确定。
» top_p body integer 一种替代温度采样的方法,称为核采样,其中模型考虑具有 top_p 概率质量的标记的结果。
» n body integer 默认为 1
» stream body boolean 默认为 false 如果设置,则像在 ChatGPT 中一样会发送部分消息增量。
» stop body string 默认为 null
» max_tokens body integer 默认为 inf
» presence_penalty body number -2.0 和 2.0 之间的数字。
» frequency_penalty body number 默认为 0
» logit_bias body null 修改指定标记出现在补全中的可能性。
» user body string 代表您的最终用户的唯一标识符,可以帮助 OpenAI 监控和检测滥用行为。
» response_format body object 指定模型必须输出的格式的对象。
» seen body integer 此功能处于测试阶段。
» tools body [string] 模型可以调用的一组工具列表。
» tool_choice body object 控制模型调用哪个函数(如果有的话)。

# 返回示例

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "\n\nHello there, how may I assist you today?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}

# 返回结果

状态码 状态码含义 说明 数据模型
200 OK OK Inline

# 返回数据结构

状态码 200

名称 类型 必选 约束 中文名 说明
» id string true none
» object string true none
» created integer true none
» choices [object] true none
»» index integer false none
»» message object false none
»»» role string true none
»»» content string true none
»» finish_reason string false none
» usage object true none
»» prompt_tokens integer true none
»» completion_tokens integer true none
»» total_tokens integer true none