Views
No views yet


1from relik.inference.annotator import Relik
2from relik.retriever.indexers.inmemory import InMemoryDocumentIndex
3from relik.retriever.pytorch_modules.model import GoldenRetriever
4from relik.retriever.pytorch_modules import RetrievedSample
5from relik.retriever.indexers.document import Document, DocumentStore
6from relik.retriever.indexers.base import BaseDocumentIndex
7
8class GoldenSillyRetriever(GoldenRetriever):
9 def __init__(self, documents: List[str], *args, **kwargs):
10 self.documents = DocumentStore([Document(doc) for doc in documents])
11 self.document_index = BaseDocumentIndex(self.documents)
12 def retrieve(self,
13 text: Optional[Union[str, List[str]]] = None,
14 k: int = 100,
15 *args,
16 **kwargs,
17 ) -> List[List[RetrievedSample]]:
18 if isinstance(text, str):
19 text = [text]
20 elif text is None:
21 text = []
22 return [
23 [RetrievedSample(score=1.0, document=doc) for doc in self.documents[:k]]
24 for _ in text
25 ]
26 def index(self):
27 pass
28 def eval(self):
29 pass
30 def save_pretrained(self):
31 pass
32 def to(self, device):
33 pass
34
35# And pass this to the ReLiK module:
36wikidata_retriever = GoldenRetriever("relik-ie/encoder-e5-small-v2-wikipedia-relations", device="cuda")
37wikidata_index = InMemoryDocumentIndex.from_pretrained("relik-ie/encoder-e5-small-v2-wikipedia-relations-index", index_precision="bf16", device="cuda")
38
39ner_type_retriever = GoldenSillyRetriever(
40 documents=['media', 'disease', 'miscellaneous', 'event', 'person', 'location', 'time', 'celestial', 'organization', 'concept']
41 )
42
43relik = Relik.from_pretrained(
44 "relik-ie/relik-relation-extraction-small-wikipedia-ner",
45 index=wikidata_index,
46 device="cuda",
47 use_nme=True,
48 retriever={
49 TaskType.SPAN: ner_type_retriever,
50 TaskType.TRIPLET: wikidata_retriever,
51 },
52 index={
53 TaskType.SPAN: ner_type_retriever.document_index,
54 TaskType.TRIPLET: wikidata_index,
55 }
56)pip install relikpip install relik[all]pip install relik[train]pip install relik[faiss]1conda create -n relik python=3.10
2conda activate relik
3
4# install pytorch
5conda install -y pytorch=2.1.0 pytorch-cuda=12.1 -c pytorch -c nvidia
6
7# GPU
8conda install -y -c pytorch -c nvidia faiss-gpu=1.8.0
9# or GPU with NVIDIA RAFT
10conda install -y -c pytorch -c nvidia -c rapidsai -c conda-forge faiss-gpu-raft=1.8.0
11
12pip install relikpip install relik[serve]1git clone https://github.com/SapienzaNLP/relik.git
2cd relik
3pip install -e .[all]from_pretrained method to load a pre-trained pipeline.1from relik import Relik
2from relik.inference.data.objects import RelikOutput
3
4relik = Relik.from_pretrained("sapienzanlp/relik-entity-linking-large")
5relik_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},
...
]
]
]
),
)| Model | AIDA | MSNBC | Der | K50 | R128 | R500 | O15 | O16 | Tot | OOD | AIT (m:s) |
|---|---|---|---|---|---|---|---|---|---|---|---|
| GENRE | 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 |
| ReLiKBase | 85.3 | 72.3 | 55.6 | 68.0 | 48.1 | 41.6 | 62.5 | 52.3 | 60.7 | 57.2 | 00:29 |
| ➡️ ReLiKLarge | 86.4 | 75.0 | 56.3 | 72.8 | 51.7 | 43.0 | 65.1 | 57.2 | 63.4 | 60.2 | 01:46 |
1@inproceedings{orlando-etal-2024-relik,
2 title = "Retrieve, Read and LinK: Fast and Accurate Entity Linking and Relation Extraction on an Academic Budget",
3 author = "Orlando, Riccardo and Huguet Cabot, Pere-Llu{\'\i}s and Barba, Edoardo and Navigli, Roberto",
4 booktitle = "Findings of the Association for Computational Linguistics: ACL 2024",
5 month = aug,
6 year = "2024",
7 address = "Bangkok, Thailand",
8 publisher = "Association for Computational Linguistics",
9}