

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 請求與回應
<a name="model-parameters-anthropic-claude-messages-request-response"></a>

請求本文在請求 `body` 欄位中傳遞到 [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) 或 [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html)。

**注意**  
限制適用於下列操作：`InvokeModel`、`Converse`、 `InvokeModelWithResponseStream`和 `ConverseStream`。如需詳細資訊，請參閱 [API 限制](inference-api-restrictions.md)。

**警告**  
Claude Sonnet 4.5 和 Claude Haiku 4.5支援指定 `temperature`或 `top_p` 參數，但不能同時指定兩者。這不適用於任何較舊的模型。

------
#### [ Request ]

Anthropic Claude 具有下列訊息推論呼叫的推論參數。

```
{
    "anthropic_version": "bedrock-2023-05-31", 
    "anthropic_beta": ["computer-use-2024-10-22"] 
    "max_tokens": int,
    "system": string | [{"type": "text", "text": string}],    
    "messages": [
        {
            "role": string,
            "content": [
                { "type": "image", "source": { "type": "base64", "media_type": "image/jpeg", "data": "{{content image bytes}}" } },
                { "type": "text", "text": "{{content text}}" }
      ]
        }
    ],
    "temperature": float,
    "top_p": float,
    "top_k": int,
    "tools": [
        {
                "type": "custom",
                "name": string,
                "description": string,
                "input_schema": json
            
        },
        { 
            "type": "computer_20241022",  
            "name": "computer", 
            "display_height_px": int,
            "display_width_px": int,
            "display_number": 0 int
        },
        { 
            "type": "bash_20241022", 
            "name": "bash"
        },
        { 
            "type": "text_editor_20241022",
            "name": "str_replace_editor"
        }
        
    ],
    "tool_choice": {
        "type" :  string,
        "name" : string,
    },
    

 
    "stop_sequences": [string]
}
```

