Integrate with Sentence Transformers v5.4
#8
by tomaarsen HF Staff - opened
- 1_LogitScore/config.json +4 -0
- README.md +31 -2
- chat_template.jinja +11 -1
- config_sentence_transformers.json +9 -0
- modules.json +14 -0
- sentence_bert_config.json +20 -0
1_LogitScore/config.json
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"true_token_id": 9454,
|
| 3 |
+
"false_token_id": null
|
| 4 |
+
}
|
README.md
CHANGED
|
@@ -40,18 +40,47 @@ This model is released under a non-commercial license. If you'd like a commercia
|
|
| 40 |
|
| 41 |
## How to Use
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
```python
|
| 44 |
from sentence_transformers import CrossEncoder
|
| 45 |
|
| 46 |
-
model = CrossEncoder("zeroentropy/zerank-2"
|
| 47 |
|
| 48 |
query_documents = [
|
| 49 |
("What is 2+2?", "4"),
|
| 50 |
("What is 2+2?", "The answer is definitely 1 million"),
|
| 51 |
]
|
| 52 |
|
| 53 |
-
scores = model.predict(query_documents)
|
| 54 |
print(scores)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
```
|
| 56 |
|
| 57 |
The model can also be inferenced using ZeroEntropy's [/models/rerank](https://docs.zeroentropy.dev/api-reference/models/rerank) endpoint, and on [AWS Marketplace](https://aws.amazon.com/marketplace/pp/prodview-o7avk66msiukc).
|
|
|
|
| 40 |
|
| 41 |
## How to Use
|
| 42 |
|
| 43 |
+
### Using Sentence Transformers
|
| 44 |
+
|
| 45 |
+
Install Sentence Transformers:
|
| 46 |
+
|
| 47 |
+
```bash
|
| 48 |
+
pip install sentence_transformers
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
Then load the model and score query/document pairs. `model.predict` returns the raw "Yes" logit per pair; rankings can be used directly. To map the logits to a 0-1 score range, apply a temperature-scaled sigmoid: `sigmoid(score / 5)`.
|
| 52 |
+
|
| 53 |
```python
|
| 54 |
from sentence_transformers import CrossEncoder
|
| 55 |
|
| 56 |
+
model = CrossEncoder("zeroentropy/zerank-2")
|
| 57 |
|
| 58 |
query_documents = [
|
| 59 |
("What is 2+2?", "4"),
|
| 60 |
("What is 2+2?", "The answer is definitely 1 million"),
|
| 61 |
]
|
| 62 |
|
| 63 |
+
scores = model.predict(query_documents, convert_to_tensor=True)
|
| 64 |
print(scores)
|
| 65 |
+
# tensor([ 5.4062, -4.5000], device='cuda:0', dtype=torch.bfloat16)
|
| 66 |
+
|
| 67 |
+
# Optional: convert to 0-1 probabilities
|
| 68 |
+
probabilities = (scores / 5).sigmoid()
|
| 69 |
+
print(probabilities)
|
| 70 |
+
# tensor([0.7461, 0.2891], device='cuda:0', dtype=torch.bfloat16)
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
You can also use `model.rank` to score and sort a list of documents for a single query:
|
| 74 |
+
|
| 75 |
+
```python
|
| 76 |
+
rankings = model.rank(
|
| 77 |
+
"What is 2+2?",
|
| 78 |
+
["4", "The answer is definitely 1 million"],
|
| 79 |
+
)
|
| 80 |
+
for r in rankings:
|
| 81 |
+
print(r)
|
| 82 |
+
# {'corpus_id': 0, 'score': np.float32(5.40625)}
|
| 83 |
+
# {'corpus_id': 1, 'score': np.float32(-4.5)}
|
| 84 |
```
|
| 85 |
|
| 86 |
The model can also be inferenced using ZeroEntropy's [/models/rerank](https://docs.zeroentropy.dev/api-reference/models/rerank) endpoint, and on [AWS Marketplace](https://aws.amazon.com/marketplace/pp/prodview-o7avk66msiukc).
|
chat_template.jinja
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
{%- if tools %}
|
| 2 |
{{- '<|im_start|>system\n' }}
|
| 3 |
{%- if messages[0].role == 'system' %}
|
|
@@ -86,4 +95,5 @@
|
|
| 86 |
{%- if enable_thinking is defined and enable_thinking is false %}
|
| 87 |
{{- '<think>\n\n</think>\n\n' }}
|
| 88 |
{%- endif %}
|
| 89 |
-
{%- endif %}
|
|
|
|
|
|
| 1 |
+
{%- set zerank_query = messages | selectattr("role", "eq", "query") | map(attribute="content") | list -%}
|
| 2 |
+
{%- set zerank_document = messages | selectattr("role", "eq", "document") | map(attribute="content") | list -%}
|
| 3 |
+
{%- if zerank_query and zerank_document -%}
|
| 4 |
+
{{- '<|im_start|>system\n' + (zerank_query | first) + '<|im_end|>\n' -}}
|
| 5 |
+
{{- '<|im_start|>user\n' + (zerank_document | first) + '<|im_end|>\n' -}}
|
| 6 |
+
{%- if add_generation_prompt -%}
|
| 7 |
+
{{- '<|im_start|>assistant\n' -}}
|
| 8 |
+
{%- endif -%}
|
| 9 |
+
{%- else -%}
|
| 10 |
{%- if tools %}
|
| 11 |
{{- '<|im_start|>system\n' }}
|
| 12 |
{%- if messages[0].role == 'system' %}
|
|
|
|
| 95 |
{%- if enable_thinking is defined and enable_thinking is false %}
|
| 96 |
{{- '<think>\n\n</think>\n\n' }}
|
| 97 |
{%- endif %}
|
| 98 |
+
{%- endif %}
|
| 99 |
+
{%- endif -%}
|
config_sentence_transformers.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"__version__": {
|
| 3 |
+
"sentence_transformers": "5.4.0"
|
| 4 |
+
},
|
| 5 |
+
"activation_fn": "torch.nn.modules.linear.Identity",
|
| 6 |
+
"default_prompt_name": null,
|
| 7 |
+
"model_type": "CrossEncoder",
|
| 8 |
+
"prompts": {}
|
| 9 |
+
}
|
modules.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"idx": 0,
|
| 4 |
+
"name": "0",
|
| 5 |
+
"path": "",
|
| 6 |
+
"type": "sentence_transformers.base.modules.transformer.Transformer"
|
| 7 |
+
},
|
| 8 |
+
{
|
| 9 |
+
"idx": 1,
|
| 10 |
+
"name": "1",
|
| 11 |
+
"path": "1_LogitScore",
|
| 12 |
+
"type": "sentence_transformers.cross_encoder.modules.logit_score.LogitScore"
|
| 13 |
+
}
|
| 14 |
+
]
|
sentence_bert_config.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"transformer_task": "text-generation",
|
| 3 |
+
"modality_config": {
|
| 4 |
+
"text": {
|
| 5 |
+
"method": "forward",
|
| 6 |
+
"method_output_name": "logits"
|
| 7 |
+
},
|
| 8 |
+
"message": {
|
| 9 |
+
"method": "forward",
|
| 10 |
+
"method_output_name": "logits",
|
| 11 |
+
"format": "flat"
|
| 12 |
+
}
|
| 13 |
+
},
|
| 14 |
+
"module_output_name": "causal_logits",
|
| 15 |
+
"processing_kwargs": {
|
| 16 |
+
"chat_template": {
|
| 17 |
+
"add_generation_prompt": true
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
}
|