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

# 创建响应

> 使用 OpenAI Responses API 格式创建响应

Responses API 是 OpenAI 更新的有状态对话 API。TokenLab 将此格式作为兼容模型的高级可选路径提供支持；除非你明确需要 Responses 特定行为，否则请将 `POST /v1/chat/completions` 作为默认的兼容 OpenAI 路由。

## 请求体

<ParamField body="model" type="string" required>
  要使用的模型 ID。可用选项请参见 [Models](https://tokenlab.sh/zh/models)。
</ParamField>

<ParamField body="input" type="array" required>
  由对话组成的输入项列表。

  每个项可以是：

  * `message`: 带有角色和内容的对话消息
  * `function_call`: 一个函数调用请求
  * `function_call_output`: 来自函数调用的输出

  对于多模态输入，`message.content` 可以是普通字符串，也可以是内容块数组。对于支持图像的模型（例如 GPT-5.4 变体），请将图像作为 `input_image` 块传递，而不是将 URL 或 Base64 字符串直接嵌入普通文本中。

  示例内容块：

  * `{ "type": "input_text", "text": "Describe this image" }`
  * `{ "type": "input_image", "image_url": "https://example.com/image.jpg" }`
  * `{ "type": "input_image", "image_url": "data:image/png;base64,..." }`
</ParamField>

<ParamField body="instructions" type="string">
  提供给模型的系统指令（等同于 system 消息）。
</ParamField>

<ParamField body="max_output_tokens" type="integer">
  生成的最大 token 数量。
</ParamField>

<ParamField body="temperature" type="number" default="1">
  采样温度，范围在 0 到 2 之间。
</ParamField>

<ParamField body="tools" type="array">
  模型可能调用的工具列表。

  对于使用默认图片工具模型，或显式设置 `model: "gpt-image-2"` 的 hosted `image_generation` 工具，TokenLab 会在转发请求前移除不支持的 `input_fidelity`，因为 GPT Image 2 已经以高保真处理图片输入。请不要对这个工具传入 `background: "transparent"`；TokenLab 不会静默移除它，因为这会改变输出语义。
</ParamField>

<ParamField body="stream" type="boolean" default="false">
  如果为 true，则返回事件流。
</ParamField>

<ParamField body="previous_response_id" type="string">
  用于从某个先前响应继续对话的响应 ID。
</ParamField>

<ParamField body="store" type="boolean" default="true">
  是否存储响应以便后续检索。
</ParamField>

<ParamField body="metadata" type="object">
  附加到响应以用于跟踪的元数据。
</ParamField>

<ParamField body="text" type="object">
  文本生成的配置选项。`text.format` 的行为取决于所选模型和路由路径；并不保证在每个模型上都一致。
</ParamField>

<ParamField body="parallel_tool_calls" type="boolean" default="true">
  是否允许并行进行多个工具调用。
</ParamField>

<ParamField body="top_p" type="number">
  Nucleus 采样参数（0-1）。
</ParamField>

<ParamField body="reasoning" type="object">
  针对具备推理能力的模型（例如 GPT-5 系列变体）的推理配置。

  * `effort` (string): 推理努力级别（`low`, `medium`, `high`）
</ParamField>

## 响应

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

<ResponseField name="object" type="string">
  始终为 `response`。
</ResponseField>

<ResponseField name="created" type="integer">
  响应创建时的 Unix 时间戳。
</ResponseField>

<ResponseField name="output" type="array">
  模型生成的输出项列表。
</ResponseField>

<ResponseField name="usage" type="object">
  token 使用统计。
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST "https://api.tokenlab.sh/v1/responses" \
    -H "Authorization: Bearer sk-your-api-key" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-4o",
      "input": [
        {"type": "message", "role": "user", "content": "Hello!"}
      ],
      "max_output_tokens": 1000
    }'
  ```

  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key="sk-your-api-key",
      base_url="https://api.tokenlab.sh/v1"
  )

  response = client.responses.create(
      model="gpt-4o",
      input=[
          {"type": "message", "role": "user", "content": "Hello!"}
      ],
      max_output_tokens=1000
  )

  print(response.output)
  ```

  ```javascript JavaScript theme={null}
  import OpenAI from 'openai';

  const client = new OpenAI({
    apiKey: 'sk-your-api-key',
    baseURL: 'https://api.tokenlab.sh/v1'
  });

  const response = await client.responses.create({
    model: 'gpt-4o',
    input: [
      { type: 'message', role: 'user', content: 'Hello!' }
    ],
    max_output_tokens: 1000
  });

  console.log(response.output);
  ```

  ```go Go theme={null}
  package main

  import (
      "bytes"
      "encoding/json"
      "fmt"
      "net/http"
  )

  func main() {
      payload := map[string]interface{}{
          "model": "gpt-4o",
          "input": []map[string]interface{}{
              {"type": "message", "role": "user", "content": "Hello!"},
          },
          "max_output_tokens": 1000,
      }
      body, _ := json.Marshal(payload)

      req, _ := http.NewRequest("POST", "https://api.tokenlab.sh/v1/responses", bytes.NewBuffer(body))
      req.Header.Set("Authorization", "Bearer sk-your-api-key")
      req.Header.Set("Content-Type", "application/json")

      client := &http.Client{}
      resp, _ := client.Do(req)
      defer resp.Body.Close()

      var result map[string]interface{}
      json.NewDecoder(resp.Body).Decode(&result)
      fmt.Println(result["output"])
  }
  ```

  ```php PHP theme={null}
  <?php
  $ch = curl_init('https://api.tokenlab.sh/v1/responses');

  curl_setopt_array($ch, [
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_POST => true,
      CURLOPT_HTTPHEADER => [
          'Content-Type: application/json',
          'Authorization: Bearer sk-your-api-key'
      ],
      CURLOPT_POSTFIELDS => json_encode([
          'model' => 'gpt-4o',
          'input' => [
              ['type' => 'message', 'role' => 'user', 'content' => 'Hello!']
          ],
          'max_output_tokens' => 1000
      ])
  ]);

  $response = curl_exec($ch);
  curl_close($ch);

  $data = json_decode($response, true);
  print_r($data['output']);
  ```

  ## 视觉输入示例

  通过将图像放入 `message.content` 中作为 `input_image` 块来使用支持图像的模型。`image_url` 值可以是公开 URL 或 Base64 数据 URL。

  ```json theme={null}
  {
    "model": "gpt-5.4",
    "input": [
      {
        "type": "message",
        "role": "user",
        "content": [
          {
            "type": "input_text",
            "text": "Please describe this image."
          },
          {
            "type": "input_image",
            "image_url": "https://example.com/demo.jpg"
          }
        ]
      }
    ]
  }
  ```

  ```json theme={null}
  {
    "model": "gpt-5.4",
    "input": [
      {
        "type": "message",
        "role": "user",
        "content": [
          {
            "type": "input_text",
            "text": "Please describe this image."
          },
          {
            "type": "input_image",
            "image_url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ..."
          }
        ]
      }
    ]
  }
  ```
</RequestExample>

<ResponseExample>
  ```json Response theme={null}
  {
    "id": "resp_abc123",
    "object": "response",
    "created": 1706000000,
    "model": "gpt-4o",
    "output": [
      {
        "type": "message",
        "role": "assistant",
        "content": [
          {"type": "text", "text": "Hello! How can I help you today?"}
        ]
      }
    ],
    "usage": {
      "input_tokens": 10,
      "output_tokens": 12,
      "total_tokens": 22
    }
  }
  ```
</ResponseExample>
