llmlingua 2 bert base multilingual cased meetingbank
简介
核心亮点
- 大幅压缩 Prompt Token 数量,降低 API 调用成本
- 基于多语言 BERT,支持多种语言的文本精简
- 有效降低推理延迟,提升大模型响应速度
- 作为预处理插件,无缝集成至现有 AI 工作流
使用方法
# 安装 Hugging Face transformers
pip install transformers torch
# 使用 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 目录为例)
huggingface-cli download microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank config.json --local-dir ./dir
SDK 下载
# 模型下载
from huggingface_hub import snapshot_download
model_dir = snapshot_download('microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank')
Git 下载
请确保 lfs 已经被正确安装
git lfs install
git clone https://huggingface.co/microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank
如果您希望跳过 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
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 目录为例)
modelscope download --model microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank README.md --local_dir ./dir
SDK 下载
# 模型下载
from modelscope import snapshot_download
model_dir = snapshot_download('microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank')
Git 下载
请确保 lfs 已经被正确安装
git lfs install
git clone https://www.modelscope.cn/microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank.git
如果您希望跳过 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
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')
完整文档
---
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
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
@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",
}