Views
No views yet
1from llmlingua import PromptCompressor
2
3compressor = PromptCompressor(
4 model_name="microsoft/llmlingua-2-xlm-roberta-large-meetingbank",
5 use_llmlingua2=True
6)
7
8original_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.
9Sarah: 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.
10"""
11results = compressor.compress_prompt_llmlingua2(
12 original_prompt,
13 rate=0.6,
14 force_tokens=['\n', '.', '!', '?', ','],
15 chunk_end_tokens=['.', '\n'],
16 return_word_label=True,
17 drop_consecutive=True
18)
19
20print(results.keys())
21print(f"Compressed prompt: {results['compressed_prompt']}")
22print(f"Original tokens: {results['origin_tokens']}")
23print(f"Compressed tokens: {results['compressed_tokens']}")
24print(f"Compression rate: {results['rate']}")
25
26# get the annotated results over the original prompt
27word_sep = "\t\t|\t\t"
28label_sep = " "
29lines = results["fn_labeled_original_prompt"].split(word_sep)
30annotated_results = []
31for line in lines:
32 word, label = line.split(label_sep)
33 annotated_results.append((word, '+') if label == '1' else (word, '-')) # list of tuples: (word, label)
34print("Annotated results:")
35for word, label in annotated_results[:10]:
36 print(f"{word} {label}")@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",
}