> ## Documentation Index
> Fetch the complete documentation index at: https://ppio.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 创建聊天对话请求

根据指定的聊天对话生成模型回复

## 请求头

<ParamField header="Content-Type" type="string" required={true}>
  枚举值: `application/json`
</ParamField>

<ParamField header="Authorization" type="string" required={true}>
  Bearer 身份验证格式，例如：Bearer \{\{API 密钥}}。
</ParamField>

## 请求体

<ParamField body="model" type="string" required={true}>
  对应的模型名称。可用模型名称请参考：<a href="https://ppio.com/model-api/product/llm-api" target="_blank">模型列表</a>
</ParamField>

<ParamField body="messages" type="object[]" required={true}>
  截至目前组成对话的消息列表。

  <Expandable title="properties" defaultOpen={true}>
    <ParamField body="content" type="string | object[] | null" required={true}>
      消息的内容。所有消息都需要 content，对于包含函数调用的 assistant 消息，content 可以为 null。

      您可以根据不同的模态使用以下参数。

      <Frame>
        <div class="param_frame">
          <Tabs>
            <Tab title="文本内容">
              <p class="param_text">选项 1：</p>
              <p class="param_text">您可以使用字符串类型来表示消息的文本内容。</p>

              <br />

              <p class="param_text">选项 2：</p>
              <p class="param_text">使用内容部分的数组，object\[]。详细字段如下：</p>

              <ParamField body="type" type="string" required={true}>
                内容部分的类型，在此情况下为 `text`。
              </ParamField>

              <ParamField body="text" type="string" required={true}>
                文本内容。
              </ParamField>
            </Tab>

            <Tab title="图像内容">
              <p class="param_text">仅视觉语言模型可使用。</p>
              <p class="param_text">内容部分的数组，object\[]。详细字段如下：</p>

              <ParamField body="type" type="string" required={true}>
                内容部分的类型，在此情况下为 `image_url`。
              </ParamField>

              <ParamField body="image_url" type="string" required={true}>
                <Expandable title="properties" defaultOpen={true}>
                  <ParamField body="url" type="string" required={true}>
                    图像的 URL 或 base64 编码的图像数据。
                  </ParamField>
                </Expandable>
              </ParamField>
            </Tab>

            <Tab title="视频内容">
              <p class="param_text">仅支持视频的模型可使用。</p>
              <p class="param_text">内容部分的数组，object\[]。详细字段如下：</p>

              <ParamField body="type" type="string" required={true}>
                内容部分的类型，在此情况下为 `video_url`。
              </ParamField>

              <ParamField body="video_url" type="string" required={true}>
                <Expandable title="properties" defaultOpen={true}>
                  <ParamField body="url" type="string" required={true}>
                    视频的 URL。
                  </ParamField>
                </Expandable>
              </ParamField>
            </Tab>

            <Tab title="音频内容">
              <p class="param_text mb-2">仅支持音频的模型可使用。</p>
              <p class="param_text">输出模态参数：</p>

              <ParamField body="modalities" type="string[]" required={false}>
                设置模型输出的模态。当前支持两种：`["text"]`、`["text","audio"]`。
                如果传入 `["text"]`，模型只返回文字内容。如果传入 `["text","audio"]`，模型会同时返回文字内容和音频内容。
                默认为 `["text"]`。
              </ParamField>

              <p class="param_text">内容部分的数组，object\[]。详细字段如下：</p>

              <ParamField body="type" type="string" required={true}>
                内容部分的类型，在此情况下为 `input_audio`。
              </ParamField>

              <ParamField body="input_audio" type="object" required={true}>
                <Expandable title="properties" defaultOpen={true}>
                  <ParamField body="data" type="string" required={true}>
                    音频的 URL 或者 base64 编码。
                  </ParamField>

                  <ParamField body="format" type="string" required={true}>
                    音频的格式。如 `wav`, `mp3`。
                  </ParamField>
                </Expandable>
              </ParamField>
            </Tab>
          </Tabs>
        </div>
      </Frame>
    </ParamField>

    <ParamField body="role" type="string" required={true}>
      消息作者的角色。可以是 system、user、assistant 之一。Enum: `system, user, assistant`
    </ParamField>

    <ParamField body="name" type="string | null" required={undefined}>
      此消息的作者名称。可以包含小写字母 a-z、大写字母 A-Z、数字 0-9 和下划线，最大长度为 64 个字符。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="max_tokens" type="integer" required={true}>
  在生成对话时可产生的最大 tokens 数。<br /><br />如果您的提示（之前的消息）中的 tokens 数量加上 max\_tokens 超过了模型的上下文长度，则行为取决于 context\_length\_exceeded\_behavior。默认情况下，max\_tokens 会被调整以适应上下文窗口，而不是返回错误。
