Skip to main content
Open In ColabOpen on GitHub

基础架构

注意

您当前所在的页面记录了将Amazon Bedrock模型用作文本完成模型的使用方法。Bedrock上许多流行的模型都是聊天完成模型

您可能想查找的是这个页面

Amazon Bedrock is a fully managed service that offers a choice of high-performing foundation models (FMs) from leading AI companies like AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon via a single API, along with a broad set of capabilities you need to build generative AI applications with security, privacy, and responsible AI. Using Amazon Bedrock, you can easily experiment with and evaluate top FMs for your use case, privately customize them with your data using techniques such as fine-tuning and Retrieval Augmented Generation (RAG), and build agents that execute tasks using your enterprise systems and data sources. Since Amazon Bedrock is serverless, you don't have to manage any infrastructure, and you can securely integrate and deploy generative AI capabilities into your applications using the AWS services you are already familiar with.

%pip install --upgrade --quiet langchain_aws
from langchain_aws import BedrockLLM

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

自定义模型

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()],
)