模型库 / Qwen/Qwen3-Embedding-4B

Qwen3-Embedding-4B

Qwen feature-extraction sentence-transformers
Qwen/Qwen3-Embedding-4B
2,076,631
下载量
262
收藏数
15
浏览量
apache-2.0
许可

简介

The Qwen3 Embedding model series is the latest proprietary model of the Qwen family, specifically designed for text embedding and ranking tasks. Building upon the dense foundational models of the Qwen3 series, it provides a comprehensive range of text embeddings and reranking models in various sizes (0.6B, 4B, and 8B). This series inherits the exceptional multilingual capabilities, long-text understanding, and reasoning skills of its foundational model. The Qwen3 Embedding series represents sign

模型卡片

许可协议 apache-2.0
transformers sentence-transformers sentence-similarity feature-extraction text-embeddings-inference

模型配置

模型类型 qwen3
架构 Qwen3ForCausalLM

模型详情

已翻译

Qwen3-Embedding-4B

亮点

Qwen3 Embedding 模型系列是 Qwen 家族最新的专有模型,专为文本 embedding 和排序任务设计。该系列基于 Qwen3 系列的稠密基础模型构建,提供多种尺寸(0.6B、4B 和 8B)的全面文本 embedding 和重排序模型。该系列继承了基础模型卓越的多语言能力、长文本理解能力和推理能力。Qwen3 Embedding 系列在多项文本 embedding 和排序任务上取得了显著进步,包括文本检索、代码检索、文本分类、文本聚类和双语文本挖掘。

卓越的多功能性:该 embedding 模型在广泛的下游应用评估中达到了业界领先水平。8B 尺寸的 embedding 模型在 MTEB 多语言排行榜中排名第一(截至 2025 年 6 月 5 日,得分 70.58),而重排序模型在各种文本检索场景中表现优异。

全面的灵活性:Qwen3 Embedding 系列提供从 0.6B 到 8B 的全尺寸 embedding 和重排序模型,满足注重效率与效果的不同应用场景。开发者可以无缝组合这两个模块。此外,embedding 模型支持在所有维度上灵活定义向量,且 embedding 和重排序模型均支持用户自定义指令,以提升特定任务、语言或场景的性能。

多语言能力:得益于 Qwen3 模型的多语言能力,Qwen3 Embedding 系列支持超过 100 种语言。这包括多种编程语言,并提供强大的多语言、跨语言和代码检索能力。

模型概述

Qwen3-Embedding-4B 具有以下特点:

  • 模型类型:文本 Embedding
  • 支持语言:100+ 种语言
  • 参数量:4B
  • 上下文长度:32k
  • Embedding 维度:最高 2560,支持用户自定义输出维度,范围从 32 到 2560

更多详情,包括基准评估、硬件要求和推理性能,请参考我们的 博客GitHub

Qwen3 Embedding 系列模型列表

模型类型 模型 参数量 层数 序列长度 Embedding 维度 支持 MRL 支持指令感知
文本 Embedding Qwen3-Embedding-0.6B 0.6B 28 32K 1024
文本 Embedding Qwen3-Embedding-4B 4B 36 32K 2560
文本 Embedding Qwen3-Embedding-8B 8B 36 32K 4096
文本重排序 Qwen3-Reranker-0.6B 0.6B 28 32K - -
文本重排序 Qwen3-Reranker-4B 4B 36 32K - -
文本重排序 Qwen3-Reranker-8B 8B 36 32K - -

注意
- 支持 MRL 表示 embedding 模型是否支持最终 embedding 的自定义维度。
- 支持指令感知 表示 embedding 或重排序模型是否支持根据不同任务自定义输入指令。
- 我们的评估表明,对于大多数下游任务,使用指令(instruct)相比不使用通常能带来 1% 到 5% 的性能提升。因此,我们建议开发者针对自己的任务和场景定制专属指令。在多语言场景下,我们也建议用户使用英文编写指令,因为模型训练过程中使用的大部分指令最初都是英文编写的。

使用方法

使用早于 4.51.0 版本的 Transformers 时,可能会遇到以下错误:

KeyError: 'qwen3'

Sentence Transformers 使用方法

# Requires transformers>=4.51.0
# Requires sentence-transformers>=2.7.0

from sentence_transformers import SentenceTransformer

# Load the model
model = SentenceTransformer("Qwen/Qwen3-Embedding-4B")

