Skip to main content

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.

本页面涵盖所有与 Amazon Web Services (AWS) 平台的 LangChain 集成。

聊天模型

Bedrock 聊天

Amazon Bedrock 是一项完全托管的服务,通过单一 API 提供来自领先的 AI 公司(如 AI21 LabsAnthropicCohereMetaStability AIAmazon)的高性能基础模型(FMs)选择,以及构建生成式 AI 应用所需的安全、隐私和负责任 AI 的一系列广泛功能。使用 Amazon Bedrock,您可以轻松实验和评估适用于您用例的顶级 FMs,使用微调等技术利用您的数据对其进行私有定制和 检索增强生成 (RAG),并构建使用企业系统和数据源执行任务的代理。由于 Amazon Bedrock 是无服务器的,您无需管理任何基础设施,并且可以使用您熟悉的 AWS 服务安全地将生成式 AI 功能集成和部署到您的应用中。
查看 使用示例
from langchain_aws import ChatBedrock

Bedrock 对话

AWS Bedrock 维护了一个 Converse API,为 Bedrock 模型提供统一的对话接口。此 API 尚不支持自定义模型。您可以在此处查看所有 支持的模型列表
我们建议不需要使用自定义模型的用户使用 Converse API。可以通过 ChatBedrockConverse 访问它。
查看 使用示例
from langchain_aws import ChatBedrockConverse

大语言模型

Bedrock

查看 使用示例
from langchain_aws import BedrockLLM

Amazon API Gateway

Amazon API Gateway 是一项完全托管的服务,使开发人员能够轻松创建、发布、维护、监控和保障任意规模的 API。API 充当应用程序访问后端服务中的数据、业务逻辑或功能的“前门”。使用 API Gateway,您可以创建 RESTful API 和 WebSocket API,以支持实时双向通信应用程序。API Gateway 支持容器化和无服务器工作负载,以及 Web 应用程序。 API Gateway 处理接受和处理多达数十万次并发 API 调用的所有任务,包括流量管理、CORS 支持、授权和访问控制、限流、监控和 API 版本管理。API Gateway 没有最低费用或启动成本。您只需为收到的 API 调用和传输出的数据量付费,并且通过 API Gateway 分层定价模型,随着 API 用量的扩展,您可以降低成本。
查看 使用示例
from langchain_community.llms import AmazonAPIGateway

SageMaker 端点

Amazon SageMaker 是一个系统,可以构建、训练和部署 机器学习 (ML) 模型,具有完全托管的基础设施、工具和工作流程。
我们使用 SageMaker 来托管我们的模型并将其作为 SageMaker 端点 暴露。 查看 使用示例
from langchain_aws import SagemakerEndpoint

嵌入模型

Bedrock

查看 使用示例
from langchain_aws import BedrockEmbeddings

SageMaker 端点

查看 使用示例
from langchain_community.embeddings import SagemakerEndpointEmbeddings
from langchain_community.llms.sagemaker_endpoint import ContentHandlerBase

文档加载器

AWS S3 目录和文件

Amazon Simple Storage Service (Amazon S3) 是一项对象存储服务。 AWS S3 目录 AWS S3 存储桶
查看 S3DirectoryLoader 的使用示例 查看 S3FileLoader 的使用示例
from langchain_community.document_loaders import S3DirectoryLoader, S3FileLoader

Amazon Textract

Amazon Textract 是一项机器 学习 (ML) 服务,可自动从扫描文档中提取文本、手写内容和数据。
查看 使用示例
from langchain_community.document_loaders import AmazonTextractPDFLoader

Amazon Athena

Amazon Athena 是一项无服务器交互式分析服务,构建于 开源框架之上,支持开放表格式和文件格式。
查看 使用示例
from langchain_community.document_loaders.athena import AthenaLoader

AWS Glue

AWS Glue 数据目录 是一个集中式元数据 存储库,允许您管理、访问和共享有关存储在 AWS 中的数据的元数据。它充当您的数据资产的元数据存储库, 使各种 AWS 服务和您的应用程序能够高效地查询和连接所需的数据。
查看 使用示例
from langchain_community.document_loaders.glue_catalog import GlueCatalogLoader

向量存储

Amazon OpenSearch 服务

Amazon OpenSearch Service 执行 交互式日志分析、实时应用程序监控、网站搜索等。OpenSearch 是 一个开源分布式搜索和分析套件,源自 ElasticsearchAmazon OpenSearch Service 提供 最新版本的 OpenSearch,支持许多版本的 Elasticsearch,以及由 OpenSearch DashboardsKibana 驱动的 可视化功能。
我们需要安装几个 Python 库。
pip install boto3 requests requests-aws4auth
查看 使用示例
from langchain_community.vectorstores import OpenSearchVectorSearch

Amazon DocumentDB 向量搜索

