🚀 gte-multilingual-base
gte-multilingual-base 模型是 GTE(通用文本嵌入)系列模型中的最新成员,具备高性能、多语言支持等特性,可用于多语言检索等多种下游任务。
🚀 快速开始
模型信息
属性 |
详情 |
模型类型 |
gte-multilingual-base 是通用文本嵌入模型,属于 GTE 模型家族。 |
训练数据 |
未提及 |
模型大小 |
305M |
嵌入维度 |
768 |
最大输入令牌数 |
8192 |
模型特性
- 高性能:在多语言检索任务和多任务表示模型评估中,与同规模模型相比,达到了当前最优(SOTA)结果。
- 训练架构:采用仅编码器的 Transformer 架构进行训练,模型尺寸更小。与之前基于仅解码器的大语言模型架构(如 gte-qwen2-1.5b-instruct)的模型不同,该模型推理时对硬件要求更低,推理速度提高了 10 倍。
- 长上下文支持:支持长达 8192 个令牌的文本长度。
- 多语言能力:支持超过 70 种语言。
- 弹性密集嵌入:在保持下游任务有效性的同时,支持弹性输出密集表示,显著降低了存储成本并提高了执行效率。
- 稀疏向量生成:除了密集表示外,还可以生成稀疏向量。
使用建议
⚠️ 重要提示
💻 使用示例
基础用法
使用 Transformers 获取密集嵌入
import torch.nn.functional as F
from transformers import AutoModel, AutoTokenizer
input_texts = [
"what is the capital of China?",
"how to implement quick sort in python?",
"北京",
"快排算法介绍"
]
model_name_or_path = 'Alibaba-NLP/gte-multilingual-base'
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
model = AutoModel.from_pretrained(model_name_or_path, trust_remote_code=True)
batch_dict = tokenizer(input_texts, max_length=8192, padding=True, truncation=True, return_tensors='pt')
outputs = model(**batch_dict)
dimension=768
embeddings = outputs.last_hidden_state[:, 0][:dimension]
embeddings = F.normalize(embeddings, p=2, dim=1)
scores = (embeddings[:1] @ embeddings[1:].T) * 100
print(scores.tolist())
使用 sentence-transformers
from sentence_transformers import SentenceTransformer
from sentence_transformers.util import cos_sim
import numpy as np
input_texts = [
"what is the capital of China?",
"how to implement quick sort in python?",
"北京",
"快排算法介绍"
]
model_name_or_path="Alibaba-NLP/gte-multilingual-base"
model = SentenceTransformer(', trust_remote_code=True)
embeddings = model.encode(input_texts) # embeddings.shape (4, 768)
# normalized embeddings
norms = np.linalg.norm(embeddings, ord=2, axis=1, keepdims=True)
norms[norms == 0] = 1
embeddings = embeddings / norms
# sim scores
scores = (embeddings[:1] @ embeddings[1:].T)
print(scores.tolist())
# [[0.301699697971344, 0.7503870129585266, 0.32030850648880005]]
高级用法
使用自定义代码获取密集嵌入和稀疏令牌权重
from gte_embedding import GTEEmbeddidng
model_name_or_path = 'Alibaba-NLP/gte-multilingual-base'
model = GTEEmbeddidng(model_name_or_path)
query = "中国的首都在哪儿"
docs = [
"what is the capital of China?",
"how to implement quick sort in python?",
"北京",
"快排算法介绍"
]
embs = model.encode(docs, return_dense=True,return_sparse=True)
print('dense_embeddings vecs', embs['dense_embeddings'])
print('token_weights', embs['token_weights'])
pairs = [(query, doc) for doc in docs]
dense_scores = model.compute_scores(pairs, dense_weight=1.0, sparse_weight=0.0)
sparse_scores = model.compute_scores(pairs, dense_weight=0.0, sparse_weight=1.0)
hybrid_scores = model.compute_scores(pairs, dense_weight=1.0, sparse_weight=0.3)
print('dense_scores', dense_scores)
print('sparse_scores', sparse_scores)
print('hybrid_scores', hybrid_scores)
📚 详细文档
评估
我们在多个下游任务上验证了 gte-multilingual-base 模型的性能,包括在 MTEB 排行榜 上的多语言检索、跨语言检索、长文本检索和通用文本表示评估等。
检索任务
在 MIRACL 和 MLDR(多语言)、MKQA(跨语言)、BEIR 和 LoCo(英语)上的检索结果。


MTEB
在 MTEB 英语、中文、法语、波兰语上的结果。

更多详细实验结果可在 论文 中找到。
云 API 服务
除了开源的 GTE 系列模型外,GTE 系列模型还作为商业 API 服务在阿里云上提供。
- 嵌入模型:提供三种版本的文本嵌入模型:text-embedding-v1/v2/v3,其中 v3 是最新的 API 服务。
- 重排序模型:提供 gte-rerank 模型服务。
请注意,商业 API 背后的模型与开源模型并不完全相同。
📄 许可证
本项目采用 Apache-2.0 许可证。
🔧 引用
如果您发现我们的论文或模型有帮助,请考虑引用:
@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},
}