請求與回應 - Amazon Bedrock

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

請求與回應

請求本文在請求 body 欄位中傳遞到 InvokeModelInvokeModelWithResponseStream。您可以在請求中傳送的承載大小上限為 20MB。

如需詳細資訊,請參閱 https://docs.anthropic.com/claude/reference/messages_post

警告

Claude Sonnet 4.5 和 Claude Haiku 4.5 僅支援其中一個 temperaturetop_p 參數的規格,但無法同時處理兩者。這不適用於任何較舊的模型。

Request

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

{ "anthropic_version": "bedrock-2023-05-31", "anthropic_beta": ["computer-use-2024-10-22"] "max_tokens": int, "system": 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 模型對此參數具有不同的最大值。如需詳細資訊,請參閱模型比較

  • 訊息 – (必要) 輸入訊息。

    • role – 對話回合的角色。有效值為 userassistant

      下限 上限

      0

      2000

    • content – (必要) 對話回合的內容,作為物件陣列。每個物件都包含一個類型欄位,您可以在其中指定下列其中一個值:

      • text – 如果您指定此類型,則必須包含文字欄位,並指定文字提示作為其值。如果陣列中的另一個物件是影像,則此文字提示會套用至影像。

      • image – 如果您指定此類型,則必須包含對應至具有下列欄位之物件的來源欄位:

        • type – (必要) 影像的編碼類型。您可以指定 base64

        • media_type – (必要) 影像的類型。您可以指定下列影像格式。

          • image/jpeg

          • image/png

          • image/webp

          • image/gif

        • data – (必要) 影像的 Base64 編碼影像位元組。影像大小上限為 3.75 MB。影像的高度和寬度上線為 8000 像素。

以下是選用參數。

  • system – (選用) 請求的系統提示。

    系統提示是提供內容和指示給 Anthropic Claude 的一種方式,例如指定特定目標或角色。如需詳細資訊,請參閱 Anthropic 文件中的系統提示

    注意

    您可以使用系統提示搭配 Anthropic Claude 2.1 版或更新版本。

  • anthropic_beta – (選用) anthropic beta 參數是 Beta 版標頭的字串清單,用於表示選擇加入特定的 Beta 版功能。

    注意

    100 萬個字符內容長度變體Claude Sonnet 4可在特定AWS區域中做為「Beta Service」使用,如 AWS服務條款所定義。它受您與 AWS和 AWS服務條款以及適用模型 EULA 的協議約束。如需較長內容請求定價的詳細資訊,請參閱 Amazon Bedrock 定價頁面。適用個別的服務配額 (如需詳細資訊,請參閱 中的Service QuotasAWS 管理主控台)。

    可用的 Beta 版標頭包括下列項目:

    Beta 版功能

    Beta 版標頭

    備註

    電腦使用

    computer-use-2025-01-24

    與 Claude 3.7 Sonnet 相容。

    工具使用

    token-efficient-tools-2025-02-19

    與 Claude 3.7 Sonnet 和 Claude 4+ 相容。

    交錯思考

    Interleaved-thinking-2025-05-14

    相容於 Claude 4+ 模型。

    啟用高達 128K 的輸出字符

    output-128k-2025-02-19

    與 Claude 3.7 Sonnet 相容。

    在 Claude 4+ 模型上進行原始思考的開發人員模式

    dev-full-thinking-2025-05-14

    僅與 Claude 4+ 模型相容。請聯絡您的客戶團隊以存取此 Beta 版。

    100 萬個字符

    context-1m-2025-08-07

    與 Claude Sonnet 4 相容

    內容管理

    context-management-2025-06-27

    與 Claude Sonnet 4.5 和 Claude Haiku 4.5 相容

    努力

    effort-2025-11-24

    與 Claude Opus 4.5 相容

    工具搜尋工具

    tool-search-tool-2025-10-19

    與 Claude Opus 4.5 相容

    工具使用範例

    tool-examples-2025-10-29

    與 Claude Opus 4.5 相容

  • stop_sequences – (選用) 導致模型停止產生的自訂文字序列。Anthropic Claude 模型通常會在自然完成輪換時停止,在此情況下,stop_reason 回應欄位的值為 end_turn。如果您希望模型在遇到自訂文字字串時停止產生,您可以使用 stop_sequences 參數。如果模型遇到其中一個自訂文字字串,stop_reason 回應欄位的值為 stop_sequence,而 stop_sequence 的值包含相符的停止序列。

    項目的數目上限為 8191。

  • temperature – (選用) 注入回應的隨機性量。

    預設 下限 上限

    1

    0

    1

  • top_p – (選用) 使用核心取樣。

    在核心取樣中,Anthropic Claude 會以遞減的機率順序計算每個後續字符所有選項的累積分佈,並在達到 top_p 指定的特定機率時將其切斷。調整取樣參數時,請修改 temperaturetop_p。請勿同時修改兩者。

    預設 下限 上限

    0.999

    0

    1

  • top_k – (選用) 僅針對每個後續字符杖從前 K 個選項中取樣。

    使用 top_k 移除長尾低機率回應。

    預設 下限 上限

    預設為停用。

    0

    500

  • 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 – 由模型控制的顯示高度,以像素為單位。

      預設 下限 上限

      1

      無上限

    • (必要) display_width_px – 模型控制之顯示器的寬度,以像素為單位。

      預設 下限 上限

      1

      無上限

    • (選用) display_number – 要控制的顯示號碼 (僅與 X11 環境相關)。如果指定,工具會在工具定義中提供顯示號碼。

      預設 下限 上限

      0

      N

    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

  • 內容 – 模型產生的內容。傳回為陣列。內容有三種類型:texttool_useimage

    • text – 文字回應。

      • type – 內容的類型。此值為 text

      • text – 如果 type 的值是文字,則包含內容的文字。

    • tool use – 來自模型使用工具的請求。

      • type – 內容的類型。此值為 tool_use

      • id – 模型請求使用的工具 ID。

      • name – 包含所請求工具的名稱。

      • input – 要傳遞至工具的輸入參數。

    • image – 來自模型使用工具的請求。

      • type – 內容的類型。此值為 image

      • source – 包含影像。如需詳細資訊,請參閱多模態提示

  • usage – 您在請求中所提供字符數目的容器,以及模型在回應中產生的字符數目。

    • input_tokens – 請求中的輸入字符數目。

    • output_tokens – 模型在回應中產生的字符數目。

    • stop_sequence – 模型會產生您在 stop_sequences 輸入欄位中指定的其中一個停止序列。

Effort 參數 (Beta 版)

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" }] }