Qwen3 Embedding 4B

提供商Qwen
分类feature-extraction
许可证apache-2.0
下载量991.4K
星标65

简介

Qwen3 Embedding 4B 是阿里通义千问团队推出的最新文本向量化模型。它将自然语言转化为高维向量,是构建企业级 RAG(检索增强生成)系统的核心组件。相比于轻量级模型,4B 的参数规模显著提升了语义捕捉的精准度,尤其在处理中文复杂长文本和专业领域知识时,能更有效地减少检索噪声。对于开发者而言,它可直接替代传统的向量模型,与 LangChain 或 LlamaIndex 等框架无缝衔接,上手门槛低,是提升 AI 知识库回答准确率的理想选择。

核心亮点

  • 语义检索精度高,显著优化 RAG 知识库召回率
  • 深度优化中文理解,适配复杂专业领域文本
  • 兼容主流向量数据库,快速集成至现有 AI 工作流
  • Apache-2.0 协议,支持企业级商业化灵活部署

使用方法

安装依赖
# 安装 Hugging Face transformers
pip install transformers torch
SDK 使用
# 使用 transformers 加载模型
from transformers import AutoModel, AutoTokenizer

model = AutoModel.from_pretrained("Qwen/Qwen3-Embedding-4B")
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-Embedding-4B")

Hugging Face 下载

我们推荐使用命令行或者 Hugging Face Hub SDK 来进行模型的下载。

操作指引:在下载前,请先通过如下命令安装 huggingface_hub:

操作指引
pip install -U huggingface_hub

命令行下载

下载完整模型库

下载完整模型库
huggingface-cli download Qwen/Qwen3-Embedding-4B

下载单个文件到指定本地文件夹(以下载 config.json 到当前路径下 ./dir 目录为例)

下载单个文件到指定本地文件夹(以下载 config.json 到当前路径下 ./dir 目录为例)
huggingface-cli download Qwen/Qwen3-Embedding-4B config.json --local-dir ./dir

更多命令行下载选项,可参见官方文档

SDK 下载

SDK 下载
# 模型下载
from huggingface_hub import snapshot_download
model_dir = snapshot_download('Qwen/Qwen3-Embedding-4B')

Git 下载

请确保 lfs 已经被正确安装

Git 下载
git lfs install
git clone https://huggingface.co/Qwen/Qwen3-Embedding-4B

如果您希望跳过 lfs 大文件下载,可以使用如下命令

跳过 LFS
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/Qwen/Qwen3-Embedding-4B

模型文件托管在 Hugging Face Hub,使用 HF CLI / SDK / Git 直接下载,不经过本站。

PyTorch / Transformers 使用

安装 Transformers

安装 Transformers
pip install -U transformers torch

模型加载和推理

模型加载和推理
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained('Qwen/Qwen3-Embedding-4B')
tokenizer = AutoTokenizer.from_pretrained('Qwen/Qwen3-Embedding-4B')

模型下载

我们推荐使用命令行或者 ModelScope SDK 来进行模型的下载。

操作指引:在下载前,请先通过如下命令安装 ModelScope:

操作指引
pip install modelscope

命令行下载

下载完整模型库

下载完整模型库
modelscope download --model Qwen/Qwen3-Embedding-4B

下载单个文件到指定本地文件夹(以下载 README.md 到当前路径下 dir 目录为例)

下载单个文件到指定本地文件夹(以下载 README.md 到当前路径下 dir 目录为例)
modelscope download --model Qwen/Qwen3-Embedding-4B README.md --local_dir ./dir

更多更丰富的命令行下载选项,可参见具体文档

SDK 下载

SDK 下载
# 模型下载
from modelscope import snapshot_download
model_dir = snapshot_download('Qwen/Qwen3-Embedding-4B')

Git 下载

请确保 lfs 已经被正确安装

Git 下载
git lfs install
git clone https://www.modelscope.cn/Qwen/Qwen3-Embedding-4B.git

如果您希望跳过 lfs 大文件下载,可以使用如下命令

