Bedrock
Amazon Bedrock 是一项完全托管的服务,通过单一 API 提供来自领先人工智能公司(如
AI21 Labs
、Anthropic
、Cohere
、Meta
、Stability AI
和Amazon
)的高性能基础模型(FM)供您选择,同时还提供构建安全、私密且负责任的生成式 AI 应用程序所需的广泛功能。使用Amazon Bedrock
,您可以轻松地为您的用例尝试和评估顶级 FM,利用微调和检索增强生成
(RAG
) 等技术使用您的数据私密地定制它们,并构建使用您的企业系统和数据源执行任务的智能体。由于Amazon Bedrock
是无服务器的,您无需管理任何基础设施,并且可以使用您熟悉的 AWS 服务安全地将生成式 AI 功能集成并部署到您的应用程序中。
%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()],
)
API 参考:AsyncCallbackHandler