jina-reranker-v1-tiny-en项目介绍
项目概述
jina-reranker-v1-tiny-en
是一个专为极快的重新排序而设计的模型,同时保持具有竞争力的性能。这个模型使用JinaBERT作为其基础。JinaBERT
是BERT架构的一种独特变体,支持ALiBi的对称双向变体。这样的设计允许jina-reranker-v1-tiny-en
处理比其他重新排序模型更长的文本序列,最多可达8,192个标记(tokens)。
为了实现速度的提升,jina-reranker-v1-tiny-en
使用了一种称为知识蒸馏的技术。这种技术通过让一个复杂但较慢的模型(如原始的jina-reranker-v1-base-en)充当教师,把知识凝聚到一个较小但更快的学生模型中。这个学生模型保留了大部分教师模型的知识,使其能在极短时间内提供相似的准确性。
模型对比
以下是此项目提供的重新排序模型的对比:
模型名称 | 层数 | 隐藏尺寸 | 参数量(百万) |
---|---|---|---|
jina-reranker-v1-base-en | 12 | 768 | 137.0 |
jina-reranker-v1-turbo-en | 6 | 384 | 37.8 |
jina-reranker-v1-tiny-en | 4 | 384 | 33.0 |
从中可以看出,jina-reranker-v1-turbo-en
采用了6层和37.8百万参数,为快速搜索和重新排序提供了平衡方案。而jina-reranker-v1-tiny-en
则进一步强化了速度,凭借其4层和33.0百万参数结构,达到了最快的推理速度,适合于对绝对最高的准确性要求较低的场景。
使用方法
- 通过Jina AI的Reranker API是最简单的使用方式。
curl https://api.jina.ai/v1/rerank \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "jina-reranker-v1-tiny-en",
"query": "Organic skincare products for sensitive skin",
"documents": [
"Eco-friendly kitchenware for modern homes",
"Biodegradable cleaning supplies for eco-conscious consumers",
"Organic cotton baby clothes for sensitive skin",
"Natural organic skincare range for sensitive skin",
"Tech gadgets for smart homes: 2024 edition",
"Sustainable gardening tools and compost solutions",
"Sensitive skin-friendly facial cleansers and toners",
"Organic food wraps and storage solutions",
"All-natural pet food for dogs with allergies",
"Yoga mats made from recycled materials"
],
"top_n": 3
}'
- 或者,可以使用最新版的
sentence-transformers>=0.27.0
库,通过pip安装:
pip install -U sentence-transformers
然后使用以下代码与模型交互:
from sentence_transformers import CrossEncoder
model = CrossEncoder("jinaai/jina-reranker-v1-tiny-en", trust_remote_code=True)
query = "Organic skincare products for sensitive skin"
documents = [
"Eco-friendly kitchenware for modern homes",
"Biodegradable cleaning supplies for eco-conscious consumers",
"Organic cotton baby clothes for sensitive skin",
"Natural organic skincare range for sensitive skin",
"Tech gadgets for smart homes: 2024 edition",
"Sustainable gardening tools and compost solutions",
"Sensitive skin-friendly facial cleansers and toners",
"Organic food wraps and storage solutions",
"All-natural pet food for dogs with allergies",
"Yoga mats made from recycled materials"
]
results = model.rank(query, documents, return_documents=True, top_k=3)
- 也可以使用
transformers
库通过编程方式与模型交互。
!pip install transformers
from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained(
'jinaai/jina-reranker-v1-tiny-en', num_labels=1, trust_remote_code=True
)
query = "Organic skincare products for sensitive skin"
documents = [
"Eco-friendly kitchenware for modern homes",
"Biodegradable cleaning supplies for eco-conscious consumers",
"Organic cotton baby clothes for sensitive skin",
"Natural organic skincare range for sensitive skin",
"Tech gadgets for smart homes: 2024 edition",
"Sustainable gardening tools and compost solutions",
"Sensitive skin-friendly facial cleansers and toners",
"Organic food wraps and storage solutions",
"All-natural pet food for dogs with allergies",
"Yoga mats made from recycled materials"
]
sentence_pairs = [[query, doc] for doc in documents]
scores = model.compute_score(sentence_pairs)
- 还可以使用
transformers.js
库直接在JavaScript(浏览器、Node.js、Deno等)中运行模型。
通过NPM安装Transformers.js JavaScript库:
npm i @xenova/transformers
然后可以使用以下代码与模型互动:
import { AutoTokenizer, AutoModelForSequenceClassification } from '@xenova/transformers';
const model_id = 'jinaai/jina-reranker-v1-tiny-en';
const model = await AutoModelForSequenceClassification.from_pretrained(model_id, { quantized: false });
const tokenizer = await AutoTokenizer.from_pretrained(model_id);
async function rank(query, documents, {
top_k = undefined,
return_documents = false,
} = {}) {
const inputs = tokenizer(
new Array(documents.length).fill(query),
{ text_pair: documents, padding: true, truncation: true }
)
const { logits } = await model(inputs);
return logits.sigmoid().tolist()
.map(([score], i) => ({
corpus_id: i,
score,
...(return_documents ? { text: documents[i] } : {})
})).sort((a, b) => b.score - a.score).slice(0, top_k);
}
const query = "Organic skincare products for sensitive skin"
const documents = [
"Eco-friendly kitchenware for modern homes",
"Biodegradable cleaning supplies for eco-conscious consumers",
"Organic cotton baby clothes for sensitive skin",
"Natural organic skincare range for sensitive skin",
"Tech gadgets for smart homes: 2024 edition",
"Sustainable gardening tools and compost solutions",
"Sensitive skin-friendly facial cleansers and toners",
"Organic food wraps and storage solutions",
"All-natural pet food for dogs with allergies",
"Yoga mats made from recycled materials",
]
const results = await rank(query, documents, { return_documents: true, top_k: 3 });
console.log(results);
性能评估
jina-reranker
在3个关键基准测试中进行评估,以确保顶级性能和搜索相关性。
模型名称 | NDCG@10 (17 BEIR数据集) | NDCG@10 (5 LoCo数据集) | 命中率 (LlamaIndex RAG) |
---|---|---|---|
jina-reranker-v1-base-en | 52.45 | 87.31 | 85.53 |
jina-reranker-v1-turbo-en | 49.60 | 69.21 | 85.13 |
jina-reranker-v1-tiny-en | 48.54 | 70.29 | 85.00 |
mxbai-rerank-base-v1 | 49.19 | - | 82.50 |
mxbai-rerank-xsmall-v1 | 48.80 | - | 83.69 |
ms-marco-MiniLM-L-6-v2 | 48.64 | - | 82.63 |
ms-marco-MiniLM-L-4-v2 | 47.81 | - | 83.82 |
bge-reranker-base | 47.89 | - | 83.03 |
NDCG@10
是衡量排名质量的一种标准,分数越高表示搜索结果越好,而命中率
则衡量相关文档出现在前10个搜索结果中的百分比。对于其他模型,由于不支持超过512个标记的长文档,因此没有LoCo数据集的结果。
更多详情请参考我们的基准测试表。
联系
欢迎加入我们的Discord社区,与其他社区成员交流想法。