

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

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

本部分介绍使用 Anthropic Claude Messages API 所需的推理参数和代码示例。

**Topics**
+ [Anthropic Claude Messages 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 Messages API 概述
<a name="model-parameters-anthropic-claude-messages-overview"></a>

您可以使用 Messages API 来创建聊天机器人或虚拟助手应用程序。API 管理用户和 Anthropic Claude 模型（助手）之间的对话交流。

**注意**  
本主题介绍如何将AnthropicClaude消息 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 提供了一组统一的参数，适用于所有支持消息的模型。有关更多信息，请参阅 [使用匡威 API 进行推理](conversation-inference.md)。
限制适用于以下操作：`InvokeModel`、`InvokeModelWithResponseStream``Converse`、和`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{"},
]
```

每个输入消息内容可以是单个字符串，也可以是内容块数组，其中每个内容块都有特定的类型。使用字符串是“文本”类型的一个内容块数组的简称。以下输入消息具有同等效果：

```
{"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)。如果您想将现有的[文本补全](model-parameters-anthropic-claude-text-completion.md)提示迁移到 Messages API，请参阅[从文本补全迁移](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)。