# We recommend enabling flash_attention_2 for better acceleration and memory saving,
# together with setting `padding_side` to "left":
# model = SentenceTransformer(
#     "Qwen/Qwen3-Embedding-4B",
#     model_kwargs={"attn_implementation": "flash_attention_2", "device_map": "auto"},
#     tokenizer_kwargs={"padding_side": "left"},
# )

# The queries and documents to embed
queries = [
    "What is the capital of China?",
    "Explain gravity",
]
documents = [
    "The capital of China is Beijing.",
    "Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun.",
]

# Encode the queries and documents. Note that queries benefit from using a prompt
# Here we use the prompt called "query" stored under `model.prompts`, but you can
# also pass your own prompt via the `prompt` argument
query_embeddings = model.encode(queries, prompt_name="query")
document_embeddings = model.encode(documents)

# Compute the (cosine) similarity between the query and document embeddings
similarity = model.similarity(query_embeddings, document_embeddings)
print(similarity)
# tensor([[0.7534, 0.1147],
#         [0.0320, 0.6258]])

Transformers 使用方法

# Requires transformers>=4.51.0
import torch
import torch.nn.functional as F

from torch import Tensor
from transformers import AutoTokenizer, AutoModel

def last_token_pool(last_hidden_states: Tensor,
                 attention_mask: Tensor) -> Tensor:
    left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
    if left_padding:
        return last_hidden_states[:, -1]
    else:
        sequence_lengths = attention_mask.sum(dim=1) - 1
        batch_size = last_hidden_states.shape[0]
        return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]

def get_detailed_instruct(task_description: str, query: str) -> str:
    return f'Instruct: {task_description}\nQuery:{query}'

# Each query must come with a one-sentence instruction that describes the task
task = 'Given a web search query, retrieve relevant passages that answer the query'

queries = [
    get_detailed_instruct(task, 'What is the capital of China?'),
    get_detailed_instruct(task, 'Explain gravity')
]
# No need to add instruction for retrieval documents
documents = [
    "The capital of China is Beijing.",
    "Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun."
]
input_texts = queries + documents

tokenizer = AutoTokenizer.from_pretrained('Qwen/Qwen3-Embedding-4B', padding_side='left')
model = AutoModel.from_pretrained('Qwen/Qwen3-Embedding-4B')

# We recommend enabling flash_attention_2 for better acceleration and memory saving.
# model = AutoModel.from_pretrained('Qwen/Qwen3-Embedding-4B', attn_implementation="flash_attention_2", torch_dtype=torch.float16).cuda()

max_length = 8192

# Tokenize the input texts
batch_dict = tokenizer(
    input_texts,
    padding=True,
    truncation=True,
    max_length=max_length,
    return_tensors="pt",
)
batch_dict.to(model.device)
outputs = model(**batch_dict)
embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])

# normalize embeddings
embeddings = F.normalize(embeddings, p=2, dim=1)
scores = (embeddings[:2] @ embeddings[2:].T)
print(scores.tolist())
# [[0.7534257769584656, 0.1146894246339798], [0.03198453038930893, 0.6258305311203003]]

vLLM 使用方法

# Requires vllm>=0.8.5
import torch
import vllm
from vllm import LLM

def get_detailed_instruct(task_description: str, query: str) -> str:
    return f'Instruct: {task_description}\nQuery:{query}'

# Each query must come with a one-sentence instruction that describes the task
task = 'Given a web search query, retrieve relevant passages that answer the query'

queries = [
    get_detailed_instruct(task, 'What is the capital of China?'),
    get_detailed_instruct(task, 'Explain gravity')
]
# No need to add instruction for retrieval documents
documents = [
    "The capital of China is Beijing.",
    "Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun."
]
input_texts = queries + documents

model = LLM(model="Qwen/Qwen3-Embedding-4B", task="embed")

outputs = model.embed(input_texts)
embeddings = torch.tensor([o.outputs.embedding for o in outputs])
scores = (embeddings[:2] @ embeddings[2:].T)
print(scores.tolist())
# [[0.7525103688240051, 0.1143278032541275], [0.030893627554178238, 0.6239761114120483]]

📌 提示:我们建议开发者根据具体场景、任务和语言自定义 instruct。我们的测试表明,

标签

qwen3 text-generation sentence-similarity text-embeddings-inference arxiv:2506.05176 base_model:Qwen/Qwen3-4B-Base base_model:finetune:Qwen/Qwen3-4B-Base license:apache-2.0

操作


详细信息

厂商
Qwen
任务
feature-extraction
框架
sentence-transformers
模型类型
qwen3
许可(HF)
apache-2.0