michaelfeil commited on
Commit
7d4ba8c
1 Parent(s): 64a2057

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +46 -0
README.md CHANGED
@@ -364,6 +364,52 @@ with torch.no_grad():
364
  print(scores)
365
  ```
366
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
  ## Evaluation
368
 
369
  `baai-general-embedding` models achieve **state-of-the-art performance on both MTEB and C-MTEB leaderboard!**
 
364
  print(scores)
365
  ```
366
 
367
+ #### Usage reranker with the ONNX files
368
+
369
+ ```python
370
+ from optimum.onnxruntime import ORTModelForSequenceClassification # type: ignore
371
+
372
+ import torch
373
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
374
+
375
+ tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-large')
376
+ model = AutoModelForSequenceClassification.from_pretrained('BAAI/bge-reranker-large')
377
+ model_ort = ORTModelForFeatureExtraction.from_pretrained('BAAI/bge-reranker-large', file_name="onnx/model.onnx")
378
+
379
+ # Sentences we want sentence embeddings for
380
+ pairs = [['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']]
381
+
382
+ # Tokenize sentences
383
+ encoded_input = tokenizer(pairs, padding=True, truncation=True, return_tensors='pt')
384
+
385
+ scores_ort = model_ort(**inputs, return_dict=True).logits.view(-1, ).float()
386
+ # Compute token embeddings
387
+ with torch.inference_mode():
388
+ scores = model_ort(**inputs, return_dict=True).logits.view(-1, ).float()
389
+
390
+ # scores and scores_ort are identical
391
+ ```
392
+ #### Usage reranker with infinity
393
+
394
+ Its also possible to deploy the onnx files with the [infinity_emb](https://github.com/michaelfeil/infinity) pip package.
395
+ ```python
396
+ import asyncio
397
+ from infinity_emb import AsyncEmbeddingEngine, EngineArgs
398
+
399
+ query='what is panda?'
400
+ docs = ['The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear', "Paris is in France."]
401
+
402
+ engine = AsyncEmbeddingEngine.from_args(
403
+ EngineArgs(model_name_or_path = "BAAI/bge-large-en-v1.5", device="cpu", engine="optimum" # or engine="torch"
404
+ ))
405
+
406
+ async def main():
407
+ async with engine:
408
+ ranking, usage = await engine.rerank(query=query, docs=docs)
409
+ print(list(zip(ranking, docs)))
410
+ asyncio.run(main())
411
+ ```
412
+
413
  ## Evaluation
414
 
415
  `baai-general-embedding` models achieve **state-of-the-art performance on both MTEB and C-MTEB leaderboard!**