热门搜索:和平精英 原神 街篮2 

您的位置:首页 > > 教程攻略 > ai资讯 >【RAG】FastEmbed:一种轻量的快速文本嵌入工具

【RAG】FastEmbed:一种轻量的快速文本嵌入工具

来源:互联网 更新时间:2026-08-27 14:39

前言

说起文本嵌入,尤其是RAG系统里的那一步,一个又快又轻量的工具简直是刚需。Fair enough,市面上有不少选择,但今天要聊的FastEmbed,从名字就能看出来——它把“快”和“轻”刻进了骨子里。不仅要快,还要保证嵌入质量不掉链子。而且,它不只是文本模型,连图像嵌入也一并支持。

它的核心特点其实就几个关键词:

  • :底层跑在ONNX Runtime上,大规模数据场景下效率很能打。
  • :依赖少,资源占用低,不管你是云上还是本地,甚至边缘设备,都能跑得动。
  • :不挑任务,文本分类、语义搜索、聚类……随便用。
  • 能上GPU

    :支持GPU加速,真·快上加快。

使用

安装

安装没什么花头,一行命令搞定,CPU版和GPU版分开选:

# CPU版
pip install fastembed

# GPU版
pip install fastembed-gpu

装完之后,直接跑段代码感受一下——下面这个例子演示了最基本的文档嵌入流程,注意返回的是生成器,取出来用就行:

from fastembed import TextEmbedding
from typing import List

# Example list of documents
documents: List[str] = [
    "This is built to be faster and lighter than other embedding libraries e.g. Transformers, Sentence-Transformers, etc.",
    "fastembed is supported by and maintained by Qdrant.",
]

# This will trigger the model download and initialization
embedding_model = TextEmbedding()
print("The model BAAI/bge-small-en-v1.5 is ready to use.")

embeddings_generator = embedding_model.embed(documents)  # reminder this is a generator
embeddings_list = list(embedding_model.embed(documents))
# you can also convert the generator to a list, and that to a numpy array
print(len(embeddings_list[0]) ) # Vector of 384 dimensions

密集文本嵌入

再具体一点,密集嵌入是最常用的场景。指定模型名,调一行embed方法,出来就是向量数组:

from fastembed import TextEmbedding

model = TextEmbedding(model_name="BAAI/bge-small-en-v1.5")
embeddings = list(model.embed(documents))

# [
#   array([-0.1115,  0.0097,  0.0052,  0.0195, ...], dtype=float32),
#   array([-0.1019,  0.0635, -0.0332,  0.0522, ...], dtype=float32)
# ]

稀疏文本嵌入

除了密集向量,FastEmbed也支持稀疏嵌入,用的是SPLADE++模型。如果你做的是关键词匹配或精确召回,这个很有用:

from fastembed import SparseTextEmbedding

model = SparseTextEmbedding(model_name="prithivida/Splade_PP_en_v1")
embeddings = list(model.embed(documents))

# [
#   SparseEmbedding(indices=[ 17, 123, 919, ... ], values=[0.71, 0.22, 0.39, ...]),
#   SparseEmbedding(indices=[ 38,  12,  91, ... ], values=[0.11, 0.22, 0.39, ...])
# ]

图像嵌入

最后,如果你需要做多模态,比如图像嵌入,FastEmbed同样有对应的模型。传入图片路径,结果和文本嵌入一样,都是浮点数组:

from fastembed import ImageEmbedding

images = [
    "./path/to/image1.jpg",
    "./path/to/image2.jpg",
]

model = ImageEmbedding(model_name="Qdrant/clip-ViT-B-32-vision")
embeddings = list(model.embed(images))

# [
#   array([-0.1115,  0.0097,  0.0052,  0.0195, ...], dtype=float32),
#   array([-0.1019,  0.0635, -0.0332,  0.0522, ...], dtype=float32)
# ]
关于宇宙的好的网名有哪些
关于宇宙的好的网名有哪些

类型:角色扮演

大小:1

语言:简体中文

平台:互联网

游戏下载

热门手游

手机号码测吉凶
本站所有软件,都由网友上传,如有侵犯你的版权,请发邮件haolingcc@hotmail.com 联系删除。 版权所有 Copyright@2012-2013 haoling.cc