Improve language tag
Browse filesHi! As the model is multilingual, this is a PR to add other languages than English to the language tag to improve the referencing. Note that 29 languages are announced in the README, but only 13 are explicitly listed. I was therefore only able to add these 13 languages.
README.md
CHANGED
|
@@ -1,358 +1,290 @@
|
|
| 1 |
-
---
|
| 2 |
-
library_name: transformers
|
| 3 |
-
license: apache-2.0
|
| 4 |
-
language:
|
| 5 |
-
-
|
| 6 |
-
-
|
| 7 |
-
-
|
| 8 |
-
-
|
| 9 |
-
-
|
| 10 |
-
-
|
| 11 |
-
-
|
| 12 |
-
-
|
| 13 |
-
-
|
| 14 |
-
-
|
| 15 |
-
-
|
| 16 |
-
-
|
| 17 |
-
-
|
| 18 |
-
|
| 19 |
-
-
|
| 20 |
-
|
| 21 |
-
-
|
| 22 |
-
-
|
| 23 |
-
|
| 24 |
-
-
|
| 25 |
-
|
| 26 |
-
-
|
| 27 |
-
|
| 28 |
-
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
-
|
| 35 |
-
|
| 36 |
-
-
|
| 37 |
-
|
| 38 |
-
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
-
|
| 52 |
-
-
|
| 53 |
-
|
| 54 |
-
-
|
| 55 |
-
-
|
| 56 |
-
|
| 57 |
-
-
|
| 58 |
-
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
-
|
| 66 |
-
|
| 67 |
-
-
|
| 68 |
-
|
| 69 |
-
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 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 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
]
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
</
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
#
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
def
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
)
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
)
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
<
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
```python
|
| 291 |
-
from openai import OpenAI
|
| 292 |
-
import numpy as np
|
| 293 |
-
from multiprocessing import Pool
|
| 294 |
-
from tqdm.auto import tqdm
|
| 295 |
-
|
| 296 |
-
client = OpenAI(
|
| 297 |
-
base_url="https://api-inference.huggingface.co/v1/",
|
| 298 |
-
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
|
| 299 |
-
)
|
| 300 |
-
|
| 301 |
-
def make_reranker_input(t, q):
|
| 302 |
-
return f"<<<Context>>>\n{t}\n\n<<<Query>>>\n{q}"
|
| 303 |
-
|
| 304 |
-
def make_reranker_inference_conversation(context, question):
|
| 305 |
-
system_message = "Given a piece of text and a query, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
|
| 306 |
-
|
| 307 |
-
return [
|
| 308 |
-
{"role": "system", "content": system_message},
|
| 309 |
-
{"role": "user", "content": make_reranker_input(context, question)},
|
| 310 |
-
]
|
| 311 |
-
|
| 312 |
-
def get_reranker_score(context_question_tuple):
|
| 313 |
-
question, context = context_question_tuple
|
| 314 |
-
|
| 315 |
-
messages = make_reranker_inference_conversation(context, question)
|
| 316 |
-
|
| 317 |
-
completion = client.chat.completions.create(
|
| 318 |
-
model="lightblue/lb-reranker-0.5B-v1.0-rev",
|
| 319 |
-
messages=messages,
|
| 320 |
-
max_tokens=1,
|
| 321 |
-
temperature=0.0,
|
| 322 |
-
logprobs=True,
|
| 323 |
-
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
|
| 324 |
-
)
|
| 325 |
-
|
| 326 |
-
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
|
| 327 |
-
|
| 328 |
-
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
|
| 329 |
-
|
| 330 |
-
return calculated_score
|
| 331 |
-
|
| 332 |
-
query_texts = [
|
| 333 |
-
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
|
| 334 |
-
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
|
| 335 |
-
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
|
| 336 |
-
]
|
| 337 |
-
|
| 338 |
-
with Pool(processes=16) as p: # Allows for parallel processing
|
| 339 |
-
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
|
| 340 |
-
|
| 341 |
-
print(expected_vals)
|
| 342 |
-
# [6.64866580, 1.85144404, 1.010719508]
|
| 343 |
-
```
|
| 344 |
-
|
| 345 |
-
</details></li>
|
| 346 |
-
</ul>
|
| 347 |
-
|
| 348 |
-
# License
|
| 349 |
-
|
| 350 |
-
We share this model under an Apache 2.0 license.
|
| 351 |
-
|
| 352 |
-
# Developed by
|
| 353 |
-
|
| 354 |
-
<a href="https://www.lightblue-tech.com">
|
| 355 |
-
<img src="https://www.lightblue-tech.com/wp-content/uploads/2023/08/color_%E6%A8%AA%E5%9E%8B-1536x469.png" alt="Lightblue technology logo" width="400"/>
|
| 356 |
-
</a>
|
| 357 |
-
|
| 358 |
This model was trained by Peter Devine ([ptrdvn](https://huggingface.co/ptrdvn)) for Lightblue
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers
|
| 3 |
+
license: apache-2.0
|
| 4 |
+
language:
|
| 5 |
+
- zho
|
| 6 |
+
- eng
|
| 7 |
+
- fra
|
| 8 |
+
- spa
|
| 9 |
+
- por
|
| 10 |
+
- deu
|
| 11 |
+
- ita
|
| 12 |
+
- rus
|
| 13 |
+
- jpn
|
| 14 |
+
- kor
|
| 15 |
+
- vie
|
| 16 |
+
- tha
|
| 17 |
+
- ara
|
| 18 |
+
datasets:
|
| 19 |
+
- lightblue/reranker_continuous_filt_max7_train
|
| 20 |
+
base_model:
|
| 21 |
+
- Qwen/Qwen2.5-0.5B-Instruct
|
| 22 |
+
pipeline_tag: text-generation
|
| 23 |
+
tags:
|
| 24 |
+
- reranker
|
| 25 |
+
widget:
|
| 26 |
+
- text: '<<<Query>>>
|
| 27 |
+
|
| 28 |
+
How many languages has LB-Reranker been trained on?
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
<<<Context>>>
|
| 33 |
+
|
| 34 |
+
LB-Reranker has been trained on more than 95 languages.'
|
| 35 |
+
example_title: Positive example (7/7)
|
| 36 |
+
- text: '<<<Query>>>
|
| 37 |
+
|
| 38 |
+
How many languages has LB-Reranker been trained on?
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
<<<Context>>>
|
| 43 |
+
|
| 44 |
+
AA-Reranker is applicable to a broad range of use cases.'
|
| 45 |
+
example_title: Negative example (2/7)
|
| 46 |
+
---
|
| 47 |
+
|
| 48 |
+
# LB Reranker v1.0
|
| 49 |
+
|
| 50 |
+
<div style="width: 100%; height: 160px;
|
| 51 |
+
display: flex; align-items: center;
|
| 52 |
+
justify-content: center;
|
| 53 |
+
border: 8px solid black;
|
| 54 |
+
font-size: 120px; font-weight: bold;
|
| 55 |
+
text-align: center;
|
| 56 |
+
color: #438db8;
|
| 57 |
+
font-family: 'Helvetica Neue', sans-serif;">
|
| 58 |
+
LBR-r
|
| 59 |
+
</div>
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
This is a reversed version of the original LB Reranker - (lightblue/lb-reranker-0.5B-v1.0)[https://huggingface.co/lightblue/lb-reranker-0.5B-v1.0].
|
| 63 |
+
With this version, you input the text, then the query into the reranker, allowing for caching of the text instead of the query.
|
| 64 |
+
|
| 65 |
+
The LB Reranker has been trained to determine the relatedness of a given query to a piece of text, therefore allowing it to be used as a ranker or reranker in various retrieval-based tasks.
|
| 66 |
+
|
| 67 |
+
This model is fine-tuned from a [Qwen/Qwen2.5-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct) model checkpoint and was trained for roughly 5.5 hours using the 8 x L20 instance ([ecs.gn8is-8x.32xlarge](https://www.alibabacloud.com/help/en/ecs/user-guide/gpu-accelerated-compute-optimized-and-vgpu-accelerated-instance-families-1)) on [Alibaba Cloud](https://www.alibabacloud.com/).
|
| 68 |
+
|
| 69 |
+
The training data for this model can be found at [lightblue/reranker_continuous_filt_max7_train](https://huggingface.co/datasets/lightblue/reranker_continuous_filt_max7_train) and the code for generating this data as well as running the training of the model can be found on [our Github repo](https://github.com/lightblue-tech/lb-reranker).
|
| 70 |
+
|
| 71 |
+
Trained on data in over 95 languages, this model is applicable to a broad range of use cases.
|
| 72 |
+
|
| 73 |
+
This model has three main benefits over comparable rerankers.
|
| 74 |
+
1. It has shown slightly higher performance on evaluation benchmarks.
|
| 75 |
+
2. It has been trained on more languages than any previous model.
|
| 76 |
+
3. It is a simple Causal LM model trained to output a string between "1" and "7".
|
| 77 |
+
|
| 78 |
+
This last point means that this model can be used natively with many widely available inference packages, including vLLM and LMDeploy.
|
| 79 |
+
This in turns allows our reranker to benefit from improvements to inference as and when these packages release them.
|
| 80 |
+
|
| 81 |
+
Update: We have also found that this model works pretty well as a code snippet reranker too (P@1 of 96%)! See our [Colab](https://colab.research.google.com/drive/1ABL1xaarekLIlVJKbniYhXgYu6ZNwfBm?usp=sharing) for more details.
|
| 82 |
+
|
| 83 |
+
# How to use
|
| 84 |
+
|
| 85 |
+
The model was trained to expect an input such as:
|
| 86 |
+
|
| 87 |
+
```
|
| 88 |
+
<<<Context>>>
|
| 89 |
+
{your_context_here}
|
| 90 |
+
|
| 91 |
+
<<<Query>>>
|
| 92 |
+
{your_query_here}
|
| 93 |
+
```
|
| 94 |
+
|
| 95 |
+
And to output a string of a number between 1-7.
|
| 96 |
+
|
| 97 |
+
In order to make a continuous score that can be used for reranking query-context pairs (i.e. a method with few ties), we calculate the expectation value of the scores.
|
| 98 |
+
|
| 99 |
+
We include scripts to do this in vLLM, LMDeploy, and OpenAI (hosted for free on Huggingface):
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
<ul>
|
| 103 |
+
<li><b>vLLM</b>
|
| 104 |
+
|
| 105 |
+
Install [vLLM](https://github.com/vllm-project/vllm/) using `pip install vllm`.
|
| 106 |
+
|
| 107 |
+
<details open>
|
| 108 |
+
<summary>Show vLLM code</summary>
|
| 109 |
+
|
| 110 |
+
```python
|
| 111 |
+
from vllm import LLM, SamplingParams
|
| 112 |
+
import numpy as np
|
| 113 |
+
|
| 114 |
+
def make_reranker_input(t, q):
|
| 115 |
+
return f"<<<Context>>>\n{t}\n\n<<<Query>>>\n{q}"
|
| 116 |
+
|
| 117 |
+
def make_reranker_inference_conversation(context, question):
|
| 118 |
+
system_message = "Given a piece of text and a query, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
|
| 119 |
+
|
| 120 |
+
return [
|
| 121 |
+
{"role": "system", "content": system_message},
|
| 122 |
+
{"role": "user", "content": make_reranker_input(context, question)},
|
| 123 |
+
]
|
| 124 |
+
|
| 125 |
+
def get_prob(logprob_dict, tok_id):
|
| 126 |
+
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
|
| 127 |
+
|
| 128 |
+
llm = LLM("lightblue/lb-reranker-0.5B-v1.0-rev")
|
| 129 |
+
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
|
| 130 |
+
tok = llm.llm_engine.tokenizer.tokenizer
|
| 131 |
+
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
|
| 132 |
+
|
| 133 |
+
query_texts = [
|
| 134 |
+
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
|
| 135 |
+
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
|
| 136 |
+
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
|
| 137 |
+
]
|
| 138 |
+
|
| 139 |
+
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
|
| 140 |
+
responses = llm.chat(chats, sampling_params)
|
| 141 |
+
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
|
| 142 |
+
|
| 143 |
+
N = probs.shape[1]
|
| 144 |
+
M = probs.shape[0]
|
| 145 |
+
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
|
| 146 |
+
|
| 147 |
+
expected_vals = (probs * idxs).sum(axis=1)
|
| 148 |
+
print(expected_vals)
|
| 149 |
+
# [6.66570732 1.86686378 1.01102923]
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
</details></li>
|
| 153 |
+
<li><b>LMDeploy</b>
|
| 154 |
+
|
| 155 |
+
Install [LMDeploy](https://github.com/InternLM/lmdeploy) using `pip install lmdeploy`.
|
| 156 |
+
|
| 157 |
+
<details>
|
| 158 |
+
<summary>Show LMDeploy code</summary>
|
| 159 |
+
|
| 160 |
+
```python
|
| 161 |
+
# Un-comment this if running in a Jupyter notebook, Colab etc.
|
| 162 |
+
# import nest_asyncio
|
| 163 |
+
# nest_asyncio.apply()
|
| 164 |
+
|
| 165 |
+
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
|
| 166 |
+
import numpy as np
|
| 167 |
+
|
| 168 |
+
def make_reranker_input(t, q):
|
| 169 |
+
return f"<<<Context>>>\n{t}\n\n<<<Query>>>\n{q}"
|
| 170 |
+
|
| 171 |
+
def make_reranker_inference_conversation(context, question):
|
| 172 |
+
system_message = "Given a piece of text and a query, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
|
| 173 |
+
|
| 174 |
+
return [
|
| 175 |
+
{"role": "system", "content": system_message},
|
| 176 |
+
{"role": "user", "content": make_reranker_input(context, question)},
|
| 177 |
+
]
|
| 178 |
+
|
| 179 |
+
def get_prob(logprob_dict, tok_id):
|
| 180 |
+
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
|
| 181 |
+
|
| 182 |
+
pipe = pipeline(
|
| 183 |
+
"lightblue/lb-reranker-0.5B-v1.0-rev",
|
| 184 |
+
chat_template_config=ChatTemplateConfig(
|
| 185 |
+
model_name='qwen2d5',
|
| 186 |
+
capability='chat'
|
| 187 |
+
)
|
| 188 |
+
)
|
| 189 |
+
tok = pipe.tokenizer.model
|
| 190 |
+
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
|
| 191 |
+
|
| 192 |
+
query_texts = [
|
| 193 |
+
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
|
| 194 |
+
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
|
| 195 |
+
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
|
| 196 |
+
]
|
| 197 |
+
|
| 198 |
+
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
|
| 199 |
+
responses = pipe(
|
| 200 |
+
chats,
|
| 201 |
+
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
|
| 202 |
+
)
|
| 203 |
+
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
|
| 204 |
+
|
| 205 |
+
N = probs.shape[1]
|
| 206 |
+
M = probs.shape[0]
|
| 207 |
+
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
|
| 208 |
+
|
| 209 |
+
expected_vals = (probs * idxs).sum(axis=1)
|
| 210 |
+
print(expected_vals)
|
| 211 |
+
# [6.66415229 1.84342025 1.01133205]
|
| 212 |
+
```
|
| 213 |
+
|
| 214 |
+
</details></li>
|
| 215 |
+
<li><b>OpenAI (Hosted on Huggingface)</b>
|
| 216 |
+
|
| 217 |
+
Install [openai](https://github.com/openai/openai-python) using `pip install openai`.
|
| 218 |
+
|
| 219 |
+
<details>
|
| 220 |
+
<summary>Show OpenAI + Huggingface Inference code</summary>
|
| 221 |
+
|
| 222 |
+
```python
|
| 223 |
+
from openai import OpenAI
|
| 224 |
+
import numpy as np
|
| 225 |
+
from multiprocessing import Pool
|
| 226 |
+
from tqdm.auto import tqdm
|
| 227 |
+
|
| 228 |
+
client = OpenAI(
|
| 229 |
+
base_url="https://api-inference.huggingface.co/v1/",
|
| 230 |
+
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
def make_reranker_input(t, q):
|
| 234 |
+
return f"<<<Context>>>\n{t}\n\n<<<Query>>>\n{q}"
|
| 235 |
+
|
| 236 |
+
def make_reranker_inference_conversation(context, question):
|
| 237 |
+
system_message = "Given a piece of text and a query, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
|
| 238 |
+
|
| 239 |
+
return [
|
| 240 |
+
{"role": "system", "content": system_message},
|
| 241 |
+
{"role": "user", "content": make_reranker_input(context, question)},
|
| 242 |
+
]
|
| 243 |
+
|
| 244 |
+
def get_reranker_score(context_question_tuple):
|
| 245 |
+
question, context = context_question_tuple
|
| 246 |
+
|
| 247 |
+
messages = make_reranker_inference_conversation(context, question)
|
| 248 |
+
|
| 249 |
+
completion = client.chat.completions.create(
|
| 250 |
+
model="lightblue/lb-reranker-0.5B-v1.0-rev",
|
| 251 |
+
messages=messages,
|
| 252 |
+
max_tokens=1,
|
| 253 |
+
temperature=0.0,
|
| 254 |
+
logprobs=True,
|
| 255 |
+
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
|
| 259 |
+
|
| 260 |
+
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
|
| 261 |
+
|
| 262 |
+
return calculated_score
|
| 263 |
+
|
| 264 |
+
query_texts = [
|
| 265 |
+
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
|
| 266 |
+
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
|
| 267 |
+
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
|
| 268 |
+
]
|
| 269 |
+
|
| 270 |
+
with Pool(processes=16) as p: # Allows for parallel processing
|
| 271 |
+
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
|
| 272 |
+
|
| 273 |
+
print(expected_vals)
|
| 274 |
+
# [6.64866580, 1.85144404, 1.010719508]
|
| 275 |
+
```
|
| 276 |
+
|
| 277 |
+
</details></li>
|
| 278 |
+
</ul>
|
| 279 |
+
|
| 280 |
+
# License
|
| 281 |
+
|
| 282 |
+
We share this model under an Apache 2.0 license.
|
| 283 |
+
|
| 284 |
+
# Developed by
|
| 285 |
+
|
| 286 |
+
<a href="https://www.lightblue-tech.com">
|
| 287 |
+
<img src="https://www.lightblue-tech.com/wp-content/uploads/2023/08/color_%E6%A8%AA%E5%9E%8B-1536x469.png" alt="Lightblue technology logo" width="400"/>
|
| 288 |
+
</a>
|
| 289 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
This model was trained by Peter Devine ([ptrdvn](https://huggingface.co/ptrdvn)) for Lightblue
|