# @arizeai/openinference-genai 中文文档

规范地址：https://npmcn.com/package/%40arizeai/openinference-genai/

版本：0\.4\.1
npm 元数据更新时间：2026-10-23T22:16:38.369Z
中文译文更新时间：2026-11-06T19:16:58.010Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40arizeai/openinference-genai
原文对照：https://npmcn.com/package/%40arizeai/openinference-genai/?lang=original
项目主页：https://github.com/arize-ai/openinference/tree/main/js/packages/openinference-genai
源码仓库：https://github.com/Arize-ai/openinference

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：377 kB，59 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：1 个
- peer 依赖：@opentelemetry/api \>=1\.9\.0，@opentelemetry/semantic-conventions \>=1\.41\.1
- 首次发布：2025-10-20T21:01:06.983Z
- 最新版本发布：2026-10-23T22:16:37.748Z
- 近 30 天下载量：742223（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：22（近 12 个月 21 个）

## 中文简介

将 OpenTeleometry GenAI 跨属性转换为 OpenInference 跨属性的功能

## 安装

```sh
npm install @arizeai/openinference-genai
```

也可以使用：`pnpm add @arizeai/openinference-genai`、`yarn add @arizeai/openinference-genai`、`bun add @arizeai/openinference-genai`

## README

# 打开推论 GenAI

[![npm version](https://badge.fury.io/js/@arizeai%2Fopeninference-genai.svg)](https://badge.fury.io/js/@arizeai%2Fopeninference-genai)

这个软件包提供一套转换的公用设施 [OpenTeleometry GenAI 函数](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/instrumentation/opentelemetry-instrumentation-genai) 横跨属性到 OpenInference 横跨属性.

> \[尖叫声\] OpenTeleometry GenAI公约仍在孵化中,可能包括随时突破变化. 此软件包将尝试将 OpenTeleometry GenAI 属性的一个子集转换为 OpenInference 属性的最佳努力. 目前,属性反映了截至2025年10月的定义.

## 安装

```shell
npm install --save @arizeai/openinference-genai
```

## 使用量

`@arizeai/openinference-genai` 可以作为一组独立的帮助器函数, 或与 SpanProcessor 相配合,以便自动将 OpenTeleometry GenAI 跨度转换为 OpenInference 跨度.

### 独立

您可以通过使用独立的帮助器函数来突变设置的跨度属性 .

> \[大 Span 突变不为 OpenTelemetery SDK 所支持, 因此确保您是 在跨度寿命的最后一英里进行突变(即在将跨度导出为 SpanProcessor 之前).

```ts
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](https://npmcn.com/package/%40arizeai/openinference-genai/examples/export-spans.ts) 用于以下样本代码的可运行版本.

通过安装软件包开始

```shell
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 属性.

```ts
// 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);
  }
}
```

然后用在你选择的处理器里.

```ts
// 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();
```

## 实例

见 [实例](https://npmcn.com/package/%40arizeai/openinference-genai/examples) 用于此软件包中更多可执行示例的目录.

要执行示例, 请运行以下命令:

```shell
cd js/packages/openinference-genai
pnpm install
pnpm -r build
pnpx tsx examples/export-spans.ts
```

## 常见问题

### @arizeai/openinference-genai 是什么？

将 OpenTeleometry GenAI 跨属性转换为 OpenInference 跨属性的功能

### 如何安装 @arizeai/openinference-genai？

在项目目录运行 npm install @arizeai/openinference-genai，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@opentelemetry/api（\>=1\.9\.0）、@opentelemetry/semantic-conventions（\>=1\.41\.1）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @arizeai/openinference-genai 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @arizeai/openinference-genai 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @arizeai/openinference-genai 可以商用吗？

@arizeai/openinference-genai 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @arizeai/openinference-genai 的下载量有多少？

根据 npm 官方下载统计，@arizeai/openinference-genai 在 2026/10/07 – 2026/11/05 这 30 天内被下载 742,223 次，日均约 24,741 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @arizeai/openinference-genai 还在维护吗？

最新版本 0\.4\.1 发布于 2026/10/24（14 天前）；至今共发布 22 个版本，其中过去 12 个月发布了 21 个；首次发布于 2025/10/21。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