Amazon DocumentDB(兼容 MongoDB) 使在云中设置、操作和扩展 MongoDB 兼容数据库变得容易。 使用 Amazon DocumentDB,您可以运行相同的应用程序代码,并使用与 MongoDB 相同的驱动程序和工具。 Amazon DocumentDB 的向量搜索结合了基于 JSON 的文档数据库的灵活性和丰富的查询能力与向量搜索的强大功能。

安装和设置

查看 详细配置说明 我们需要安装 pymongo Python 包。
pip install pymongo

在 AWS 上部署 DocumentDB

Amazon DocumentDB(兼容 MongoDB) 是一项快速、可靠且完全托管的数据库服务。Amazon DocumentDB 使在云中设置、操作和扩展 MongoDB 兼容数据库变得容易。 AWS 提供计算、数据库、存储、分析和其他功能的服务。有关所有 AWS 服务的概述,请参阅 使用 Amazon Web Services 进行云计算 查看 使用示例
from langchain_community.vectorstores import DocumentDBVectorSearch

Amazon MemoryDB

Amazon MemoryDB 是一项持久性内存数据库服务,提供超快性能。MemoryDB 兼容 Redis OSS,这是一种流行的开源数据存储, 使您能够使用他们今天已经使用的相同灵活友好的 Redis OSS API 和命令快速构建应用程序。 InMemoryVectorStore 类提供了一个向量存储,用于连接 Amazon MemoryDB。
from langchain_aws.vectorstores.inmemorydb import InMemoryVectorStore

vds = InMemoryVectorStore.from_documents(
            chunks,
            embeddings,
            redis_url="rediss://cluster_endpoint:6379/ssl=True ssl_cert_reqs=none",
            vector_schema=vector_schema,
            index_name=INDEX_NAME,
        )
查看 使用示例

Valkey

Valkey 是一个开源高性能键值数据存储,支持缓存、消息队列等工作负载,并可作为主数据库使用。使用 ValkeyVectorStore 连接 Amazon ElastiCache for ValkeyAmazon MemoryDB for Valkey
from langchain_aws.vectorstores import ValkeyVectorStore
查看 使用示例

检索器

Amazon Kendra

Amazon Kendra 是由 Amazon Web Services (AWS) 提供的智能搜索服务 服务。它利用先进的自然语言处理 (NLP) 和机器学习算法,实现跨组织内各种数据源的强大搜索功能。 Kendra 旨在帮助用户快速准确地找到所需信息, 提高生产力和决策能力。
使用 Kendra,我们可以搜索各种内容类型,包括文档、常见问题解答、知识库、 手册和网站。它支持多种语言,可以理解复杂的查询、同义词和 上下文含义,以提供高度相关的搜索结果。
我们需要安装 langchain-aws 库。
pip install langchain-aws
查看 使用示例
from langchain_aws import AmazonKendraRetriever

Amazon Bedrock(知识库)

Amazon Bedrock 的知识库Amazon Web Services (AWS) 的一项服务,让您能够使用您的 私有数据定制基础模型响应,从而快速构建 RAG 应用程序。
我们需要安装 langchain-aws 库。
pip install langchain-aws
查看 使用示例
from langchain_aws import AmazonKnowledgeBasesRetriever

工具

AWS Lambda

Amazon AWS Lambda 是由 Amazon Web Services (AWS) 提供的无服务器计算服务。它帮助开发人员构建和运行应用程序和服务,而无需 配置或管理服务器。这种无服务器架构使您能够专注于编写和 部署代码,而 AWS 会自动处理扩展、修补和管理运行应用程序所需的 基础设施。
我们需要安装 boto3 Python 库。
pip install boto3
查看 使用示例
from langchain_community.chat_message_histories import DynamoDBChatMessageHistory

Amazon Bedrock AgentCore 浏览器

Amazon Bedrock AgentCore 浏览器 使代理能够通过托管的 Chrome 浏览器与网页交互,用于导航、内容提取和 Web 自动化。
pip install langchain-aws bedrock-agentcore playwright beautifulsoup4
查看 使用示例
from langchain_aws.tools import create_browser_toolkit

# 创建工具包
toolkit, browser_tools = create_browser_toolkit(region="us-west-2")

# 与代理一起使用
agent = create_react_agent(model=llm, tools=browser_tools)
result = await agent.ainvoke(
    {"messages": [{"role": "user", "content": "Go to example.com and get the heading"}]},
    config={"configurable": {"thread_id": "session-1"}}
)

# 完成后清理
await toolkit.cleanup()

Amazon Bedrock AgentCore 代码解释器

Amazon Bedrock AgentCore 代码解释器 使代理能够在安全、托管的沙箱环境中执行 Python、JavaScript 和 TypeScript 代码,用于计算、数据分析和可视化。
pip install langchain-aws bedrock-agentcore
查看 使用示例
from langchain_aws.tools import create_code_interpreter_toolkit

