快速开始
安装
bash
# npm
npm install @yingdao-ai/agent-observability
# bun
bun add @yingdao-ai/agent-observability
# pnpm
pnpm add @yingdao-ai/agent-observability前置条件
- Node.js 22+ 或 Bun
- 一个可访问的 Langfuse 实例(自托管或云端)
- Langfuse Public Key 和 Secret Key
最小示例
ts
import { AgentObserver, LangfuseDirectTransport } from "@yingdao-ai/agent-observability"
// 1. 创建 observer
const observer = new AgentObserver({
transport: new LangfuseDirectTransport({
host: "https://langfuse.example.com",
publicKey: process.env.LANGFUSE_PUBLIC_KEY!,
secretKey: process.env.LANGFUSE_SECRET_KEY!,
environment: "prod",
}),
})
// 2. 开始运行
const run = observer.startRun({
id: "session-1",
name: "my_agent",
input: "用户输入",
})
if (run) {
// 3. LLM 调用
run.startGeneration({ model: "openai/gpt-4.1" })
run.appendText("assistant 输出")
run.endGeneration({ usage: { input: 10, output: 3, total: 13 } })
// 4. 结束运行
await observer.endRun("session-1", "最终输出")
// 5. 等待所有数据上报完成
await observer.gate.waitAll()
}环境变量
| 变量 | 说明 |
|---|---|
LANGFUSE_PUBLIC_KEY | Langfuse public key |
LANGFUSE_SECRET_KEY | Langfuse secret key |