</ParamField>

<ParamField body="stream" type="boolean | null" required={undefined}>
  是否使用流式传输。默认为 false，如果设置了，tokens 将以 data-only server-sent events（SSE）发送，并以 data: \[DONE] 消息终止流。
</ParamField>

<ParamField body="stream_options" type="object" required={undefined}>
  流式回复选项。仅当 stream 设置为 true 时设置。

  <Expandable title="properties" defaultOpen={false}>
    <ParamField body="include_usage" type="boolean" required={false}>
      如果设置，将在数据: \[DONE] 消息之前流式传输一个额外的 chunk。此 chunk 中的 usage 字段显示整个请求的令牌使用统计信息，而 choices 字段始终为空数组。所有其他 chunk 也将包含一个 usage 字段，但值为 null。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="n" type="integer | null" required={undefined}>
  每个提示生成多少个对话。默认值为 1。<br /><br />注意：由于此参数会生成多个对话，因此可能会快速消耗您的 tokens 计费额度。请谨慎使用，并确保为 max\_tokens 和 stop 设置了合理的值。<br />所需范围：1 \< x \< 128
</ParamField>

<ParamField body="seed" type="integer | null" required={undefined}>
  如果指定，我们的系统将尽最大努力进行确定性采样，以便相同的 seed 和参数的重复请求应返回相同的结果。
</ParamField>

<ParamField body="frequency_penalty" type="number | null" required={undefined}>
  默认值为 0，正值会根据新 tokens 在当前文本中的出现频率对其进行惩罚，从而降低模型重复相同内容的可能性。<br /><br />如果目的是仅仅减少重复样本，合理的值大约在 0.1 到 1 之间。如果目的是强烈抑制重复，可以将系数提高到 2，但这可能会明显降低样本质量。负值可以用来增加重复的可能性。<br /><br />另见 presence\_penalty，用于以固定速率惩罚至少出现一次的 tokens<br />所需范围：-2 \< x \< 2
</ParamField>

<ParamField body="presence_penalty" type="number | null" required={undefined}>
  默认值为 0，正值会根据新 tokens 是否出现在当前文本中对其进行惩罚，从而增加模型谈论新话题的可能性。<br /><br />如果目的是稍微减少重复样本，合理的值大约在 0.1 到 1 之间。如果目的是强烈抑制重复，可以将系数提高到 2，但这可能会显著降低样本质量。负值可以用来增加重复的可能性。<br /><br />另见 frequency\_penalty，用于根据 tokens 出现的频率按递增速率进行惩罚<br />所需范围：-2 \< x \< 2
</ParamField>

<ParamField body="repetition_penalty" type="number | null" required={undefined}>
  对重复的 tokens 应用惩罚，以抑制或鼓励重复。值为 1.0 表示没有惩罚，允许自由重复。大于 1.0 的值会惩罚重复，降低重复 tokens 的可能性。介于 0.0 和 1.0 之间的值会奖励重复，增加重复 tokens 的机会。为了达到良好的平衡，通常建议使用 1.2。请注意，在仅解码器模型中，惩罚会同时应用于生成的输出和提示。<br />所需范围：0 \< x \< 2
</ParamField>

<ParamField body="stop" type="string | null" required={undefined}>
  最多 4 个序列，API 将停止生成更多 tokens。返回的文本包含停止序列。
</ParamField>

<ParamField body="temperature" type="number | null" required={undefined}>
  对话中的随机性程度，默认值为 1，介于 0 和 2 之间。较高的值（如 0.8）会使输出更加随机，而较低的值（如 0.2），会使输出更集中且确定性更强。<br /><br />我们通常建议只调整此项或 top\_p，而不是同时调整两者。<br />所需范围：0 \< x \< 2
