Reformat code snippets (#1)
Browse files- Update code snippets, MRL (d1cf1c162c1e6a7ae9035f824eae495f07d0eb9c)
- Add default query for 'model.encode_query' usage (88d5ea6d50c4c8f43c13def2f2166f441ef2ff09)
- README.md +78 -18
- config_sentence_transformers.json +1 -1
README.md
CHANGED
|
@@ -32,7 +32,7 @@ tags:
|
|
| 32 |
- Model Size: 0.5B
|
| 33 |
- Embedding Dimension: 896
|
| 34 |
- Max Input Tokens: 32k
|
| 35 |
-
-
|
| 36 |
- Attn: Bidirectional attention
|
| 37 |
- Pooling: Mean pooling
|
| 38 |
|
|
@@ -93,20 +93,32 @@ Then you can use the model like this:
|
|
| 93 |
|
| 94 |
```python
|
| 95 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
model.max_seq_length = 512
|
| 102 |
|
|
|
|
| 103 |
embeddings = model.encode(
|
| 104 |
-
sentences,
|
| 105 |
normalize_embeddings=True,
|
| 106 |
-
batch_size=256,
|
| 107 |
-
show_progress_bar=True
|
| 108 |
-
|
| 109 |
print(embeddings)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
```
|
| 111 |
|
| 112 |
We add task instructions for asymmetric tasks: retrieval, reranking, classification, and clustering.
|
|
@@ -115,22 +127,70 @@ If you want to add task instructions to the query, you can use the model like th
|
|
| 115 |
|
| 116 |
```python
|
| 117 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
model.max_seq_length = 512
|
| 124 |
|
| 125 |
-
|
|
|
|
| 126 |
embeddings = model.encode(
|
| 127 |
-
sentences,
|
| 128 |
prompt=prompt,
|
| 129 |
normalize_embeddings=True,
|
| 130 |
-
batch_size=256,
|
| 131 |
-
show_progress_bar=True
|
| 132 |
-
|
| 133 |
print(embeddings)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
```
|
| 135 |
|
| 136 |
### vllm support
|
|
|
|
| 32 |
- Model Size: 0.5B
|
| 33 |
- Embedding Dimension: 896
|
| 34 |
- Max Input Tokens: 32k
|
| 35 |
+
- MRL dimensions: 896, 512, 256, 128, and 64
|
| 36 |
- Attn: Bidirectional attention
|
| 37 |
- Pooling: Mean pooling
|
| 38 |
|
|
|
|
| 93 |
|
| 94 |
```python
|
| 95 |
from sentence_transformers import SentenceTransformer
|
| 96 |
+
import torch
|
| 97 |
|
| 98 |
+
model = SentenceTransformer(
|
| 99 |
+
"KaLM-Embedding/KaLM-embedding-multilingual-mini-instruct-v2.5",
|
| 100 |
+
trust_remote_code=True,
|
| 101 |
+
model_kwargs={
|
| 102 |
+
"torch_dtype": torch.bfloat16,
|
| 103 |
+
"attn_implementation": "flash_attention_2", # Optional
|
| 104 |
+
},
|
| 105 |
+
)
|
| 106 |
model.max_seq_length = 512
|
| 107 |
|
| 108 |
+
sentences = ["This is an example sentence", "Each sentence is converted"]
|
| 109 |
embeddings = model.encode(
|
| 110 |
+
sentences,
|
| 111 |
normalize_embeddings=True,
|
| 112 |
+
batch_size=256,
|
| 113 |
+
show_progress_bar=True,
|
| 114 |
+
)
|
| 115 |
print(embeddings)
|
| 116 |
+
'''
|
| 117 |
+
[[-0.01043701 -0.02172852 0.0100708 ... -0.02807617 0.00157166
|
| 118 |
+
-0.03637695]
|
| 119 |
+
[-0.00424194 0.02966309 0.03686523 ... -0.02587891 0.01953125
|
| 120 |
+
-0.00125122]]
|
| 121 |
+
'''
|
| 122 |
```
|
| 123 |
|
| 124 |
We add task instructions for asymmetric tasks: retrieval, reranking, classification, and clustering.
|
|
|
|
| 127 |
|
| 128 |
```python
|
| 129 |
from sentence_transformers import SentenceTransformer
|
| 130 |
+
import torch
|
| 131 |
|
| 132 |
+
model = SentenceTransformer(
|
| 133 |
+
"KaLM-Embedding/KaLM-embedding-multilingual-mini-instruct-v2.5",
|
| 134 |
+
trust_remote_code=True,
|
| 135 |
+
model_kwargs={
|
| 136 |
+
"torch_dtype": torch.bfloat16,
|
| 137 |
+
"attn_implementation": "flash_attention_2", # Optional
|
| 138 |
+
},
|
| 139 |
+
)
|
| 140 |
model.max_seq_length = 512
|
| 141 |
|
| 142 |
+
sentences = ["This is an example sentence", "Each sentence is converted"]
|
| 143 |
+
prompt = "Instruct: Classifying the category of french news.\nQuery:"
|
| 144 |
embeddings = model.encode(
|
| 145 |
+
sentences,
|
| 146 |
prompt=prompt,
|
| 147 |
normalize_embeddings=True,
|
| 148 |
+
batch_size=256,
|
| 149 |
+
show_progress_bar=True,
|
| 150 |
+
)
|
| 151 |
print(embeddings)
|
| 152 |
+
'''
|
| 153 |
+
[[-0.01867676 0.02319336 0.00280762 ... -0.02075195 0.00196838
|
| 154 |
+
-0.0703125 ]
|
| 155 |
+
[-0.0067749 0.03491211 0.01434326 ... -0.0043335 0.00509644
|
| 156 |
+
-0.04174805]]
|
| 157 |
+
'''
|
| 158 |
+
```
|
| 159 |
+
|
| 160 |
+
Or you can use `encode_query` and `encode_document` to automatically add the default prompt for queries (`"Instruct: Given a query, retrieve documents that answer the query \n Query: "`) and documents (`""`), respectively.
|
| 161 |
+
|
| 162 |
+
```python
|
| 163 |
+
from sentence_transformers import SentenceTransformer
|
| 164 |
+
import torch
|
| 165 |
+
|
| 166 |
+
model = SentenceTransformer(
|
| 167 |
+
"KaLM-Embedding/KaLM-embedding-multilingual-mini-instruct-v2.5",
|
| 168 |
+
trust_remote_code=True,
|
| 169 |
+
model_kwargs={
|
| 170 |
+
"torch_dtype": torch.bfloat16,
|
| 171 |
+
"attn_implementation": "flash_attention_2", # Optional
|
| 172 |
+
},
|
| 173 |
+
)
|
| 174 |
+
model.max_seq_length = 512
|
| 175 |
+
|
| 176 |
+
queries = [
|
| 177 |
+
"What is the capital of China?",
|
| 178 |
+
"Explain gravity",
|
| 179 |
+
]
|
| 180 |
+
documents = [
|
| 181 |
+
"The capital of China is Beijing.",
|
| 182 |
+
"Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun.",
|
| 183 |
+
]
|
| 184 |
+
|
| 185 |
+
query_embeddings = model.encode_query(queries)
|
| 186 |
+
document_embeddings = model.encode_document(documents)
|
| 187 |
+
|
| 188 |
+
similarities = model.similarity(query_embeddings, document_embeddings)
|
| 189 |
+
print(similarities)
|
| 190 |
+
'''
|
| 191 |
+
tensor([[0.9034, 0.2563],
|
| 192 |
+
[0.3153, 0.7396]])
|
| 193 |
+
'''
|
| 194 |
```
|
| 195 |
|
| 196 |
### vllm support
|
config_sentence_transformers.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
| 5 |
"pytorch": "2.1.2+cu121"
|
| 6 |
},
|
| 7 |
"prompts": {
|
| 8 |
-
"query": "",
|
| 9 |
"document": ""
|
| 10 |
},
|
| 11 |
"default_prompt_name": null,
|
|
|
|
| 5 |
"pytorch": "2.1.2+cu121"
|
| 6 |
},
|
| 7 |
"prompts": {
|
| 8 |
+
"query": "Instruct: Given a query, retrieve documents that answer the query \n Query: ",
|
| 9 |
"document": ""
|
| 10 |
},
|
| 11 |
"default_prompt_name": null,
|