> ## 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 message

使用 Anthropic Messages API 格式建立訊息。支援文字、圖片、PDF、工具和延伸思考。

## 認證

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

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

## 請求

此端點接受一個 JSON 物件。

<ParamField body="model" type="string" required>
  將完成您提示的模型。在 [sunra.ai/models](https://sunra.ai/models) 瀏覽可用模型。
</ParamField>

<ParamField body="messages" type="object[]" required>
  輸入訊息。每個輸入訊息都有一個 `role` 和 `content`。

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

    <ParamField body="content" type="string | object[]" required>
      訊息的內容。可以是單一字串或內容區塊的陣列。

      <Expandable title="內容區塊類型">
        <ParamField body="type" type="string" required>
          內容區塊的類型。支援的值：`text`、`image`、`tool_use`、`tool_result`。
        </ParamField>

        <ParamField body="text" type="string">
          文字內容。當類型為 `text` 時使用。
        </ParamField>

        <ParamField body="source" type="object">
          圖片來源。當類型為 `image` 時使用。

          <Expandable title="屬性">
            <ParamField body="type" type="string" required>
              來源類型。支援的值：`base64`、`url`。
            </ParamField>

            <ParamField body="media_type" type="string" required>
              圖片的媒體類型。例如 `image/jpeg`、`image/png`、`image/gif`、`image/webp`。
            </ParamField>

            <ParamField body="data" type="string">
              Base64 編碼的圖片資料。當來源類型為 `base64` 時為必填。
            </ParamField>

            <ParamField body="url" type="string">
              圖片 URL。當來源類型為 `url` 時為必填。
            </ParamField>
          </Expandable>
        </ParamField>
      </Expandable>
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="max_tokens" type="integer" required>
  停止前要生成的最大令牌數。請注意，模型可能在達到此上限之前就停止。
</ParamField>

<ParamField body="system" type="string | object[]">
  系統提示。系統提示是一種向模型提供上下文和指令的方式。可以是字串或內容區塊的陣列。
</ParamField>

<ParamField body="stream" type="boolean" default={false}>
  是否使用伺服器傳送事件（SSE）逐步串流傳輸回應。
</ParamField>

<ParamField body="temperature" type="number" default={1}>
  注入到回應中的隨機性量。範圍從 0.0 到 1.0。分析/選擇題任務使用接近 0.0 的 `temperature`，創意和生成任務使用接近 1.0 的值。
</ParamField>

<ParamField body="top_p" type="number">
  使用核取樣。在核取樣中，我們按機率遞減順序計算所有後續令牌選項的累積分佈，並在達到 `top_p` 指定的特定機率時截斷。
</ParamField>

<ParamField body="top_k" type="integer">
  僅從每個後續令牌的前 K 個選項中取樣。用於移除「長尾」低機率回應。僅建議進階使用案例使用。
</ParamField>

<ParamField body="stop_sequences" type="string[]">
  自訂文字序列，將導致模型停止生成。返回的文字不會包含停止序列。
</ParamField>

<ParamField body="tools" type="object[]">
  模型可以使用的工具定義。

  <Expandable title="屬性">
    <ParamField body="name" type="string" required>
      工具的名稱。
    </ParamField>

    <ParamField body="description" type="string">
      此工具功能的描述。
    </ParamField>

    <ParamField body="input_schema" type="object" required>
      此工具輸入的 JSON schema。定義您的工具接受的 `input` 的形狀。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="tool_choice" type="object">
  模型應如何使用提供的工具。

  <Expandable title="屬性">
    <ParamField body="type" type="string" required>
      支援的值：`auto`（預設，模型決定）、`any`（模型必須使用工具）、`tool`（模型必須使用特定工具）。
    </ParamField>

    <ParamField body="name" type="string">
      要使用的工具名稱。當類型為 `tool` 時為必填。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="metadata" type="object">
  描述請求中繼資料的物件。

  <Expandable title="屬性">
    <ParamField body="user_id" type="string">
      與請求關聯的使用者的外部識別碼。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="thinking" type="object">
  延伸思考的配置。啟用後，模型將在回應前進行思考。

  <Expandable title="屬性">
    <ParamField body="type" type="string" required>
      必須為 `enabled`。
    </ParamField>

    <ParamField body="budget_tokens" type="integer" required>
      用於思考的最大令牌數。必須大於或等於 1024。
    </ParamField>
  </Expandable>
</ParamField>

## 回應

成功的訊息回應。

<ResponseField name="id" type="string">
  唯一訊息識別碼，例如 `msg_01XFDUDYJgAACzvnptvVoYEL`。
</ResponseField>

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

<ResponseField name="role" type="string">
  生成訊息的對話角色。始終為 `assistant`。
</ResponseField>

<ResponseField name="content" type="object[]">
  模型生成的內容。這是一個內容區塊的陣列。

  <Expandable title="屬性">
    <ResponseField name="type" type="string">
      內容區塊的類型。可為 `text`、`tool_use` 或 `thinking`。
    </ResponseField>

    <ResponseField name="text" type="string">
      生成的文字。當類型為 `text` 時存在。
    </ResponseField>

    <ResponseField name="id" type="string">
      工具使用區塊的 ID。當類型為 `tool_use` 時存在。
    </ResponseField>

    <ResponseField name="name" type="string">
      工具的名稱。當類型為 `tool_use` 時存在。
    </ResponseField>

    <ResponseField name="input" type="object">
      工具的輸入。當類型為 `tool_use` 時存在。
    </ResponseField>

    <ResponseField name="thinking" type="string">
      思考內容。當類型為 `thinking` 時存在。
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="model" type="string">
  處理請求的模型。
</ResponseField>

<ResponseField name="stop_reason" type="string | null">
  模型停止的原因。可為 `end_turn`（模型到達自然停止點）、`max_tokens`（超過 `max_tokens` 或模型的上限）、`stop_sequence`（生成了您的自訂停止序列之一）或 `tool_use`（模型呼叫了一個或多個工具）。
</ResponseField>

<ResponseField name="stop_sequence" type="string | null">
  生成了哪個自訂停止序列（如果有的話）。
</ResponseField>

<ResponseField name="usage" type="object">
  帳單和速率限制使用量。三個輸入桶互斥——參見 [Token 用量](/zh-Hant/llm/token-usage)。

  <Expandable title="屬性">
    <ResponseField name="input_tokens" type="integer">
      使用的全新輸入令牌數。不包含兩個快取桶。
    </ResponseField>

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

    <ResponseField name="total_tokens" type="integer">
      所有輸入桶加上 `output_tokens`。streaming 回應會省略此欄位。
    </ResponseField>

    <ResponseField name="cache_creation_input_tokens" type="integer">
      用於建立快取條目的輸入令牌數。
    </ResponseField>

    <ResponseField name="cache_read_input_tokens" type="integer">
      從快取讀取的輸入令牌數。
    </ResponseField>

    <ResponseField name="sunra_usage_semantics" type="string | null">
      當 Sunra 已正規化此回應時，會以 `anthropic.exclusive.v1` 出現。請對此值做斷言，而不要從數字推斷所使用的慣例。參見 [Token 用量](/zh-Hant/llm/token-usage)。
    </ResponseField>
  </Expandable>
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api-llm.sunra.ai/v1/messages \
    -H "Authorization: Bearer <SUNRA_KEY>" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "anthropic/claude-sonnet-4-20250514",
      "max_tokens": 1024,
      "messages": [
        {
          "role": "user",
          "content": "Hello, how are you?"
        }
      ]
    }'
  ```

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

  response = requests.post(
      "https://api-llm.sunra.ai/v1/messages",
      headers={
          "Authorization": "Bearer <SUNRA_KEY>",
          "Content-Type": "application/json"
      },
      json={
          "model": "anthropic/claude-sonnet-4-20250514",
          "max_tokens": 1024,
          "messages": [
              {"role": "user", "content": "Hello, how are you?"}
          ]
      }
  )
  print(response.json())
  ```

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

<ResponseExample>
  ```json 200 theme={null}
  {
    "id": "msg_01XFDUDYJgAACzvnptvVoYEL",
    "type": "message",
    "role": "assistant",
    "content": [
      {
        "type": "text",
        "text": "Hello! I'm doing well, thank you for asking. How can I help you today?"
      }
    ],
    "model": "anthropic/claude-sonnet-4-20250514",
    "stop_reason": "end_turn",
    "stop_sequence": null,
    "usage": {
      "input_tokens": 12,
      "output_tokens": 19,
      "total_tokens": 31,
      "sunra_usage_semantics": "anthropic.exclusive.v1"
    }
  }
  ```
</ResponseExample>
