# @datastax/langflow-client 中文文档

规范地址：https://npmcn.com/package/%40datastax/langflow-client/

版本：0\.4\.1
npm 元数据更新时间：2025-09-01T07:09:50.868Z
中文译文更新时间：2026-11-11T19:09:53.150Z
软件包许可：Apache-2\.0

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

原始 npm 页面：https://www.npmjs.com/package/%40datastax/langflow-client
原文对照：https://npmcn.com/package/%40datastax/langflow-client/?lang=original
项目主页：https://github.com/datastax/langflow-client-ts
源码仓库：https://github.com/datastax/langflow-client-ts

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

- 包体积（解压后）：280 kB，89 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：1 个
- 首次发布：2025-01-17T04:39:34.726Z
- 最新版本发布：2025-09-01T07:09:50.604Z
- 近 30 天下载量：4530（2026-10-12 至 2026-11-10，npm 官方下载统计）
- 已发布版本数：8（近 12 个月 0 个）

## 中文简介

Langflow API 的 JavaScript 客户端

## 安装

```sh
npm install @datastax/langflow-client
```

也可以使用：`pnpm add @datastax/langflow-client`、`yarn add @datastax/langflow-client`、`bun add @datastax/langflow-client`

## README

# Langflow API 的 JavaScript 客户端

