

用于实体链接和关系提取的实体的快速且轻巧的信息提取模型。
来自PYPI的安装
pip install relik安装所有可选依赖项。
pip install relik[all]安装可选的依赖项,以进行培训和评估。
pip install relik[train]安装faiss的可选依赖项
Faiss PYPI软件包仅适用于CPU。对于GPU,请从源安装或使用Conda软件包。
对于CPU:
pip install relik[faiss]对于GPU:
conda create -n relik python=3.10
conda activate relik
# install pytorch
conda install -y pytorch=2.1.0 pytorch-cuda=12.1 -c pytorch -c nvidia
# GPU
conda install -y -c pytorch -c nvidia faiss-gpu=1.8.0
# or GPU with NVIDIA RAFT
conda install -y -c pytorch -c nvidia -c rapidsai -c conda-forge faiss-gpu-raft=1.8.0
pip install relik以可选的依赖项安装,以使用FastApi和Ray为模型提供服务。
pip install relik[serve]git clone https://github.com/SapienzaNLP/relik.git
cd relik
pip install -e .[all]Relik for Relik用于关系提取( relik-ie/relik-relation-extraction-large
封闭https://huggingface.co/relik-ie/relik-cie-large提取(?
封闭信息提取(?我们的thicc boi for el + re) : relik-ie/relik-cie-xl
Relik Small用于实体链接(??⚡微小而快速的EL,COLAB✅) : sapienzanlp/relik-entity-linking-small
Relik Small用于实体链接(⚡小型和快速EL) : sapienzanlp/relik-entity-linking-small
Relik Smill用于封闭信息提取(EL + RE) : relik-ie/relik-cie-small
Relik for实体链接(野外的EL) : relik-ie/relik-entity-linking-large-robust
Relik Small用于实体链接(re + ner) : relik-ie/relik-relation-extraction-small-wikipedia-ner
纸上的模型:
sapienzanlp/relik-entity-linking-largesapienzanlp/relik-entity-linking-basesapienzanlp/relik-relation-extraction-nyt-large可以找到完整的模型列表?拥抱脸。
其他型号的尺寸将来可以使用吗?
Relik是实体链接和关系提取的轻巧且快速的模型。它由两个主要组成部分组成:一个猎犬和一个读者。猎犬负责从大型收藏中检索相关文件,而读者负责从检索到的文件中提取实体和关系。 RELIK可以与from_pretrained方法一起使用,以加载预训练的管道。
这是如何将Relik用于实体链接的示例:
from relik import Relik
from relik . inference . data . objects import RelikOutput
relik = Relik . from_pretrained ( "sapienzanlp/relik-entity-linking-large" )
relik_out : RelikOutput = relik ( "Michael Jordan was one of the best players in the NBA." )输出:
RelikOutput(
text="Michael Jordan was one of the best players in the NBA.",
tokens=['Michael', 'Jordan', 'was', 'one', 'of', 'the', 'best', 'players', 'in', 'the', 'NBA', '.'],
id=0,
spans=[
Span(start=0, end=14, label="Michael Jordan", text="Michael Jordan"),
Span(start=50, end=53, label="National Basketball Association", text="NBA"),
],
triples=[],
candidates=Candidates(
span=[
[
[
{"text": "Michael Jordan", "id": 4484083},
{"text": "National Basketball Association", "id": 5209815},
{"text": "Walter Jordan", "id": 2340190},
{"text": "Jordan", "id": 3486773},
{"text": "50 Greatest Players in NBA History", "id": 1742909},
...
]
]
]
),
)
并进行关系提取:
from relik import Relik
from relik . inference . data . objects import RelikOutput
relik = Relik . from_pretrained ( "sapienzanlp/relik-relation-extraction-nyt-large" )
relik_out : RelikOutput = relik ( "Michael Jordan was one of the best players in the NBA." )输出:
RelikOutput(
text='Michael Jordan was one of the best players in the NBA.',
tokens=Michael Jordan was one of the best players in the NBA.,
id=0,
spans=[
Span(start=0, end=14, label='--NME--', text='Michael Jordan'),
Span(start=50, end=53, label='--NME--', text='NBA')
],
triplets=[
Triplets(
subject=Span(start=0, end=14, label='--NME--', text='Michael Jordan'),
label='company',
object=Span(start=50, end=53, label='--NME--', text='NBA'),
confidence=1.0
)
],
candidates=Candidates(
span=[],
triplet=[
[
[
{"text": "company", "id": 4, "metadata": {"definition": "company of this person"}},
{"text": "nationality", "id": 10, "metadata": {"definition": "nationality of this person or entity"}},
{"text": "child", "id": 17, "metadata": {"definition": "child of this person"}},
{"text": "founded by", "id": 0, "metadata": {"definition": "founder or co-founder of this organization, religion or place"}},
{"text": "residence", "id": 18, "metadata": {"definition": "place where this person has lived"}},
...
]
]
]
),
)
猎犬和读者可以单独使用。对于仅此奖项的Relik,输出将包含输入文本的候选物。
仅此猎犬的例子:
from relik import Relik
from relik . inference . data . objects import RelikOutput
# If you want to use only the retriever
retriever = Relik . from_pretrained ( "sapienzanlp/relik-entity-linking-large" , reader = None )
relik_out : RelikOutput = retriever ( "Michael Jordan was one of the best players in the NBA." )输出:
RelikOutput(
text="Michael Jordan was one of the best players in the NBA.",
tokens=['Michael', 'Jordan', 'was', 'one', 'of', 'the', 'best', 'players', 'in', 'the', 'NBA', '.'],
id=0,
spans=[],
triples=[],
candidates=Candidates(
span=[
[
{"text": "Michael Jordan", "id": 4484083},
{"text": "National Basketball Association", "id": 5209815},
{"text": "Walter Jordan", "id": 2340190},
{"text": "Jordan", "id": 3486773},
{"text": "50 Greatest Players in NBA History", "id": 1742909},
...
]
],
triplet=[],
),
)
仅读者的示例:
from relik import Relik
from relik . inference . data . objects import RelikOutput
# If you want to use only the reader
reader = Relik . from_pretrained ( "sapienzanlp/relik-entity-linking-large" , retriever = None )
candidates = [
"Michael Jordan" ,
"National Basketball Association" ,
"Walter Jordan" ,
"Jordan" ,
"50 Greatest Players in NBA History" ,
]
text = "Michael Jordan was one of the best players in the NBA."
relik_out : RelikOutput = reader ( text , candidates = candidates )输出:
RelikOutput(
text="Michael Jordan was one of the best players in the NBA.",
tokens=['Michael', 'Jordan', 'was', 'one', 'of', 'the', 'best', 'players', 'in', 'the', 'NBA', '.'],
id=0,
spans=[
Span(start=0, end=14, label="Michael Jordan", text="Michael Jordan"),
Span(start=50, end=53, label="National Basketball Association", text="NBA"),
],
triples=[],
candidates=Candidates(
span=[
[
[
{
"text": "Michael Jordan",
"id": -731245042436891448,
},
{
"text": "National Basketball Association",
"id": 8135443493867772328,
},
{
"text": "Walter Jordan",
"id": -5873847607270755146,
"metadata": {},
},
{"text": "Jordan", "id": 6387058293887192208, "metadata": {}},
{
"text": "50 Greatest Players in NBA History",
"id": 2173802663468652889,
},
]
]
],
),
)
Relik提供了一个CLI,可以为模型提供FastAPI服务器或在数据集上执行推断。
relik serverelik serve --help
Usage: relik serve [OPTIONS] RELIK_PRETRAINED [DEVICE] [RETRIEVER_DEVICE]
[DOCUMENT_INDEX_DEVICE] [READER_DEVICE] [PRECISION]
[RETRIEVER_PRECISION] [DOCUMENT_INDEX_PRECISION]
[READER_PRECISION] [ANNOTATION_TYPE]
╭─ Arguments ─────────────────────────────────────────────────────────────────────────────────────────╮
│ * relik_pretrained TEXT [default: None] [required] │
│ device [DEVICE] The device to use for relik (e.g., │
│ ' cuda ' , ' cpu ' ). │
│ [default: None] │
│ retriever_device [RETRIEVER_DEVICE] The device to use for the retriever │
│ (e.g., ' cuda ' , ' cpu ' ). │
│ [default: None] │
│ document_index_device [DOCUMENT_INDEX_DEVICE] The device to use for the index │
│ (e.g., ' cuda ' , ' cpu ' ). │
│ [default: None] │
│ reader_device [READER_DEVICE] The device to use for the reader │
│ (e.g., ' cuda ' , ' cpu ' ). │
│ [default: None] │
│ precision [PRECISION] The precision to use for relik │
│ (e.g., ' 32 ' , ' 16 ' ). │
│ [default: 32] │
│ retriever_precision [RETRIEVER_PRECISION] The precision to use for the │
│ retriever (e.g., ' 32 ' , ' 16 ' ). │
│ [default: None] │
│ document_index_precision [DOCUMENT_INDEX_PRECISION] The precision to use for the index │
│ (e.g., ' 32 ' , ' 16 ' ). │
│ [default: None] │
│ reader_precision [READER_PRECISION] The precision to use for the reader │
│ (e.g., ' 32 ' , ' 16 ' ). │
│ [default: None] │
│ annotation_type [ANNOTATION_TYPE] The type of annotation to use (e.g., │
│ ' CHAR ' , ' WORD ' ). │
│ [default: char] │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ───────────────────────────────────────────────────────────────────────────────────────────╮
│ --host TEXT [default: 0.0.0.0] │
│ --port INTEGER [default: 8000] │
│ --frontend --no-frontend [default: no-frontend] │
│ --help Show this message and exit. │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────╯
例如:
relik serve sapienzanlp/relik-entity-linking-largerelik inferencerelik inference --help
Usage: relik inference [OPTIONS] MODEL_NAME_OR_PATH INPUT_PATH OUTPUT_PATH
╭─ Arguments ─────────────────────────────────────────────────────────────────────────────────────────────╮
│ * model_name_or_path TEXT [default: None] [required] │
│ * input_path TEXT [default: None] [required] │
│ * output_path TEXT [default: None] [required] │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ───────────────────────────────────────────────────────────────────────────────────────────────╮
│ --batch-size INTEGER [default: 8] │
│ --num-workers INTEGER [default: 4] │
│ --device TEXT [default: cuda] │
│ --precision TEXT [default: fp16] │
│ --top-k INTEGER [default: 100] │
│ --window-size INTEGER [default: None] │
│ --window-stride INTEGER [default: None] │
│ --annotation-type TEXT [default: char] │
│ --progress-bar --no-progress-bar [default: progress-bar] │
│ --model-kwargs TEXT [default: None] │
│ --inference-kwargs TEXT [default: None] │
│ --help Show this message and exit. │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯例如:
relik inference sapienzanlp/relik-entity-linking-large data.txt output.jsonlRelik的Docker图像可在Docker Hub上找到。您可以用以下方式提取最新图像
docker pull sapienzanlp/relik:latest并以:
docker run -p 12345:8000 sapienzanlp/relik:latest -c relik-ie/relik-cie-small API将在http://localhost:12345提供。它以几个可以传递给模型的参数公开了一个端点/relik 。可以在http://localhost:12345/docs找到API的快速文档。这是如何查询API的一个简单示例:
curl -X ' GET '
' http://127.0.0.1:12345/api/relik?text=Michael%20Jordan%20was%20one%20of%20the%20best%20players%20in%20the%20NBA.&is_split_into_words=false&retriever_batch_size=32&reader_batch_size=32&return_windows=false&use_doc_topic=false&annotation_type=char&relation_threshold=0.5 '
-H ' accept: application/json '在这里,可以传递给Docker映像的参数的完整列表:
docker run sapienzanlp/relik:latest -h
Usage: relik [-h --help] [-c --config] [-p --precision] [-d --device] [--retriever] [--retriever-device]
[--retriever-precision] [--index-device] [--index-precision] [--reader] [--reader-device] [--reader-precision]
[--annotation-type] [--frontend] [--workers] -- start the FastAPI server for the RElik model
where:
-h --help Show this help text
-c --config Pretrained ReLiK config name (from HuggingFace) or path
-p --precision Precision, default ' 32 ' .
-d --device Device to use, default ' cpu ' .
--retriever Override retriever model name.
--retriever-device Override retriever device.
--retriever-precision Override retriever precision.
--index-device Override index device.
--index-precision Override index precision.
--reader Override reader model name.
--reader-device Override reader device.
--reader-precision Override reader precision.
--annotation-type Annotation type ( ' char ' , ' word ' ), default ' char ' .
--frontend Whether to start the frontend server.
--workers Number of workers to use.在以下各节中,我们提供了有关如何准备数据,训练猎犬和读者并评估模型的分步指南。
您所有的数据都应具有以下结构:
{
"doc_id" : int, # Unique identifier for the document
"doc_text" : txt, # Text of the document
"doc_span_annotations" : # Char level annotations
[
[ start, end, label ],
[ start, end, label ],
...
]
}我们使用眨眼(Wu等,2019)和Aida(Hoffart等,2011)数据集进行培训和评估。更具体地说,我们使用眨眼数据集预先培训猎犬和AIDA数据集来微调猎犬并培训读者。
可以使用此脚本从类型存储库中下载眨眼数据集。我们将blink-train-kilt.jsonl和blink-dev-kilt.jsonl用作培训和验证数据集。假设我们已经在data/blink文件夹中下载了两个文件,我们使用以下脚本将眨眼数据集转换为Relik格式:
# Train
python scripts/data/blink/preprocess_genre_blink.py
data/blink/blink-train-kilt.jsonl
data/blink/processed/blink-train-kilt-relik.jsonl
# Dev
python scripts/data/blink/preprocess_genre_blink.py
data/blink/blink-dev-kilt.jsonl
data/blink/processed/blink-dev-kilt-relik.jsonl AIDA数据集并非公开可用,但是我们提供了没有text字段的文件。您可以在data/aida/processed文件夹中找到相关格式的文件。
我们使用的Wikipedia索引可以从这里下载。
您所有的数据都应具有以下结构:
{
"doc_id" : int, # Unique identifier for the document
"doc_words: list[txt] # Tokenized text of the document
"doc_span_annotations" : # Token level annotations of mentions (label is optional)
[
[ start, end, label ],
[ start, end, label ],
...
],
"doc_triplet_annotations" : # Triplet annotations
[
{
"subject" : [ start, end, label ], # label is optional
"relation" : name, # type is optional
"object" : [ start, end, label ], # label is optional
},
{
"subject" : [ start, end, label ], # label is optional
"relation" : name, # type is optional
"object" : [ start, end, label ], # label is optional
},
]
}对于关系提取,我们提供了一个示例,说明如何从Copyre中获取的RAW_NYT预处理NYT数据集。将数据集下载到数据/RAW_NYT,然后运行:
python scripts/data/nyt/preprocess_nyt.py data/raw_nyt data/nyt/processed/请注意,在公平的比较中,我们重现了先前工作的预处理,这导致复制的三胞胎,因为对实体跨度重复的表面形式的处理不正确。如果要正确地将原始数据解析为Relik格式,则可以设置标志 - Legygacy-Format False。请注意,提供的RE NYT模型接受了旧式格式的培训。
我们为猎犬执行了两步训练过程。首先,我们使用Blink(Wu等,2019)数据集“预训练”猎犬,然后使用AIDA“微调”(Hoffart等,2011)。
猎犬需要类似于DPR: jsonl文件的格式的数据集,其中每行都是带有以下键的字典:
{
"question" : " .... " ,
"positive_ctxs" : [{
"title" : " ... " ,
"text" : " .... "
}],
"negative_ctxs" : [{
"title" : " ... " ,
"text" : " .... "
}],
"hard_negative_ctxs" : [{
"title" : " ... " ,
"text" : " .... "
}]
}猎犬还需要索引来搜索文档。索引的文档可以是JSONL文件,也可以是类似于DPR的TSV文件:
jsonl :每行都是带有以下键的JSON对象: id , text , metadatatsv :每行都是一个带有id和text列的选项卡分隔字符串,然后是将存储在metadata字段中的任何其他列jsonl示例:
{
"id" : " ... " ,
"text" : " ... " ,
"metadata" : [ " {...} " ]
},
... tsv示例:
id t text t any other column
... 一旦以Relik格式使用了眨眼数据集,就可以使用以下脚本创建窗口:
# train
relik data create-windows
data/blink/processed/blink-train-kilt-relik.jsonl
data/blink/processed/blink-train-kilt-relik-windowed.jsonl
# dev
relik data create-windows
data/blink/processed/blink-dev-kilt-relik.jsonl
data/blink/processed/blink-dev-kilt-relik-windowed.jsonl然后将其转换为DPR格式:
# train
relik data convert-to-dpr
data/blink/processed/blink-train-kilt-relik-windowed.jsonl
data/blink/processed/blink-train-kilt-relik-windowed-dpr.jsonl
data/kb/wikipedia/documents.jsonl
--title-map data/kb/wikipedia/title_map.json
# dev
relik data convert-to-dpr
data/blink/processed/blink-dev-kilt-relik-windowed.jsonl
data/blink/processed/blink-dev-kilt-relik-windowed-dpr.jsonl
data/kb/wikipedia/documents.jsonl
--title-map data/kb/wikipedia/title_map.json由于AIDA数据集尚未公开可用,因此我们可以以Relik格式为AIDA数据集提供注释。假设您在data/aida中拥有完整的AIDA数据集,则可以将其转换为Relik格式,然后使用以下脚本创建窗口:
relik data create-windows
data/aida/processed/aida-train-relik.jsonl
data/aida/processed/aida-train-relik-windowed.jsonl然后将其转换为DPR格式:
relik data convert-to-dpr
data/aida/processed/aida-train-relik-windowed.jsonl
data/aida/processed/aida-train-relik-windowed-dpr.jsonl
data/kb/wikipedia/documents.jsonl
--title-map data/kb/wikipedia/title_map.jsonrelik data create-windows
data/data/processed/nyt/train.jsonl
data/data/processed/nyt/train-windowed.jsonl
--is-split-into-words
--window-size none 然后将其转换为DPR格式:
relik data convert-to-dpr
data/data/processed/nyt/train-windowed.jsonl
data/data/processed/nyt/train-windowed-dpr.jsonlrelik retriever train命令可用于训练猎犬。它需要以下参数:
config_path :配置文件的路径。overrides :以格式key=value配置文件覆盖列表。配置文件的示例可以在relik/retriever/conf文件夹中找到。
relik/retriever/conf中的配置文件是pretrain_iterable_in_batch.yaml和finetune_iterable_in_batch.yaml ,我们分别用于预先培训和调整此检索器。
例如,要在AIDA数据集上训练猎犬,您可以运行以下命令:
relik retriever train relik/retriever/conf/finetune_iterable_in_batch.yaml
model.language_model=intfloat/e5-base-v2
data.train_dataset_path=data/aida/processed/aida-train-relik-windowed-dpr.jsonl
data.val_dataset_path=data/aida/processed/aida-dev-relik-windowed-dpr.jsonl
data.test_dataset_path=data/aida/processed/aida-test-relik-windowed-dpr.jsonl
data.shared_params.documents_path=data/kb/wikipedia/documents.jsonlrelik/retriever/conf中的配置文件是finetune_nyt_iterable_in_batch.yaml ,我们用它来微调NYT数据集的回收器。对于CIE,我们在上一步中重新利用了从眨眼中预算的那个。
例如,要在NYT数据集上训练Retriever,您可以运行以下命令:
relik retriever train relik/retriever/conf/finetune_nyt_iterable_in_batch.yaml
model.language_model=intfloat/e5-base-v2
data.train_dataset_path=data/nyt/processed/nyt-train-relik-windowed-dpr.jsonl
data.val_dataset_path=data/nyt/processed/nyt-dev-relik-windowed-dpr.jsonl
data.test_dataset_path=data/nyt/processed/nyt-test-relik-windowed-dpr.jsonl通过传递train.only_test=True to to relik retriever train命令,您可以跳过培训,只能评估模型。它还需要通往Pytorch Lightning检查点和数据集的路径。
relik retriever train relik/retriever/conf/finetune_iterable_in_batch.yaml
train.only_test=True
test_dataset_path=data/aida/processed/aida-test-relik-windowed-dpr.jsonl
model.checkpoint_path=path/to/checkpoint可以使用以下命令从检查点保存回收器编码器:
from relik . retriever . lightning_modules . pl_modules import GoldenRetrieverPLModule
checkpoint_path = "path/to/checkpoint"
retriever_folder = "path/to/retriever"
# If you want to push the model to the Hugging Face Hub set push_to_hub=True
push_to_hub = False
# If you want to push the model to the Hugging Face Hub set the repo_id
repo_id = "sapienzanlp/relik-retriever-e5-base-v2-aida-blink-encoder"
pl_module = GoldenRetrieverPLModule . load_from_checkpoint ( checkpoint_path )
pl_module . model . save_pretrained ( retriever_folder , push_to_hub = push_to_hub , repo_id = repo_id )使用push_to_hub=True该模型将被推到?用repo_id拥抱面枢纽作为将推动模型的存储库ID。
猎犬需要索引来搜索文档。可以使用relik retriever create-index
relik retriever create-index --help
Usage: relik retriever build-index [OPTIONS] QUESTION_ENCODER_NAME_OR_PATH
DOCUMENT_PATH OUTPUT_FOLDER
╭─ Arguments ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ * question_encoder_name_or_path TEXT [default: None] [required] │
│ * document_path TEXT [default: None] [required] │
│ * output_folder TEXT [default: None] [required] │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --document-file-type TEXT [default: jsonl] │
│ --passage-encoder-name-or-path TEXT [default: None] │
│ --indexer-class TEXT [default: relik.retriever.indexers.inmemory.InMemoryDocumentIndex] │
│ --batch-size INTEGER [default: 512] │
│ --num-workers INTEGER [default: 4] │
│ --passage-max-length INTEGER [default: 64] │
│ --device TEXT [default: cuda] │
│ --index-device TEXT [default: cpu] │
│ --precision TEXT [default: fp32] │
│ --push-to-hub --no-push-to-hub [default: no-push-to-hub] │
│ --repo-id TEXT [default: None] │
│ --help Show this message and exit. │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯使用编码器和索引,可以从回购ID或本地路径上加载回收者:
from relik . retriever import GoldenRetriever
encoder_name_or_path = "sapienzanlp/relik-retriever-e5-base-v2-aida-blink-encoder"
index_name_or_path = "sapienzanlp/relik-retriever-e5-base-v2-aida-blink-wikipedia-index"
retriever = GoldenRetriever (
question_encoder = encoder_name_or_path ,
document_index = index_name_or_path ,
device = "cuda" , # or "cpu"
precision = "16" , # or "32", "bf16"
index_device = "cuda" , # or "cpu"
index_precision = "16" , # or "32", "bf16"
)然后可以用于检索文档:
retriever . retrieve ( "Michael Jordan was one of the best players in the NBA." , top_k = 100 )读者负责从一组候选人(例如,可能的实体或关系)中提取实体和关系。可以训练读者进行跨度提取或三重提取。 RelikReaderForSpanExtraction用于跨度提取,IE实体链接,而RelikReaderForTripletExtraction用于三重态提取,即IE关系提取。
读者需要我们在部分中创建的窗口化数据集,然后再与猎犬的候选人进行增强。可以使用relik retriever add-candidates命令将候选人添加到数据集中。
relik retriever add-candidates --help
Usage: relik retriever add-candidates [OPTIONS] QUESTION_ENCODER_NAME_OR_PATH
DOCUMENT_NAME_OR_PATH INPUT_PATH
OUTPUT_PATH
╭─ Arguments ─────────────────────────────────────────────────────────────────────────────────────────────────╮
│ * question_encoder_name_or_path TEXT [default: None] [required] │
│ * document_name_or_path TEXT [default: None] [required] │
│ * input_path TEXT [default: None] [required] │
│ * output_path TEXT [default: None] [required] │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ───────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --passage-encoder-name-or-path TEXT [default: None] │
│ --relations BOOLEAN [default: False] │
│ --top-k INTEGER [default: 100] │
│ --batch-size INTEGER [default: 128] │
│ --num-workers INTEGER [default: 4] │
│ --device TEXT [default: cuda] │
│ --index-device TEXT [default: cpu] │
│ --precision TEXT [default: fp32] │
│ --use-doc-topics --no-use-doc-topics [default: no-use-doc-topics] │
│ --help Show this message and exit. │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯我们需要使用我们先前训练的猎犬将候选人添加到读者将使用的每个窗口中。这是使用我们在AIDA上已经训练过的猎犬进行火车分割的示例:
relik retriever add-candidates sapienzanlp/relik-retriever-e5-base-v2-aida-blink-encoder sapienzanlp/relik-retriever-e5-base-v2-aida-blink-wikipedia-index data/aida/processed/aida-train-relik-windowed.jsonl data/aida/processed/aida-train-relik-windowed-candidates.jsonl关系提取也发生了同样的事情。如果您想使用我们训练有素的检索员:
relik retriever add-candidates sapienzanlp/relik-retriever-small-nyt-question-encoder sapienzanlp/relik-retriever-small-nyt-document-index data/nyt/processed/nyt-train-relik-windowed.jsonl data/nyt/processed/nyt-train-relik-windowed-candidates.jsonl与猎犬类似, relik reader train命令可用于训练猎犬。它需要以下参数:
config_path :配置文件的路径。overrides :以格式key=value配置文件覆盖列表。配置文件的示例可以在relik/reader/conf文件夹中找到。
relik/reader/conf中的配置文件是large.yaml和base.yaml ,我们用来分别训练大型和基本读者。例如,在AIDA数据集运行中训练大型读者:
relik reader train relik/reader/conf/large.yaml
train_dataset_path=data/aida/processed/aida-train-relik-windowed-candidates.jsonl
val_dataset_path=data/aida/processed/aida-dev-relik-windowed-candidates.jsonl
test_dataset_path=data/aida/processed/aida-dev-relik-windowed-candidates.jsonlrelik/reader/conf中的配置文件是large_nyt.yaml , base_nyt.yaml和small_nyt.yaml ,我们用来分别训练大型,基础和小读者。例如,在AIDA数据集运行中训练大型读者:
relik reader train relik/reader/conf/large_nyt.yaml
train_dataset_path=data/nyt/processed/nyt-train-relik-windowed-candidates.jsonl
val_dataset_path=data/nyt/processed/nyt-dev-relik-windowed-candidates.jsonl
test_dataset_path=data/nyt/processed/nyt-test-relik-windowed-candidates.jsonl可以使用以下命令从检查点保存读者:
from relik . reader . lightning_modules . relik_reader_pl_module import RelikReaderPLModule
checkpoint_path = "path/to/checkpoint"
reader_folder = "path/to/reader"
# If you want to push the model to the Hugging Face Hub set push_to_hub=True
push_to_hub = False
# If you want to push the model to the Hugging Face Hub set the repo_id
repo_id = "sapienzanlp/relik-reader-deberta-v3-large-aida"
pl_model = RelikReaderPLModule . load_from_checkpoint (
trainer . checkpoint_callback . best_model_path
)
pl_model . relik_reader_core_model . save_pretrained ( experiment_path , push_to_hub = push_to_hub , repo_id = repo_id )使用push_to_hub=True该模型将被推到?以repo_id作为存储库ID拥抱脸部集线器,该存储库ID将在其中上传。
可以从回购ID或本地路径加载读者:
from relik . reader import RelikReaderForSpanExtraction , RelikReaderForTripletExtraction
# the reader for span extraction
reader_span = RelikReaderForSpanExtraction (
"sapienzanlp/relik-reader-deberta-v3-large-aida"
)
# the reader for triplet extraction
reader_tripltes = RelikReaderForTripletExtraction (
"sapienzanlp/relik-reader-deberta-v3-large-nyt"
)并用于提取实体和关系:
# an example of candidates for the reader
candidates = [ "Michael Jordan" , "NBA" , "Chicago Bulls" , "Basketball" , "United States" ]
reader_span . read ( "Michael Jordan was one of the best players in the NBA." , candidates = candidates )我们评估了Relik在使用Gerbil链接的实体上的性能。下表显示了Relik宽大和基础的结果(Inkb Micro F1):
| 模型 | 艾达 | MSNBC | der | K50 | R128 | R500 | O15 | O16 | tot | ood | AIT(M:S) |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 类型 | 83.7 | 73.7 | 54.1 | 60.7 | 46.7 | 40.3 | 56.1 | 50.0 | 58.2 | 54.5 | 38:00 |
| entqa | 85.8 | 72.1 | 52.9 | 64.5 | 54.1 | 41.9 | 61.1 | 51.3 | 60.5 | 56.4 | 20:00 |
| Relik Small | 82.2 | 72.7 | 55.6 | 68.3 | 48.0 | 42.3 | 62.7 | 53.6 | 60.7 | 57.6 | 00:29 |
| Relik基地 | 85.3 | 72.3 | 55.6 | 68.0 | 48.1 | 41.6 | 62.5 | 52.3 | 60.7 | 57.2 | 00:29 |
| Relik大 | 86.4 | 75.0 | 56.3 | 72.8 | 51.7 | 43.0 | 65.1 | 57.2 | 63.4 | 60.2 | 01:46 |
比较系统对内域AIDA测试集和室外MSNBC(MSN),Derczynski(DER),Kore50(K50),N3-Reuters-128(R128),N3-RSS-500(R500(R500),OKE-15(OKE-15(O15(O15)),比较系统的评估(INKB MICRO F1)。粗体表示最佳模型。类型使用提到字典。 AIT列显示了系统需要使用NVIDIA RTX 4090来处理整个AIDA测试集的时间(m:s),除了不适合24GB RAM的ENTQA,并且使用A100。
为了评估Relik,我们使用以下步骤:
从这里下载Gerbil服务器。
启动Gerbil服务器:
cd gerbil && ./start.sh cd gerbil-SpotWrapNifWS4Test && mvn clean -Dmaven.tomcat.port=1235 tomcat:runsapienzanlp/relik-entity-linking-large ): python relik/reader/utils/gerbil.py --relik-model-name sapienzanlp/relik-entity-linking-large下表显示了NYT数据集上的Relik的结果(Micro F1):
| 模型 | 纽约 | NYT(pripr) | AIT(M:S) |
|---|---|---|---|
| 反叛 | 93.1 | 93.4 | 01:45 |
| 你 | 93.5 | - - | - - |
| USM | 94.0 | 94.1 | - - |
| Relik大 | 95.0 | 94.9 | 00:30 |
为了评估关系提取,我们可以将读者直接使用脚本relik/reader/triber/prective_re.py,指向已检索的候选人的文件。如果您想使用我们训练有素的读者:
python relik/reader/trainer/predict_re.py --model_path sapienzanlp/relik-reader-deberta-v3-large-nyt --data_path /Users/perelluis/Documents/relik/data/debug/test.window.candidates.jsonl --is-eval请注意,我们计算基于开发集预测关系的阈值。要在评估时计算它,您可以运行以下内容:
python relik/reader/trainer/predict_re.py --model_path sapienzanlp/relik-reader-deberta-v3-large-nyt --data_path /Users/perelluis/Documents/relik/data/debug/dev.window.candidates.jsonl --is-eval --compute-threshold如果您使用此工作的任何部分,请考虑以下内容:
@inproceedings { orlando-etal-2024-relik ,
title = " Retrieve, Read and LinK: Fast and Accurate Entity Linking and Relation Extraction on an Academic Budget " ,
author = " Orlando, Riccardo and Huguet Cabot, Pere-Llu{'i}s and Barba, Edoardo and Navigli, Roberto " ,
booktitle = " Findings of the Association for Computational Linguistics: ACL 2024 " ,
month = aug,
year = " 2024 " ,
address = " Bangkok, Thailand " ,
publisher = " Association for Computational Linguistics " ,
}数据和软件是在创意共享归因非商业共享4.0下许可的。