Views
No views yet

jina-embeddings-v2-base-en is to use Jina AI's Embedding API.jina-embeddings-v2-base-en is an English, monolingual embedding model supporting 8192 sequence length.
It is based on a BERT architecture (JinaBERT) that supports the symmetric bidirectional variant of ALiBi to allow longer sequence length.
The backbone jina-bert-v2-base-en is pretrained on the C4 dataset.
The model is further trained on Jina AI's collection of more than 400 millions of sentence pairs and hard negatives.
These pairs were obtained from various domains and were carefully selected through a thorough cleaning process.jina-embeddings-v2-small-en: 33 million parameters.jina-embeddings-v2-base-en: 137 million parameters (you are here).jina-embeddings-v2-base-zh: Chinese-English Bilingual embeddings.jina-embeddings-v2-base-de: German-English Bilingual embeddings.jina-embeddings-v2-base-es: Spanish-English Bilingual embeddings.mean poooling takes all token embeddings from model output and averaging them at sentence/paragraph level.
It has been proved to be the most effective way to produce high-quality sentence embeddings.
We offer an encode function to deal with this.encode function:1import torch
2import torch.nn.functional as F
3from transformers import AutoTokenizer, AutoModel
4
5def mean_pooling(model_output, attention_mask):
6 token_embeddings = model_output[0]
7 input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
8 return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
9
10sentences = ['How is the weather today?', 'What is the current weather like today?']
11
12tokenizer = AutoTokenizer.from_pretrained('jinaai/jina-embeddings-v2-small-en')
13model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-small-en', trust_remote_code=True)
14
15encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
16
17with torch.no_grad():
18 model_output = model(**encoded_input)
19
20embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
21embeddings = F.normalize(embeddings, p=2, dim=1)1!pip install transformers
2from transformers import AutoModel
3from numpy.linalg import norm
4
5cos_sim = lambda a,b: (a @ b.T) / (norm(a)*norm(b))
6model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-en', trust_remote_code=True) # trust_remote_code is needed to use the encode method
7embeddings = model.encode(['How is the weather today?', 'What is the current weather like today?'])
8print(cos_sim(embeddings[0], embeddings[1]))max_length parameter to the encode function:1embeddings = model.encode(
2 ['Very long ... document'],
3 max_length=2048
4)1!pip install -U sentence-transformers
2from sentence_transformers import SentenceTransformer
3from sentence_transformers.util import cos_sim
4
5model = SentenceTransformer(
6 "jinaai/jina-embeddings-v2-base-en", # switch to en/zh for English or Chinese
7 trust_remote_code=True
8)
9
10# control your input sequence length up to 8192
11model.max_seq_length = 1024
12
13embeddings = model.encode([
14 'How is the weather today?',
15 'What is the current weather like today?'
16])
17print(cos_sim(embeddings[0], embeddings[1]))In summary, to achieve the peak performance in both hit rate and MRR, the combination of OpenAI or JinaAI-Base embeddings with the CohereRerank/bge-reranker-large reranker stands out.

trust_remote_code=True flag when calling AutoModel.from_pretrained or initializing the model via the SentenceTransformer class, you will receive an error that the model weights could not be initialized.
This is caused by tranformers falling back to creating a default BERT model, instead of a jina-embedding model:Some weights of the model checkpoint at jinaai/jina-embeddings-v2-base-en 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', ...1OSError: jinaai/jina-embeddings-v2-base-en is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models'
2If 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`.@misc{günther2023jina,
title={Jina Embeddings 2: 8192-Token General-Purpose Text Embeddings for Long Documents},
author={Michael Günther and Jackmin Ong and Isabelle Mohr and Alaeddine Abdessalem and Tanguy Abel and Mohammad Kalim Akram and Susana Guzman and Georgios Mastrapas and Saba Sturua and Bo Wang and Maximilian Werk and Nan Wang and Han Xiao},
year={2023},
eprint={2310.19923},
archivePrefix={arXiv},
primaryClass={cs.CL}
}