[![NPM Version](https://img.shields.io/npm/v/%40datastax%2Flangflow-client?logo=npm&color=%23ea2039)](https://www.npmjs.com/@datastax/langflow-client) [![Tests](https://github.com/datastax/langflow-client-ts/actions/workflows/test.yml/badge.svg)](https://github.com/datastax/langflow-client-ts/actions/workflows/test.yml)

* * *

这个软件包提供了使用 [朗福尔 API](https://docs.datastax.com/en/langflow/api.html) 以运行服务器侧 JavaScript 应用程序中的流.

-   [安装](https://npmcn.com/package/%40datastax/langflow-client/#readme-installation)
    -   [与 npm 关系](https://npmcn.com/package/%40datastax/langflow-client/#readme-with-npm)
    -   [用纱线](https://npmcn.com/package/%40datastax/langflow-client/#readme-with-yarn)
    -   [与pnpm](https://npmcn.com/package/%40datastax/langflow-client/#readme-with-pnpm)
-   [先决条件](https://npmcn.com/package/%40datastax/langflow-client/#readme-prerequisites)
-   [配置](https://npmcn.com/package/%40datastax/langflow-client/#readme-configuration)
    -   [数据税朗流](https://npmcn.com/package/%40datastax/langflow-client/#readme-datastax-langflow)
    -   [开源朗流](https://npmcn.com/package/%40datastax/langflow-client/#readme-open-source-langflow)
-   [使用量](https://npmcn.com/package/%40datastax/langflow-client/#readme-usage)
    -   [初始化](https://npmcn.com/package/%40datastax/langflow-client/#readme-initialization)
    -   [运行一流](https://npmcn.com/package/%40datastax/langflow-client/#readme-running-a-flow)
        -   [流回声](https://npmcn.com/package/%40datastax/langflow-client/#readme-flow-reponses)
    -   [流线](https://npmcn.com/package/%40datastax/langflow-client/#readme-streaming)
    -   [文件上传](https://npmcn.com/package/%40datastax/langflow-client/#readme-file-upload)
        -   [图像](https://npmcn.com/package/%40datastax/langflow-client/#readme-images)
        -   [文件上传](https://npmcn.com/package/%40datastax/langflow-client/#readme-file-uploads)
    -   [日志](https://npmcn.com/package/%40datastax/langflow-client/#readme-logs)
        -   [获取日志](https://npmcn.com/package/%40datastax/langflow-client/#readme-fetching-the-logs)
        -   [流出日志](https://npmcn.com/package/%40datastax/langflow-client/#readme-streaming-the-logs)
    -   [中止请求](https://npmcn.com/package/%40datastax/langflow-client/#readme-aborting-requests)
-   [捐款](https://npmcn.com/package/%40datastax/langflow-client/#readme-contributing)

## 安装

### 与 npm 关系

```sh
npm install @datastax/langflow-client
```

### 用纱线

```sh
yarn add @datastax/langflow-client
```

### 与pnpm

```sh
pnpm add @datastax/langflow-client
```

## 先决条件

要使用这个 Langflow 客户端, 您需要一个 DataStax 账户, 您可以使用该账户 [数据税朗流](https://docs.datastax.com/en/langflow/index.html)。在调用它的 API 之前,您需要一个流量 。 如果你还没有一个,你可以开始 [基本提示流量](https://docs.datastax.com/en/langflow/quickstart.html)。 。 。 .

您也可以在安装时使用 [开源朗流](https://www.langflow.org/)。 。 。 .

## 配置

### 数据税朗流

你需要一个 [朗福斯 API 密钥](https://docs.datastax.com/en/langflow/concepts/settings.html#langflow-api)。当您创建新的客户端时,您提供了 API 密钥。 你还需要你的Langflow身份证.

您可以在 Langflow 流程编辑器中生成 API 密钥并在 API 模式上找到您的 Langflow ID .

### 开源朗流

你需要安装Langflow。 您也可要求 [API 密钥](https://docs.langflow.org/configuration-api-keys) 如果你已经设置 [langflow 上的认证](https://docs.langflow.org/configuration-authentication)。 。 。 .

## 使用量

您可以这样导入客户端 :

```js
import { LangflowClient } from "@datastax/langflow-client";
```

### 初始化

然后,您可以创建一个新的客户对象对象,其选项如下:

-   `baseURL`: 此设置为 `https://api.langflow.astra.datastax.com` 默认情况下, 如果您正在运行自己的 Langflow 实例, 您需要提供 URL
-   `langflowId`: 您组织的ID, 可以在流程编辑器的 API 模式中找到 。 这对你自己来说并不必要
-   `apiKey`: 可以在您的 DataStax 账户内或在开源 Langflow 设置中生成的 Langflow API 密钥

```js
// using DataStax Langflow, you do not need to provide the baseUrl
const dsLangflowClient = new LangflowClient({ langflowId, apiKey });

// for open-source langflow, you will need to provide a baseUrl and optionally an apiKey
const baseUrl = "http://localhost:7860";
const apiKey = "sk-...";
const osLangflowClient = new LangflowClient({ baseUrl, apiKey });
```

### 运行一流

[运行流程的朗福文档](https://docs.langflow.org/api/simplified-run-flow)。 。 。 .

一旦有客户端,就可以使用 `flowID`。这可以在Langflow的API模式中找到.

```js
const flow = client.flow(flowId);
```

你可以通过电话运行一个流 `run` ,其中文本输入到流程中:

```js
const response = await client.flow(flowId).run(input);
```

您可以添加 [微调](https://docs.langflow.org/concepts-api#tweaks) 像这样流出:

```js
const response = await client
  .flow(flowId)
  .tweak(tweakName, tweakOptions)
  .run(input);
```

或者你可以通过所有微调 作为物体:

```js
const response = await client.flow(flowId).run(input, { tweaks });
```

也可以通过输入和输出选项,以及会话ID.

```js
import { InputTypes, OutputTypes } from "@datastax/langflow-client/consts";

const response = await client.flow(flowId).run(input, {
  input_type: InputTypes.CHAT,
  output_type: OutputTypes.CHAT,
  session_id,
  tweaks,
});
```

可用的输入类型有"聊天","文字"和"任何"等. 可用的输出类型有"聊天","文字","任何"和"调试"等. 两者的默认是"聊天".

#### 流回声

Langflow在输出上非常灵活. 所以说 `FlowResponse` 对象允许您原始访问 `sessionId` 编辑 `outputs`。 。 。 .

```js
const response = await client.flow(flowId).run(input);
console.log(response.outputs);
```

有一个便利功能可以返回第一个聊天输出消息文本 。 如果您的流量中只有一个聊天输出组件, 这是获取该响应的有用快捷方式 .

```js
const response = await client.flow(flowId).run(input);
console.log(response.chatOutputText());
```

### 流线

Langflow API支持流回响应. 而不是打电话 `run` 编号 `Flow` 对象,您可以调用 `stream` 同样的论据,答复将是: [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) 对象.

```js
const response = await client.flow(flowId).stream(input);

for await (const event of response) {
  console.log(event);
}
```

有三个不同的事件: `add_message`, (中文(简体) ), `token`,以及 `end`这些事件意味着:

-   `add_message`: 一条消息被添加到聊天中,可以指: 人输入消息或AI的回复
-   `token`: 作为流中生成的电文的一部分而发布的符号
-   `end`: 所有符号都已返回, 此信件也将包含完整 `FlowResponse`

事件对象的格式 :

```typescript
{
  event: "add_message" | "token" | "end",
  data: object
}
```

这个 `event.data` 每个事件类型都不同。 这个 `token` 事件类型最简单, 看起来像这样:

```typescript
{
  "event": "token",
  "data": {
    "chunk": "hello ",
    "id": "6686ff20-0c95-40bb-8879-fd90ed3d634e",
    "timestamp": "2025-02-12 22:18:04 UTC"
  }
}
```

还有呢 [langflow 文档中的文档和流化响应实例](https://docs.langflow.org/api-reference-api-examples#run-flow)。 。 。 .

### 文件上传

#### 图像

这个 [Langflow v1 文件上传 API](https://docs.langflow.org/api-files#filesv1-endpoints) 支持将图像文件上传到流中,然后可以在流中使用.

聊天输入组件支持文件作为输入和文本. 您需要先上传您的文件, 使用文件上传功能, 然后以 tweak 为流程提供文件路径 .

```js
const buffer = await readFile(path);
const file = new File([buffer], "image.jpg", { type: "image/jpeg" });

const flow = client.flow(flowId)
const file = await flow.uploadFile(file);
console.log(file);
// => { flowId: "XXX", filePath: "YYY" }

const response = await flow
  .tweak("ChatInput-abcd": { files: file.filePath })
  .run("What can you see in this image?");
```

> \[尖叫声\]  
> DataStax Langflow不提供文件上传功能,你会收到一个501 Not已执行错误.

#### 文件上传

这个 [Langflow v2 文件上传 API](https://docs.langflow.org/api-files#filesv2-endpoints) 支持向用户上传文件。 然后,这些可用于: [文件组件](https://docs.langflow.org/components-data#file)。 。 。 .

> \[尖叫声\] V2 文件上传,文件组件不支持图像上传. 对于图像,您应该使用 [v1 文件上传 API](https://npmcn.com/package/%40datastax/langflow-client/#readme-images)。 。 。 .

您可以上传这样的文件 :

```js
const buffer = await readFile(path);
const file = new File([buffer], "document.pdf", { type: "application/pdf" });

const fileUpload = await client.files.upload(file);
console.log(file);
// => { path: "abc123/document.pdf", name: "document", ... }
```

然后,您可以使用微调在流程中将其发送到文件组件.

```js
const flow = client.flow(flowId);
flow.tweak("File-abc123", {
  path: fileUpload.path,
});
```

您也可以列出您上传的文件 `files.list()` 函数 :

```js
const files = await client.files.list();

console.log(files);
// [{ path: "...", }, ...]
```

> \[注\] TODO:通过API可以使用的其他文件方法:下载,编辑,删除,并删除全部

### 日志

[日志 API 的 Langflow 文件](https://docs.langflow.org/api/logs)。 。 。 .

#### 获取日志

您可以为您的 Langflow 实例获取日志 .

```js
const logs = await client.logs.fetch();
```

获取日志时,可以通过 `timestamp` 或数 `lines_before` 或者说 `lines_after` 时间戳。 例如,以下代码将获得一个小时前发生的10个日志行:

```js
const logs = await client.logs.fetch({
  timestamp: Date.now() - 60 * 60 * 1000,
  lines_after: 10,
});
```

#### 流出日志

您也可以通过请求 [流出端点](https://docs.langflow.org/api/stream-logs)。 。 。 .

```js
for await (const log of await client.logs.stream()) {
  console.log(log);
}
```

### 中止请求

您可以使用标准 [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) 通过 a 取消请求 `signal` 页:1 `run` 或者说 `uploadFile` 函数。 函数将拒绝 a `DOMException` 名称出错 `AbortError` 或者,如果你使用 [`AbortSignal.timeout`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static), (中文(简体) ), `TimeoutError`。 。 。 .

例如,在运行以下代码时,如果整个请求需要超过500ms,则承诺会拒绝,错误消息会是"由于超时而中止操作".

```js
const signal = AbortSignal.timeout(500);
try {
  const response = await client.flow(flowId).run(input, { signal });
} catch (error) {
  console.error(error.message);
}
```

## 捐款

要运行和贡献到此库, 您可以克隆寄存器 :

```sh
git clone git@github.com:datastax/langflow-client-ts.git
cd langflow-client-ts
```

安装依赖性 :

```sh
npm install
```

运行测试 :

```sh
npm test
```

将 TypeScript 传送到 JavaScript :

```sh
npm run build
```

在监视模式下将 TypeScript 传送到 JavaScript :

```sh
npm run build:watch
```

打开代码 :

```sh
npm run lint
```

格式化代码 :

```sh
npm run format
```

检查代码的格式 :

```sh
npm run format:check
```

## 常见问题

### @datastax/langflow-client 是什么？

Langflow API 的 JavaScript 客户端

### 如何安装 @datastax/langflow-client？

在项目目录运行 npm install @datastax/langflow-client，或使用 pnpm add / yarn add / bun add。

### @datastax/langflow-client 支持 TypeScript 吗？

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

### @datastax/langflow-client 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @datastax/langflow-client 可以商用吗？

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

### @datastax/langflow-client 的下载量有多少？

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

### @datastax/langflow-client 还在维护吗？

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

