由 Jina AI 训练的文本嵌入模型。
开始使用 jina-embeddings-v2-base-es 最简单的方法是使用 Jina AI 的 嵌入 API。
jina-embeddings-v2-base-es 是一个支持 8192 序列长度的西班牙语/英语双语文本嵌入模型。
它基于 BERT 架构(JinaBERT),该架构支持 ALiBi 的对称双向变体,以实现更长的序列长度。
我们将其设计为在单语和跨语言应用中具有高性能,并对其进行专门训练以支持西班牙语-英语混合输入,且不带偏向性。
此外,我们还提供以下嵌入模型:
jina-embeddings-v2-base-es 是一个支持 8192 序列长度的英语/西班牙语双语文本嵌入模型。
它基于 BERT 架构(JinaBERT),该架构集成了 ALiBi 的对称双向变体,以支持更长的序列长度。
我们设计此模型旨在单语和双语应用中实现高性能,并且经过专门训练以支持西班牙语和英语的混合输入,且不带偏向性。
此外,我们提供以下嵌入模型:
jina-embeddings-v2-base-es: 西班牙语-英语双语嵌入 (当前模型)。数据和训练详情在本技术报告中描述。
均值池化 从模型输出中获取所有标记嵌入,并在句子/段落级别对它们进行平均。
事实证明,这是生成高质量句子嵌入的最有效方法。
我们提供了一个 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-es')
model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-es', trust_remote_code=True)
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)您可以直接通过 transformers 包使用 Jina Embedding 模型:
!pip install transformers
# coding = utf-8
import os
import torch
import torch_npu
from sentence_transformers import SentenceTransformer
from sentence_transformers.util import cos_sim
import argparse
from openmind import pipeline, is_torch_npu_available
parser = argparse.ArgumentParser(description='manual to this script')
parser.add_argument("--model_name_or_path", type=str, default="./")
args = parser.parse_args()
model_path = args.model_name_or_path
device = None
if is_torch_npu_available():
device = "npu:0"
else:
device = "cpu"
model = SentenceTransformer(model_path)
model = model.to(device)
embeddings = model.encode(['How is the weather today?', '¿Qué tiempo hace hoy?'])
cosine_scores = cos_sim(embeddings[0], embeddings[1])
print(f"cosine_scores: {cosine_scores}")根据 LLamaIndex 最新的博客文章:
总之,要在命中率和 MRR 上实现最佳性能,OpenAI 或 JinaAI-Base 嵌入模型与 CohereRerank/bge-reranker-large 重排序器的组合表现突出。
加入我们的 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}
}