跳过 LFS
GIT_LFS_SKIP_SMUDGE=1 git clone https://www.modelscope.cn/Qwen/Qwen3-Embedding-4B.git

ModelScope 模型页直接下载模型文件;无需将模型文件放在本站服务器。

Notebook 快速开发

下载并安装 ModelScope library

下载并安装 ModelScope library
pip install "modelscope[audio,cv,nlp,multi-modal,science]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html

模型加载和推理

模型加载和推理
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

p = pipeline('text-generation', 'Qwen/Qwen3-Embedding-4B')

完整文档

来源: HuggingFace

---
license: apache-2.0
base_model:

  • Qwen/Qwen3-4B-Base

tags:
  • transformers

  • sentence-transformers

  • sentence-similarity

  • feature-extraction

  • text-embeddings-inference

---

Qwen3-Embedding-4B

<p align="center">
<img src="https://qianwen-res.oss-accelerate-overseas.aliyuncs.com/logo_qwen3.png" width="400"/>
<p>

Highlights

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 significant advancements in multiple text embedding and ranking tasks, including text retrieval, code retrieval, text classification, text clustering, and bitext mining.

Exceptional Versatility: The embedding model has achieved state-of-the-art performance across a wide range of downstream application evaluations. The 8B size embedding model ranks No.1 in the MTEB multilingual leaderboard (as of June 5, 2025, score 70.58), while the reranking model excels in various text retrieval scenarios.

Comprehensive Flexibility: The Qwen3 Embedding series offers a full spectrum of sizes (from 0.6B to 8B) for both embedding and reranking models, catering to diverse use cases that prioritize efficiency and effectiveness. Developers can seamlessly combine these two modules. Additionally, the embedding model allows for flexible vector definitions across all dimensions, and both embedding and reranking models support user-defined instructions to enhance performance for specific tasks, languages, or scenarios.

Multilingual Capability: The Qwen3 Embedding series offer support for over 100 languages, thanks to the multilingual capabilites of Qwen3 models. This includes various programming languages, and provides robust multilingual, cross-lingual, and code retrieval capabilities.

Model Overview

Qwen3-Embedding-4B has the following features:

  • Model Type: Text Embedding
  • Supported Languages: 100+ Languages
  • Number of Paramaters: 4B
  • Context Length: 32k
  • Embedding Dimension: Up to 2560, supports user-defined output dimensions ranging from 32 to 2560

For more details, including benchmark evaluation, hardware requirements, and inference performance, please refer to our blog, GitHub.

Qwen3 Embedding Series Model list

| Model Type | Models | Size | Layers | Sequence Length | Embedding Dimension | MRL Support | Instruction Aware |
|------------------|----------------------|------|--------|-----------------|---------------------|-------------|----------------|
| Text Embedding | Qwen3-Embedding-0.6B | 0.6B | 28 | 32K | 1024 | Yes | Yes |
| Text Embedding | Qwen3-Embedding-4B | 4B | 36 | 32K | 2560 | Yes | Yes |
| Text Embedding | Qwen3-Embedding-8B | 8B | 36 | 32K | 4096 | Yes | Yes |
| Text Reranking | Qwen3-Reranker-0.6B | 0.6B | 28 | 32K | - | - | Yes |
| Text Reranking | Qwen3-Reranker-4B | 4B | 36 | 32K | - | - | Yes |
| Text Reranking | Qwen3-Reranker-8B | 8B | 36 | 32K | - | - | Yes |

> Note:
> - MRL Support indicates whether the embedding model supports custom dimensions for the final embedding.
> - Instruction Aware notes whether the embedding or reranking model supports customizing the input instruction according to different tasks.
> - Our evaluation indicates that, for most downstream tasks, using instructions (instruct) typically yields an improvement of 1% to 5% compared to not using them. Therefore, we recommend that developers create tailored instructions specific to their tasks and scenarios. In multilingual contexts, we also advise users to write their instructions in English, as most instructions utilized during the model training process were originally written in English.

Usage

With Transformers versions earlier than 4.51.0, you may encounter the following error:

code
KeyError: 'qwen3'

Sentence Transformers Usage

python
# 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 Usage

```python

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.

#