> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# ChatBedrock 集成

> 使用 LangChain Python 与 ChatBedrock 聊天模型集成。

本文档将帮助您开始使用 AWS Bedrock [聊天模型](/oss/python/langchain/models)。Amazon Bedrock 是一项全托管服务，通过单一 API 提供来自领先 AI 公司（如 AI21 Labs、Anthropic、Cohere、Meta、Stability AI 和 Amazon）的高性能基础模型（FMs）选择，以及构建具有安全性、隐私性和负责任 AI 的生成式 AI 应用程序所需的一系列广泛功能。使用 Amazon Bedrock，您可以轻松针对您的用例试验和评估顶级 FMs，使用微调和检索增强生成（RAG）等技术利用您的数据对其进行私有定制，并构建利用您的企业系统和数据源执行任务的智能体。由于 Amazon Bedrock 是无服务器的，您无需管理任何基础设施，并且可以使用您已经熟悉的 AWS 服务，安全地将生成式 AI 功能集成并部署到您的应用程序中。

AWS Bedrock 维护一个 [Converse API](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html)，为 Bedrock 模型提供统一的对话接口。此 API 目前尚不支持自定义模型。您可以在此处查看所有[支持的模型列表](https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html)。

<Info>
  **对于不需要使用自定义模型的用户，我们推荐使用 Converse API。可以通过 [`ChatBedrockConverse`](https://reference.langchain.com/python/langchain-aws/chat_models/bedrock_converse/ChatBedrockConverse) 访问。**
</Info>

<Info>
  **Bedrock 上的 Anthropic 模型**

  特别是对于 Anthropic 模型，您可以使用 `ChatAnthropicBedrock`，它扩展了 `ChatAnthropic`，并在 AWS Bedrock 上运行时提供相同的 API。详情请参阅下面的 [ChatAnthropicBedrock 部分](#chatanthropicbedrock)。
</Info>

有关所有 Bedrock 功能和配置的详细文档，请参阅 [API 参考](https://reference.langchain.com/python/langchain-aws/chat_models/bedrock_converse/ChatBedrockConverse)。

## 概述

### 集成详情

| 类                                                                                                                              | 包                                                                       | 可序列化 | [JS 支持](https://js.langchain.com/docs/integrations/chat/bedrock) |                                               下载量                                              |                                              版本                                             |
| :----------------------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------- | :--: | :--------------------------------------------------------------: | :--------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------: |
| [`ChatBedrock`](https://reference.langchain.com/python/langchain-aws/chat_models/bedrock/ChatBedrock)                          | [`langchain-aws`](https://reference.langchain.com/python/langchain-aws) | beta |                                 ✅                                | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-aws?style=flat-square\&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-aws?style=flat-square\&label=%20) |
| [`ChatBedrockConverse`](https://reference.langchain.com/python/langchain-aws/chat_models/bedrock_converse/ChatBedrockConverse) | [`langchain-aws`](https://reference.langchain.com/python/langchain-aws) | beta |                                 ✅                                | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-aws?style=flat-square\&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-aws?style=flat-square\&label=%20) |

### 模型特性

以下特性同时适用于 `ChatBedrock` 和 `ChatBedrockConverse`。

| [工具调用](/oss/python/langchain/tools) | [结构化输出](/oss/python/langchain/structured-output) | [图像输入](/oss/python/langchain/messages#multimodal) | 音频输入 | 视频输入 | [令牌级流式传输](/oss/python/langchain/streaming/) | 原生异步 | [令牌使用量](/oss/python/langchain/models#token-usage) | [对数概率](/oss/python/langchain/models#log-probabilities) |
| :---------------------------------: | :----------------------------------------------: | :-----------------------------------------------: | :--: | :--: | :-----------------------------------------: | :--: | :-----------------------------------------------: | :----------------------------------------------------: |
|                  ✅                  |                         ✅                        |                         ✅                         |   ❌  |   ❌  |                      ✅                      |   ❌  |                         ✅                         |                            ❌                           |

## 设置

要访问 Bedrock 模型，您需要创建一个 AWS 账户，设置 Bedrock API 服务，获取访问密钥 ID 和密钥，并安装 `langchain-aws` 集成包。

### 凭证

请前往 [AWS 文档](https://docs.aws.amazon.com/bedrock/latest/userguide/setting-up.html) 注册 AWS 并设置您的凭证。

或者，`ChatBedrockConverse` 默认会从以下环境变量中读取：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# os.environ["AWS_ACCESS_KEY_ID"] = "..."
# os.environ["AWS_SECRET_ACCESS_KEY"] = "..."

# 除非使用临时凭证，否则不需要。
# os.environ["AWS_SESSION_TOKEN"] = "..."
```

您还需要为您的账户启用模型访问权限，您可以按照[这些说明](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html)操作。

要启用模型调用的自动追踪，请设置您的 [LangSmith](/langsmith/home) API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

LangChain Bedrock 集成位于 `langchain-aws` 包中：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-aws
```

## 实例化

现在我们可以实例化我们的模型对象并生成聊天补全：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_aws import ChatBedrockConverse

llm = ChatBedrockConverse(
    model_id="anthropic.claude-3-5-sonnet-20240620-v1:0",
    # region_name=...,
    # aws_access_key_id=...,
    # aws_secret_access_key=...,
    # aws_session_token=...,
    # temperature=...,
    # max_tokens=...,
    # 其他参数...
)
```

## 调用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
messages = [
    (
        "system",
        "You are a helpful assistant that translates English to French. Translate the user sentence.",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
AIMessage(content="J'adore la programmation.", additional_kwargs={}, response_metadata={'ResponseMetadata': {'RequestId': 'b07d1630-06f2-44b1-82bf-e82538dd2215', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Wed, 16 Apr 2025 19:35:34 GMT', 'content-type': 'application/json', 'content-length': '206', 'connection': 'keep-alive', 'x-amzn-requestid': 'b07d1630-06f2-44b1-82bf-e82538dd2215'}, 'RetryAttempts': 0}, 'stopReason': 'end_turn', 'metrics': {'latencyMs': [488]}, 'model_name': 'anthropic.claude-3-5-sonnet-20240620-v1:0'}, id='run-d09ed928-146a-4336-b1fd-b63c9e623494-0', usage_metadata={'input_tokens': 29, 'output_tokens': 11, 'total_tokens': 40, 'input_token_details': {'cache_creation': 0, 'cache_read': 0}})
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(ai_msg.content)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
J'adore la programmation.
```

### 流式传输

请注意，`ChatBedrockConverse` 在流式传输时会发出内容块：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
for chunk in llm.stream(messages):
    print(chunk)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
content=[] additional_kwargs={} response_metadata={} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd'
content=[{'type': 'text', 'text': 'J', 'index': 0}] additional_kwargs={} response_metadata={} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd'
content=[{'type': 'text', 'text': "'adore la", 'index': 0}] additional_kwargs={} response_metadata={} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd'
content=[{'type': 'text', 'text': ' programmation.', 'index': 0}] additional_kwargs={} response_metadata={} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd'
content=[{'index': 0}] additional_kwargs={} response_metadata={} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd'
content=[] additional_kwargs={} response_metadata={'stopReason': 'end_turn'} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd'
content=[] additional_kwargs={} response_metadata={'metrics': {'latencyMs': 600}, 'model_name': 'anthropic.claude-3-5-sonnet-20240620-v1:0'} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd' usage_metadata={'input_tokens': 29, 'output_tokens': 11, 'total_tokens': 40, 'input_token_details': {'cache_creation': 0, 'cache_read': 0}}
```

您可以使用输出上的 [`text`](https://reference.langchain.com/python/langchain-core/messages/ai/AIMessage) 属性过滤为文本：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
for chunk in llm.stream(messages):
    print(chunk.text, end="|")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
|J|'adore la| programmation.||||
```

### 流式传输工具调用和结构化输出

当使用 Anthropic 模型进行[工具调用](/oss/python/langchain/tools)或[结构化输出](/oss/python/langchain/structured-output)时，工具调用的参数默认以部分 JSON 块的形式流式传输。

为了降低延迟并获得更均匀分布的块，您可以启用 Anthropic 的细粒度工具流式传输测试版：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_aws import ChatBedrockConverse

llm = ChatBedrockConverse(
    model_id="us.anthropic.claude-sonnet-4-5-20250514-v1:0",
    additional_model_request_fields={
        "anthropic_beta": ["fine-grained-tool-streaming-2025-05-14"]
    }
)
```

<Note>
  细粒度工具流式传输在 Claude 4.5+ 模型上受支持。详情请参阅 [Claude 文档](https://platform.claude.com/docs/en/agents-and-tools/tool-use/fine-grained-tool-streaming)。
</Note>

<Warning>
  使用细粒度工具流式传输时，您可能会收到无效或部分的 JSON 输入。请确保在您的代码中处理这些边缘情况。
</Warning>

## 扩展思考

本指南重点介绍如何使用 AWS Bedrock 和 LangChain 的 `ChatBedrockConverse` 集成来实现扩展思考。

### 支持的模型

扩展思考可用于 AWS Bedrock 上的以下 Claude 模型：

| 模型                    | 模型 ID                                          |
| --------------------- | ---------------------------------------------- |
| **Claude Opus 4**     | `anthropic.claude-opus-4-20250514-v1:0`        |
| **Claude Sonnet 4**   | `anthropic.claude-sonnet-4-20250514-v1:0`      |
| **Claude 3.7 Sonnet** | `us.anthropic.claude-3-7-sonnet-20250219-v1:0` |

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_aws import ChatBedrockConverse

llm = ChatBedrockConverse(
    model_id="us.anthropic.claude-sonnet-4-20250514-v1:0",
    region_name="us-west-2",
    max_tokens=4096,
    additional_model_request_fields={
        "thinking": {"type": "enabled", "budget_tokens": 1024},
    },
)

ai_msg = llm.invoke(messages)
ai_msg.content_blocks
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[{'type': 'reasoning',
  'reasoning': 'The user wants me to translate "I love programming" from English to French.\n\n"I love" translates to "J\'aime" in French.\n"Programming" translates to "la programmation" in French.\n\nSo the full translation would be "J\'aime la programmation."',
  'extras': {'signature': 'EpkDCkgIBxABGAIqQGI0KGz8LoVaFwqSAYPN7N+FecI1ZGtb0zpfPr5F8Sb1yxtQHQlmbKUS8JByenWCFGpRKigNaQh1+rLZ59GEX/sSDB+6gxZAT24DJrq4pxoMySVhzwALI6FEC+1UIjDcozOIznjRTYlDWPcYUNYvpt8rwF9IHE38Ha2uqVY8ROJa1tjOMk3OEnbSoV13Pa8q/gETsz+1UwxNX5tgxOa+38jLEryhdFyyAk2JDLrmluZBM6TMrtyzALQvVbZqjpkKAXdtcVCrsz8zUo/LZT1B/92Ukux2dE0O1ZOdcW3tORK+NFLSBaWuqigcFUTDH9XNQoHd2WpQNhl+ypnCItbL2wDRscN/tEBkgGMQugvPmL0LAuLKBmsRKStKRi/RMYGJb3Ft2yEDsRnYNJBJ6TtgxXFvjDwqc/UaI9cIcTxdoVVlsPFsYccpVwirzwAOiz6CSQ1oOQTYJVT90eQ71QW74n1ubbFIZAvDBKk0KG8jK1FGx4FpuuZyFhBpXtfrgOCdrlVSAO/EE9fKCbP9FlhPbRgB'}},
 {'type': 'text', 'text': "J'aime la programmation."}]
```

### 扩展思考的工作原理

当启用扩展思考时，Claude 会创建思考内容块，在其中输出其内部推理。Claude 在构建最终响应之前会结合这些推理的见解。API 响应将包含思考内容块，然后是文本内容块。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
next_messages = messages + [("ai", ai_msg.content), ("human", "I love AI")]

ai_msg = llm.invoke(next_messages)
ai_msg.content_blocks
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[{'type': 'reasoning',
  'reasoning': 'The user wants me to translate "I love AI" from English to French. \n\n"I love" translates to "J\'aime" in French.\n"AI" stands for "Artificial Intelligence" which in French is "Intelligence Artificielle" or "IA" (the French abbreviation).\n\nSo the translation would be "J\'aime l\'IA" or "J\'aime l\'intelligence artificielle".\n\nI think using the abbreviation "IA" would be more natural and concise, similar to how the user used "AI" in English.',
  'extras': {'signature': 'EuAECkgIBxABGAIqQLWbkzJ8RzfxhVN1BhfRj5+On8/M9Utt0yH9kvj9P2zlQkO5xloq6I/AiEeArwwdJeqJVcLRjqLtinh6HIBbSDwSDFwt0GL409TqjSZNBhoMPQtJdZmx/uiPrLHUIjCJXyyjgSK3vzbcSEnsvo7pdpoo+waUFrAPDCGL/CIN5u7c8ueLCuCn8W0qGGc+BNgqxQO6UbV11RnMdnUyFmVgTPJErfzBr6U6KyUHd5dJmFWIUVpbbxT2C9vawpbKMPThaRW3BhItEafWGUpPqztzFhqJpSegXtXehIn5iY4yHzTUZ5FPdkNIuAmTsFNNGxiKr9H/gqknvQ2B7I4ushRHLg+drU4cH18EGZlAo5Tu1O9yH5GbweIEew4Uv7oWje+R8TIku0OFVhrbnQqqqukBicMV2JRifUYuz6dYM1UDYS8SfxQ1MmcVY5t1L9LDpoL4F/CtpL8/6YDsB/FosU37Qc1qm+D+pKEPTYnyxaP5tRXqTBfqUIiNJGqr9Egl17Akoy6NIv234rPfuf8HjTcu5scZoPGhOreG5rWxJ7AbTCIXgGWqpcf2TqDtniOac3jW4OtnlID9fsloKNq6Y5twgXHDR47c4Jh6vWmucZiIlL6hkklQzt5To6vOnqcTOGUtuCis8Y2wRzlNGeR2d8A+ocYm7mBvR/Y5DvDgstJwB/vCLoQlIL+jm6+h8k6EX/24GqOsh5hxsS5IsNIob/p8tr4TBbc9noCoUSYkMhbQPi2xpRrNML9GUIo7Skbh1ni67uqeShj1xuUrFG+cN6x4yzDaRb59LCAYAQ=='}},
 {'type': 'text', 'text': "J'aime l'IA."}]
```

## 提示缓存

Bedrock 支持对提示中的元素进行[缓存](https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html)，包括消息和工具。这允许您重用大型文档、指令、[少样本文档](/langsmith/create-few-shot-evaluators)和其他数据，以降低延迟和成本。

<Note>
  **并非所有模型都支持提示缓存。请参阅 [Bedrock 提示缓存支持的模型](https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html#prompt-caching-models)。**
</Note>

要启用对提示中某个元素的缓存，请使用 `cachePoint` 键标记其关联的内容块。请参阅以下示例：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import requests
from langchain_aws import ChatBedrockConverse

llm = ChatBedrockConverse(model="us.anthropic.claude-sonnet-4-6")

# 拉取 LangChain 自述文件
get_response = requests.get(
    "https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md"
)
readme = get_response.text

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "What's LangChain, according to its README?",
            },
            {
                "type": "text",
                "text": f"{readme}",
            },
            {
                "cachePoint": {"type": "default"},
            },
        ],
    },
]

response_1 = llm.invoke(messages)
response_2 = llm.invoke(messages)

usage_1 = response_1.usage_metadata["input_token_details"]
usage_2 = response_2.usage_metadata["input_token_details"]

print(f"First invocation:\n{usage_1}")
print(f"\nSecond:\n{usage_2}")
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
First invocation:
{'cache_creation': 1528, 'cache_read': 0}

Second:
{'cache_creation': 0, 'cache_read': 1528}
```

## 引用

如果在输入文档上启用了引用功能，则可以生成引用。文档可以以 Bedrock 的[原生格式](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_DocumentBlock.html)或 LangChain 的[标准类型](/oss/python/langchain/messages#multimodal)指定：

<CodeGroup>
  ```python Bedrock 格式 theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain_aws import ChatBedrockConverse

  llm = ChatBedrockConverse(model="us.anthropic.claude-sonnet-4-20250514-v1:0")

  pdf_path = "path/to/your/file.pdf"

  with open(pdf_path, "rb") as f:
      pdf_bytes = f.read()

  document = {
      "document": {
          "format": "pdf",
          "source": {"bytes": pdf_bytes},
          "name": "my-pdf",
          "citations": {"enabled": True},  # [!code highlight]
      },
  }

  response = llm.invoke(
      [
          {
              "role": "user",
              "content": [
                  {"type": "text", "text": "Describe this document."},
                  document,
              ]
          },
      ]
  )
  response.content_blocks
  ```

  ```python LangChain 标准格式 theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import base64
  from langchain_aws import ChatBedrockConverse

  llm = ChatBedrockConverse(model="us.anthropic.claude-sonnet-4-20250514-v1:0")

  pdf_path = "path/to/your/file.pdf"

  with open(pdf_path, "rb") as f:
      pdf_base64 = base64.b64encode(f.read()).decode("utf-8")

  document = {
      "type": "file",
      "mime_type": "application/pdf",
      "base64": pdf_base64,
      "name": "my-pdf",  # Converse 需要一个文件名
      "citations": {"enabled": True},  # [!code highlight]
  }

  response = llm.invoke(
      [
          {
              "role": "user",
              "content": [
                  {"type": "text", "text": "Describe this document."},
                  document,
              ]
          },
      ]
  )
  response.content_blocks
  ```
</CodeGroup>

***

## ChatAnthropicBedrock

对于专门对 AWS Bedrock 上的 Anthropic 模型感兴趣的 AWS Bedrock 用户，`langchain-aws` 提供了 `ChatAnthropicBedrock`。该类扩展了 `ChatAnthropic`，并在 AWS Bedrock 基础设施上运行时提供相同的接口。这利用了 Anthropic SDK 的 [Bedrock 客户端](https://platform.claude.com/docs/en/build-with-claude/claude-on-amazon-bedrock)。

### 安装

安装带有 `anthropic` 额外依赖项的 `langchain-aws` 以获取所需的依赖项：

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install --upgrade "langchain-aws[anthropic]"
  ```

  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add "langchain-aws[anthropic]"
  ```
</CodeGroup>

### 用法

`ChatAnthropicBedrock` 支持与 `ChatAnthropic` 相同的功能和参数。您可以使用 AWS 特定参数初始化它：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_aws import ChatAnthropicBedrock

model = ChatAnthropicBedrock(
    model="us.anthropic.claude-haiku-4-5-20251001-v1:0",
    region_name="us-west-2",
    aws_access_key_id="...",
    aws_secret_access_key="...",
    aws_session_token="...",
)
```

AWS 凭证也可以从环境变量读取或由 boto3 自动发现：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 设置环境变量
# os.environ["AWS_ACCESS_KEY_ID"] = "..."
# os.environ["AWS_SECRET_ACCESS_KEY"] = "..."
# os.environ["AWS_REGION"] = "..."

from langchain_aws import ChatAnthropicBedrock

model = ChatAnthropicBedrock(model="us.anthropic.claude-haiku-4-5-20251001-v1:0")
```

有关可用参数和功能的详细文档，请参阅 [`ChatAnthropic` 集成页面](/oss/python/integrations/chat/anthropic)。

***

## API 参考

有关 `ChatBedrock`、`ChatBedrockConverse` 和 `ChatAnthropicBedrock` 所有功能和配置的详细文档，请参阅 [API 参考](https://reference.langchain.com/python/langchain-aws)。

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\chat\bedrock.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
