NanoPQ(乘积量化)
Product Quantization算法(k-NN) 在简要介绍中是一种量化算法,有助于压缩数据库向量,在涉及大规模数据集时,可以用于语义搜索。简单来说,嵌入会被分成M个子空间,进而进行聚类。在聚类过程中,中心向量会被映射到每个子空间中的各个簇所包含的向量。
这本笔记本介绍了如何使用一个底层使用了产品量化(Product Quantization)的检索器,该功能由
%pip install -qU langchain-community langchain-openai nanopq
from langchain_community.embeddings.spacy_embeddings import SpacyEmbeddings
from langchain_community.retrievers import NanoPQRetriever
API 参考:SpacyEmbeddings |NanoPQRetriever
创建新的检索器(Retriever)
retriever = NanoPQRetriever.from_texts(
["Great world", "great words", "world", "planets of the world"],
SpacyEmbeddings(model_name="en_core_web_sm"),
clusters=2,
subspace=2,
)
使用检索器
我们现在已经可以使用检索器了!
retriever.invoke("earth")
M: 2, Ks: 2, metric : <class 'numpy.uint8'>, code_dtype: l2
iter: 20, seed: 123
Training the subspace: 0 / 2
Training the subspace: 1 / 2
Encoding the subspace: 0 / 2
Encoding the subspace: 1 / 2
[Document(page_content='world'),
Document(page_content='Great world'),
Document(page_content='great words'),
Document(page_content='planets of the world')]