游乐游手机版
首页/AI热点日报/热点详情

Qwen3-Embedding最新嵌入模型使用指南:完整教程与最佳实践

类型:热点整理2026-07-19
Qwen3-Embedding是2025年6月推出的文本嵌入与排序模型系列,覆盖0 6B、4B、8B三种参数规模,8B版本在MTEB多语言排行榜排名第一。支持超100种语言,具备多语言、长文本理解及推理能力,嵌入与重排序模块可灵活组合,并支持自定义指令。

Qwen3-Embedding系列模型在多语言文本处理领域表现极为出色,特别是8B版本,直接在MTEB排行榜上登顶第一,为开发者提供了强大的文本嵌入与重排序能力。(Qwen3-Embedding最新嵌入模型使用指南

本文将围绕以下核心内容展开:Qwen3-Embedding模型的多语言支持与性能优势、不同参数规模模型的特点与适用场景,以及具体的安装与使用指南。

概述

Qwen3-Embedding是Qwen系列于2025年6月发布的最新专有模型,专为文本嵌入与排序任务而设计。该模型基于Qwen3系列的密集基础模型,提供0.6B、4B和8B三种参数规模,兼顾效率与性能。

它继承了基础模型的多语言能力、长文本理解与推理能力。在文本检索、代码检索、文本分类、文本聚类及双文本挖掘等任务中,均展现出优异表现。

三个方面值得特别关注:

  • 卓越的多功能性:嵌入模型在广泛的应用评估中表现优异。其中8B嵌入模型在MTEB多语言排行榜上排名第一(截至2025年6月5日,得分70.58),而重排序模型在多种文本检索场景中同样表现出色。
  • 全面的灵活性:从0.6B到8B的完整尺寸覆盖,可灵活应对各类场景。嵌入模块与重排序模块可无缝组合,嵌入模型还支持在所有维度上灵活定义向量,且两个模型均支持用户自定义指令,以增强特定任务、语言或场景的性能。
  • 多语言功能:依托Qwen3模型的多语言基础,该系列支持超过100种语言,涵盖多种编程语言,在多语言、跨语言及代码检索方面能力突出。

以Qwen3-Embedding-8B为例,其具体参数如下:

  • 模型类型:文本嵌入
  • 支持的语言:100余种语言
  • 参数数量:8B
  • 上下文长度:32k
  • 嵌入维度:最大支持4096,支持32~4096自定义输出维度

模型列表

以下是官方提供的模型清单,包含嵌入和重排序两大类,覆盖不同参数规模:

模型类型 模型 参数大小 层数 序列长度 嵌入维度 MRL 支持 指令感知
文本嵌入 Qwen3-Embedding-0.6B 0.6B 28 32K 1024
文本嵌入 Qwen3-Embedding-4B 4B 36 32K 2560
文本嵌入 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 - -

安装

Ollama中安装

由于Qwen官方尚未发布模型,目前需要通过第三方发布的模型来使用。可以根据需要执行以下命令下载并运行:

# 0.6B模型  
ollama run dengcao/Qwen3-Embedding-0.6B:Q8_0
ollama run dengcao/Qwen3-Embedding-0.6B:F16

# 4B模型
ollama run dengcao/Qwen3-Embedding-4B:Q4_K_M
ollama run dengcao/Qwen3-Embedding-4B:Q5_K_M
ollama run dengcao/Qwen3-Embedding-4B:Q8_0
ollama run dengcao/Qwen3-Embedding-4B:F16

# 8B模型
ollama run dengcao/Qwen3-Embedding-8B:Q4_K_M
ollama run dengcao/Qwen3-Embedding-8B:Q5_K_M
ollama run dengcao/Qwen3-Embedding-8B:Q8_0
ollama run dengcao/Qwen3-Embedding-8B:F16

关于量化版本的说明:

  • q8_0:与浮点数16几乎无差异,资源占用高,推理速度慢,不推荐大多数用户使用。
  • q6_k:对所有张量使用Q8_K量化。
  • q5_k_m:将Q6_K用于一半的attention.wv和feed_forward.w2张量,其余使用Q5_K。
  • q5_0:原始量化方法,5位。精度更高,资源占用更高,推理速度更慢。
  • q4_k_m:将Q6_K用于一半的attention.wv和feed_forward.w2张量,其余使用Q4_K。
  • q4_0:原始量化方法,4位。
  • q3_k_m:将Q4_K用于attention.wv、attention.wo和feed_forward.w2张量,其余使用Q3_K。
  • q2_k:将Q4_K用于attention.vw和feed_forward.w2张量,其他张量使用Q2_K。

根据经验,建议使用Q5_K_M,它能保留模型的大部分性能。如果想节省内存,可考虑Q4_K_M。

Hugging Face下载

也可以使用huggingface-cli将模型预先下载到本地。Hugging Face官网已提供Qwen官方模型:

huggingface-cli download Qwen/Qwen3-Embedding-0.6B-GGUF --local-dir /home/models/Qwen3-Embedding-0.6B-GGUF
huggingface-cli download Qwen/Qwen3-Embedding-4B-GGUF  --local-dir /home/models/Qwen3-Embedding-4B-GGUF
huggingface-cli download Qwen/Qwen3-Embedding-8B-GGUF  --local-dir /home/models/Qwen3-Embedding-8B-GGUF

huggingface-cli download Qwen/Qwen3-Embedding-0.6B --local-dir /home/models/Qwen3-Embedding-0.6B
huggingface-cli download Qwen/Qwen3-Embedding-4B  --local-dir /home/models/Qwen3-Embedding-4B
huggingface-cli download Qwen/Qwen3-Embedding-8B  --local-dir /home/models/Qwen3-Embedding-8B

huggingface-cli download Qwen/Qwen3-Reranker-0.6B-GGUF  --local-dir /home/models/Qwen3-Reranker-0.6B-GGUF
huggingface-cli download Qwen/Qwen3-Reranker-4B-GGUF  --local-dir /home/models/Qwen3-Reranker-4B-GGUF
huggingface-cli download Qwen/Qwen3-Reranker-8B-GGUF   --local-dir /home/models/Qwen3-Reranker-8B-GGUF

模型使用

代码实现并不复杂,关键点已写在注释中,此处不再赘述。

使用SentenceTransformer

版本限制:

  • transformers>=4.51.0
  • sentence-transformers>=2.7.0
from sentence_transformers import SentenceTransformer

# 加载模型,可以使用本地路径以便加载预先下载好的模型
model = SentenceTransformer("Qwen/Qwen3-Embedding-8B")

# 设置flash_attention_2 以及将`padding_side` 设置为"left",可以加快模型的加载与运行速度
# model = SentenceTransformer(
#     "Qwen/Qwen3-Embedding-8B",
#     model_kwargs={"attn_implementation": "flash_attention_2", "device_map": "auto"},
#     tokenizer_kwargs={"padding_side": "left"},
# )

# 需要计算的文档与查询
queries = [
    "What is the capital of China?",
    "Explain gra vity",
]
documents = [
    "The capital of China is Beijing.",
    "Gra vity 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.",
]

# 对查询与文档进行向量计算
query_embeddings = model.encode(queries, prompt_name="query")
document_embeddings = model.encode(documents)

# 计算查询与文档的余弦相似度
similarity = model.similarity(query_embeddings, document_embeddings)
print(similarity)

输出结果:

tensor([[0.7493, 0.0751],
        [0.0880, 0.6318]])

使用Transformers

版本要求:

  • 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}'

# 每个查询都必须附带一个描述任务的一句话指令
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 gra vity')
]
# 检索文档无需添加指令说明
documents = [
    "The capital of China is Beijing.",
    "Gra vity 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-8B', padding_side='left')
model = AutoModel.from_pretrained('Qwen/Qwen3-Embedding-8B')

# 设置flash_attention_2 以及将`padding_side` 设置为"left",可以加快模型的加载与运行速度
# model = AutoModel.from_pretrained('Qwen/Qwen3-Embedding-8B', attn_implementation="flash_attention_2", torch_dtype=torch.float16).cuda()

max_length = 8192

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'])

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

输出结果:

[[0.7493016123771667, 0.0750647559762001], [0.08795969933271408, 0.6318399906158447]]

使用vLLM

版本要求:

  • 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}'

# 每个查询都必须附带一个描述任务的一句话指令
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 gra vity')
]
documents = [
    "The capital of China is Beijing.",
    "Gra vity 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-8B", 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.7482624650001526, 0.07556197047233582], [0.08875375241041183, 0.6300010681152344]]
来源:https://www.53ai.com/news/LargeLanguageModel/2025072439420.html

相关热点

继续查看同栏目近期热点。

延伸阅读

补充最近整理过的热点入口。