下列是必要的參數。
+  **anthropic\_version** – (必要) Anthropic 版本。值必須為 `bedrock-2023-05-31`。
+ **max\_tokens** – (必要) 停止之前要產生的字符數目上限。

  請注意，Anthropic Claude 模型可能會在達到 `max_tokens` 的值之前停止產生字符。不同的 Anthropic Claude 模型對此參數具有不同的最大值。如需詳細資訊，請參閱[模型比較](https://docs.anthropic.com/claude/docs/models-overview#model-comparison)。
+ **訊息** – (必要) 輸入訊息。
  + **role** – 對話回合的角色。有效值為 `user` 和 `assistant`。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
  + **content** – (必要) 對話回合的內容，作為物件陣列。每個物件都包含一個**類型**欄位，您可以在其中指定下列其中一個值：
    + `text` – 如果您指定此類型，則必須包含**文字**欄位，並指定文字提示作為其值。如果陣列中的另一個物件是影像，則此文字提示會套用至影像。
    + `image` – 如果您指定此類型，則必須包含對應至具有下列欄位之物件的**來源**欄位：
      + **type** – (必要) 影像的編碼類型。您可以指定 `base64`。
      + **media\_type** – (必要) 影像的類型。您可以指定下列影像格式。
        + `image/jpeg`
        + `image/png`
        + `image/webp` 
        + `image/gif`
      + **data** – (必要) 影像的 Base64 編碼影像位元組。影像大小上限為 3.75 MB。影像的高度和寬度上線為 8000 像素。

以下是選用參數。
+  **system** – (選用) 請求的系統提示。您可以提供系統提示做為字串或內容區塊的陣列 （例如，`[{"type": "text", "text": "your prompt"}]`)。

  系統提示是提供內容和指示給 Anthropic Claude 的一種方式，例如指定特定目標或角色。如需詳細資訊，請參閱 Anthropic 文件中的[系統提示](https://docs.anthropic.com/en/docs/system-prompts)。
**注意**  
您可以使用系統提示搭配 Anthropic Claude 2.1 版或更新版本。
+ **anthropic\_beta** – (選用) anthropic beta 參數是 Beta 版標頭的字串清單，用於表示選擇加入特定的 Beta 版功能。
**注意**  
100 萬個字符內容長度變體Claude Sonnet 4可在特定 AWS 區域中做為「Beta Service」使用，如 AWS 服務條款所定義。它受您與 AWS 和 AWS 服務條款以及適用模型 EULA 的協議約束。如需較長內容請求定價的詳細資訊，請參閱 [Amazon Bedrock 定價](https://aws.amazon.com/bedrock/pricing/)頁面。對於超過 200K個輸入字符的請求，長內容速率適用於整個請求，而不只是超過閾值的字符。適用個別的服務配額 （如需詳細資訊，請參閱 中的**Service Quotas** AWS 管理主控台)。

  可用的 Beta 版標頭包括下列項目：    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
+  **stop\_sequences** – (選用) 導致模型停止產生的自訂文字序列。Anthropic Claude 模型通常會在自然完成輪換時停止，在此情況下，`stop_reason` 回應欄位的值為 `end_turn`。如果您希望模型在遇到自訂文字字串時停止產生，您可以使用 `stop_sequences` 參數。如果模型遇到其中一個自訂文字字串，`stop_reason` 回應欄位的值為 `stop_sequence`，而 `stop_sequence` 的值包含相符的停止序列。

  項目的數目上限為 8191。
+  **temperature** – (選用) 注入回應的隨機性量。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
+  **top\_p** – (選用) 使用核心取樣。

  在核心取樣中，Anthropic Claude 會以遞減的機率順序計算每個後續字符所有選項的累積分佈，並在達到 `top_p` 指定的特定機率時將其切斷。調整取樣參數時，請修改 `temperature` 或 `top_p`。請勿同時修改兩者。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
+  **top\_k** – (選用) 僅針對每個後續字符杖從前 K 個選項中取樣。

  使用 `top_k` 移除長尾低機率回應。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
+  **tools** – (選用) 模型可能使用之工具的定義。
**注意**  
需要 Anthropic Claude 3 模型。

  如果您在請求中包含 `tools`，模型可能會傳回內容 `tool_use` 區塊，代表模型使用這些工具。然後，您可以使用模型產生的工具輸入來執行這些工具，然後使用 `tool_result` 內容區塊選擇性地將結果傳回至模型。

  您可以傳遞下列工具類型：

**Custom**  
自訂工具的定義。
  + (選用) **type** – 工具的類型。如果已定義，請使用值 `custom`。
  + **name** – 工具的名稱。
  + **description** – (選用，但強烈建議) 工具的描述。
  + **input\_schema** – 工具的 JSON 結構描述。

**Computer**  
您搭配電腦使用之電腦工具的定義會使用 API。
  +  **type** – 值必須為 `computer_20241022`。
  + **name** – 值必須為 `computer`。
  + (必要) **display\_height\_px** – 由模型控制的顯示高度，以像素為單位。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
  + (必要) **display\_width\_px** – 模型控制之顯示器的寬度，以像素為單位。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)
  + (選用) **display\_number** – 要控制的顯示號碼 (僅與 X11 環境相關)。如果指定，工具會在工具定義中提供顯示號碼。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)

**bash**  
您搭配電腦使用的 bash 工具定義會使用 API。
  + (選用) **類型** – 值必須為 `bash_20241022`。
  + **name** – 值必須為 `bash`。工具。

**文字編輯器**  
您搭配電腦使用的文字編輯器工具定義會使用 API。
  + (選用) **類型** – 值必須為 `text_editor_20241022`。
  + **name** – 值必須為 `str_replace_editor`。工具。
+  **tool\_choice** – (選用) 指定模型應該如何使用提供的工具。模型可以使用特定工具、任何可用的工具，或自行決定。
**注意**  
需要 Anthropic Claude 3 模型。
  + **type** – 工具選擇類型。可能的值為 `any` (使用任何可用的工具)、`auto` (模型決定) 和 `tool` (使用指定的工具)。
  + **name** – (選用) 要使用的工具名稱。如果您在 `type` 欄位中指定 `tool`，則為必要。

------
#### [ Response ]

Anthropic Claude 模型會傳回訊息推論呼叫的下列欄位。

```
{
    "id": string,
    "model": string,
    "type" : "message",
    "role" : "assistant",
    "content": [
        {
            "type": string,
            "text": string,
            "image" :json,
            "id": string,
            "name":string,
            "input": json
        }
    ],
    "stop_reason": string,
    "stop_sequence": string,
    "usage": {
        "input_tokens": integer,
        "output_tokens": integer
    }
    
}
```

