OpenInference TanStack AI 打开推论 谭斯塔克AI
此软件包提供了用于 谭斯塔克AI。它发出OpenTeleometry 跨度,按照 OpenInference 规格定型,因此 TanStack AI 运行可以在像 : 调整 AX 财务报告和审定财务报表 凤凰。 。 。 .
安装
npm install --save @arizeai/openinference-tanstack-ai @tanstack/ai
您还需要在应用程序中设置 OpenTeleometry 。 例如:
npm install --save @arizeai/phoenix-otel
或:
npm install --save @opentelemetry/api @opentelemetry/sdk-trace-node @opentelemetry/exporter-trace-otlp-proto
安装您计划使用 TanStack AI 的提供者适配器, 例如:
npm install --save @tanstack/ai-openai
使用量
@arizeai/openinference-tanstack-ai 出口 openInferenceMiddleware,直接插入 TanStack AI的 middleware 选项.
import { chat } from "@tanstack/ai";
import { openaiText } from "@tanstack/ai-openai";
import { openInferenceMiddleware } from "@arizeai/openinference-tanstack-ai";
const stream = chat({
adapter: openaiText("gpt-4o-mini"),
messages: [{ role: "user", content: "What is OpenInference?" }],
middleware: [openInferenceMiddleware()],
});
中相器既为流相也为非流相所通的TanStack AI调用.
const text = await chat({
adapter: openaiText("gpt-4o-mini"),
stream: false,
systemPrompts: ["You are a concise technical explainer."],
messages: [{ role: "user", content: "Explain OpenInference in one sentence." }],
middleware: [openInferenceMiddleware()],
});
追踪器设置
此软件包使用您应用程序中已有的 OpenTeleometry tracker 提供者和导出者 。 它本身不出口.
[注] 您的仪表代码应该在中间软件应用前运行. 这保证了追踪器提供者在中间软件开始放出跨度前得到适当配置.
推荐的快速启动是将其与 @arizeai/phoenix-otel。 。 。 .
import { register } from "@arizeai/phoenix-otel";
register({
projectName: "my-tanstack-ai-app",
endpoint: process.env["PHOENIX_COLLECTOR_ENDPOINT"] ?? "http://localhost:6006/v1/traces",
apiKey: process.env["PHOENIX_API_KEY"],
});
如果你已经有一个标准的 OpenTeleometry 设置, 那也是有效的。 例如,一个当地的凤凰城收藏家,一个最低限度的手动设置看起来是这样的:
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
import { Resource } from "@opentelemetry/resources";
import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { SEMRESATTRS_PROJECT_NAME } from "@arizeai/openinference-semantic-conventions";
const tracerProvider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_PROJECT_NAME]: "my-tanstack-ai-app",
}),
spanProcessors: [
new SimpleSpanProcessor(
new OTLPTraceExporter({
url: process.env["PHOENIX_COLLECTOR_ENDPOINT"] ?? "http://localhost:6006/v1/traces",
headers:
process.env["PHOENIX_API_KEY"] == null
? undefined
: {
Authorization: `Bearer ${process.env["PHOENIX_API_KEY"]}`,
},
}),
),
],
});
tracerProvider.register();
自定义跟踪器
默认情况下,中件会为此包使用全局跟踪器. 如果您的应用程序已经设置了请求扫描器或自定义跟踪器,请明确通过.
import { trace } from "@opentelemetry/api";
const tracer = trace.getTracer("tanstack-ai-request");
const middleware = openInferenceMiddleware({ tracer });
当您想要中间软件在不依赖全局默认的情况下参与特定的跟踪器设置时,此功能是有用的.
追踪到什么
中间软件为一个 TanStack AI 运行发出以下跨度结构 :
- 一个
AGENT跨度chat()援引 - 一个
LLM每个模型转弯的间距 - 一个
TOOL每个已执行的工具调用的时间间隔
对于工具循环,追踪一般会像:
AGENTLLM 1TOOLLLM 2
这个 AGENT 跨度捕捉顶级请求和最后回应。 这个 LLM 跨度捕捉提供者/模型元数据、输入信息、输出信息、工具定义、符号计数以及模型的完成原因(llm.finish_reason提供时。 这个 TOOL 横跨.
实例
此软件包包含实例文件 examples/数字 :
examples/chat-with-tools.ts- 一个工具呼叫的 OpenAI 示例examples/anthropic-multi-tool.ts- 有多个工具呼叫的Anthropic实例examples/non-streaming-chat.ts- 带有系统提示的非流化实例
见 examples/README.md 用于设置和运行命令.
页:1
- 这一一揽子措施只针对无害环境管理,因为TanStack AI只针对无害环境管理.
- 中间软件在服务器和客户端环境中都起作用,但客户端/服务器的痕量缝合取决于您的应用程序的上下文传播设置.