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

# Chat Completions

> <ul><li>根据对话历史创建模型响应。支持流式和非流式响应。</li><li>兼容 OpenAI Chat Completions API。</li></ul>

<RequestExample>
  ```bash cURL theme={null} theme={null}
  curl --request POST \
    --url 'https://api.elkapi.com/v1/chat/completions' \
    --header 'Authorization: Bearer <token>' \
    --header 'Content-Type: application/json' \
    --data '{
    "model": "gpt-5.5",
    "messages": [
      {
        "role": "user",
        "content": "你好，请介绍一下你自己"
      }
    ],
    "temperature": 1,
    "stream": false,
    "max_tokens": 1024,
    "response_format": "json"
  }'
  ```

  ```python Python theme={null} theme={null}
  import requests

  url = "https://api.elkapi.com/v1/chat/completions"
  headers = {
      "Authorization": "Bearer <token>"
  }
  headers["Content-Type"] = "application/json"
  payload = {
      "model": "gpt-5.5",
      "messages": [
          {
              "role": "user",
              "content": "你好，请介绍一下你自己"
          }
      ],
      "temperature": 1,
      "stream": False,
      "max_tokens": 1024,
      "response_format": "json"
  }

  response = requests.request("POST", url, headers=headers, json=payload)
  print(response.json())
  ```

  ```javascript JavaScript theme={null} theme={null}
  const url = "https://api.elkapi.com/v1/chat/completions";

  const headers = {
    "Authorization": "Bearer <token>"
  };
  headers["Content-Type"] = "application/json";
  const payload = {
    "model": "gpt-5.5",
    "messages": [
      {
        "role": "user",
        "content": "你好，请介绍一下你自己"
      }
    ],
    "temperature": 1,
    "stream": false,
    "max_tokens": 1024,
    "response_format": "json"
  };

  const response = await fetch(url, {
    method: "POST",
    headers,
    body: JSON.stringify(payload)
  });

  console.log(await response.json());
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null} theme={null}
  {
    "id": "id",
    "object": "chat.completion",
    "created": 1,
    "model": "gpt-5.5",
    "choices": [
      {
        "index": 1,
        "message": {
          "role": "user",
          "content": "你好，请介绍一下你自己",
          "name": "name",
          "tool_calls": [
            {
              "id": {},
              "type": {},
              "function": {}
            }
          ],
          "tool_call_id": "tool_call_id",
          "reasoning_content": "reasoning_content"
        },
        "finish_reason": "stop"
      }
    ],
    "usage": {
      "prompt_tokens": 1,
      "completion_tokens": 1,
      "total_tokens": 1,
      "prompt_tokens_details": {
        "cached_tokens": 1,
        "text_tokens": 1,
        "audio_tokens": 1,
        "image_tokens": 1
      },
      "completion_tokens_details": {
        "text_tokens": 1,
        "audio_tokens": 1,
        "reasoning_tokens": 1
      }
    },
    "system_fingerprint": "system_fingerprint"
  }
  ```

  ```json 400 theme={null} theme={null}
  {
    "error": {
      "message": "message",
      "type": "type",
      "param": "param",
      "code": "code"
    }
  }
  ```

  ```json 429 theme={null} theme={null}
  {
    "error": {
      "message": "message",
      "type": "type",
      "param": "param",
      "code": "code"
    }
  }
  ```
</ResponseExample>

## Authorizations

<ParamField header="Authorization" type="string" required>
  所有接口均需要使用 Bearer Token 进行认证。

  在请求头中添加：

  ```
  Authorization: Bearer YOUR_API_KEY
  ```
</ParamField>

## Body

<ParamField body="model" type="string" required default="gpt-5.5">
  模型 ID

  示例：`gpt-5.5`
</ParamField>

<ParamField body="messages" type="array<object>" required>
  对话消息列表

  <ParamField body="messages.role" type="string" required>
    消息角色

    可选值：`system`、`user`、`assistant`、`tool`、`developer`
  </ParamField>

  <ParamField body="messages.content" type="string or array<object>" required>
    消息内容
  </ParamField>

  <ParamField body="messages.name" type="string">
    发送者名称
  </ParamField>

  <ParamField body="messages.tool_calls" type="array<object>">
    <ParamField body="messages.tool_calls.id" type="string" />

    <ParamField body="messages.tool_calls.type" type="string">
      示例：`function`
    </ParamField>

    <ParamField body="messages.tool_calls.function" type="object">
      <ParamField body="messages.tool_calls.function.name" type="string" />

      <ParamField body="messages.tool_calls.function.arguments" type="string" />
    </ParamField>
  </ParamField>

  <ParamField body="messages.tool_call_id" type="string">
    工具调用 ID（用于 tool 角色消息）
  </ParamField>

  <ParamField body="messages.reasoning_content" type="string">
    推理内容
  </ParamField>
</ParamField>

<ParamField body="temperature" type="number" default="1">
  采样温度
</ParamField>

<ParamField body="top_p" type="number" default="1">
  核采样参数
