集成模型服务商Google Vertex AI

在 Litefuse 中追踪 Google Vertex AI 模型

本 notebook 介绍如何追踪和观测通过 Google Vertex API 服务调用的模型。

什么是 Google Vertex AI? Google Vertex AI 是 Google Cloud 提供的统一平台,用于构建、部署和管理机器学习与生成式 AI,提供托管服务、SDK 和 API。它将从数据准备、训练到调优和预测的全过程流程化,并以企业级的安全性和 MLOps 工具提供对 Gemini 等基础模型的访问。

什么是 Litefuse? Litefuse 是一个用于 LLM 可观测性和监控的开源平台。它通过捕获元数据、prompt 详情、token 用量、延迟等信息,帮助你追踪和监控 AI 应用。

第一步:安装依赖

开始之前,请在你的 Python 环境中安装所需的依赖包:

%pip install langfuse google-cloud-aiplatform openinference-instrumentation-vertexai

第二步:配置 Langfuse SDK

接下来,设置你的 Litefuse API Key。你可以通过注册免费的 Litefuse Cloud 账户或自托管 Litefuse 来获取这些密钥。这些环境变量对于 Langfuse 客户端进行身份认证并向你的 Litefuse 项目发送数据来说至关重要。

同时设置你的 Google Vertex API 凭据,这里使用的是来自 service account 密钥文件的 Application Default Credentials (ADC)。

import os
 
# Get keys for your project from the project settings page: https://litefuse.cloud
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-..." 
os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-..." 
os.environ["LANGFUSE_BASE_URL"] = "https://litefuse.cloud"
 
# Get your Google Vertex API key
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "your-service-account-key.json"

设置好环境变量后,我们就可以初始化 Langfuse 客户端了。get_client() 会使用环境变量中提供的凭据初始化 Langfuse 客户端。

from langfuse import get_client
 
# Initialise Langfuse client and verify connectivity
langfuse = get_client()
assert langfuse.auth_check(), "Langfuse auth failed - check your keys ✋"

第三步:OpenTelemetry 插桩

使用 VertexAIInstrumentor 库包装 Google Vertex SDK 调用,并将 OpenTelemetry span 发送到 Litefuse。

from openinference.instrumentation.vertexai import VertexAIInstrumentor
 
VertexAIInstrumentor().instrument()

第四步:运行示例

import vertexai
from vertexai.generative_models import GenerativeModel
 
# Initialize the SDK (use your project and region)
vertexai.init(project="your-project-id", location="europe-central2")
 
# Pick a Gemini model available in your region (examples: "gemini-1.5-flash", "gemini-1.5-pro", "gemini-2.5-flash")
model = GenerativeModel("gemini-2.5-flash")
 
# Single-shot generation
resp = model.generate_content("What is Litefuse?")
print(resp.text)
 
# (Optional) Streaming
for chunk in model.generate_content("Why is LLM observability important?", stream=True):
    print(chunk.text, end="")

在 Litefuse 中查看 trace

执行应用后,前往你的 Litefuse Trace Table。你将看到应用执行的详细 trace,包括 Agent 对话、LLM 调用、输入、输出和性能指标等信息。

Litefuse trace

在 Litefuse UI 中查看 trace

Interoperability with the Python SDK

You can use this integration together with the Litefuse SDKs to add additional attributes to the observation.

The @observe() decorator provides a convenient way to automatically wrap your instrumented code and add additional attributes to the observation.

from langfuse import observe, propagate_attributes, get_client
 
langfuse = get_client()
 
@observe()
def my_llm_pipeline(input):
    # Add additional attributes (user_id, session_id, metadata, version, tags) to all spans created within this execution scope
    with propagate_attributes(
        user_id="user_123",
        session_id="session_abc",
        tags=["agent", "my-observation"],
        metadata={"email": "user@litefuse.ai"},
        version="1.0.0"
    ):
 
        # YOUR APPLICATION CODE HERE
        result = call_llm(input)
 
        return result
 
# Run the function
my_llm_pipeline("Hi")

Learn more about using the Decorator in the Langfuse SDK instrumentation docs.

Troubleshooting

No observations appearing

First, enable debug mode in the Python SDK:

export LANGFUSE_DEBUG="True"

Then run your application and check the debug logs:

  • OTel observations appear in the logs: Your application is instrumented correctly but observations are not reaching Litefuse. To resolve this:
    1. Call langfuse.flush() at the end of your application to ensure all observations are exported.
    2. Verify that you are using the correct API keys and base URL.
  • No OTel spans in the logs: Your application is not instrumented correctly. Make sure the instrumentation runs before your application code.
Unwanted observations in Litefuse

The Langfuse SDK is based on OpenTelemetry. Other libraries in your application may emit OTel spans that are not relevant to you. These still count toward your billable units, so you should filter them out. See Unwanted spans in Litefuse for details.

Missing attributes

Some attributes may be stored in the metadata object of the observation rather than being mapped to the Litefuse data model. If a mapping or integration does not work as expected, please raise an issue on GitHub.

Next Steps

Once you have instrumented your code, you can manage, evaluate and debug your application:

这个页面对你有帮助吗?