适用于 ADK 的 Phoenix 可观测性(Observability)工具¶
在 ADK 中得到支持。Python
Arize Phoenix 这是一个开源的可观测性(observability)与评估平台,来自…… Arize AI 适用于本地开发、开源软件(OSS)的工作流程以及自托管的跟踪系统。该工具为你的 Google ADK 应用程序提供了全面的跟踪和评估功能。要开始使用,请先注册一个账户。 免费账户.
对于专为基于人工智能的团队和企业打造的全功能生产平台,请使用…… Arize AX 与 ADK 的集成该产品既可作为托管型云服务提供,也可供企业自行部署(即企业自行在本地服务器上安装和使用)。Arize的…… 代理评估指南 和 大语言模型(LLM)评估指南 展示如何利用追踪(traces)功能来支持代理(agents)和大型语言模型(LLM)应用程序的评估工作流程。
概述¶
Phoenix 可以使用相关工具自动从 Google ADK 中收集追踪数据(traces)。 OpenInference 工具集(用于性能分析和代码调试)这使您能够:
- 跟踪代理之间的交互(即代理之间的通信或数据交换行为) - 自动记录每次代理运行的详细信息(包括工具调用、模型请求及响应结果),并附带完整的上下文数据和元数据。
- 评估性能 - 使用自定义或预先构建的评估工具来分析代理的行为,并通过实验来测试不同的代理配置。
- 调试问题 - 分析详细的日志记录,以便快速识别性能瓶颈、工具调用失败的情况以及代理程序的异常行为。
- 自托管的控制系统(即控制软件或应用程序由用户自己部署在服务器上,而非依赖于第三方服务) - 将您的数据存储在自己的基础设施上。
安装¶
1. 安装所需的软件包。¶
设置¶
1. 启动 Phoenix¶
这些说明向您展示了如何使用 Phoenix Cloud。您也可以…… 启动 Phoenix 你可以在笔记本中记录这些内容,也可以通过终端来操作;或者,使用容器(container)来自己托管这些服务。
- 注册一个…… 免费的 Phoenix 账户.
- 在您的新 Phoenix Space 应用的“设置”页面上,创建您的 API 密钥。
- 复制您的终端点(endpoint)地址,其格式应如下:`https://app.phoenix.arize.com/s/[your-space-name]`
请设置您的 Phoenix 终端地址(endpoint)和 API 密钥(API Key)。
import os
os.environ["PHOENIX_API_KEY"] = "ADD YOUR PHOENIX API KEY"
os.environ["PHOENIX_COLLECTOR_ENDPOINT"] = "ADD YOUR PHOENIX COLLECTOR ENDPOINT"
# If you created your Phoenix Cloud instance before June 24th, 2025, set the API key as a header:
# os.environ["PHOENIX_CLIENT_HEADERS"] = f"api_key={os.getenv('PHOENIX_API_KEY')}"
2. 将您的应用程序连接到 Phoenix。¶
from phoenix.otel import register
# Configure the Phoenix tracer
tracer_provider = register(
project_name="my-llm-app", # Default is 'default'
auto_instrument=True # Auto-instrument your app based on installed OI dependencies
)
观察;留意¶
现在,由于您已经完成了跟踪功能的设置,所有来自 Google ADK SDK 的请求都会被传输到 Phoenix 系统中,以便进行监控和评估。
import asyncio
import nest_asyncio
nest_asyncio.apply()
from google.adk.agents import Agent
from google.adk.runners import InMemoryRunner
from google.genai import types
# Define a tool function
def get_weather(city: str) -> dict:
"""Retrieves the current weather report for a specified city.
Args:
city (str): The name of the city for which to retrieve the weather report.
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 using weather tools.",
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
async def main():
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() and event.content and event.content.parts:
print(event.content.parts[0].text.strip())
asyncio.run(main())