跳到主要内容

用于追踪的 MLflow Typescript SDK

期待您的反馈

MLflow Typescript SDK 是一个实验性软件包。我们正在积极开发中,并期待您的反馈。请在GitHub上提出功能请求或错误报告。

MLflow 的 Typescript SDK 通过将MLflow 的追踪功能引入 Typescript 和 Javascript,赋能 AI 应用程序开发者。只需最少的代码更改,您就可以使用 MLflow 强大的可观察性功能调试评估监控您的应用程序,并充分利用值得信赖的端到端 MLOps 平台。

快速入门

如果您是 MLflow 追踪的新手,请从以下快速入门指南开始

安装

npm install mlflow-tracing

安装后,按照连接您的应用程序与 MLflow 服务器指南将您的应用程序连接到 MLflow 服务器。

基本用法

// Wrap a function with mlflow.trace to generate a span when the function is called.
const getWeather = mlflow.trace(
(city: string) => {
return `The weather in ${city} is sunny`;
},
// Pass options to set span name.
{ name: 'get-weather' }
);

// Invoke the function as usual, MLflow will automatically create a span and capture
// inputs, outputs, latency, and exception information.
getWeather('San Francisco');

// Alternatively, start and end spans manually.
const span = mlflow.startSpan({ name: 'my-span', inputs: { message: 'Hi, MLflow!' } });
span.end({ outputs: { message: 'Hi, what can I do for you?' } });

自动追踪

MLflow Tracing Typescript SDK 目前支持以下库的自动追踪

OpenAI Logo

手动追踪

使用 `trace` API 追踪函数

`trace` API 在您想要追踪函数时很有用。

import * as mlflow from "mlflow-tracing";

const getWeather = async (city: string) => {
return `The weather in ${city} is sunny`;
};

// Wrap the function with mlflow.trace to create a traced function.
const tracedGetWeather = mlflow.trace(
getWeather,
{ name: 'get-weather' }
);

// Invoke the traced function as usual.
await tracedGetWeather('San Francisco');

在被追踪函数调用时,MLflow 将自动创建一个 span,捕获

  • 输入参数
  • 返回值
  • 如果抛出异常,则包含异常信息
  • 延迟

捕获嵌套函数调用

如果您追踪嵌套函数,MLflow 将生成一个包含多个 span 的追踪,其中 span 结构捕获了嵌套函数调用。

const sum = mlflow.trace(
(a: number, b: number) => {
return a + b;
},
{ name: 'sum' }
);

const multiply = mlflow.trace(
(a: number, b: number) => {
return a * b;
},
{ name: 'multiply' }
);

const computeArea = mlflow.trace(
(a: number, b: number, h: number) => {
const sumOfBase = sum(a, b);
const area = multiply(sumOfBase, h);
return multiply(area, 0.5);
},
{ name: 'compute-area' }
);

computeArea(1, 2, 3);

追踪将如下所示

- compute-area
- sum (a=1, b=2)
- multiply (a=3, b=3)
- multiply (a=9, b=0.5)

使用 `@trace` API 追踪类方法

TypeScript 5.0+ 版本支持装饰器。MLflow Tracing 支持此语法,以便轻松追踪类方法。MLflow 将自动创建一个 span,捕获

  • 输入参数
  • 返回值
  • 如果抛出异常,则包含异常信息
  • 延迟
import * as mlflow from "mlflow-tracing";

class MyClass {
@mlflow.trace({ spanType: mlflow.SpanType.LLM })
generateText(prompt: string) {
return "It's sunny in Seattle!";
}
}

const myClass = new MyClass();
myClass.generateText("What's the weather like in Seattle?");

使用 `withSpan` API 追踪代码块

`withSpan` API 在您想要追踪代码块而非函数时很有用。

import * as mlflow from "mlflow-tracing";

const question = "What's the weather like in Seattle?";

const result = await mlflow.withSpan(
async (span: mlflow.Span) => {
return "It's sunny in Seattle!";
},
// Pass name, span type, and inputs as options.
{
name: "generateText",
spanType: mlflow.SpanType.TOOL,
inputs: { prompt: question },
}
);

显式创建和结束 Span

为了更好地控制 span 的生命周期,您可以显式创建和结束 span。

import * as mlflow from "mlflow-tracing";

const span = mlflow.startSpan({
name: "generateText",
spanType: mlflow.SpanType.LLM,
inputs: { prompt: question },
});

span.end({
outputs: { answer: "It's sunny in Seattle!" },
status: 'OK',
});

按用户和会话对追踪进行分组

许多实际应用程序使用会话来维护多轮用户交互。另一方面,追踪通常是按请求生成的。MLflow 支持按用户会话对追踪进行分组,以帮助您了解最终用户的旅程并识别问题。有关更多详细信息,请参阅追踪用户和会话指南。

常见问题

问:我发现 Python SDK 中有一个功能在 Typescript SDK 中不可用。

Typescript SDK 启动时间晚于 Python SDK,因此某些功能尚未可用。请在GitHub上提出功能请求,以引起维护者的注意。

问:我需要安装 Python 才能使用 MLflow Typescript SDK 吗?

您的应用程序不需要安装 Python。但是,如果您想将追踪发送到自托管的 MLflow 服务器,您的服务器环境需要安装 Python。

或者,您可以免费注册托管 MLflow,并将追踪发送到云托管的 MLflow 服务器。