跟踪 Microsoft Agent Framework

MLflow 跟踪为微软开发的灵活、模块化的 AI 智能体框架 Microsoft Agent Framework 提供了自动跟踪功能。MLflow 通过 OpenTelemetry 集成支持对 Microsoft Agent Framework 进行跟踪。
步骤 1:安装库
bash
pip install 'mlflow[genai]>=3.6.0' agent-framework opentelemetry-exporter-otlp-proto-http
步骤 2:启动 MLflow Tracking Server
使用基于 SQL 的后端存储启动 MLflow Tracking Server
bash
mlflow server --backend-store-uri sqlite:///mlflow.db --port 5000
本示例使用 SQLite 作为后端存储。要使用其他类型的 SQL 数据库(如 PostgreSQL、MySQL 和 MSSQL),请按照 后端存储文档 中的说明更改存储 URI。文件型后端存储不支持 OpenTelemetry 摄取。
步骤 3:配置 OpenTelemetry
配置 OpenTelemetry 跟踪器,以便将跟踪信息导出到 MLflow 跟踪服务器的端点。
- 将端点设置为 MLflow 跟踪服务器的
/v1/traces端点 (OTLP)。 - 将
x-mlflow-experiment-id标头设置为 MLflow 实验 ID。如果您没有实验 ID,请使用 Python SDK 或 MLflow UI 创建一个。
python
from agent_framework.observability import setup_observability
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
# Create the OTLP span exporter with endpoint and headers
MLFLOW_TRACKING_URI = "https://:5000"
MLFLOW_EXPERIMENT_ID = "1234567890"
OTEL_EXPORTER_OTLP_ENDPOINT = f"{MLFLOW_TRACKING_URI}/v1/traces"
OTEL_EXPORTER_OTLP_HEADERS = {"x-mlflow-experiment-id": MLFLOW_EXPERIMENT_ID}
exporter = OTLPSpanExporter(
endpoint=OTEL_EXPORTER_OTLP_ENDPOINT, headers=OTEL_EXPORTER_OTLP_HEADERS
)
# enable_sensitive_data=True is required for recording LLM inputs and outputs.
setup_observability(enable_sensitive_data=True, exporters=[exporter])
步骤 4:运行代理
像平常一样在 Python 脚本(例如 agent.py)中定义和调用智能体。Microsoft Agent Framework 将为您的智能体生成跟踪信息并将其发送到 MLflow 跟踪服务器的端点。
python
import asyncio
from pydantic import Field
from random import randint
from typing import Annotated
from agent_framework.openai import OpenAIAssistantsClient
def get_weather(
location: Annotated[str, Field(description="The location to get the weather for.")],
) -> str:
"""Get the weather for a given location."""
conditions = ["sunny", "cloudy", "rainy", "stormy"]
return f"The weather in {location} is {conditions[randint(0, 3)]} with a high of {randint(10, 30)}°C."
async def main():
async with OpenAIAssistantsClient(model_id="gpt-4o-mini").create_agent(
instructions="You are a helpful weather agent.",
tools=get_weather,
) as agent:
query = "What's the weather like in Seattle?"
print(f"User: {query}")
result = await agent.run(query)
print(f"Agent: {result}\n")
# Comment this out if you are using notebook.
if __name__ == "__main__":
asyncio.run(main())
运行脚本以调用智能体。
bash
python agent.py
在 https://:5000 打开 MLflow UI,然后导航到该实验以查看跟踪信息。