DocArrayHnswSearch 是 Docarray 提供的一种轻量级文档索引实现,完全在本地运行,最适合中小型数据集。它将向量存储在磁盘上的 hnswlib 中,并将所有其他数据存储在 SQLite 中。使用此集成需要通过
pip install -qU langchain-community 安装 langchain-community。
本 notebook 展示如何使用与 DocArrayHnswSearch 相关的功能。
设置
如果尚未安装 docarray 并设置 OpenAI API 密钥,请取消注释以下单元格进行安装和设置。Copy
pip install -qU "docarray[hnswlib]"
Copy
# 获取 OpenAI 令牌:https://platform.openai.com/account/api-keys
# import os
# from getpass import getpass
# OPENAI_API_KEY = getpass()
# os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
使用 DocArrayHnswSearch
Copy
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import DocArrayHnswSearch
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter
Copy
documents = TextLoader("../../how_to/state_of_the_union.txt").load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
embeddings = OpenAIEmbeddings()
db = DocArrayHnswSearch.from_documents(
docs, embeddings, work_dir="hnswlib_store/", n_dim=1536
)
相似度搜索
Copy
query = "What did the president say about Ketanji Brown Jackson"
docs = db.similarity_search(query)
Copy
print(docs[0].page_content)
Copy
Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you're at it, pass the Disclose Act so Americans can know who is funding our elections.
Tonight, I'd like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.
One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court.
And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation's top legal minds, who will continue Justice Breyer's legacy of excellence.
带分数的相似度搜索
返回的距离分数为余弦距离,分数越低表示越相似。Copy
docs = db.similarity_search_with_score(query)
Copy
docs[0]
Copy
(Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you're at it, pass the Disclose Act so Americans can know who is funding our elections. \n\nTonight, I'd like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation's top legal minds, who will continue Justice Breyer's legacy of excellence.', metadata={}),
0.36962226)
Copy
import shutil
# 删除目录
shutil.rmtree("hnswlib_store")
通过 MCP 将这些文档连接 到 Claude、VSCode 等,获取实时解答。

