跳到主要内容
您目前正在查看的页面是关于将 Amazon Bedrock 模型用作文本补全模型的文档。Bedrock 上许多流行的模型都是聊天补全模型您可能正在寻找此页面
Amazon Bedrock 是一项完全托管的服务,通过单一 API 提供来自领先 AI 公司(如 AI21 LabsAnthropicCohereMetaStability AIAmazon)的多种高性能基础模型(FM),以及构建具有安全性、隐私性和负责任 AI 的生成式 AI 应用程序所需的一整套功能。使用 Amazon Bedrock,您可以轻松地为您的用例尝试和评估顶级 FM,使用微调和 检索增强生成 (RAG) 等技术使用您的数据私下定制它们,并构建使用您的企业系统和数据源执行任务的代理。由于 Amazon Bedrock 是无服务器的,您无需管理任何基础设施,并且可以使用您已经熟悉的 AWS 服务安全地将生成式 AI 功能集成并部署到您的应用程序中。
pip install -qU langchain-aws
from langchain_aws import BedrockLLM

llm = BedrockLLM(
    credentials_profile_name="bedrock-admin", model_id="amazon.titan-text-express-v1"
)

自定义模型

custom_llm = BedrockLLM(
    credentials_profile_name="bedrock-admin",
    provider="cohere",
    model_id="<Custom model ARN>",  # ARN like 'arn:aws:bedrock:...' obtained via provisioning the custom model
    model_kwargs={"temperature": 1},
    streaming=True,
)

custom_llm.invoke(input="What is the recipe of mayonnaise?")

适用于 Amazon Bedrock 的护栏

适用于 Amazon Bedrock 的护栏根据用例特定策略评估用户输入和模型响应,并提供额外的保护层,无论底层模型如何。护栏可应用于各种模型,包括 Anthropic Claude、Meta Llama 2、Cohere Command、AI21 Labs Jurassic 和 Amazon Titan Text,以及微调模型。注意:适用于 Amazon Bedrock 的护栏目前处于预览阶段,尚未普遍可用。如果您希望访问此功能,请通过您常用的 AWS 支持联系人联系。在本节中,我们将设置一个带有特定护栏的 Bedrock 语言模型,其中包括跟踪功能。
from typing import Any

from langchain_core.callbacks import AsyncCallbackHandler


class BedrockAsyncCallbackHandler(AsyncCallbackHandler):
    # Async callback handler that can be used to handle callbacks from langchain.

    async def on_llm_error(self, error: BaseException, **kwargs: Any) -> Any:
        reason = kwargs.get("reason")
        if reason == "GUARDRAIL_INTERVENED":
            print(f"Guardrails: {kwargs}")


# Guardrails for Amazon Bedrock with trace
llm = BedrockLLM(
    credentials_profile_name="bedrock-admin",
    model_id="<Model_ID>",
    model_kwargs={},
    guardrails={"id": "<Guardrail_ID>", "version": "<Version>", "trace": True},
    callbacks=[BedrockAsyncCallbackHandler()],
)

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.