本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
請求與回應
請求本文在請求 body 欄位中傳遞到 InvokeModel 或 InvokeModelWithResponseStream。您可以在請求中傳送的承載大小上限為 20MB。
如需詳細資訊,請參閱 https://https://docs.anthropic.com/claude/reference/messages_post
警告
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, "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 – (必要) 停止之前要產生的字符數量上限。
請注意,AnthropicClaude模型可能會在達到 的值之前停止產生字符
max_tokens。不同的AnthropicClaude模型對此參數具有不同的最大值。如需詳細資訊,請參閱模型比較。 -
訊息 – (必要) 輸入訊息。
-
角色 – 對話輪換的角色。有效值為
user和assistant。下限 最大 0
2000
-
內容 – (必要) 對話的內容,做為物件陣列。每個物件都包含一個類型欄位,您可以在其中指定下列其中一個值:
-
text– 如果您指定此類型,則必須包含文字欄位,並指定文字提示做為其值。如果陣列中的另一個物件是映像,則此文字提示會套用至映像。 -
image– 如果您指定此類型,則必須包含對應至具有下列欄位之物件的來源欄位:-
type – (必要) 影像的編碼類型。您可以指定
base64。 -
media_type – (必要) 影像的類型。您可以指定下列影像格式。
-
image/jpeg -
image/png -
image/webp -
image/gif
-
-
data – (必要) 影像的 base64 編碼影像位元組。影像大小上限為 3.75MB。影像的最大高度和寬度為 8000 像素。
-
-
-
以下是選用參數。
-
system – (選用) 請求的系統提示。
系統提示是提供內容和指示給 Anthropic 的一種方式Claude,例如指定特定目標或角色。如需詳細資訊,請參閱 Anthropic 文件中的系統提示
。 注意
您可以使用 2.1 AnthropicClaude版或更新版本的系統提示。
-
anthropic_beta – (選用) anthropic beta 參數是 Beta 標頭的字串清單,用於表示選擇加入特定的 Beta 功能集。
注意
100 萬個字符內容長度變體Claude Sonnet 4可在特定 AWS 區域中做為「Beta Service」使用,如 AWS 服務條款所定義。它受您與 AWS 和 AWS 服務條款以及適用模型 EULA 的協議約束。如需較長內容請求定價的詳細資訊,請參閱 Amazon Bedrock
定價頁面。適用個別的服務配額 (如需詳細資訊,請參閱 中的Service Quotas AWS Management Console)。 可用的 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。
在 4 Claude 個以上的模型上進行原始思考的開發人員模式
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
-
stop_sequences – (選用) 導致模型停止產生的自訂文字序列。 AnthropicClaude模型通常會在自然完成輪換時停止,在此情況下,
stop_reason回應欄位的值為end_turn。如果您希望模型在遇到自訂文字字串時停止產生,您可以使用stop_sequences參數。如果模型遇到其中一個自訂文字字串,stop_reason回應欄位的值為 ,stop_sequence而 的值stop_sequence包含相符的停止序列。項目數量上限為 8191。
-
temperature – (選用) 注入回應的隨機性量。
預設 下限 最大 1
0
1
-
top_p – (選用) 使用核取樣。
在核心取樣中, 會以降低的機率順序AnthropicClaude計算每個後續字符所有選項的累積分佈,並在達到 指定的特定機率時將其切斷
top_p。調整取樣參數時,請修改temperature或top_p。請勿同時修改兩者。預設 下限 最大 0.999
0
1
-
top_k – (選用) 僅針對每個後續字符從頂端 K 選項中抽樣。
使用
top_k移除長尾低機率回應。預設 下限 最大 預設停用
0
500
-
tools – (選用) 模型可能使用之工具的定義。
注意
需要 Anthropic Claude 3 個模型。
如果您在請求
tools中包含 ,模型可能會傳回內容tool_use區塊,代表模型使用這些工具。然後,您可以使用模型產生的工具輸入來執行這些工具,然後使用tool_result內容區塊選擇性地將結果傳回至模型。您可以傳遞下列工具類型:
自訂
自訂工具的定義。
-
(選用) 類型 – 工具的類型。如果已定義,請使用值
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 的格式和長度可能會隨著時間而變更。
-
模型 – 提出請求之AnthropicClaude模型的 ID。
-
stop_reason – AnthropicClaude停止產生回應的原因。
-
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。 -
內容 – 模型產生的內容。傳回為陣列。內容有三種類型:文字、 tool_use 和映像。
-
text – 文字回應。
-
type – 內容的類型。此值為
text。 -
text – 如果 的值
type是文字,則 會包含內容的文字。
-
-
工具使用 – 來自模型使用工具的請求。
-
type – 內容的類型。此值為
tool_use。 -
id – 模型請求使用的工具 ID。
-
name – 包含所請求工具的名稱。
-
input – 要傳遞至工具的輸入參數。
-
-
影像 – 來自模型使用工具的請求。
-
type – 內容的類型。此值為
image。 -
來源 – 包含映像。如需詳細資訊,請參閱多模式提示。
-
-
-
usage – 您在請求中提供的字符數量的容器,以及模型在回應中產生的字符數量。
-
input_tokens – 請求中的輸入字符數量。
-
output_tokens – 模型在回應中產生的權杖數目。
-
stop_sequence – 模型會產生您在
stop_sequences輸入欄位中指定的其中一個停止序列。
-
-