🚀 jina-embeddings-v2-base-de
jina-embeddings-v2-base-de
是一款由 Jina AI 训练的德英双语文本嵌入模型,支持长达 8192 的序列长度。该模型在单语言和跨语言应用中表现出色,能无偏处理德英混合输入。
🚀 快速开始
使用 jina-embeddings-v2-base-de
最简单的方法是使用 Jina AI 的 Embedding API。
✨ 主要特性
- 双语支持:支持德语和英语两种语言,能无偏处理德英混合输入。
- 长序列处理:支持长达 8192 的序列长度。
- 高性能:基于 BERT 架构(JinaBERT),采用对称双向的 ALiBi 变体,在单语言和跨语言应用中表现出色。
此外,还提供以下嵌入模型:
📦 安装指南
使用前需安装 transformers
库:
!pip install transformers
若使用 sentence-transformers
,需安装并更新:
!pip install -U sentence-transformers
💻 使用示例
基础用法
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModel
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0]
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
sentences = ['How is the weather today?', 'What is the current weather like today?']
tokenizer = AutoTokenizer.from_pretrained('jinaai/jina-embeddings-v2-base-de')
model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-de', trust_remote_code=True, torch_dtype=torch.bfloat16)
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
model_output = model(**encoded_input)
embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
embeddings = F.normalize(embeddings, p=2, dim=1)
高级用法
import torch
from transformers import AutoModel
from numpy.linalg import norm
cos_sim = lambda a,b: (a @ b.T) / (norm(a)*norm(b))
model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-de', trust_remote_code=True, torch_dtype=torch.bfloat16)
embeddings = model.encode(['How is the weather today?', 'Wie ist das Wetter heute?'])
print(cos_sim(embeddings[0], embeddings[1]))
处理短序列
embeddings = model.encode(
['Very long ... document'],
max_length=2048
)
使用 sentence-transformers
!pip install -U sentence-transformers
from sentence_transformers import SentenceTransformer
from sentence_transformers.util import cos_sim
model = SentenceTransformer(
"jinaai/jina-embeddings-v2-base-de",
trust_remote_code=True
)
model.max_seq_length = 1024
embeddings = model.encode([
'How is the weather today?',
'Wie ist das Wetter heute?'
])
print(cos_sim(embeddings[0], embeddings[1]))
📚 详细文档
数据与参数
数据和训练细节详见此 技术报告。
替代使用方式
- 托管 SaaS:在 Jina AI 的 Embedding API 上获取免费密钥开始使用。
- 私有高性能部署:从模型套件中选择模型,并在 AWS Sagemaker 上进行部署。
基准测试结果
在 MTEB 基准测试 上对双语模型进行了所有可用的德语和英语评估任务的评估。此外,还在额外的德语评估任务中与其他几个德语、英语和多语言模型进行了对比评估:

在 RAG 中使用 Jina Embeddings
根据 LLamaIndex 最新博客文章:
综上所述,为了在命中率和 MRR 方面达到最佳性能,将 OpenAI 或 JinaAI-Base 嵌入与 CohereRerank/bge-reranker-large 重排器结合使用效果最佳。

🔧 技术细节
为什么使用平均池化?
mean pooling
会获取模型输出的所有词元嵌入,并在句子/段落级别对其进行平均。实践证明,这是生成高质量句子嵌入最有效的方法。提供了一个 encode
函数来处理此操作。
若不使用默认的 encode
函数,可参考以下代码:
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModel
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0]
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
sentences = ['How is the weather today?', 'What is the current weather like today?']
tokenizer = AutoTokenizer.from_pretrained('jinaai/jina-embeddings-v2-base-de')
model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-de', trust_remote_code=True, torch_dtype=torch.bfloat16)
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
model_output = model(**encoded_input)
embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
embeddings = F.normalize(embeddings, p=2, dim=1)
📄 许可证
本项目采用 Apache 2.0 许可证。
联系我们
加入我们的 Discord 社区,与其他社区成员交流想法。
引用
如果您在研究中发现 Jina Embeddings 很有用,请引用以下论文:
@article{mohr2024multi,
title={Multi-Task Contrastive Learning for 8192-Token Bilingual Text Embeddings},
author={Mohr, Isabelle and Krimmel, Markus and Sturua, Saba and Akram, Mohammad Kalim and Koukounas, Andreas and G{\"u}nther, Michael and Mastrapas, Georgios and Ravishankar, Vinit and Mart{\'\i}nez, Joan Fontanals and Wang, Feng and others},
journal={arXiv preprint arXiv:2402.17016},
year={2024}
}