打开推论核心
@arizeai/openinference-core 是OpenInform JS软件包的共同追踪基础。 它规定:
- 上下文属性传播( e)
session.id, (中文(简体) ),user.id、元数据、标记、即时模板) - 跨度包装( o)
withSpan, (中文(简体) ),traceChain, (中文(简体) ),traceAgent, (中文(简体) ),traceTool(中文(简体) ) - 方法装饰器追踪(
@observe(中文(简体) ) - lLM/检索/嵌入/工具以及注释/评价属性的助手
- 通过可选的敏感数据遮盖
OITracer跟踪配置
安装
npm install @arizeai/openinference-core
# Only needed if you want to run the Quick Start example in this README:
npm install @arizeai/openinference-semantic-conventions @opentelemetry/sdk-trace-node @opentelemetry/resources
快速启动( 可启动)
此示例的导出跨度为 stdout并设置了 OpenInference 项目名 .
import {
OpenInferenceSpanKind,
SEMRESATTRS_PROJECT_NAME,
} from "@arizeai/openinference-semantic-conventions";
import {
ConsoleSpanExporter,
NodeTracerProvider,
SimpleSpanProcessor,
} from "@opentelemetry/sdk-trace-node";
import { resourceFromAttributes } from "@opentelemetry/resources";
import { withSpan } from "@arizeai/openinference-core";
const provider = new NodeTracerProvider({
resource: resourceFromAttributes({
[SEMRESATTRS_PROJECT_NAME]: "openinference-core-demo",
}),
spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())],
});
provider.register();
const answerQuestion = withSpan(
async (question: string) => {
return `Answer: ${question}`;
},
{
name: "answer-question",
kind: OpenInferenceSpanKind.CHAIN,
},
);
async function main() {
const answer = await answerQuestion("What is OpenInference?");
console.log(answer);
}
void main();
背景属性
每个设置器返回一个新的 OpenTeleometry 上下文 。 编译以传播请求级别属性 :
setSession(context, { sessionId })setUser(context, { userId })setMetadata(context, metadataObject)setTags(context, string[])setPromptTemplate(context, { template, variables?, version? })setAttributes(context, attributes)
import { context } from "@opentelemetry/api";
import {
setAttributes,
setMetadata,
setPromptTemplate,
setSession,
setTags,
setUser,
} from "@arizeai/openinference-core";
let ctx = context.active();
ctx = setSession(ctx, { sessionId: "sess-42" });
ctx = setUser(ctx, { userId: "user-7" });
ctx = setMetadata(ctx, { tenant: "acme", environment: "prod" });
ctx = setTags(ctx, ["support", "priority-high"]);
ctx = setPromptTemplate(ctx, {
template: "Answer using docs about {topic}",
variables: { topic: "billing" },
version: "v3",
});
ctx = setAttributes(ctx, { "app.request_id": "req-123" });
context.with(ctx, async () => {
// spans started in this context by openinference-core wrappers
// include these propagated attributes automatically
});
如果您使用平面的 OpenTelemetry 跟踪器手动创建跨度, 请明确应用所传播的属性 :
import { context, trace } from "@opentelemetry/api";
import { getAttributesFromContext } from "@arizeai/openinference-core";
const tracer = trace.getTracer("manual-tracer");
const span = tracer.startSpan("manual-span");
span.setAttributes(getAttributesFromContext(context.active()));
span.end();
函数包装器
withSpan
import { OpenInferenceSpanKind } from "@arizeai/openinference-semantic-conventions";
import { withSpan } from "@arizeai/openinference-core";
const retrieve = withSpan(
async (query: string) => {
return [`Document for ${query}`];
},
{
name: "retrieve-documents",
kind: OpenInferenceSpanKind.RETRIEVER,
},
);
traceChain, (中文(简体) ), traceAgent, (中文(简体) ), traceTool,还有朋友
这些包裹呼叫 withSpan 设置 kind 自动。 每一次都有一个
开源词跨度类型( O)traceLLM, (中文(简体) ), traceRetriever, (中文(简体) ), traceReranker, (中文(简体) ),
traceEmbedding, (中文(简体) ), traceGuardrail, (中文(简体) ), traceEvaluator, (中文(简体) ), tracePrompt, (中文(简体) ), traceDecision) (中文(简体) ).
import { traceAgent, traceChain, traceTool } from "@arizeai/openinference-core";
const tracedChain = traceChain(async (q: string) => `chain result: ${q}`, {
name: "rag-chain",
});
const tracedTool = traceTool(async (city: string) => ({ temp: 72, city }), {
name: "weather-tool",
});
const tracedAgent = traceAgent(
async (q: string) => {
const toolResult = await tracedTool("seattle");
return tracedChain(`${q} (${toolResult.temp}F)`);
},
{ name: "qa-agent" },
);
自定义输入/输出处理器
import { getInputAttributes, getRetrieverAttributes, withSpan } from "@arizeai/openinference-core";
const retriever = withSpan(async (query: string) => [`Doc A for ${query}`, `Doc B for ${query}`], {
name: "retriever",
kind: "RETRIEVER",
processInput: (query) => getInputAttributes(query),
processOutput: (documents) =>
getRetrieverAttributes({
documents: documents.map((content, i) => ({
id: `doc-${i}`,
content,
})),
}),
});
装饰者@observe(中文(简体) )
observe 将分类方法与跟踪和保存方法相包装 this 上下文.
应用时使用类型脚本 5+ 标准装饰器 @observe。 。 。 .
import { OpenInferenceSpanKind } from "@arizeai/openinference-semantic-conventions";
import { observe } from "@arizeai/openinference-core";
class ChatService {
@observe({ kind: OpenInferenceSpanKind.CHAIN })
async runWorkflow(message: string) {
return `processed: ${message}`;
}
@observe({ name: "llm-call", kind: OpenInferenceSpanKind.LLM })
async callModel(prompt: string) {
return `model output for: ${prompt}`;
}
}
属性助手 API
使用这些助手生成 OpenInference 兼容属性并附加到相间 :
getLLMAttributes({ provider, modelName, inputMessages, outputMessages, tokenCount, tools, ... })getDecisionAttributes({ provider, system, modelName, requestModelName, responseModelName, tokenCount })getEmbeddingAttributes({ modelName, embeddings })getRetrieverAttributes({ documents })getAnnotationAttributes({ annotations, scope? })getEvaluationAttributes({ evaluations, scope? })getToolAttributes({ name, description?, parameters })getMetadataAttributes(metadataObject)getInputAttributes(input)编号 :getOutputAttributes(output)defaultProcessInput(...args)编号 :defaultProcessOutput(result)
示例
import { trace } from "@opentelemetry/api";
import { getLLMAttributes } from "@arizeai/openinference-core";
const tracer = trace.getTracer("llm-service");
tracer.startActiveSpan("llm-inference", (span) => {
span.setAttributes(
getLLMAttributes({
provider: "openai",
modelName: "gpt-4o-mini",
inputMessages: [{ role: "user", content: "What is OpenInference?" }],
outputMessages: [{ role: "assistant", content: "OpenInference is..." }],
tokenCount: { prompt: 12, completion: 44, total: 56 },
invocationParameters: { temperature: 0.2 },
}),
);
span.end();
});
跟踪配置和编辑OITracer(中文(简体) )
OITracer 将 OpenTeleometry 跟踪器包裹起来, 并可以在写入间隔前修改或删除敏感属性 :
import { trace } from "@opentelemetry/api";
import { OpenInferenceSpanKind } from "@arizeai/openinference-semantic-conventions";
import { OITracer, withSpan } from "@arizeai/openinference-core";
const tracer = new OITracer({
tracer: trace.getTracer("my-service"),
traceConfig: {
hideInputs: true,
hideOutputText: true,
hideEmbeddingVectors: true,
base64ImageMaxLength: 8_000,
},
});
const traced = withSpan(async (prompt: string) => `model response for ${prompt}`, {
tracer,
kind: OpenInferenceSpanKind.LLM,
name: "safe-llm-call",
});
您也可以配置环境变量的掩码 :
OPENINFERENCE_HIDE_LLM_TOOLSOPENINFERENCE_HIDE_INPUTSOPENINFERENCE_HIDE_OUTPUTSOPENINFERENCE_HIDE_INPUT_MESSAGESOPENINFERENCE_HIDE_OUTPUT_MESSAGESOPENINFERENCE_HIDE_INPUT_IMAGESOPENINFERENCE_HIDE_INPUT_TEXTOPENINFERENCE_HIDE_OUTPUT_TEXTOPENINFERENCE_HIDE_EMBEDDING_VECTORSOPENINFERENCE_BASE64_IMAGE_MAX_LENGTHOPENINFERENCE_HIDE_PROMPTS
辅助工具
withSafety({ fn, onError? }): 包围一个函数并返回null错误safelyJSONStringify(value)编号 :safelyJSONParse(value)守卫JSON行动
节点模块中的文档和源代码
一旦你安装了开源核心软件包, 你已经有了完整的 内部本地可用的开源核心文档和源代码 节点 模块. 您的编码代理可以直接读取这些内容 — 没有网络访问 所需经费.
node_modules/@arizeai/openinference-core/src/ # Full source code organized by module
node_modules/@arizeai/openinference-core/docs/ # Official documentation with examples
这意味着你的代理人可以查询准确的 API 签名,执行,和 直接从安装的软件包中使用示例 -- -- 确保它总是使用 实际安装在您的项目中的 SDK 版本 .
文档
- API 参考 : @ arizeai/ openinference- core
- OpenInformation JS 文件: 开放式推论/js
- 源代码 : js/包装/开放核心