# 聊天接口

## OpenAPI Specification

```yaml
openapi: 3.0.1
info:
  title: ''
  description: ''
  version: 1.0.0
paths:
  /v1/chat/completions:
    post:
      summary: 聊天接口
      deprecated: false
      description: |+
        给定一个提示，该模型将返回一个或多个预测的完成，并且还可以返回每个位置的替代标记的概率。

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

      tags:
        - 模型接口/聊天接口（Chat）
      parameters:
        - name: Authorization
          in: header
          description: ''
          required: true
          example: Bearer {{YOUR_API_KEY}}
          schema:
            type: string
        - name: Content-Type
          in: header
          description: ''
          required: true
          example: application/json
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                model:
                  type: string
                  description: >-
                    要使用的模型的 ID。有关哪些模型适用于聊天 API
                    的详细信息，请参阅[模型端点兼容性表。](https://platform.openai.com/docs/models/model-endpoint-compatibility)
                messages:
                  type: array
                  items:
                    type: object
                    properties:
                      role:
                        type: string
                      content:
                        type: string
                    x-apifox-orders:
                      - role
                      - content
                  description: >-
                    以[聊天格式](https://platform.openai.com/docs/guides/chat/introduction)生成聊天完成的消息。
                temperature:
                  type: number
                  description: >-
                    使用什么采样温度，介于 0 和 2 之间。较高的值（如 0.8）将使输出更加随机，而较低的值（如
                    0.2）将使输出更加集中和确定。  我们通常建议改变这个或`top_p`但不是两者。
                top_p:
                  type: number
                  description: >-
                    一种替代温度采样的方法，称为核采样，其中模型考虑具有 top_p 概率质量的标记的结果。所以 0.1 意味着只考虑构成前
                    10% 概率质量的标记。  我们通常建议改变这个或`temperature`但不是两者。
                'n':
                  type: integer
                  description: 为每个输入消息生成多少个聊天完成选项。
                stream:
                  type: boolean
                  description: >-
                    如果设置，将发送部分消息增量，就像在 ChatGPT
                    中一样。当令牌可用时，令牌将作为纯数据[服务器发送事件](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format)`data:
                    [DONE]`发送，流由消息终止。[有关示例代码](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_stream_completions.ipynb)，请参阅
                    OpenAI Cookbook 。
                stop:
                  type: string
                  description: API 将停止生成更多令牌的最多 4 个序列。
                max_tokens:
                  type: integer
                  description: 聊天完成时生成的最大令牌数。  输入标记和生成标记的总长度受模型上下文长度的限制。
                presence_penalty:
                  type: number
                  description: >-
                    -2.0 和 2.0 之间的数字。正值会根据到目前为止是否出现在文本中来惩罚新标记，从而增加模型谈论新主题的可能性。 
                    [查看有关频率和存在惩罚的更多信息。](https://platform.openai.com/docs/api-reference/parameter-details)
                frequency_penalty:
                  type: number
                  description: >-
                    -2.0 和 2.0 之间的数字。正值会根据新标记在文本中的现有频率对其进行惩罚，从而降低模型逐字重复同一行的可能性。 
                    [查看有关频率和存在惩罚的更多信息。](https://platform.openai.com/docs/api-reference/parameter-details)
                logit_bias:
                  type: object
                  properties: {}
                  description: >-
                    修改指定标记出现在生成中的可能性。  接受一个 json 对象，该对象将标记（由标记器中的标记 ID 指定）映射到从
                    -100 到 100 的关联偏差值。从数学上讲，偏差会在采样之前添加到模型生成的 logits
                    中。确切的效果因模型而异，但 -1 和 1 之间的值应该会减少或增加选择的可能性；像 -100 或 100
                    这样的值应该导致相关令牌的禁止或独占选择。
                  x-apifox-orders: []
                user:
                  type: string
                  description: >-
                    代表您的最终用户的唯一标识符，可以帮助 OpenAI
                    监控和检测滥用行为。[了解更多](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids)。
                stream_options:
                  type: object
                  properties:
                    include_usage:
                      type: boolean
                      description: >-
                        如果设置，则在消息data:
                        [DONE]之前会流式传输一个额外的usage块。此块上的字段显示整个请求的令牌使用情况统计信息，并且该choices字段将始终为空数组。
                  x-apifox-orders:
                    - include_usage
                  description: 流式响应的选项。
                logprobs:
                  type: boolean
                  description: 是否返回输出标记的对数概率。如果为 true，则返回message的content中返回的每个输出token的对数概率。
                top_logprobs:
                  type: integer
                  description: >-
                    0 到 20
                    之间的整数，指定在每个标记位置返回的最可能标记的数量，每个标记均具有相关的对数概率。如果使用此参数，则logprobs必须设置为true。
                response_format:
                  type: object
                  properties:
                    type:
                      type: string
                      description: 必须是text或json_object。
                  x-apifox-orders:
                    - type
                  description: >-
                    指定模型必须输出的格式的对象。与GPT-4 Turbo和所有比gpt-3.5-turbo-1106更新的 GPT-3.5
                    Turbo 模型兼容。 设置为{ "type": "json_object" }启用 JSON
                    模式，保证模型生成的消息是有效的 JSON。 重要提示：使用 JSON
                    模式时，您还必须通过系统或用户消息指示模型自行生成
                    JSON。如果不这样做，模型可能会生成无休止的空白流，直到生成达到令牌限制，从而导致长时间运行且看似“卡住”的请求。另请注意，如果finish_reason="length"，消息内容可能会被部分截断，这表示生成超出max_tokens或对话超出了最大上下文长度。
                tools:
                  type: array
                  items:
                    type: object
                    properties:
                      type:
                        type: string
                      function:
                        type: object
                        properties:
                          description:
                            type: string
                          name:
                            type: string
                          parameters:
                            type: object
                            properties: {}
                            x-apifox-orders: []
                        x-apifox-orders:
                          - description
                          - name
                          - parameters
                        required:
                          - name
                    x-apifox-orders:
                      - type
                      - function
                    required:
                      - type
                  description: >-
                    函数调用使用，参考[OpenAI官方文档](https://platform.openai.com/docs/guides/function-calling)
                tool_choice:
                  oneOf:
                    - type: string
                    - type: object
                      additionalProperties: false
                      x-apifox-orders:
                        - type
                        - function
                      properties:
                        type:
                          type: string
                        function:
                          type: object
                          properties:
                            name:
                              type: string
                          x-apifox-orders:
                            - name
                          required:
                            - name
                      required:
                        - type
                        - function
                  description: >-
                    函数调用使用，参考[OpenAI官方文档](https://platform.openai.com/docs/guides/function-calling)
              required:
                - model
                - messages
              x-apifox-orders:
                - messages
                - model
                - frequency_penalty
                - logit_bias
                - logprobs
                - top_logprobs
                - max_tokens
                - 'n'
                - presence_penalty
                - response_format
                - stop
                - stream
                - stream_options
                - temperature
                - top_p
                - tools
                - tool_choice
                - user
            example:
              model: gpt-3.5-turbo
              messages:
                - role: system
                  content: You are a helpful assistant.
                - role: user
                  content: Hello!
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                  object:
                    type: string
                  created:
                    type: integer
                  choices:
                    type: array
                    items:
                      type: object
                      properties:
                        index:
                          type: integer
                        message:
                          type: object
                          properties:
                            role:
                              type: string
                            content:
                              type: string
                          required:
                            - role
                            - content
                          x-apifox-orders:
                            - role
                            - content
                        finish_reason:
                          type: string
                      x-apifox-orders:
                        - index
                        - message
                        - finish_reason
                  usage:
                    type: object
                    properties:
                      prompt_tokens:
                        type: integer
                      completion_tokens:
                        type: integer
                      total_tokens:
                        type: integer
                    required:
                      - prompt_tokens
                      - completion_tokens
                      - total_tokens
                    x-apifox-orders:
                      - prompt_tokens
                      - completion_tokens
                      - total_tokens
                required:
                  - id
                  - object
                  - created
                  - choices
                  - usage
                x-apifox-orders:
                  - id
                  - object
                  - created
                  - choices
                  - usage
              example:
                id: chatcmpl-123
                object: chat.completion
                created: 1677652288
                choices:
                  - index: 0
                    message:
                      role: assistant
                      content: |-


                        Hello there, how may I assist you today?
                    finish_reason: stop
                usage:
                  prompt_tokens: 9
                  completion_tokens: 12
                  total_tokens: 21
          headers: {}
          x-apifox-name: OK
      security: []
      x-apifox-folder: 模型接口/聊天接口（Chat）
      x-apifox-status: released
      x-run-in-apifox: https://app.apifox.com/web/project/2946232/apis/api-92222076-run
components:
  schemas: {}
  securitySchemes: {}
servers:
  - url: https://api.chatanywhere.tech
    description: 正式环境
security: []

```
