集成模型服务商Amazon Bedrock

Amazon Bedrock 集成

Amazon Bedrock 是 AWS 提供的全托管服务,让你可以使用基础模型和自定义模型来生成文本、图像和音频。

将 Litefuse 与 Amazon Bedrock 一起使用时,你可以轻松捕获每个请求的详细 trace 和指标,从而深入了解应用的性能和行为。

Litefuse 中除追踪之外的所有 UI 内功能(playground、LLM-as-a-judge 评估、prompt 实验)都与 Amazon Bedrock 完全兼容——只需在项目设置中添加你的 Bedrock 配置即可。

集成方式

你可以通过以下几种方式为 Amazon Bedrock 捕获 trace 和指标:

  1. 通过已与 Litefuse 集成的应用框架:

  2. 通过代理,例如 LiteLLM

  3. 通过使用 Litefuse 装饰器 包装 Bedrock SDK(参见下方示例

如何包装 Amazon Bedrock SDK(Converse API)

# install requirements
%pip install boto3 langfuse awscli --quiet

认证 AWS 会话

使用具备 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?

示例 trace:https://litefuse.cloud/project/cloramnkj0002jz088vzn1ja4/traces/f01a828c-fed1-45e1-b836-cd74c331597d?observation=512a4d7f-5a6c-461e-bd8f-76f6bdcc91fd

Bedrock Converse API trace

我可以在 Litefuse 中监控 Amazon Bedrock 的成本和 token 使用情况吗?

可以,你可以在 Litefuse 中监控 Bedrock 调用的成本和 token 使用情况。与 LLM 应用框架以及 LiteLLM 代理的原生集成会自动向 Litefuse 上报 token 使用情况。

如果你使用 Litefuse 装饰器或上下文管理器,可以直接上报 token 使用情况,并且(可选)也可以上报成本信息。具体示例见上文。

你可以通过 Litefuse 的 dashboard 或 UI 自定义价格信息(查看文档),以匹配你在 Amazon Bedrock 上所用模型的精确定价。

更多资源

这个页面对你有帮助吗?