license: apache-2.0
本文介绍的是Chen等人在2023年发表的论文《Dense X Retrieval: What Retrieval Granularity Should We Use?》(论文链接)中提出的命题分割模型。
使用方法
模型的输入提示格式为:标题: {标题}. 章节: {章节}. 内容: {内容}
。模型的输出是一个JSON格式的命题列表。
例如,如果我们用该模型分解以下段落:
标题: 比萨斜塔. 章节: . 内容: 在1990年至2001年进行的修复工作之前,比萨斜塔的倾斜角度为5.5度,但现在塔的倾斜角度约为3.99度。这意味着塔顶相对于中心水平位移了3.9米(12英尺10英寸)。
输出将是:
["在1990年至2001年进行的修复工作之前,比萨斜塔的倾斜角度为5.5度。", "比萨斜塔现在的倾斜角度约为3.99度。", "比萨斜塔的塔顶相对于中心水平位移了3.9米(12英尺10英寸)。"]
示例代码
示例:
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch
import json
model_name = "chentong00/propositionizer-wiki-flan-t5-large"
device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)
title = "比萨斜塔"
section = ""
content = "在1990年至2001年进行的修复工作之前,比萨斜塔的倾斜角度为5.5度,但现在塔的倾斜角度约为3.99度。这意味着塔顶相对于中心水平位移了3.9米(12英尺10英寸)。"
input_text = f"标题: {title}. 章节: {section}. 内容: {content}"
input_ids = tokenizer(input_text, return_tensors="pt").input_ids
outputs = model.generate(input_ids.to(device), max_new_tokens=512).cpu()
output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
try:
prop_list = json.loads(output_text)
except:
prop_list = []
print("[错误] 无法将输出文本解析为JSON。")
print(json.dumps(prop_list, indent=2))
预期输出:
[
"在1990年至2001年进行的修复工作之前,比萨斜塔的倾斜角度为5.5度。",
"比萨斜塔现在的倾斜角度约为3.99度。",
"比萨斜塔的塔顶相对于中心水平位移了3.9米(12英尺10英寸)。"
]
引用
@article{chen2023densex,
title={Dense X Retrieval: What Retrieval Granularity Should We Use?},
author={Tong Chen and Hongwei Wang and Sihao Chen and Wenhao Yu and Kaixin Ma and Xinran Zhao and Hongming Zhang and Dong Yu},
journal={arXiv preprint arXiv:2312.06648},
year={2023},
URL = {https://arxiv.org/pdf/2312.06648.pdf}
}