Views
No views yet

jina-reranker-v2-base-multilingual) is a transformer-based model that has been fine-tuned for text reranking task, which is a crucial component in many information retrieval systems. It is a cross-encoder model that takes a query and a document pair as input and outputs a score indicating the relevance of the document to the query. The model is trained on a large dataset of query-document pairs and is capable of reranking documents in multiple languages with high accuracy.jina-reranker-v1-base-en, the Jina Reranker v2 model has demonstrated competitiveness across a series of benchmarks targeting for text retrieval, multilingual capability, function-calling-aware and text-to-SQL-aware reranking, and code retrieval tasks.jina-reranker-v2-base-multilingual model is capable of handling long texts with a context length of up to 1024 tokens, enabling the processing of extensive inputs. To enable the model to handle long texts that exceed 1024 tokens, the model uses a sliding window approach to chunk the input text into smaller pieces and rerank each chunk separately.jina-reranker-v2-base-multilingual is to call Jina AI's Reranker API.1curl https://api.jina.ai/v1/rerank \
2 -H "Content-Type: application/json" \
3 -H "Authorization: Bearer YOUR_API_KEY" \
4 -d '{
5 "model": "jina-reranker-v2-base-multilingual",
6 "query": "Organic skincare products for sensitive skin",
7 "documents": [
8 "Organic skincare for sensitive skin with aloe vera and chamomile.",
9 "New makeup trends focus on bold colors and innovative techniques",
10 "Bio-Hautpflege für empfindliche Haut mit Aloe Vera und Kamille",
11 "Neue Make-up-Trends setzen auf kräftige Farben und innovative Techniken",
12 "Cuidado de la piel orgánico para piel sensible con aloe vera y manzanilla",
13 "Las nuevas tendencias de maquillaje se centran en colores vivos y técnicas innovadoras",
14 "针对敏感肌专门设计的天然有机护肤产品",
15 "新的化妆趋势注重鲜艳的颜色和创新的技巧",
16 "敏感肌のために特別に設計された天然有機スキンケア製品",
17 "新しいメイクのトレンドは鮮やかな色と革新的な技術に焦点を当てています"
18 ],
19 "top_n": 3
20}'transformers library to interact with the model programmatically.transformers and einops libraries:pip install transformers einops1from transformers import AutoModelForSequenceClassification
2
3model = AutoModelForSequenceClassification.from_pretrained(
4 'jinaai/jina-reranker-v2-base-multilingual',
5 torch_dtype="auto",
6 trust_remote_code=True,
7)
8
9model.to('cuda') # or 'cpu' if no GPU is available
10model.eval()
11
12# Example query and documents
13query = "Organic skincare products for sensitive skin"
14documents = [
15 "Organic skincare for sensitive skin with aloe vera and chamomile.",
16 "New makeup trends focus on bold colors and innovative techniques",
17 "Bio-Hautpflege für empfindliche Haut mit Aloe Vera und Kamille",
18 "Neue Make-up-Trends setzen auf kräftige Farben und innovative Techniken",
19 "Cuidado de la piel orgánico para piel sensible con aloe vera y manzanilla",
20 "Las nuevas tendencias de maquillaje se centran en colores vivos y técnicas innovadoras",
21 "针对敏感肌专门设计的天然有机护肤产品",
22 "新的化妆趋势注重鲜艳的颜色和创新的技巧",
23 "敏感肌のために特別に設計された天然有機スキンケア製品",
24 "新しいメイクのトレンドは鮮やかな色と革新的な技術に焦点を当てています",
25]
26
27# construct sentence pairs
28sentence_pairs = [[query, doc] for doc in documents]
29
30scores = model.compute_score(sentence_pairs, max_length=1024)1[0.8311430811882019, 0.09401018172502518,
2 0.6334102749824524, 0.08269733935594559,
3 0.7620701193809509, 0.09947021305561066,
4 0.9263036847114563, 0.05834583938121796,
5 0.8418256044387817, 0.11124119907617569]jina-reranker-v2-base-multilingual model uses flash attention, which requires certain types of GPU hardware to run.
If you encounter any issues, you can try call AutoModelForSequenceClassification.from_pretrained() with use_flash_attn=False.
This will use the standard attention mechanism instead of flash attention.1pip install ninja # required for flash attention
2pip install flash-attn --no-build-isolationtransformers.js library to run the model directly in JavaScript (in-browser, Node.js, Deno, etc.)!npm i xenova/transformers.js#v31import { AutoTokenizer, XLMRobertaModel } from '@xenova/transformers';
2
3const model_id = 'jinaai/jina-reranker-v2-base-multilingual';
4const model = await XLMRobertaModel.from_pretrained(model_id, { dtype: 'fp32' });
5const tokenizer = await AutoTokenizer.from_pretrained(model_id);
6
7/**
8 * Performs ranking with the CrossEncoder on the given query and documents. Returns a sorted list with the document indices and scores.
9 * @param {string} query A single query
10 * @param {string[]} documents A list of documents
11 * @param {Object} options Options for ranking
12 * @param {number} [options.top_k=undefined] Return the top-k documents. If undefined, all documents are returned.
13 * @param {number} [options.return_documents=false] If true, also returns the documents. If false, only returns the indices and scores.
14 */
15async function rank(query, documents, {
16 top_k = undefined,
17 return_documents = false,
18} = {}) {
19 const inputs = tokenizer(
20 new Array(documents.length).fill(query),
21 { text_pair: documents, padding: true, truncation: true }
22 )
23 const { logits } = await model(inputs);
24 return logits.sigmoid().tolist()
25 .map(([score], i) => ({
26 corpus_id: i,
27 score,
28 ...(return_documents ? { text: documents[i] } : {})
29 })).sort((a, b) => b.score - a.score).slice(0, top_k);
30}
31
32// Example usage:
33const query = "Organic skincare products for sensitive skin"
34const documents = [
35 "Organic skincare for sensitive skin with aloe vera and chamomile.",
36 "New makeup trends focus on bold colors and innovative techniques",
37 "Bio-Hautpflege für empfindliche Haut mit Aloe Vera und Kamille",
38 "Neue Make-up-Trends setzen auf kräftige Farben und innovative Techniken",
39 "Cuidado de la piel orgánico para piel sensible con aloe vera y manzanilla",
40 "Las nuevas tendencias de maquillaje se centran en colores vivos y técnicas innovadoras",
41 "针对敏感肌专门设计的天然有机护肤产品",
42 "新的化妆趋势注重鲜艳的颜色和创新的技巧",
43 "敏感肌のために特別に設計された天然有機スキンケア製品",
44 "新しいメイクのトレンドは鮮やかな色と革新的な技術に焦点を当てています",
45]
46
47const results = await rank(query, documents, { return_documents: true, top_k: 3 });
48console.log(results);jina-reranker-v2-base-multilingual model in your projects.compute_score() function, the jina-reranker-v2-base-multilingual model also provides a model.rerank() function that can be used to rerank documents based on a query. You can use it as follows:1result = model.rerank(
2 query,
3 documents,
4 max_query_length=512,
5 max_length=1024,
6 top_n=3
7)result object, you will find the reranked documents along with their scores. You can use this information to further process the documents as needed.rerank() function will automatically chunk the input documents into smaller pieces if they exceed the model's maximum input length. This allows you to rerank long documents without running into memory issues.
Specifically, the rerank() function will split the documents into chunks of size max_length and rerank each chunk separately. The scores from all the chunks are then combined to produce the final reranking results. You can control the query length and document length in each chunk by setting the max_query_length and max_length parameters. The rerank() function also supports the overlap parameter (default is 80) which determines how much overlap there is between adjacent chunks. This can be useful when reranking long documents to ensure that the model has enough context to make accurate predictions.jina-reranker-v2-base-multilingual has been integrated with CrossEncoder from the sentence-transformers library.sentence-transformers libraries:pip install sentence-transformersCrossEncoder class supports a predict method to get query-document relevance scores, and a rank method to rank all documents given your query.1from sentence_transformers import CrossEncoder
2
3model = CrossEncoder(
4 "jinaai/jina-reranker-v2-base-multilingual",
5 automodel_args={"torch_dtype": "auto"},
6 trust_remote_code=True,
7)
8
9# Example query and documents
10query = "Organic skincare products for sensitive skin"
11documents = [
12 "Organic skincare for sensitive skin with aloe vera and chamomile.",
13 "New makeup trends focus on bold colors and innovative techniques",
14 "Bio-Hautpflege für empfindliche Haut mit Aloe Vera und Kamille",
15 "Neue Make-up-Trends setzen auf kräftige Farben und innovative Techniken",
16 "Cuidado de la piel orgánico para piel sensible con aloe vera y manzanilla",
17 "Las nuevas tendencias de maquillaje se centran en colores vivos y técnicas innovadoras",
18 "针对敏感肌专门设计的天然有机护肤产品",
19 "新的化妆趋势注重鲜艳的颜色和创新的技巧",
20 "敏感肌のために特別に設計された天然有機スキンケア製品",
21 "新しいメイクのトレンドは鮮やかな色と革新的な技術に焦点を当てています",
22]
23
24# construct sentence pairs
25sentence_pairs = [[query, doc] for doc in documents]
26
27scores = model.predict(sentence_pairs, convert_to_tensor=True).tolist()
28"""
29[0.828125, 0.0927734375, 0.6328125, 0.08251953125, 0.76171875, 0.099609375, 0.92578125, 0.058349609375, 0.84375, 0.111328125]
30"""
31
32rankings = model.rank(query, documents, return_documents=True, convert_to_tensor=True)
33print(f"Query: {query}")
34for ranking in rankings:
35 print(f"ID: {ranking['corpus_id']}, Score: {ranking['score']:.4f}, Text: {ranking['text']}")
36"""
37Query: Organic skincare products for sensitive skin
38ID: 6, Score: 0.9258, Text: 针对敏感肌专门设计的天然有机护肤产品
39ID: 8, Score: 0.8438, Text: 敏感肌のために特別に設計された天然有機スキンケア製品
40ID: 0, Score: 0.8281, Text: Organic skincare for sensitive skin with aloe vera and chamomile.
41ID: 4, Score: 0.7617, Text: Cuidado de la piel orgánico para piel sensible con aloe vera y manzanilla
42ID: 2, Score: 0.6328, Text: Bio-Hautpflege für empfindliche Haut mit Aloe Vera und Kamille
43ID: 9, Score: 0.1113, Text: 新しいメイクのトレンドは鮮やかな色と革新的な技術に焦点を当てています
44ID: 5, Score: 0.0996, Text: Las nuevas tendencias de maquillaje se centran en colores vivos y técnicas innovadoras
45ID: 1, Score: 0.0928, Text: New makeup trends focus on bold colors and innovative techniques
46ID: 3, Score: 0.0825, Text: Neue Make-up-Trends setzen auf kräftige Farben und innovative Techniken
47ID: 7, Score: 0.0583, Text: 新的化妆趋势注重鲜艳的颜色和创新的技巧
48"""| Model Name | Model Size | MKQA(nDCG@10, 26 langs) | BEIR(nDCG@10, 17 datasets) | MLDR(recall@10, 13 langs) | CodeSearchNet (MRR@10, 3 tasks) | AirBench (nDCG@10, zh/en) | ToolBench (recall@3, 3 tasks) | TableSearch (recall@3) |
|---|---|---|---|---|---|---|---|---|
| jina-reranker-v2-multilingual | 278M | 54.83 | 53.17 | 68.95 | 71.36 | 61.33 | 77.75 | 93.31 |
| bge-reranker-v2-m3 | 568M | 54.17 | 53.65 | 59.73 | 62.86 | 61.28 | 78.46 | 74.86 |
| mmarco-mMiniLMv2-L12-H384-v1 | 118M | 53.37 | 45.40 | 28.91 | 51.78 | 56.46 | 58.39 | 53.60 |
| jina-reranker-v1-base-en | 137M | - | 52.45 | - | - | - | 74.13 | 72.89 |