跳转到内容

使用 Galileo 工具实现代理的可观测性(Agent Observability)与评估(Evaluation)

Galileo Galileo 是一个用于 AI 评估和监控的平台,能够为 AI 应用程序提供端到端的追踪、评估和监控功能。该平台支持直接从 ADK(Agent Deployment Kit)中导入 OpenTelemetry (OTel) 的追踪数据,这些数据包括代理运行过程中的各种操作(如工具调用和模型请求等)。

如需更多信息,请参阅《伽利略的……》(Galileo’s……)。 Google ADK(Android Development Kit)集成 docs.

先决条件

安装依赖项

pip install google-adk openinference-instrumentation-google-adk python-dotenv galileo

(注:根据原文要求,此处应保留“Optionally, use the”这一句,因为它是文档中的指令或建议内容。如果需要进一步翻译或解释该内容,请提供更多背景信息。) **翻译结果:** **可选地,可以使用……** **说明:** - “Optionally”表示“可以选择性地使用”或“并非强制性的”; - “use the”指“使用某项工具、方法或资源”。 - 整句话用于说明某项操作或功能是可选的(即用户可以根据需要决定是否使用它)。 requirements.txt from the 已完成的示例.

设置环境变量

配置环境变量:

my_agent/.env
# Gemini environment variables
GOOGLE_GENAI_USE_ENTERPRISE=0
GOOGLE_API_KEY="YOUR_API_KEY"

# Galileo environment variables
GALILEO_API_KEY="YOUR_API_KEY"
GALILEO_PROJECT="YOUR_PROJECT"
GALILEO_LOG_STREAM="YOUR_LOG_STREAM"

配置 OpenTelemetry(必需)

在使用任何 ADK 组件之前,您必须配置一个 OTLP 导出器并设置一个全局跟踪器提供者(global tracer provider),以确保所有跟踪数据(spans)能够被发送到 Galileo 系统中。

# my_agent/agent.py

from dotenv import load_dotenv

load_dotenv()

# OpenTelemetry imports
from opentelemetry.sdk import trace as trace_sdk

# Galileo span processor (auto-configures OTLP headers & endpoint from env vars)
from galileo import otel

# OpenInference instrumentation for ADK
from openinference.instrumentation.google_adk import GoogleADKInstrumentor

# Create tracer provider and register Galileo span processor
tracer_provider = trace_sdk.TracerProvider()
galileo_span_processor = otel.GalileoSpanProcessor()
tracer_provider.add_span_processor(galileo_span_processor)

# Instrument Google ADK with OpenInference (this captures inputs/outputs)
GoogleADKInstrumentor().instrument(tracer_provider=tracer_provider)

示例:追踪一个 ADK 代理程序(Trace an ADK agent)。

现在,你可以在用于设置 OTLP 导出器和追踪器提供者的代码之后,添加用于实现简单当前时间显示功能的代理代码。

# my_agent/agent.py

from google.adk.agents import Agent

def get_current_time(city: str) -> dict:
    """Returns the current time in a specified city."""
    return {"status": "success", "city": city, "time": "10:30 AM"}


root_agent = Agent(
    model="gemini-flash-latest",
    name="root_agent",
    description="Tells the current time in a specified city.",
    instruction=(
        "You are a helpful assistant that tells the current time in cities. "
        "Use the 'get_current_time' tool for this purpose."
    ),
    tools=[get_current_time],
)

使用以下命令运行代理:

adk run my_agent

然后向它提出一个问题:

What time is it in London?
[root_agent]: The current time in London is 10:30 AM.

请查看完整内容。 Google ADK + OpenTelemetry 示例项目 请参考一个已完成的示例(for a completed example)。

在 Galileo 中查看跟踪记录(trace information)。

请选择您的项目,然后查看日志流(Log Stream)中的跟踪记录(traces)和时间跨度(spans)。

Galileo Traces

资源