</ParamField>

<ParamField body="n" type="integer" default="1">
  生成数量
</ParamField>

<ParamField body="stream" type="boolean" default="false">
  是否流式响应
</ParamField>

<ParamField body="stream_options" type="object">
  <ParamField body="stream_options.include_usage" type="boolean" />
</ParamField>

<ParamField body="stop" type="string or array<string>">
  停止序列
</ParamField>

<ParamField body="max_tokens" type="integer">
  最大生成 Token 数
</ParamField>

<ParamField body="max_completion_tokens" type="integer">
  最大补全 Token 数
</ParamField>

<ParamField body="presence_penalty" type="number" default="0" />

<ParamField body="frequency_penalty" type="number" default="0" />

<ParamField body="logit_bias" type="object" />

<ParamField body="user" type="string" />

<ParamField body="tools" type="array<object>">
  <ParamField body="tools.type" type="string">
    示例：`function`
  </ParamField>

  <ParamField body="tools.function" type="object">
    <ParamField body="tools.function.name" type="string" />

    <ParamField body="tools.function.description" type="string" />

    <ParamField body="tools.function.parameters" type="object">
      JSON Schema 格式的参数定义
    </ParamField>
  </ParamField>
</ParamField>

<ParamField body="tool_choice" type="string or object">
  <ParamField body="tool_choice.type" type="string" />

  <ParamField body="tool_choice.function" type="object">
    <ParamField body="tool_choice.function.name" type="string" />
  </ParamField>
</ParamField>

<ParamField body="response_format" type="object">
  <ParamField body="response_format.type" type="string">
    可选值：`text`、`json_object`、`json_schema`
  </ParamField>

  <ParamField body="response_format.json_schema" type="object">
    JSON Schema 定义
  </ParamField>
</ParamField>

<ParamField body="seed" type="integer" />

<ParamField body="reasoning_effort" type="string">
  推理强度 (用于支持推理的模型)

  可选值：`low`、`medium`、`high`
</ParamField>

<ParamField body="modalities" type="array<string>" />

<ParamField body="audio" type="object">
  <ParamField body="audio.voice" type="string" />

  <ParamField body="audio.format" type="string" />
</ParamField>

## Response

<ResponseField name="id" type="string" />

<ResponseField name="object" type="string">
  示例：`chat.completion`
</ResponseField>

<ResponseField name="created" type="integer" />

<ResponseField name="model" type="string" default="gpt-5.5">
  示例：`gpt-5.5`
</ResponseField>

<ResponseField name="choices" type="array<object>">
  <ResponseField name="choices.index" type="integer" />

  <ResponseField name="choices.message" type="object">
    <ResponseField name="choices.message.role" type="string" required>
      消息角色

      可选值：`system`、`user`、`assistant`、`tool`、`developer`
    </ResponseField>

    <ResponseField name="choices.message.content" type="string or array<object>" required>
      消息内容
    </ResponseField>

    <ResponseField name="choices.message.name" type="string">
      发送者名称
    </ResponseField>

    <ResponseField name="choices.message.tool_calls" type="array<object>">
      <ResponseField name="choices.message.tool_calls.id" type="string" />

      <ResponseField name="choices.message.tool_calls.type" type="string">
        示例：`function`
      </ResponseField>

      <ResponseField name="choices.message.tool_calls.function" type="object" />
    </ResponseField>

    <ResponseField name="choices.message.tool_call_id" type="string">
      工具调用 ID（用于 tool 角色消息）
    </ResponseField>

    <ResponseField name="choices.message.reasoning_content" type="string">
      推理内容
    </ResponseField>
  </ResponseField>

  <ResponseField name="choices.finish_reason" type="string">
    可选值：`stop`、`length`、`tool_calls`、`content_filter`
  </ResponseField>
</ResponseField>

<ResponseField name="usage" type="object">
  <ResponseField name="usage.prompt_tokens" type="integer">
    提示词 Token 数
  </ResponseField>

  <ResponseField name="usage.completion_tokens" type="integer">
    补全 Token 数
  </ResponseField>

  <ResponseField name="usage.total_tokens" type="integer">
    总 Token 数
  </ResponseField>

  <ResponseField name="usage.prompt_tokens_details" type="object">
    <ResponseField name="usage.prompt_tokens_details.cached_tokens" type="integer" />

    <ResponseField name="usage.prompt_tokens_details.text_tokens" type="integer" />

    <ResponseField name="usage.prompt_tokens_details.audio_tokens" type="integer" />

    <ResponseField name="usage.prompt_tokens_details.image_tokens" type="integer" />
  </ResponseField>

  <ResponseField name="usage.completion_tokens_details" type="object">
    <ResponseField name="usage.completion_tokens_details.text_tokens" type="integer" />

    <ResponseField name="usage.completion_tokens_details.audio_tokens" type="integer" />

    <ResponseField name="usage.completion_tokens_details.reasoning_tokens" type="integer" />
  </ResponseField>
</ResponseField>

<ResponseField name="system_fingerprint" type="string" />