# 创建工具包(异步)
toolkit, code_tools = await create_code_interpreter_toolkit(region="us-west-2")

# 与代理一起使用
agent = create_react_agent(model=llm, tools=code_tools)
result = await agent.ainvoke(
    {"messages": [{"role": "user", "content": "Calculate factorial of 10"}]},
    config={"configurable": {"thread_id": "session-1"}}
)

# 完成后清理
await toolkit.cleanup()

沙箱

AgentCore 沙箱

用于 deepagents 的 Amazon Bedrock AgentCore 代码解释器沙箱后端。

Amazon Neptune

Amazon Neptune 是一项高性能图分析和无服务器数据库,具有卓越的扩展性和可用性。
对于下面的 Cypher 和 SPARQL 集成,我们需要安装 langchain-aws 库。
pip install langchain-aws

使用 Cypher 的 Amazon Neptune

查看 使用示例
from langchain_aws.graphs import NeptuneGraph
from langchain_aws.graphs import NeptuneAnalyticsGraph
from langchain_aws.chains import create_neptune_opencypher_qa_chain

使用 SPARQL 的 Amazon Neptune

from langchain_aws.graphs import NeptuneRdfGraph
from langchain_aws.chains import create_neptune_sparql_qa_chain

内存

Amazon Bedrock AgentCore 内存

Amazon Bedrock AgentCore 内存 提供 LangGraph 代理的托管持久化,支持跨会话的对话历史和状态管理,具有自动扩展和高可用性。
pip install langgraph-checkpoint-aws
from langgraph_checkpoint_aws import AgentCoreMemorySaver

# 创建检查点器
checkpointer = AgentCoreMemorySaver(
    memory_id="your-memory-id",
    region_name="us-west-2"
)

# 与 LangGraph 一起使用
graph = workflow.compile(checkpointer=checkpointer)

# 使用 thread_id 和 actor_id 调用以实现对话持久化
config = {
    "configurable": {
        "thread_id": "user-123",
        "actor_id": "my-agent"  # AgentCore 必需
    }
}
result = graph.invoke({"messages": []}, config)
关键功能:
  • 托管基础设施,无需数据库设置
  • 自动扩展和高可用性
  • 通过 actor_id 隔离支持多代理
  • 静态和传输加密

Amazon Bedrock AgentCore 内存存储

Amazon Bedrock AgentCore 内存存储 提供 长期记忆,具有语义搜索功能,用于 LangGraph 代理,支持跨会话存储和检索用户偏好、事实和提取的记忆。
from langgraph_checkpoint_aws import AgentCoreMemoryStore

# 初始化长期记忆存储
store = AgentCoreMemoryStore(memory_id="your-memory-id", region_name="us-west-2")

# 在预模型钩子中使用以保存和检索记忆
def pre_model_hook(state, config, *, store):
    actor_id = config["configurable"]["actor_id"]
    thread_id = config["configurable"]["thread_id"]
    namespace = (actor_id, thread_id)

    # 保存一条消息
    store.put(namespace, str(uuid.uuid4()), {"message": msg})

    # 搜索相关记忆
    results = store.search(("preferences", actor_id), query="user preferences", limit=5)
    return {"model_input_messages": state["messages"]}

回调

Bedrock 令牌使用情况

from langchain_community.callbacks.bedrock_anthropic_callback import BedrockAnthropicTokenUsageCallbackHandler

SageMaker 跟踪

Amazon SageMaker 是一项完全托管的服务,用于快速 轻松地构建、训练和部署机器学习 (ML) 模型。
Amazon SageMaker 实验Amazon SageMaker 的一项功能 允许您组织、跟踪、 比较和评估 ML 实验和模型版本。
我们需要安装几个 Python 库。
pip install google-search-results sagemaker
查看 使用示例
from langchain_community.callbacks import SageMakerCallbackHandler

Amazon Comprehend 审核链

Amazon Comprehend 是一项自然语言处理 (NLP) 服务, 使用机器学习揭示文本中有价值的见解和联系。
我们需要安装 boto3nltk 库。
pip install boto3 nltk
查看 使用示例
from langchain_experimental.comprehend_moderation import AmazonComprehendModerationChain

运行时

Amazon Bedrock AgentCore 运行时

Amazon Bedrock AgentCore 运行时 提供 托管、无服务器执行,用于 LangGraph 代理,具有内置的可观测性、自动扩展和与其他 AgentCore 服务的无缝集成。
pip install bedrock-agentcore
from bedrock_agentcore.runtime import BedrockAgentCoreApp

app = BedrockAgentCoreApp()

@app.entrypoint
def agent_invocation(payload, context):
    result = graph.invoke({"messages": [{"role": "user", "content": payload["prompt"]}]})
    return {"result": result["messages"][-1].content}

app.run()
使用 AgentCore CLI 部署:
# 配置您的代理
agentcore configure

# 部署到 AgentCore 运行时
agentcore launch -e your_agent.py