LangWatch 为 ADK 提供的可观测性(Observability)功能¶
在 ADK 中得到支持。Python
LangWatch 这是一个开源的 LLMOps 平台,用于实现可观测性(observability)、性能评估(evaluation)以及提示生成(prompt optimization)功能。该平台为使用 ADK(Android Development Kit)的代理程序(agents)提供了全面的追踪(tracing)功能。 OpenInference (用于数据分析和机器学习的工具)这使您能够在开发阶段和生产环境中监控、调试并优化您的代理程序(agents)。
概述¶
LangWatch 通过其内置的 OpenTelemetry 支持功能,从 ADK 中捕获相关数据(即“追踪信息”),从而为您提供以下功能:
- 自动追踪(Automatic tracing) - 记录每次代理运行(agent run)、工具调用(tool call)以及模型请求(model request)的所有详细信息(包括相关上下文)。
- 在线评估 - 持续对生产流量进行质量与安全方面的检测(即持续监控生产数据,以确保其符合质量与安全标准)。
- 护栏(Guardrails) - 实时阻止或修改有害的响应行为
- 提示信息管理 - 使用内置的 A/B 测试功能来生成不同的版本(如“测试版”和“优化版”)的提示内容。
- 数据集与实验 - 从真实的系统日志数据中构建评估集,并执行批量实验。
安装¶
安装所需的软件包:
设置¶
在 [此处填写网站地址] 注册账号。 langwatch.ai 或者 **self-host** 首先,登录该平台; 然后,设置您的 API 密钥:
初始化跟踪功能:
import langwatch
from openinference.instrumentation.google_adk import GoogleADKInstrumentor
langwatch.setup(
instrumentors=[GoogleADKInstrumentor()]
)
就这样。所有与 ADK 代理相关的活动现在都会被追踪,并自动发送到您的 LangWatch 仪表板中。
观察;留意¶
在追踪功能被启用后,您可以像往常一样运行您的 ADK 代理程序;所有交互操作都会显示在 LangWatch 中。
import langwatch
from google.adk.agents import Agent
from google.adk.runners import InMemoryRunner
from google.genai import types
from openinference.instrumentation.google_adk import GoogleADKInstrumentor
langwatch.setup(
instrumentors=[GoogleADKInstrumentor()]
)
# Define a tool
def get_weather(city: str) -> dict:
"""Retrieves the current weather report for a specified city.
Args:
city (str): The name of the city.
Returns:
dict: status and result or error msg.
"""
if city.lower() == "new york":
return {
"status": "success",
"report": (
"The weather in New York is sunny with a temperature of 25 degrees"
" Celsius (77 degrees Fahrenheit)."
),
}
else:
return {
"status": "error",
"error_message": f"Weather information for '{city}' is not available.",
}
# Create an agent with tools
agent = Agent(
name="weather_agent",
model="gemini-flash-latest",
description="Agent to answer questions about the weather.",
instruction="You must use the available tools to find an answer.",
tools=[get_weather],
)
app_name = "weather_app"
user_id = "test_user"
session_id = "test_session"
runner = InMemoryRunner(agent=agent, app_name=app_name)
session_service = runner.session_service
await session_service.create_session(
app_name=app_name,
user_id=user_id,
session_id=session_id,
)
# Run the agent — all interactions will be traced
async for event in runner.run_async(
user_id=user_id,
session_id=session_id,
new_message=types.Content(
role="user",
parts=[types.Part(text="What is the weather in New York?")],
),
):
if event.is_final_response():
print(event.content.parts[0].text.strip())
添加自定义元数据¶
使用 @langwatch.trace() 这是一个装饰器(decorator),用于为您的跟踪数据(traces)添加额外的上下文信息。
@langwatch.trace(name="ADK Weather Agent")
def run_agent(user_message: str):
current_trace = langwatch.get_current_trace()
if current_trace:
current_trace.update(
metadata={
"user_id": "user_123",
"agent_name": "weather_agent",
"environment": "production",
}
)
user_msg = types.Content(
role="user", parts=[types.Part(text=user_message)]
)
for event in runner.run(
user_id="demo-user",
session_id="demo-session",
new_message=user_msg,
):
if event.is_final_response():
return event.content.parts[0].text
return "No response generated"