跳至主要內容

NanoPQ (乘積量化)

簡而言之,乘積量化演算法 (k-NN)是一種量化演算法,有助於壓縮資料庫向量,從而在涉及大型資料集時幫助進行語義搜尋。簡而言之,嵌入被拆分為 M 個子空間,這些子空間進一步進行聚類。在對向量進行聚類後,質心向量會映射到每個子空間的聚類中存在的向量。

此筆記本介紹了如何使用在底層使用乘積量化的檢索器,該乘積量化由 nanopq 軟體包實現。

%pip install -qU langchain-community langchain-openai nanopq
from langchain_community.embeddings.spacy_embeddings import SpacyEmbeddings
from langchain_community.retrievers import NanoPQRetriever

使用文本建立新的檢索器

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')]

此頁面是否有幫助?