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

# Create a response

使用 OpenAI Responses API 格式建立串流或非串流回應。

## 認證

<ParamField header="Authorization" type="string" required>
  Bearer 令牌。在 Authorization 請求標頭中使用您的 API 金鑰作為 Bearer 令牌。

  Format: `Bearer <SUNRA_KEY>`
</ParamField>

## 請求

此端點接受一個 JSON 物件。

<ParamField body="model" type="string" required>
  用於生成回應的模型 ID。在 [sunra.ai/models](https://sunra.ai/models) 瀏覽可用模型。
</ParamField>

<ParamField body="input" type="string | object[]">
  回應請求的輸入。可以是字串或輸入項目的陣列。

  <Expandable title="屬性（陣列時）">
    <ParamField body="type" type="string">
      輸入項目的類型。支援的值：`message`、`item_reference`。
    </ParamField>

    <ParamField body="role" type="string">
      訊息作者的角色。支援的值：`user`、`assistant`、`system`。
    </ParamField>

    <ParamField body="content" type="string | object[]">
      輸入訊息的內容。可以是字串或內容部分的陣列。
    </ParamField>

    <ParamField body="id" type="string">
      項目的 ID。對話歷史中的 `assistant` 訊息為必填。
    </ParamField>

    <ParamField body="status" type="string">
      項目的狀態。對話歷史中的 `assistant` 訊息為必填。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="instructions" type="string | null">
  在模型上下文的第一個項目中插入系統（或開發者）訊息。與 `input` 一起使用時，指令會插入到輸入的開頭。
</ParamField>

<ParamField body="stream" type="boolean" default={false}>
  如果設為 `true`，將使用伺服器傳送事件（SSE）串流傳輸回應。
</ParamField>

<ParamField body="max_output_tokens" type="integer">
  輸出令牌數的上限，包括可見輸出令牌和推理令牌。
</ParamField>

<ParamField body="temperature" type="number" default={1}>
  取樣溫度，介於 0 到 2 之間。較高的值增加隨機性。
</ParamField>

<ParamField body="top_p" type="number" default={1}>
  核取樣參數。作為溫度取樣的替代方案。
</ParamField>

<ParamField body="frequency_penalty" type="number" default={0}>
  介於 -2.0 和 2.0 之間的數字。正值會根據新令牌在文本中的現有頻率進行懲罰。
</ParamField>

<ParamField body="presence_penalty" type="number" default={0}>
  介於 -2.0 和 2.0 之間的數字。正值會根據新令牌是否已出現在文本中進行懲罰。
</ParamField>

<ParamField body="tools" type="object[]">
  模型可以呼叫的工具陣列。

  <Expandable title="屬性">
    <ParamField body="type" type="string" required>
      工具的類型。支援的值：`function`、`web_search_preview`。
    </ParamField>

    <ParamField body="name" type="string">
      函式的名稱。當類型為 `function` 時為必填。
    </ParamField>

    <ParamField body="description" type="string">
      函式的描述。
    </ParamField>

    <ParamField body="parameters" type="object">
      定義函式參數的 JSON Schema 物件。
    </ParamField>

    <ParamField body="strict" type="boolean" default={false}>
      是否啟用嚴格的 schema 遵循。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="tool_choice" type="string | object">
  控制工具選擇行為。支援的字串值：`none`、`auto`、`required`。也可以指定特定函式。
</ParamField>

<ParamField body="parallel_tool_calls" type="boolean" default={true}>
  是否允許模型並行執行工具呼叫。
</ParamField>

<ParamField body="text" type="object">
  文字回應格式的配置。

  <Expandable title="屬性">
    <ParamField body="format" type="object">
      文字格式配置。

      <Expandable title="屬性">
        <ParamField body="type" type="string" required>
          格式類型。支援的值：`text`、`json_object`、`json_schema`。
        </ParamField>

        <ParamField body="name" type="string">
          回應格式的名稱。當類型為 `json_schema` 時為必填。
        </ParamField>

        <ParamField body="schema" type="object">
          JSON schema。當類型為 `json_schema` 時為必填。
        </ParamField>

        <ParamField body="strict" type="boolean">
          是否啟用嚴格的 schema 遵循。
        </ParamField>
      </Expandable>
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="reasoning" type="object">
  推理輸出的配置。

  <Expandable title="屬性">
    <ParamField body="effort" type="string">
      限制推理的力度。支援的值：`low`、`medium`、`high`。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="store" type="boolean" default={true}>
  是否儲存生成的回應以供後續檢索。
</ParamField>

<ParamField body="metadata" type="object">
  可附加到回應的 16 組鍵值對。鍵為最多 64 個字元的字串。值為最多 512 個字元的字串。
</ParamField>

<ParamField body="user" type="string">
  代表您終端使用者的唯一識別碼。最多 128 個字元。
</ParamField>

## 回應

成功的回應物件。

<ResponseField name="id" type="string">
  唯一回應識別碼。
</ResponseField>

<ResponseField name="object" type="string">
  物件類型。始終為 `response`。
</ResponseField>

<ResponseField name="created_at" type="number">
  回應建立時的 Unix 時間戳（以秒為單位）。
</ResponseField>

<ResponseField name="status" type="string">
  回應的狀態。可能的值：`completed`、`failed`、`in_progress`、`cancelled`。
</ResponseField>

<ResponseField name="model" type="string">
  用於生成回應的模型。
</ResponseField>

<ResponseField name="output" type="object[]">
  模型生成的內容項目陣列。

  <Expandable title="屬性">
    <ResponseField name="type" type="string">
      輸出項目的類型。例如 `message`。
    </ResponseField>

    <ResponseField name="id" type="string">
      輸出項目的唯一 ID。
    </ResponseField>

    <ResponseField name="role" type="string">
      角色。始終為 `assistant`。
    </ResponseField>

    <ResponseField name="status" type="string">
      訊息的狀態。例如 `completed`。
    </ResponseField>

    <ResponseField name="content" type="object[]">
      輸出訊息的內容。

      <Expandable title="屬性">
        <ResponseField name="type" type="string">
          內容類型。例如 `output_text`。
        </ResponseField>

        <ResponseField name="text" type="string">
          生成的文字內容。
        </ResponseField>

        <ResponseField name="annotations" type="object[]">
          內容的註解（例如來自網頁搜尋的引用）。
        </ResponseField>
      </Expandable>
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="usage" type="object">
  回應的令牌使用量統計。

  <Expandable title="屬性">
    <ResponseField name="input_tokens" type="integer">
      輸入令牌數。
    </ResponseField>

    <ResponseField name="output_tokens" type="integer">
      輸出令牌數。
    </ResponseField>

    <ResponseField name="total_tokens" type="integer">
      令牌總數。
    </ResponseField>

    <ResponseField name="input_tokens_details" type="object">
      輸入令牌的明細。

      <Expandable title="屬性">
        <ResponseField name="cached_tokens" type="integer">
          快取令牌數。
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="output_tokens_details" type="object">
      輸出令牌的明細。

      <Expandable title="屬性">
        <ResponseField name="reasoning_tokens" type="integer">
          推理令牌數。
        </ResponseField>
      </Expandable>
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="temperature" type="number">
  使用的取樣溫度。
</ResponseField>

<ResponseField name="top_p" type="number">
  使用的核取樣值。
</ResponseField>

<ResponseField name="max_output_tokens" type="integer | null">
  使用的最大輸出令牌數設定。
</ResponseField>

<ResponseField name="error" type="object | null">
  如果生成失敗，則為錯誤物件。
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api-llm.sunra.ai/v1/responses \
    -H "Authorization: Bearer <SUNRA_KEY>" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4o",
      "input": [
        {
          "type": "message",
          "role": "user",
          "content": "Hello, how are you?"
        }
      ]
    }'
  ```

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

  response = requests.post(
      "https://api-llm.sunra.ai/v1/responses",
      headers={
          "Authorization": "Bearer <SUNRA_KEY>",
          "Content-Type": "application/json"
      },
      json={
          "model": "openai/gpt-4o",
          "input": [
              {
                  "type": "message",
                  "role": "user",
                  "content": "Hello, how are you?"
              }
          ]
      }
  )
  print(response.json())
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch("https://api-llm.sunra.ai/v1/responses", {
    method: "POST",
    headers: {
      "Authorization": "Bearer <SUNRA_KEY>",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      model: "openai/gpt-4o",
      input: [
        {
          type: "message",
          role: "user",
          content: "Hello, how are you?"
        }
      ]
    })
  });
  const data = await response.json();
  console.log(data);
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null}
  {
    "id": "resp-abc123",
    "object": "response",
    "created_at": 1704067200,
    "status": "completed",
    "model": "openai/gpt-4o",
    "output": [
      {
        "type": "message",
        "id": "msg_abc123",
        "role": "assistant",
        "status": "completed",
        "content": [
          {
            "type": "output_text",
            "text": "Hello! I'm doing well, thank you for asking. How can I help you today?",
            "annotations": []
          }
        ]
      }
    ],
    "temperature": 1.0,
    "top_p": 1.0,
    "max_output_tokens": null,
    "usage": {
      "input_tokens": 15,
      "output_tokens": 18,
      "total_tokens": 33,
      "input_tokens_details": {
        "cached_tokens": 0
      },
      "output_tokens_details": {
        "reasoning_tokens": 0
      }
    },
    "error": null
  }
  ```
</ResponseExample>
