Update README.md
Browse files
README.md
CHANGED
|
@@ -1,143 +1,116 @@
|
|
| 1 |
---
|
| 2 |
tags:
|
|
|
|
| 3 |
- sentence-transformers
|
| 4 |
-
|
| 5 |
-
-
|
| 6 |
-
-
|
| 7 |
-
|
| 8 |
-
library_name: sentence-transformers
|
| 9 |
---
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
- **Model Type:** Sentence Transformer
|
| 19 |
-
<!-- - **Base model:** [Unknown](https://huggingface.co/unknown) -->
|
| 20 |
-
- **Maximum Sequence Length:** 512 tokens
|
| 21 |
-
- **Output Dimensionality:** 768 dimensions
|
| 22 |
-
- **Similarity Function:** Dot Product
|
| 23 |
-
<!-- - **Training Dataset:** Unknown -->
|
| 24 |
-
<!-- - **Language:** Unknown -->
|
| 25 |
-
<!-- - **License:** Unknown -->
|
| 26 |
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
SentenceTransformer(
|
| 37 |
-
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False, 'architecture': 'KPRModelForBert'})
|
| 38 |
-
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': True, 'pooling_mode_mean_tokens': False, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False, 'include_prompt': True})
|
| 39 |
-
)
|
| 40 |
-
```
|
| 41 |
|
| 42 |
-
##
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
Then you can load this model and run inference.
|
| 53 |
```python
|
| 54 |
-
from
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
sentences = [
|
| 60 |
-
'The weather is lovely today.',
|
| 61 |
-
"It's so sunny outside!",
|
| 62 |
-
'He drove to the stadium.',
|
| 63 |
]
|
| 64 |
-
embeddings = model.encode(sentences)
|
| 65 |
-
print(embeddings.shape)
|
| 66 |
-
# [3, 768]
|
| 67 |
-
|
| 68 |
-
# Get the similarity scores for the embeddings
|
| 69 |
-
similarities = model.similarity(embeddings, embeddings)
|
| 70 |
-
print(similarities)
|
| 71 |
-
# tensor([[743.6603, 712.7500, 674.8392],
|
| 72 |
-
# [712.7500, 743.7998, 678.3881],
|
| 73 |
-
# [674.8391, 678.3880, 743.6827]])
|
| 74 |
-
```
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
-->
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
|
|
|
| 111 |
|
| 112 |
-
##
|
| 113 |
|
| 114 |
-
|
| 115 |
-
- Python: 3.10.14
|
| 116 |
-
- Sentence Transformers: 5.2.0.dev0
|
| 117 |
-
- Transformers: 4.55.4
|
| 118 |
-
- PyTorch: 2.4.0+cu121
|
| 119 |
-
- Accelerate: 0.34.2
|
| 120 |
-
- Datasets: 2.16.1
|
| 121 |
-
- Tokenizers: 0.21.4
|
| 122 |
|
| 123 |
## Citation
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
<!--
|
| 128 |
-
## Glossary
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
<!--
|
| 140 |
-
## Model Card Contact
|
| 141 |
-
|
| 142 |
-
*Provides a way for people who have updates to the Model Card, suggestions, or questions, to contact the Model Card authors.*
|
| 143 |
-
-->
|
|
|
|
| 1 |
---
|
| 2 |
tags:
|
| 3 |
+
- transformers
|
| 4 |
- sentence-transformers
|
| 5 |
+
language:
|
| 6 |
+
- en
|
| 7 |
+
license: apache-2.0
|
| 8 |
+
library_name: transformers
|
|
|
|
| 9 |
---
|
| 10 |
+
## Introduction
|
| 11 |
|
| 12 |
+
A key limitation of large language models (LLMs) is their inability to capture less-frequent or up-to-date entity knowledge, often leading to factual inaccuracies and hallucinations. Retrieval-augmented generation (RAG), which incorporates external knowledge through retrieval, is a common approach to mitigate this issue.
|
| 13 |
|
| 14 |
+
Although RAG typically relies on embedding-based retrieval, the embedding models themselves are also based on language models and therefore struggle with queries involving less-frequent entities, often failing to retrieve the crucial knowledge needed to overcome this limitation.
|
| 15 |
|
| 16 |
+
**Knowledgeable Passage Retriever** enhances the performance with such queries by injecting real-world entity knowledge into embeddings, making them more *knowledgeable*.
|
| 17 |
+
**The entity knowledge is pluggable and can be dynamically updated with ease.**
|
| 18 |
|
| 19 |
+
For more details, refer to [our GitHub repository](https://github.com/knowledgeable-embedding/knowledgeable-embedding).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
## Model List
|
| 22 |
|
| 23 |
+
| Model | Model Size | Base Model |
|
| 24 |
+
| --- | --- | --- |
|
| 25 |
+
| [knowledgeable-ai/kpr-bert-base-uncased](https://huggingface.co/knowledgeable-ai/kpr-bert-base-uncased) | 112M | [bert-base-uncased](https://huggingface.co/google-bert/bert-base-uncased) |
|
| 26 |
+
| [knowledgeable-ai/kpr-retromae](https://huggingface.co/knowledgeable-ai/kpr-retromae) | 112M | [RetroMAE](https://huggingface.co/Shitao/RetroMAE) |
|
| 27 |
+
| [knowledgeable-ai/kpr-bge-base-en](https://huggingface.co/knowledgeable-ai/kpr-bge-base-en) | 112M | [bge-base-en](https://huggingface.co/BAAI/bge-base-en) |
|
| 28 |
+
| [knowledgeable-ai/kpr-bge-base-en-v1.5](https://huggingface.co/knowledgeable-ai/kpr-bge-base-en-v1.5) | 112M | [bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) |
|
| 29 |
+
| [knowledgeable-ai/kpr-bge-large-en-v1.5](https://huggingface.co/knowledgeable-ai/kpr-bge-large-en-v1.5) | 340M | [bge-large-en-v1.5](https://huggingface.co/BAAI/bge-large-en-v1.5) |
|
| 30 |
|
| 31 |
+
For practical use, we recommend `knowledgeable-ai/kpr-bge-*`, which significantly outperforms state-of-the-art models on queries involving less-frequent entities while performing comparably on other queries, as reported in [our paper](https://arxiv.org/abs/2507.03922).
|
| 32 |
|
| 33 |
+
Regarding the model size, we do not count the entity embeddings since they are stored in CPU memory and have a negligible impact on runtime performance. See [this page](https://github.com/knowledgeable-embedding/knowledgeable-embedding/wiki/Internals-of-Knowledgeable-Embedding) for details.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
## Model Details
|
| 36 |
|
| 37 |
+
- Base Model: [bert-base-uncased](https://huggingface.co/google-bert/bert-base-uncased)
|
| 38 |
+
- Maximum Sequence Length: 512
|
| 39 |
+
- Embedding Dimension: 768
|
| 40 |
|
| 41 |
+
## How to use
|
| 42 |
|
| 43 |
+
This model can be used via [Hugging Face Transformers](https://github.com/huggingface/transformers) or [Sentence Transformers](https://github.com/UKPLab/sentence-transformers):
|
| 44 |
+
|
| 45 |
+
### Hugging Face Transformers
|
| 46 |
|
|
|
|
| 47 |
```python
|
| 48 |
+
from transformers import AutoTokenizer, AutoModel
|
| 49 |
+
import torch
|
| 50 |
+
|
| 51 |
+
MODEL_NAME_OR_PATH = "knowledgeable-ai/kpr-bge-base-en"
|
| 52 |
|
| 53 |
+
input_texts = [
|
| 54 |
+
"Who founded Dominican Liberation Party?",
|
| 55 |
+
"Who owns Mompesson House?"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Load model and tokenizer from the Hugging Face Hub
|
| 59 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME_OR_PATH, trust_remote_code=True)
|
| 60 |
+
model = AutoModel.from_pretrained(MODEL_NAME_OR_PATH, trust_remote_code=True)
|
| 61 |
|
| 62 |
+
# Preprocess the text
|
| 63 |
+
preprocessed_inputs = tokenizer(input_texts, return_tensors="pt", padding=True)
|
| 64 |
|
| 65 |
+
# Compute embeddings
|
| 66 |
+
with torch.no_grad():
|
| 67 |
+
embeddings = model.encode(**preprocessed_inputs)
|
| 68 |
|
| 69 |
+
print("Embeddings:", embeddings)
|
| 70 |
+
```
|
| 71 |
|
| 72 |
+
### Sentence Transformers
|
| 73 |
|
| 74 |
+
```python
|
| 75 |
+
from sentence_transformers import SentenceTransformer
|
| 76 |
|
| 77 |
+
MODEL_NAME_OR_PATH = "knowledgeable-ai/kpr-bge-base-en"
|
|
|
|
| 78 |
|
| 79 |
+
input_texts = [
|
| 80 |
+
"Who founded Dominican Liberation Party?",
|
| 81 |
+
"Who owns Mompesson House?"
|
| 82 |
+
]
|
| 83 |
|
| 84 |
+
# Load model from the Hugging Face Hub
|
| 85 |
+
model = SentenceTransformer(MODEL_NAME_OR_PATH, trust_remote_code=True)
|
| 86 |
|
| 87 |
+
# Compute embeddings
|
| 88 |
+
embeddings = model.encode(input_texts)
|
| 89 |
|
| 90 |
+
print("Embeddings:", embeddings)
|
| 91 |
+
```
|
| 92 |
|
| 93 |
+
**IMPORTANT:** This code will be supported in versions of Sentence Transformers later than v5.1.0,
|
| 94 |
+
which have not yet been released at the time of writing. Until then, please install the library directly from GitHub:
|
| 95 |
|
| 96 |
+
```bash
|
| 97 |
+
pip install git+https://github.com/UKPLab/sentence-transformers.git
|
| 98 |
+
```
|
| 99 |
|
| 100 |
+
## License
|
| 101 |
|
| 102 |
+
This model is licensed under the Apache License, Version 2.0.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
## Citation
|
| 105 |
|
| 106 |
+
If you use this model in your research, please cite the following paper:
|
| 107 |
+
[Dynamic Injection of Entity Knowledge into Dense Retrievers](https://arxiv.org/abs/2507.03922)
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
```bibtex
|
| 110 |
+
@article{yamada2025kpr,
|
| 111 |
+
title={Dynamic Injection of Entity Knowledge into Dense Retrievers},
|
| 112 |
+
author={Ikuya Yamada and Ryokan Ri and Takeshi Kojima and Yusuke Iwasawa and Yutaka Matsuo},
|
| 113 |
+
journal={arXiv preprint arXiv:2507.03922},
|
| 114 |
+
year={2025}
|
| 115 |
+
}
|
| 116 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|