主题
追踪 Microsoft Agent Framework 应用
LangSmith 可以捕获 Microsoft Agent Framework 借助其内置 OpenTelemetry 埋点生成的追踪。本指南演示如何自动捕获 Microsoft Agent Framework 智能体的追踪,并发送到 LangSmith 做监控与分析。
安装
安装所需的包:
bash
pip install agent-framework opentelemetry-exporter-otlp-proto-httpbash
uv add agent-framework opentelemetry-exporter-otlp-proto-http设置
1. 配置环境变量
开启智能体的 OpenTelemetry 埋点,并把 OpenTelemetry 环境变量指向 LangSmith 的 OTEL 端点:
bash
export ENABLE_INSTRUMENTATION=true
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://api.smith.langchain.com/otel/v1/traces
export OTEL_EXPORTER_OTLP_HEADERS="x-api-key=<your_langsmith_api_key>,Langsmith-Project=<your_project_name>"2. 在应用中启用 OpenTelemetry
在你的 Microsoft Agent Framework 应用中,用内置的 configure_otel_providers 函数启用 OpenTelemetry 追踪:
python
from agent_framework.observability import configure_otel_providers
# Enable OpenTelemetry tracing
configure_otel_providers(enable_sensitive_data=True)NOTE
设置 enable_sensitive_data=True 可以把输入与输出内容一并记入追踪。若要把敏感数据排除在追踪之外,请设为 False。
3. 创建并运行你的智能体
配置完成后,你的 Microsoft Agent Framework 智能体会自动把追踪发送给 LangSmith:
python
from agent_framework import ChatAgent
from agent_framework.observability import configure_otel_providers
from agent_framework.openai import OpenAIChatClient
# Enable OpenTelemetry tracing
configure_otel_providers(enable_sensitive_data=True)
agent = ChatAgent(
chat_client=OpenAIChatClient(model_id="gpt-4o"),
)
result = await agent.run("What's the capital of Bavaria?")
print(result.text)