打开推论 GenAI
这个软件包提供一套转换的公用设施 OpenTeleometry GenAI 函数 横跨属性到 OpenInference 横跨属性.
[尖叫声] OpenTeleometry GenAI公约仍在孵化中,可能包括随时突破变化. 此软件包将尝试将 OpenTeleometry GenAI 属性的一个子集转换为 OpenInference 属性的最佳努力. 目前,属性反映了截至2025年10月的定义.
安装
npm install --save @arizeai/openinference-genai
使用量
@arizeai/openinference-genai 可以作为一组独立的帮助器函数,
或与 SpanProcessor 相配合,以便自动将 OpenTeleometry GenAI 跨度转换为 OpenInference 跨度.
独立
您可以通过使用独立的帮助器函数来突变设置的跨度属性 .
[大 Span 突变不为 OpenTelemetery SDK 所支持, 因此确保您是 在跨度寿命的最后一英里进行突变(即在将跨度导出为 SpanProcessor 之前).
import { convertGenAISpanAttributesToOpenInferenceSpanAttributes } from `@arizeai/openinference-genai`
// obtain a span with OpenTelemetry GenAI attributes from your tracing system
const span: ReadableSpan = {/* ... */}
// convert the span attributes to OpenInference attributes
const openinferenceAttributes = convertGenAISpanAttributesToOpenInferenceSpanAttributes(span.attributes)
// add the OpenInference attributes to the span
span.attributes = {...span.attributes, ...openinferenceAttributes}
块处理器
您可以使用自定义的 TraceExporter 将 OpenTelemetry GenAI 跨度自动转换为 OpenInference 跨度 .
见 实例/导出- spans.ts 用于以下样本代码的可运行版本.
通过安装软件包开始
pnpm add @opentelemetry/api @opentelemetry/core @opentelemetry/exporter-trace-otlp-proto @opentelemetry/sdk-trace-base @opentelemetry/sdk-trace-node @opentelemetry/semantic-conventions @opentelemetry/resources @arizeai/openinference-genai
创建自定义 TraceExporter,将 OpenTelemetry GenAI 属性转换为 OpenInference 属性.
// openinferenceOTLPTraceExporter.ts
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
import type { ReadableSpan } from "@opentelemetry/sdk-trace-base";
import type { ExportResult } from "@opentelemetry/core";
import { convertGenAISpanAttributesToOpenInferenceSpanAttributes } from "@arizeai/openinference-genai";
import type { Mutable } from "@arizeai/openinference-genai/types";
class OpenInferenceOTLPTraceExporter extends OTLPTraceExporter {
export(spans: ReadableSpan[], resultCallback: (result: ExportResult) => void) {
const processedSpans = spans.map((span) => {
const processedAttributes = convertGenAISpanAttributesToOpenInferenceSpanAttributes(
span.attributes,
);
// optionally you can replace the entire attributes object with the
// processed attributes if you want _only_ the OpenInference attributes
(span as Mutable<ReadableSpan>).attributes = {
...span.attributes,
...processedAttributes,
};
return span;
});
super.export(processedSpans, resultCallback);
}
}
然后用在你选择的处理器里.
// instrumentation.ts
import { resourceFromAttributes } from "@opentelemetry/resources";
import { NodeTracerProvider, BatchSpanProcessor } from "@opentelemetry/sdk-trace-node";
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
import { SEMRESATTRS_PROJECT_NAME } from "@arizeai/openinference-semantic-conventions";
import { OpenInferenceOTLPTraceExporter } from "./openinferenceOTLPTraceExporter";
const COLLECTOR_ENDPOINT = process.env.COLLECTOR_ENDPOINT;
const SERVICE_NAME = "openinference-genai-app";
export const provider = new NodeTracerProvider({
resource: resourceFromAttributes({
[ATTR_SERVICE_NAME]: SERVICE_NAME,
[SEMRESATTRS_PROJECT_NAME]: SERVICE_NAME,
}),
spanProcessors: [
new BatchSpanProcessor(
new OpenInferenceOTLPTraceExporter({
url: `${COLLECTOR_ENDPOINT}/v1/traces`,
}),
),
],
});
provider.register();
实例
见 实例 用于此软件包中更多可执行示例的目录.
要执行示例, 请运行以下命令:
cd js/packages/openinference-genai
pnpm install
pnpm -r build
pnpx tsx examples/export-spans.ts