milvus-logo
LFAI
首页
  • 模型

嵌入概述

嵌入是一种机器学习概念,用于将数据映射到高维空间,将语义相似的数据放在一起。嵌入模型通常是 BERT 或其他 Transformer 系列中的深度神经网络,可以用一系列称为向量的数字有效地表示文本、图像和其他数据类型的语义。这些模型的一个主要特点是,高维空间中向量之间的数学距离可以表示原始文本或图像语义的相似性。这一特性开启了许多信息检索应用,如谷歌和必应等网络搜索引擎、电子商务网站上的产品搜索和推荐,以及最近流行的生成式人工智能中的检索增强生成(RAG)范式。

嵌入有两大类,每一类产生不同类型的向量:

  • 密集嵌入:大多数嵌入模型将信息表示为数百到数千维的浮点向量。由于大多数维度的值都不为零,因此输出结果被称为 "密集 "向量。例如,流行的开源嵌入模型 BAAI/bge-base-en-v1.5 输出的向量包含 768 个浮点数(768 维浮点向量)。

  • 稀疏嵌入:相比之下,稀疏嵌入的输出向量大部分维数为零,即 "稀疏 "向量。这些向量通常具有更高的维度(数万或更多),这是由标记词汇量的大小决定的。稀疏向量可以通过深度神经网络或文本语料库统计分析生成。由于稀疏嵌入向量具有可解释性和更好的域外泛化能力,开发人员越来越多地采用稀疏嵌入向量作为高密度嵌入向量的补充。

Milvus 是一个向量数据库,专为向量数据管理、存储和检索而设计。通过整合主流的嵌入和重新排序模型,您可以轻松地将原始文本转换为可搜索的向量,或使用强大的模型对结果进行重新排序,从而获得更准确的 RAG 结果。这种集成简化了文本转换,无需额外的嵌入或重排组件,从而简化了 RAG 的开发和验证。

要在实际中创建嵌入,请参阅使用 PyMilvus 的模型生成文本嵌入

嵌入函数类型API 或开源
openai密集API
句子转换器密集开源
bm25稀疏开源
Splade稀疏开源
bge-m3混合开源
航程密集型应用程序接口
jina密集API
cohere密集密集向量

例 1:使用默认嵌入函数生成密集向量

要在 Milvus 中使用嵌入函数,首先要安装 PyMilvus 客户端库和model 子包,该子包封装了嵌入生成的所有实用程序。

pip install "pymilvus[model]"

model 子包支持各种嵌入模型,从OpenAISentence TransformersBGE M3BM25SPLADE预训练模型。为简便起见,本示例使用的DefaultEmbeddingFunction全 MiniLM-L6-v2句子转换器模型,该模型约 70MB,首次使用时会下载:

from pymilvus import model

# This will download "all-MiniLM-L6-v2", a light weight model.
ef = model.DefaultEmbeddingFunction()

# Data from which embeddings are to be generated 
docs = [
"Artificial intelligence was founded as an academic discipline in 1956.",
"Alan Turing was the first person to conduct substantial research in AI.",
"Born in Maida Vale, London, Turing was raised in southern England.",
]

embeddings = ef.encode_documents(docs)

# Print embeddings
print("Embeddings:", embeddings)
# Print dimension and shape of embeddings
print("Dim:", ef.dim, embeddings[0].shape)

预期输出类似于下面的内容:

Embeddings: [array([-3.09392996e-02, -1.80662833e-02,  1.34775648e-02,  2.77156215e-02,
       -4.86349640e-03, -3.12581174e-02, -3.55921760e-02,  5.76934684e-03,
        2.80773244e-03,  1.35783911e-01,  3.59678417e-02,  6.17732145e-02,
...
       -4.61330153e-02, -4.85207550e-02,  3.13997865e-02,  7.82178566e-02,
       -4.75336798e-02,  5.21207601e-02,  9.04406682e-02, -5.36676683e-02],
      dtype=float32)]
Dim: 384 (384,)

例 2:使用 BGE M3 模型一次调用生成密集向量和稀疏向量

在本例中,我们使用BGE M3混合模型将文本嵌入密集向量和稀疏向量,并使用它们检索相关文档。总体步骤如下:

  1. 使用 BGE-M3 模型将文本嵌入为密集向量和稀疏向量;

  2. 建立一个 Milvus 集合来存储密集向量和稀疏向量;

  3. 将数据插入 Milvus;

  4. 搜索并检查结果。

首先,我们需要安装必要的依赖项。

from pymilvus.model.hybrid import BGEM3EmbeddingFunction
from pymilvus import (
    utility,
    FieldSchema, CollectionSchema, DataType,
    Collection, AnnSearchRequest, RRFRanker, connections,
)

使用 BGE M3 对文档和查询进行编码,以便嵌入检索。

# 1. prepare a small corpus to search
docs = [
    "Artificial intelligence was founded as an academic discipline in 1956.",
    "Alan Turing was the first person to conduct substantial research in AI.",
    "Born in Maida Vale, London, Turing was raised in southern England.",
]
query = "Who started AI research?"

# BGE-M3 model can embed texts as dense and sparse vectors.
# It is included in the optional `model` module in pymilvus, to install it,
# simply run "pip install pymilvus[model]".

bge_m3_ef = BGEM3EmbeddingFunction(use_fp16=False, device="cpu")

docs_embeddings = bge_m3_ef(docs)
query_embeddings = bge_m3_ef([query])

例 3:使用 BM25 模型生成稀疏向量

BM25 是一种著名的方法,它使用单词出现频率来确定查询和文档之间的相关性。在本例中,我们将展示如何使用BM25EmbeddingFunction 为查询和文档生成稀疏嵌入。

首先,导入BM25EmbeddingFunction类。

from pymilvus.model.sparse import BM25EmbeddingFunction

在 BM25 中,计算文档中的统计数据以获得 IDF(反向文档频率)非常重要,它可以代表文档中的模式。IDF 衡量的是一个词提供了多少信息,即在所有文档中这个词是常见还是罕见。

# 1. prepare a small corpus to search
docs = [
    "Artificial intelligence was founded as an academic discipline in 1956.",
    "Alan Turing was the first person to conduct substantial research in AI.",
    "Born in Maida Vale, London, Turing was raised in southern England.",
]
query = "Where was Turing born?"
bm25_ef = BM25EmbeddingFunction()

# 2. fit the corpus to get BM25 model parameters on your documents.
bm25_ef.fit(docs)

# 3. store the fitted parameters to disk to expedite future processing.
bm25_ef.save("bm25_params.json")

# 4. load the saved params
new_bm25_ef = BM25EmbeddingFunction()
new_bm25_ef.load("bm25_params.json")

docs_embeddings = new_bm25_ef.encode_documents(docs)
query_embeddings = new_bm25_ef.encode_queries([query])
print("Dim:", new_bm25_ef.dim, list(docs_embeddings)[0].shape)

预期的输出结果类似于下图:

Dim: 21 (1, 21)