llmlingua 2 bert base multilingual cased meetingbank

提供商microsoft
分类token-classification
许可证apache-2.0
下载量504
星标1

简介

LLMLingua-2 是微软推出的高效 Prompt 压缩模型,该版本基于多语言 BERT 基础模型并在 MeetingBank 数据集上进行了微调。它旨在解决大模型上下文窗口限制和 Token 成本高昂的痛点,通过识别并剔除 Prompt 中的冗余信息,在保证语义完整的前提下大幅减少 Token 数量。对于开发者而言,它像是一个智能的“预处理器”,在请求发送给 GPT-4 等大模型之前先进行精简,既能降低推理延迟,又能有效节省 API 费用,且部署难度低,兼容性强。

核心亮点

  • 大幅压缩 Prompt Token 数量,降低 API 调用成本
  • 基于多语言 BERT,支持多种语言的文本精简
  • 有效降低推理延迟,提升大模型响应速度
  • 作为预处理插件,无缝集成至现有 AI 工作流

使用方法

安装依赖
# 安装 Hugging Face transformers
pip install transformers torch
SDK 使用
# 使用 transformers 加载模型
from transformers import AutoModel, AutoTokenizer

model = AutoModel.from_pretrained("microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank")
tokenizer = AutoTokenizer.from_pretrained("microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank")

Hugging Face 下载

我们推荐使用命令行或者 Hugging Face Hub SDK 来进行模型的下载。

操作指引:在下载前,请先通过如下命令安装 huggingface_hub:

操作指引
pip install -U huggingface_hub

命令行下载

下载完整模型库

下载完整模型库
huggingface-cli download microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank

下载单个文件到指定本地文件夹(以下载 config.json 到当前路径下 ./dir 目录为例)

下载单个文件到指定本地文件夹(以下载 config.json 到当前路径下 ./dir 目录为例)
huggingface-cli download microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank config.json --local-dir ./dir

更多命令行下载选项,可参见官方文档

SDK 下载

SDK 下载
# 模型下载
from huggingface_hub import snapshot_download
model_dir = snapshot_download('microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank')

Git 下载

请确保 lfs 已经被正确安装

Git 下载
git lfs install
git clone https://huggingface.co/microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank

如果您希望跳过 lfs 大文件下载,可以使用如下命令

跳过 LFS
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank

模型文件托管在 Hugging Face Hub,使用 HF CLI / SDK / Git 直接下载,不经过本站。

PyTorch / Transformers 使用

安装 Transformers

安装 Transformers
pip install -U transformers torch

模型加载和推理

模型加载和推理
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained('microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank')
tokenizer = AutoTokenizer.from_pretrained('microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank')

模型下载

我们推荐使用命令行或者 ModelScope SDK 来进行模型的下载。

操作指引:在下载前,请先通过如下命令安装 ModelScope:

操作指引
pip install modelscope

命令行下载

下载完整模型库

下载完整模型库
modelscope download --model microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank

下载单个文件到指定本地文件夹(以下载 README.md 到当前路径下 dir 目录为例)

下载单个文件到指定本地文件夹(以下载 README.md 到当前路径下 dir 目录为例)
modelscope download --model microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank README.md --local_dir ./dir

更多更丰富的命令行下载选项,可参见具体文档

SDK 下载

SDK 下载
# 模型下载
from modelscope import snapshot_download
model_dir = snapshot_download('microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank')

Git 下载

请确保 lfs 已经被正确安装

Git 下载
git lfs install
git clone https://www.modelscope.cn/microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank.git

如果您希望跳过 lfs 大文件下载,可以使用如下命令

跳过 LFS
GIT_LFS_SKIP_SMUDGE=1 git clone https://www.modelscope.cn/microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank.git

ModelScope 模型页直接下载模型文件;无需将模型文件放在本站服务器。

Notebook 快速开发

下载并安装 ModelScope library

下载并安装 ModelScope library
pip install "modelscope[audio,cv,nlp,multi-modal,science]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html

模型加载和推理

模型加载和推理
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

p = pipeline('text-generation', 'microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank')

完整文档

来源: HuggingFace

---
license: apache-2.0
---

LLMLingua-2-Bert-base-Multilingual-Cased-MeetingBank

This model was introduced in the paper LLMLingua-2: Data Distillation for Efficient and Faithful Task-Agnostic Prompt Compression (Pan et al, 2024). It is a BERT multilingual base model (cased) finetuned to perform token classification for task agnostic prompt compression. The probability $p_{preserve}$ of each token $x_i$ is used as the metric for compression. This model is trained on the extractive text compression dataset constructed with the methodology proposed in the LLMLingua-2, using training examples from MeetingBank (Hu et al, 2023) as the seed data.

You can evaluate the model on downstream tasks such as question answering (QA) and summarization over compressed meeting transcripts using this dataset.

For more details, please check the project page of LLMLingua-2 and LLMLingua Series.

Usage

python
from llmlingua import PromptCompressor

compressor = PromptCompressor(
model_name="microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank",
use_llmlingua2=True
)

original_prompt = """John: So, um, I've been thinking about the project, you know, and I believe we need to, uh, make some changes. I mean, we want the project to succeed, right? So, like, I think we should consider maybe revising the timeline.
Sarah: I totally agree, John. I mean, we have to be realistic, you know. The timeline is, like, too tight. You know what I mean? We should definitely extend it.
"""
results = compressor.compress_prompt_llmlingua2(
original_prompt,
rate=0.6,
force_tokens=['\n', '.', '!', '?', ','],
chunk_end_tokens=['.', '\n'],
return_word_label=True,
drop_consecutive=True
)

print(results.keys())
print(f"Compressed prompt: {results['compressed_prompt']}")
print(f"Original tokens: {results['origin_tokens']}")
print(f"Compressed tokens: {results['compressed_tokens']}")
print(f"Compression rate: {results['rate']}")

get the annotated results over the original prompt

word_sep = "\t\t|\t\t" label_sep = " " lines = results["fn_labeled_original_prompt"].split(word_sep) annotated_results = [] for line in lines: word, label = line.split(label_sep) annotated_results.append((word, '+') if label == '1' else (word, '-')) # list of tuples: (word, label) print("Annotated results:") for word, label in annotated_results[:10]: print(f"{word} {label}")

Citation

code
@article{wu2024llmlingua2,
    title = "{LLML}ingua-2: Data Distillation for Efficient and Faithful Task-Agnostic Prompt Compression",
    author = "Zhuoshi Pan and Qianhui Wu and Huiqiang Jiang and Menglin Xia and Xufang Luo and Jue Zhang and Qingwei Lin and Victor Ruhle and Yuqing Yang and Chin-Yew Lin and H. Vicky Zhao and Lili Qiu and Dongmei Zhang",
    url = "https://arxiv.org/abs/2403.12968",
    journal = "ArXiv preprint",
    volume = "abs/2403.12968",
    year = "2024",
}