We introduce gte-v1.5 series, upgraded gte embeddings that support the context length of up to 8192, while further enhancing model performance.
The models are built upon the transformer++ encoder backbone (BERT + RoPE + GLU).
The gte-v1.5 series achieve state-of-the-art scores on the MTEB benchmark within the same model size category and prodvide competitive on the LoCo long-context retrieval tests (refer to Evaluation).
We also present the gte-Qwen1.5-7B-instruct,
a SOTA instruction-tuned multi-lingual embedding model that ranked 2nd in MTEB and 1st in C-MTEB.
Developed by: Institute for Intelligent Computing, Alibaba Group
1# Requires transformers>=4.36.023import torch.nn.functional as F
4from transformers import AutoModel, AutoTokenizer
56input_texts =[7"what is the capital of China?",8"how to implement quick sort in python?",9"Beijing",10"sorting algorithms"11]1213model_path ='Alibaba-NLP/gte-base-en-v1.5'14tokenizer = AutoTokenizer.from_pretrained(model_path)15model = AutoModel.from_pretrained(model_path, trust_remote_code=True)1617# Tokenize the input texts18batch_dict = tokenizer(input_texts, max_length=8192, padding=True, truncation=True, return_tensors='pt')1920outputs = model(**batch_dict)21embeddings = outputs.last_hidden_state[:,0]2223# (Optionally) normalize embeddings24embeddings = F.normalize(embeddings, p=2, dim=1)25scores =(embeddings[:1] @ embeddings[1:].T)*10026print(scores.tolist())
It is recommended to install xformers and enable unpadding for acceleration, refer to enable-unpadding-and-xformers.
Use with sentence-transformers:
python
1# Requires sentence_transformers>=2.7.023from sentence_transformers import SentenceTransformer
4from sentence_transformers.util import cos_sim
56sentences =['That is a happy person','That is a very happy person']78model = SentenceTransformer('Alibaba-NLP/gte-base-en-v1.5', trust_remote_code=True)9embeddings = model.encode(sentences)10print(cos_sim(embeddings[0], embeddings[1]))
Use with transformers.js:
js
1// npm i @xenova/transformers2import{ pipeline, dot }from'@xenova/transformers';34// Create feature extraction pipeline5const extractor =awaitpipeline('feature-extraction','Alibaba-NLP/gte-base-en-v1.5',{6quantized:false,// Comment out this line to use the quantized version7});89// Generate sentence embeddings10const sentences =[11"what is the capital of China?",12"how to implement quick sort in python?",13"Beijing",14"sorting algorithms"15]16const output =awaitextractor(sentences,{normalize:true,pooling:'cls'});1718// Compute similarity scores19const[source_embeddings,...document_embeddings ]= output.tolist();20const similarities = document_embeddings.map(x=>100*dot(source_embeddings, x));21console.log(similarities);// [34.504930869007296, 64.03973265120138, 19.520042686034362]
Use with infinity:
Infinity is a MIT licensed server for OpenAI-compatible deployment.
docker run --gpus all -v $PWD/data:/app/.cache -p "7997":"7997" \
michaelf34/infinity:0.0.68 \
v2 --model-id Alibaba-NLP/gte-base-en-v1.5 --revision "4c742dc2b781e4ab062a4a77f4f7cbad4bdee970" --dtype bfloat16 --batch-size 32 --device cuda --engine torch --port 7997
Training Details
Training Data
Masked language modeling (MLM): c4-en
Weak-supervised contrastive pre-training (CPT): GTE pre-training data
Supervised contrastive fine-tuning: GTE fine-tuning data
Training Procedure
To enable the backbone model to support a context length of 8192, we adopted a multi-stage training strategy.
The model first undergoes preliminary MLM pre-training on shorter lengths.
And then, we resample the data, reducing the proportion of short texts, and continue the MLM pre-training.
If you find our paper or models helpful, please consider citing them as follows:
@misc{zhang2024mgte,
title={mGTE: Generalized Long-Context Text Representation and Reranking Models for Multilingual Text Retrieval},
author={Xin Zhang and Yanzhao Zhang and Dingkun Long and Wen Xie and Ziqi Dai and Jialong Tang and Huan Lin and Baosong Yang and Pengjun Xie and Fei Huang and Meishan Zhang and Wenjie Li and Min Zhang},
year={2024},
eprint={2407.19669},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2407.19669},
}
@misc{li2023gte,
title={Towards General Text Embeddings with Multi-stage Contrastive Learning},
author={Zehan Li and Xin Zhang and Yanzhao Zhang and Dingkun Long and Pengjun Xie and Meishan Zhang},
year={2023},
eprint={2308.03281},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2308.03281},
}