

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

# Anthropic Claude 訊息 API
<a name="model-parameters-anthropic-claude-messages"></a>

本節提供使用 Anthropic Claude 訊息 API 的推論參數和程式碼範例。

**Topics**
+ [Anthropic Claude 訊息 API 概觀](#model-parameters-anthropic-claude-messages-overview)
+ [工具使用](model-parameters-anthropic-claude-messages-tool-use.md)
+ [延伸思考](claude-messages-extended-thinking.md)
+ [適應性思維](claude-messages-adaptive-thinking.md)
+ [思考加密](claude-messages-thinking-encryption.md)
+ [跨模型版本思考的差異](claude-messages-thinking-differences.md)
+ [壓縮](claude-messages-compaction.md)
+ [從模型取得經過驗證的 JSON 結果](claude-messages-structured-outputs.md)
+ [請求與回應](model-parameters-anthropic-claude-messages-request-response.md)
+ [程式碼範例](api-inference-examples-claude-messages-code-examples.md)
+ [支援的模型](claude-messages-supported-models.md)

## Anthropic Claude 訊息 API 概觀
<a name="model-parameters-anthropic-claude-messages-overview"></a>

您可以使用訊息 API 來建立聊天機器人或虛擬助理應用程式。API 會管理使用者與 Anthropic Claude 模型 (助手) 之間的對話式交換。

**注意**  
本主題說明如何搭配使用 Anthropic Claude 訊息 API 與基本推論操作 ([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))。不過，我們建議您使用 Converse API 在應用程式中實作訊息。Converse API 提供一組統一的參數，可用於支援訊息的所有模型。如需詳細資訊，請參閱[使用 Converse API 進行推論](conversation-inference.md)。
限制適用於下列操作：`InvokeModel`、`Converse`、 `InvokeModelWithResponseStream`和 `ConverseStream`。如需詳細資訊，請參閱 [API 限制](inference-api-restrictions.md)。

Anthropic 會訓練 Claude 模型在交替使用者和助理對話回合時操作。建立新訊息時，您可以使用訊息參數指定先前的對話回合。模型接著會在對話中產生下一個訊息。

每個輸入訊息必須是具有角色和內容的物件。您可以指定單一使用者角色訊息，也可以包含多個使用者和助理訊息。

如果您使用從 Claude 預先填入回應的技術 (使用最終助理角色訊息在 Claude 回應的開頭填入)，Claude 將透過從您離開的位置挑選來回應。使用此技術時，Claude 仍會傳回具有助理角色的回應。

如果最終訊息使用助理角色，回應內容會立即從該訊息中的內容繼續。您可以使用它來限制模型回應的一部分。

具有單一使用者訊息的範例：

```
[{"role": "user", "content": "Hello, Claude"}]
```

具有多個對話回合的範例：

```
[
  {"role": "user", "content": "Hello there."},
  {"role": "assistant", "content": "Hi, I'm Claude. How can I help you?"},
  {"role": "user", "content": "Can you explain LLMs in plain English?"},
]
```

來自 Claude 部分填入回應的範例：

```
[
  {"role": "user", "content": "Please describe yourself using only JSON"},
  {"role": "assistant", "content": "Here is my JSON description:\n{"},
]
```

每個輸入訊息內容可能是單一字串或內容區塊陣列，其中每個區塊都有特定類型。對於類型為 "text" 的單一內容區塊陣列，使用字串是一種速記。下列輸入訊息相當於：

```
{"role": "user", "content": "Hello, Claude"}
```

```
{"role": "user", "content": [{"type": "text", "text": "Hello, Claude"}]}
```

如需有關建立 Anthropic Claude 模型提示的資訊，請參閱 Anthropic Claude 文件中的[提示簡介](https://docs.anthropic.com/claude/docs/intro-to-prompting)。如果您有要遷移至訊息 API 的現有[文字完成](model-parameters-anthropic-claude-text-completion.md)提示，請參閱[從文字完成遷移](https://docs.anthropic.com/claude/reference/migrating-from-text-completions-to-messages)。

**重要**  
Anthropic Claude 3.7 Sonnet 和 Claude 4 模型的推論呼叫逾時期間為 60 分鐘。根據預設， AWS SDK 用戶端會在 1 分鐘後逾時。建議您將 AWS SDK 用戶端的讀取逾時期間增加到至少 60 分鐘。例如，在 AWS Python botocore SDK 中，將 [botocore.config](https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html#) 中的 `read_timeout` 欄位值變更為至少 3600。

### 系統提示
<a name="model-parameters-anthropic-claude-messages-system-prompts"></a>

您也可以在請求中包含系統提示。系統提示可讓您提供內容和指示給 Anthropic Claude，例如指定特定目標或角色。在 `system` 欄位中指定系統提示，如下列範例所示。

```
"system": "You are Claude, an AI assistant created by Anthropic to be helpful,
                harmless, and honest. Your goal is to provide informative and substantive responses
                to queries while avoiding potential harms."
```

如需詳細資訊，請參閱 Anthropic 文件中的[系統提示](https://docs.anthropic.com/en/docs/system-prompts)。

### 多模態提示
<a name="model-parameters-anthropic-claude-messages-multimodal-prompts"></a>

多模態提示會在單一提示中結合多個模態 (影像和文字)。您可以在 `content` 輸入欄位中指定模態。下列範例顯示您可以如何要求 Anthropic Claude 描述所提供影像的內容。如需範例程式碼，請參閱 [多模態程式碼範例](api-inference-examples-claude-messages-code-examples.md#api-inference-examples-claude-multimodal-code-example)。

```
{
    "anthropic_version": "bedrock-2023-05-31", 
    "max_tokens": 1024,
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": "image/jpeg",
                        "data": "iVBORw..."
                    }
                },
                {
                    "type": "text",
                    "text": "What's in these images?"
                }
            ]
        }
    ]
}
```

您在請求中包含的每個影像都會計入您的字符用量。如需詳細資訊，請參閱 Anthropic 文件中的[影像成本](https://docs.anthropic.com/claude/docs/vision#image-costs)。