</ParamField>

<ParamField body="top_p" type="number | null" required={undefined}>
  作为 temperature 的替代方法，称为 nucleus sampling，模型会考虑具有 top\_p 概率质量的 tokens 的结果。因此，0.1 意味着只考虑构成前 10% 概率质量的 tokens。我们通常建议只调整此项或 temperature，而不是同时调整两者。<br />所需范围：0 \< x ≤ 1
</ParamField>

<ParamField body="top_k" type="integer | null" required={undefined}>
  Top-k 采样是另一种采样方法，在这种方法中，k 个最可能的下一个 tokens 会被筛选出来，并且概率质量仅在这 k 个 tokens 之间重新分配。k 的值控制了在每一步生成文本时，下一个 tokens 的候选数量。<br />所需范围：1 \< x \< 128
</ParamField>

<ParamField body="min_p" type="number | null" required={undefined}>
  表示一个 tokens 被考虑的最小概率的浮动值，相对于最有可能的 tokens 的概率。<br />所需范围：0 ≤ x ≤ 1
</ParamField>

<ParamField body="logit_bias" type="map[string, integer]" required={undefined}>
  默认为 null。修改指定 tokens 在对话中出现的可能性。接受一个 JSON 对象，将 tokens 映射到一个从 -100 到 100 的关联偏差值。
</ParamField>

<ParamField body="logprobs" type="boolean | null" required={undefined}>
  是否返回输出 tokens 的对数概率。如果为 true，则返回消息内容中每个输出 tokens 的对数概率。
</ParamField>

<ParamField body="top_logprobs" type="integer | null" required={undefined}>
  一个介于 0 到 20 之间的整数，指定在每个 tokens 位置返回的最可能的 tokens 数量，每个 tokens 都有一个关联的对数概率。如果使用此参数，必须将 logprobs 设置为 true。<br />所需范围：0 ≤ x ≤ 20
</ParamField>

