ONNX 模型库
返回模型

说明文档

<!-- TODO: 此处将添加评估结果 --> <br><br>

<p align="center"> <img src="https://huggingface.co/datasets/jinaai/documentation-images/resolve/main/logo.webp" alt="Jina AI: Your Search Foundation, Supercharged!" width="150px"> </p>

<p align="center"> <b>由 <a href="https://jina.ai/"><b>Jina AI</b></a> 训练的文本向量模型。</b> </p>

快速入门

使用 jina-embeddings-v2-base-zh 最简单的方式是使用 Jina AI 的 Embedding API

预期用途与模型信息

jina-embeddings-v2-base-zh 是一个中英双语文本向量模型,支持8192序列长度。 它基于BERT架构(JinaBERT),该架构支持ALiBi的对称双向变体,以支持更长的序列长度。 我们设计它在单语言和跨语言应用中实现高性能,并专门训练它以支持混合中英文输入,且不存在偏见。 此外,我们还提供以下向量模型:

数据与参数

数据和训练细节详见此技术报告

使用方法

<details><summary>集成模型时请使用 mean pooling(平均池化)。</summary> <p>

为什么要用 mean pooling?

mean pooling 会从模型输出中获取所有token向量,然后在句子/段落级别对它们进行平均。 已被证明是生成高质量句子向量的最有效方法。 我们提供了一个 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?', '今天天气怎么样?']

tokenizer = AutoTokenizer.from_pretrained('jinaai/jina-embeddings-v2-base-zh')
model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-zh', 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)

</p> </details>

您可以直接从transformers包使用Jina Embedding模型。

!pip install transformers
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-zh', trust_remote_code=True, torch_dtype=torch.bfloat16)
embeddings = model.encode(['How is the weather today?', '今天天气怎么样?'])
print(cos_sim(embeddings[0], embeddings[1]))

如果您只需要处理较短的序列,例如2k,可以将 max_length 参数传递给 encode 函数:

embeddings = model.encode(
    ['Very long ... document'],
    max_length=2048
)

如果您想将该模型与 sentence-transformers包 一起使用,请确保已安装最新版本并设置 trust_remote_code=True

!pip install -U sentence-transformers
from sentence_transformers import SentenceTransformer
from numpy.linalg import norm

cos_sim = lambda a,b: (a @ b.T) / (norm(a)*norm(b))
model = SentenceTransformer('jinaai/jina-embeddings-v2-base-zh', trust_remote_code=True)
embeddings = model.encode(['How is the weather today?', '今天天气怎么样?'])
print(cos_sim(embeddings[0], embeddings[1]))

使用最新版本(v2.3.0)的sentence-transformers也支持Jina embeddings(请确保您已登录huggingface):

!pip install -U sentence-transformers
from sentence_transformers import SentenceTransformer
from sentence_transformers.util import cos_sim

model = SentenceTransformer(
    "jinaai/jina-embeddings-v2-base-zh", # 切换为 en/zh 用于英文或中文
    trust_remote_code=True
)

# 控制输入序列长度,最大8192
model.max_seq_length = 1024

embeddings = model.encode([
    'How is the weather today?',
    '今天天气怎么样?'
])
print(cos_sim(embeddings[0], embeddings[1]))

使用Transformers包的替代方案

  1. 托管SaaS:在Jina AI的Embedding API上使用免费密钥开始使用。
  2. 私有高性能部署:从我们的模型套件中选择并部署在AWS Sagemaker上。

使用Jina Embeddings进行RAG

根据LLamaIndex的最新博客文章:

总之,为了在命中率和MRR方面达到最佳性能,OpenAI或JinaAI-Base向量模型与CohereRerank/bge-reranker-large重排序器的组合表现突出。

<img src="https://miro.medium.com/v2/resize:fit:4800/format:webp/1*ZP2RVejCZovF3FDCg-Bx3A.png" width="780px">

问题排查

加载模型代码失败

如果在调用 AutoModel.from_pretrained 或通过 SentenceTransformer 类初始化模型时忘记传递 trust_remote_code=True 标志,您将收到错误提示,说模型权重无法初始化。 这是因为transformers回退到创建默认的BERT模型,而不是jina-embedding模型:

Some weights of the model checkpoint at jinaai/jina-embeddings-v2-base-zh were not used when initializing BertModel: ['encoder.layer.2.mlp.layernorm.weight', 'encoder.layer.3.mlp.layernorm.weight', 'encoder.layer.10.mlp.wo.bias', 'encoder.layer.5.mlp.wo.bias', 'encoder.layer.2.mlp.layernorm.bias', 'encoder.layer.1.mlp.gated_layers.weight', 'encoder.layer.5.mlp.gated_layers.weight', 'encoder.layer.8.mlp.layernorm.bias', ...

用户未登录Huggingface

该模型仅在gated访问下可用。 这意味着您需要登录huggingface才能加载它。 如果收到以下错误,您需要提供访问令牌,可以使用huggingface-cli或通过上述环境变量提供令牌:

OSError: jinaai/jina-embeddings-v2-base-zh is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models'
If this is a private repository, make sure to pass a token having permission to this repo with `use_auth_token` or log in with `huggingface-cli login` and pass `use_auth_token=True`.

联系方式

加入我们的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}
}

jinaai/jina-embeddings-v2-base-zh

作者 jinaai

feature-extraction sentence-transformers
↓ 39.1K ♥ 246

创建时间: 2024-01-10 03:39:40+00:00

更新时间: 2025-01-06 16:25:19+00:00

在 Hugging Face 上查看

文件 (17)

.gitattributes
1_Pooling/config.json
README.md
config.json
config_sentence_transformers.json
merges.txt
model.safetensors
modules.json
onnx/model.onnx ONNX
onnx/model_fp16.onnx ONNX
onnx/model_quantized.onnx ONNX
pytorch_model.bin
sentence_bert_config.json
special_tokens_map.json
tokenizer.json
tokenizer_config.json
vocab.json