Amazon Bedrock 集成
Amazon Bedrock 是一项 AWS 全托管服务,让你可以使用基础模型与自定义模型来生成文本、图像和音频。
将 Litefuse 与 Amazon Bedrock 配合使用时,你可以轻松地为每一次请求采集详细的 trace 与指标,从而洞察应用的性能与行为。
Litefuse UI 中除 tracing 外的所有功能(playground、LLM-as-a-Judge 评估、prompt 实验)都与 Amazon Bedrock 完全兼容——只需在项目设置中添加你的 Bedrock 配置即可。
集成方式
为 Amazon Bedrock 采集 trace 与指标有以下几种方式:
-
通过已与 Litefuse 集成的应用框架:
-
通过代理网关,例如 LiteLLM
-
通过使用 Litefuse Decorator 包装 Bedrock SDK(参见下方示例)
如何包装 Amazon Bedrock SDK(Converse API)
# install requirements
%pip install boto3 langfuse awscli --quietAWS 会话认证
使用具备 Amazon Bedrock 访问权限的 AWS 角色登录。
AWS_ACCESS_KEY_ID="***"
AWS_SECRET_ACCESS_KEY="***"
AWS_SESSION_TOKEN="***"
import boto3
# used to access Bedrock configuration
bedrock = boto3.client(
service_name="bedrock",
region_name="eu-west-1",
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
aws_session_token=AWS_SESSION_TOKEN
)
# used to invoke the Bedrock Converse API
bedrock_runtime = boto3.client(
service_name="bedrock-runtime",
region_name="eu-west-1",
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
aws_session_token=AWS_SESSION_TOKEN
)# Check which models are available in your account
models = bedrock.list_inference_profiles()
for model in models["inferenceProfileSummaries"]:
print(model["inferenceProfileName"] + " - " + model["inferenceProfileId"])EU Anthropic Claude 3 Sonnet - eu.anthropic.claude-3-sonnet-20240229-v1:0
EU Anthropic Claude 3 Haiku - eu.anthropic.claude-3-haiku-20240307-v1:0
EU Anthropic Claude 3.5 Sonnet - eu.anthropic.claude-3-5-sonnet-20240620-v1:0
EU Meta Llama 3.2 3B Instruct - eu.meta.llama3-2-3b-instruct-v1:0
EU Meta Llama 3.2 1B Instruct - eu.meta.llama3-2-1b-instruct-v1:0设置 Litefuse 凭证
import os
# Get keys for your project from the project settings page
# https://litefuse.cloud
os.environ["LANGFUSE_PUBLIC_KEY"] = ""
os.environ["LANGFUSE_SECRET_KEY"] = ""
os.environ["LANGFUSE_BASE_URL"] = "https://litefuse.cloud"
# Your openai key
os.environ["OPENAI_API_KEY"] = ""设置好环境变量后,我们就可以初始化 Langfuse 客户端。get_client() 会使用环境变量中提供的凭证来初始化 Langfuse 客户端。
from langfuse import get_client
langfuse = get_client()
# Verify connection
if langfuse.auth_check():
print("Langfuse client is authenticated and ready!")
else:
print("Authentication failed. Please check your credentials and host.")包装 Bedrock SDK
from langfuse import observe
from botocore.exceptions import ClientError
@observe(as_type="generation", name="Bedrock Converse")
def wrapped_bedrock_converse(**kwargs):
# 1. extract model metadata
kwargs_clone = kwargs.copy()
input = kwargs_clone.pop('messages', None)
modelId = kwargs_clone.pop('modelId', None)
model_parameters = {
**kwargs_clone.pop('inferenceConfig', {}),
**kwargs_clone.pop('additionalModelRequestFields', {})
}
langfuse.update_current_generation(
input=input,
model=modelId,
model_parameters=model_parameters,
metadata=kwargs_clone
)
# 2. model call with error handling
try:
response = bedrock_runtime.converse(**kwargs)
except (ClientError, Exception) as e:
error_message = f"ERROR: Can't invoke '{modelId}'. Reason: {e}"
langfuse.update_current_generation(level="ERROR", status_message=error_message)
print(error_message)
return
# 3. extract response metadata
response_text = response["output"]["message"]["content"][0]["text"]
langfuse.update_current_generation(
output=response_text,
usage_details={
"input": response["usage"]["inputTokens"],
"output": response["usage"]["outputTokens"],
"total": response["usage"]["totalTokens"]
},
metadata={
"ResponseMetadata": response["ResponseMetadata"],
}
)
return response_text运行示例
# Converesation according to AWS spec including prompting + history
user_message = """You will be acting as an AI personal finance advisor named Alex, created by the company SmartFinance Advisors. Your goal is to provide financial advice and guidance to users. You will be replying to users who are on the SmartFinance Advisors site and who will be confused if you don't respond in the character of Alex.
Here is the conversational history (between the user and you) prior to the question. It could be empty if there is no history:
<history>
User: Hi Alex, I'm really looking forward to your advice!
Alex: Hello! I'm Alex, your AI personal finance advisor from SmartFinance Advisors. How can I assist you with your financial goals today?
</history>
Here are some important rules for the interaction:
- Always stay in character, as Alex, an AI from SmartFinance Advisors.
- If you are unsure how to respond, say "I'm sorry, I didn't quite catch that. Could you please rephrase your question?"
"""
conversation = [
{
"role": "user",
"content": [{"text": user_message}],
}
]
@observe()
def examples_bedrock_converse_api():
responses = {}
responses["anthropic"] = wrapped_bedrock_converse(
modelId="eu.anthropic.claude-3-5-sonnet-20240620-v1:0",
messages=conversation,
inferenceConfig={"maxTokens":500,"temperature":1},
additionalModelRequestFields={"top_k":250}
)
responses["llama3-2"] = wrapped_bedrock_converse(
modelId="eu.meta.llama3-2-3b-instruct-v1:0",
messages=conversation,
inferenceConfig={"maxTokens":500,"temperature":1},
)
return responses
res = examples_bedrock_converse_api()
for key, value in res.items():
print(f"{key.title()}\n{value}\n")Anthropic
Understood. I'll continue to act as Alex, the AI personal finance advisor from SmartFinance Advisors, maintaining that character throughout our interaction. I'll provide financial advice and guidance based on the user's questions and needs. If I'm unsure about something, I'll ask for clarification as instructed. How may I assist you with your financial matters today?
Llama3-2
Hello again! I'm glad you're excited about receiving my advice. How can I assist you with your financial goals today? Are you looking to create a budget, paying off debt, saving for a specific goal, or something else entirely?
能否在 Litefuse 中监控 Amazon Bedrock 的成本和 token 用量?
可以,你可以在 Litefuse 中监控 Bedrock 调用的成本与 token 用量。原生与 LLM 应用框架以及 LiteLLM 代理的集成会自动将 token 用量上报至 Litefuse。
如果你使用 Litefuse Decorator 或上下文管理器,则可以直接上报 token 用量,并可选地上报成本信息。详见上方示例。
你可以通过 Litefuse 仪表盘或 UI 自定义价格信息(查看文档),以匹配你在 Amazon Bedrock 上使用模型的真实定价。
更多资源
- langfuse-genaiops Notebook,由 AWS 团队维护,包含一系列 AWS 特有的示例。
- 自托管指南:在 AWS 上部署 Litefuse。
这个页面对你有帮助吗?