<ParamField body="tools" type="object[] | null">
  模型可以调用的工具列表。目前仅支持函数作为工具。使用此参数提供模型可能生成 JSON 输入的函数列表。

  在[工具调用（Function Calling）](/model/llm-function-calling)中了解更多关于函数调用的信息。

  <Expandable title="properties" defaultOpen={false}>
    <ParamField body="type" type="string" required={true}>
      工具的类型。

      支持的类型：`function`
    </ParamField>

    <ParamField body="function" type="object" required={true}>
      <Expandable title="properties" defaultOpen={false}>
        <ParamField body="name" type="string" required={true}>
          要调用的函数名称。必须是 a-z、A-Z、0-9 或包含下划线和破折号，最大长度为 64 个字符。
        </ParamField>

        <ParamField body="description" type="string | null">
          函数功能的描述，模型使用此信息来选择何时以及如何调用函数。
        </ParamField>

        <ParamField body="parameters" type="object | null">
          函数接受的参数，以 JSON Schema 对象的形式描述。有关格式的文档，请参阅 [JSON Schema 参考](https://json-schema.org/understanding-json-schema/)。
        </ParamField>

        <ParamField body="strict" type="boolean" default={false}>
          是否在生成函数调用时启用严格遵守 schema。如果设置为 true，模型将完全按照 parameters 字段中定义的 schema 进行操作。
        </ParamField>
      </Expandable>
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="response_format" type="object | null">
  允许强制模型产生特定的输出格式。

  设置为 `{ "type": "json_schema", "json_schema": {...} }` 启用结构化输出，确保模型匹配您提供的 JSON schema。

  设置为 `{ "type": "json_object" }` 启用较旧的 JSON 模式，确保模型生成的消息是有效的 JSON。对于支持的模型，推荐使用 `json_schema`。

  <Expandable title="properties" defaultOpen={false}>
    <ParamField body="type" type="string" required={true} default="text">
      枚举值：`text`, `json_object`, `json_schema`
    </ParamField>

    <ParamField body="json_schema" type="object | null">
      JSON Schema 响应格式。用于生成结构化的 JSON 响应。

      仅在 `type` 设置为 `json_schema` 时支持，且当 `type` 设置为 `json_schema` 时为必需项。

      在[结构化输出](/model/llm-structured-outputs)中了解更多信息。

      <Expandable title="properties" defaultOpen={false}>
        <ParamField body="name" type="string" required={true}>
          响应格式的名称。必须是 a-z、A-Z、0-9 或包含下划线和破折号，最大长度为 64 个字符。
        </ParamField>

        <ParamField body="description" type="string | null">
          响应格式用途的描述，模型使用此信息来确定如何以该格式响应。
        </ParamField>

        <ParamField body="schema" type="object | null">
          响应格式的 schema，以 JSON Schema 对象的形式描述。在[这里](https://json-schema.org/specification)学习如何构建 JSON schema。

          支持的类型：`string`, `number`, `integer`, `boolean`, `array`, `object`, `enum`, `anyOf`。
        </ParamField>

        <ParamField body="strict" type="boolean" default={false}>
          是否在生成输出时启用严格的 schema 遵守。如果设置为 true，模型将始终遵循 schema 字段中定义的确切 schema。当 strict 为 true 时，仅支持 JSON Schema 的子集。

          如果您通过提供 `strict: true` 启用结构化输出并使用不支持的 JSON Schema 调用 API，您将收到错误。
        </ParamField>
      </Expandable>
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="separate_reasoning" type="boolean | null" required={undefined}>
  控制是否将 “思考内容” 单独放入 “reasoning\_content” 字段中。默认为 false，即不单独返回 “思考内容”。

  目前支持的模型：deepseek/deepseek-r1-turbo
</ParamField>

<ParamField body="enable_thinking" type="boolean | null" default={false} required={undefined}>
  控制是否开启思考模式。

  目前支持的模型：

  * zai-org/glm-4.5
  * deepseek/deepseek-v3.1
  * deepseek/deepseek-v3.1-terminus
  * deepseek/deepseek-v3.2-exp
</ParamField>

## 响应参数

<ResponseField name="choices" type="array" required={true}>
  生成的对话选择列表。

  <Expandable title="properties" defaultOpen={true}>
    <ResponseField name="finish_reason" type="string" required={true}>
      模型停止生成 tokens 的原因。如果模型遇到自然停止点或提供的停止序列，则为 ‘stop’；如果请求中指定的最大 tokens 数量已达到，则为 ‘length’。枚举值: `stop,length`
    </ResponseField>

    <ResponseField name="index" type="integer" required={true}>
      对话选择的索引。
    </ResponseField>

    <ResponseField name="message" type="object" required={true}>
      <Expandable title="properties" defaultOpen={true}>
        <ResponseField name="role" type="string" required={true}>
          消息作者的角色。枚举值: `system, user, assistant`
        </ResponseField>

        <ResponseField name="content" type="string | null" required={undefined}>
          消息的内容。
        </ResponseField>

        <ResponseField name="reasoning_content" type="string | null" required={undefined}>
          思考内容。

          目前支持的模型：deepseek/deepseek-r1-turbo
        </ResponseField>
      </Expandable>
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="created" type="integer" required={true}>
  响应生成的 Unix 时间戳（以秒为单位）。
</ResponseField>

<ResponseField name="id" type="string" required={true}>
  响应的唯一标识符。
</ResponseField>

<ResponseField name="model" type="string" required={true}>
  用于对话的模型。
</ResponseField>

<ResponseField name="object" type="string" required={true}>
  对象类型，始终为 chat.completion。
</ResponseField>

<ResponseField name="usage" type="object" required={undefined}>
  使用统计。<br />对于流式回复，usage 字段被包含在返回的最后一个回复块中。

  <Expandable title="properties" defaultOpen={undefined}>
    <ResponseField name="completion_tokens" type="integer" required={true}>
      对话生成的 tokens 数。
    </ResponseField>

    <ResponseField name="prompt_tokens" type="integer" required={true}>
      prompt 中的 tokens 数。
    </ResponseField>

    <ResponseField name="total_tokens" type="integer" required={true}>
      请求中使用的总 tokens 数（prompt + completion）。
    </ResponseField>
  </Expandable>
</ResponseField>