具有新 stop\_reason 值的範例回應：

```
// Example with refusal
{
    "stop_reason": "refusal",
    "content": [
        {
            "type": "text",
            "text": "I can't help with that request."
        }
    ]
}

// Example with tool_use
{
    "stop_reason": "tool_use",
    "content": [
        {
            "type": "tool_use",
            "id": "toolu_123",
            "name": "calculator",
            "input": {"expression": "2+2"}
        }
    ]
}

// Example with model_context_window_exceeded (Claude Sonnet 4.5)
{
    "stop_reason": "model_context_window_exceeded",
    "content": [
        {
            "type": "text",
            "text": "The response was truncated due to context window limits..."
        }
    ]
}
```
+ **id** — 回應的唯一識別符。ID 的格式和長度可能會隨著時間而變更。
+ **model** – 提出請求之 Anthropic Claude 模型的 ID。
+ **stop\_reason** – Anthropic Claude 停止產生回應的原因。
  + **end\_turn** – 模型達到自然停止點
  + **max\_tokens** – 產生的文字超過 `max_tokens` 輸入欄位的值，或超過模型支援的字符數目上限。
  + **stop\_sequence** – 模型會產生您在 `stop_sequences` 輸入欄位中指定的其中一個停止序列。
  + **拒絕** – 由於安全問題，Claude 拒絕產生回應
  + **tool\_use** – Claude 正在呼叫工具，並預期您會執行它
  + **model\_context\_window\_exceeded** – 模型因達到內容視窗限制而停止產生。
    + Claude Sonnet 4.5 的新功能
+ **stop\_sequence** – 結束生成的停止序列。
+ **type** – 回應的類型。值一律為 `message`。
+ **role** – 產生訊息的對話角色。值一律為 `assistant`。
+ **內容** – 模型產生的內容。傳回為陣列。內容有三種類型：*text*、*tool\_use* 和 *image*。
  + *text* – 文字回應。
    + **type** – 內容的類型。此值為 `text`。
    + **text** – 如果 `type` 的值是文字，則包含內容的文字。
  + *tool use* – 來自模型使用工具的請求。
    + **type** – 內容的類型。此值為 `tool_use`。
    + **id** – 模型請求使用的工具 ID。
    + **name** – 包含所請求工具的名稱。
    + **input** – 要傳遞至工具的輸入參數。
  + *image* – 來自模型使用工具的請求。
    + **type** – 內容的類型。此值為 `image`。
    + **source** – 包含影像。如需詳細資訊，請參閱[多模態提示](model-parameters-anthropic-claude-messages.md#model-parameters-anthropic-claude-messages-multimodal-prompts)。
+ **usage** – 您在請求中所提供字符數目的容器，以及模型在回應中產生的字符數目。
  + **input\_tokens** – 請求中的輸入字符數目。
  + **output\_tokens** – 模型在回應中產生的字符數目。
  + **stop\_sequence** – 模型會產生您在 `stop_sequences` 輸入欄位中指定的其中一個停止序列。

------

## Effort 參數 (Beta 版）
<a name="model-parameters-anthropic-claude-effort-parameter"></a>

`effort` 參數是 4.5 Claude Opus 權杖預算的替代方案。此參數會自由地告知應該Claude如何花費字符來產生最佳結果，並在思考、工具呼叫和使用者通訊之間調整字符用量。它可以搭配或不搭配延伸思考模式使用。

工作量參數可設定為：
+ `high` （預設） – 視需要Claude花費任意數量的權杖，以獲得最佳結果
+ `medium` – 平衡字符用量
+ `low` – 保守字符用量

若要使用此功能，您必須傳遞 Beta 版標頭 `effort-2025-11-24`。

請求範例：

```
{
    "anthropic_version": "bedrock-2023-05-31",
    "anthropic_beta": [
        "effort-2025-11-24"
    ],
    "max_tokens": 4096,
    "output_config": {
        "effort": "medium"
    },
    "messages": [{
        "role": "user",
        "content": "Analyze this complex dataset and provide insights"
    }]
}
```