@atjsh/llmlingua-2

中文文档
3.0.0发布于 2026/08/16MITTypeScriptES Module构建来源已验证近 30 天下载 31.9万

JavaScript/TypeScript Implementation of LLMLingua-2

原始使用文档
文档目录4 节
  1. Installation
  2. Runtime Backend Selection
  3. Model Selection
  4. API Reference

JavaScript/TypeScript Implementation of LLMLingua-2 (Experimental)

NPM Version License

LLMLingua-2, Originally developed and implemented in Python by Microsoft, is a small-size yet powerful prompt compression method.

  • Efficient: Compresses context prompts with BERT sized models.
  • Accurate: Achieves high accuracy then other methods while requiring less computational resources.

llmlingua-2-js, ported by atjsh, is a pure JavaScript/TypeScript implementation of LLMLingua-2, designed to run in web browsers and Node.js environments.

  • Performance: Everything can be done in the browser. If your environment supports WebGPU, you can use it. Server-side processing is not required by default.
  • Correctness: The original logic will be ported to TypeScript as accurately as possible.

Try Demo Online (No Installation Required)

You can try it on the GitHub Pages without any installation.

The source code for demo is available in the examples directory. You can run it locally using the following command:

cd examples/react-vite-webgpu
yarn install

Learn More: Demo source code

Getting Started

This implementation depends on the following libraries:

Especially, the @huggingface/transformers library utilizes various computational optimizations to achieve high performance. Please consult if the running environment supports the minimum requirements from these libraries.

Installation

You can use the library by downloading the library from npm and importing it into your JavaScript/TypeScript project.

First, install the dependencies:

npm install @huggingface/transformers js-tiktoken

Then, install the library:

npm install @atjsh/llmlingua-2

Runtime Backend Selection

Pass an explicit transformerJSConfig to the factory. These portable configurations favor predictable output across supported runtimes:

const nodeTransformerJSConfig = {
  device: "cpu",
  dtype: "fp32",
} as const;

const browserTransformerJSConfig = {
  device: "wasm",
  dtype: "fp32",
} as const;

Hardware acceleration is opt-in and should be tested on each target platform:

Runtimedevicedtype
Windows Node.js with DirectML"dml""fp32"
Linux Node.js with NVIDIA CUDA"cuda""fp32"
Compatible browsers or macOS Node.js"webgpu""fp32"

The library passes the configuration to Transformers.js without detecting a backend or retrying with a fallback. For an XLM-RoBERTa model stored with one external data file, also pass:

modelSpecificOptions: {
  use_external_data_format: { "model.onnx": 1 },
}

Model Selection

You can choose between models based on your needs.

ModelSizeProsConsPublic Model
TinyBERT57.1 MBVery small,
fast
Lower accuracy
than larger models
atjsh/llmlingua-2-js-tinybert-meetingbank
MobileBERT99.2 MBSmall,
optimized for mobile
Moderate accuracy,
tradeoff in depth
atjsh/llmlingua-2-js-mobilebert-meetingbank
BERT710 MBFaster,
smaller size
Lower accuracy
than XLM-RoBERTa
Arcoldd/llmlingua4j-bert-base-onnx
XLM-RoBERTa2240 MBHigh accuracySlower,
slightly larger in size
atjsh/llmlingua-2-js-xlm-roberta-large-meetingbank

Learn More about the performance of each model (actual performance may vary).

The model files will be downloaded automatically by default.

API Reference

For more details on how to use the library, please refer to the API reference documentation.

Testing

Unit tests are not available at the moment.

E2E tests are partially available in following directories:

  • src/e2e
  • examples/**

License

See LICENSE for details.

Credits

This software includes other software related under the following licenses:

版本与发布记录

@atjsh/llmlingua-2 共发布 30 个版本,首次发布于 2025/05/17;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/08/162 个月前
2.0.52026/08/162 个月前
2.0.42026/08/162 个月前
2.0.32025/08/271.2 年前
2.0.22025/06/141.4 年前
2.0.12025/06/081.4 年前

关于 @atjsh/llmlingua-2 的常见问题

@atjsh/llmlingua-2 是什么?

JavaScript/TypeScript Implementation of LLMLingua-2

如何安装 @atjsh/llmlingua-2?

在项目目录运行 npm install @atjsh/llmlingua-2,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@huggingface/transformers(^4.2.0)、js-tiktoken(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@atjsh/llmlingua-2 支持 TypeScript 吗?

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

@atjsh/llmlingua-2 支持 ESM 还是 CommonJS?

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

@atjsh/llmlingua-2 可以商用吗?

@atjsh/llmlingua-2 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@atjsh/llmlingua-2 的下载量有多少?

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

@atjsh/llmlingua-2 还在维护吗?

最新版本 3.0.0 发布于 2026/08/16(2 个月前);至今共发布 30 个版本,其中过去 12 个月发布了 3 个;首次发布于 2025/05/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。