Bedrock
Amazon Bedrock 是一項全受管服務,可透過單一 API 提供來自領先 AI 公司(如
AI21 Labs
、Anthropic
、Cohere
、Meta
、Stability AI
和Amazon
)的一系列高效能基礎模型 (FM),以及您需要建立具有安全性、隱私性和負責任 AI 的生成式 AI 應用程式的各種功能。 使用Amazon Bedrock
,您可以輕鬆試驗和評估最適合您使用案例的頂級 FM,使用微調和Retrieval Augmented Generation
(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 的 Guardrails 會根據特定使用案例的政策評估使用者輸入和模型回應,並提供額外的安全防護層,不受基礎模型的影響。 Guardrails 可以應用於各種模型,包括 Anthropic Claude、Meta Llama 2、Cohere Command、AI21 Labs Jurassic 和 Amazon Titan Text,以及微調模型。 請注意:Amazon Bedrock 的 Guardrails 目前為預覽版,尚未正式發布。 如果您想使用此功能,請通過您常用的 AWS 支援管道聯絡我們。 在本節中,我們將設定一個具有特定 Guardrails 的 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