Skip to main content
FastEmbed 来自 Qdrant,是一个轻量、快速的 Python 库,专为嵌入生成而构建。
  • 量化模型权重
  • ONNX Runtime,无 PyTorch 依赖
  • CPU 优先设计
  • 支持大规模数据集编码的数据并行处理

依赖项

要在 LangChain 中使用 FastEmbed,请安装 fastembed Python 包。
pip install -qU  fastembed

导入

from langchain_community.embeddings.fastembed import FastEmbedEmbeddings

实例化 FastEmbed

参数

  • model_name: str(默认值:“BAAI/bge-small-en-v1.5”)
    要使用的 FastEmbedding 模型名称。您可以在此处找到支持的模型列表。
  • max_length: int(默认值:512)
    最大 token 数量。超过 512 时行为未知。
  • cache_dir: Optional[str](默认值:None)
    缓存目录路径。默认为父目录中的 local_cache
  • threads: Optional[int](默认值:None)
    单个 onnxruntime 会话可使用的线程数。
  • doc_embed_type: Literal["default", "passage"](默认值:“default”)
    “default”:使用 FastEmbed 的默认嵌入方法。
    “passage”:在嵌入前为文本添加 “passage” 前缀。
  • batch_size: int(默认值:256)
    编码的批处理大小。值越高,内存占用越多,但速度更快。
  • parallel: Optional[int](默认值:None)
    >1,将使用数据并行编码,推荐用于离线编码大型数据集。 若为 0,使用所有可用核心。 若为 None,不使用数据并行处理,而使用默认的 onnxruntime 线程策略。
embeddings = FastEmbedEmbeddings()

使用方法

生成文档嵌入

document_embeddings = embeddings.embed_documents(
    ["This is a document", "This is some other document"]
)

生成查询嵌入

query_embeddings = embeddings.embed_query("This is a query")