Hnswlib
Hnswlib 是一个快速的近似最近邻搜索索引。它是一个轻量级、仅头文件的 C++ HNSW 实现,除了 C++11 外没有其他依赖。Hnswlib 提供 Python 绑定。
%pip install llama-index%pip install llama-index-vector-stores-hnswlib%pip install llama-index-embeddings-huggingface%pip install hnswlibfrom llama_index.vector_stores.hnswlib import HnswlibVectorStorefrom llama_index.core import ( VectorStoreIndex, StorageContext, SimpleDirectoryReader,)from llama_index.embeddings.huggingface import HuggingFaceEmbedding!mkdir -p 'data/paul_graham/'!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'documents = SimpleDirectoryReader("./data/paul_graham/").load_data()print(f"Total documents: {len(documents)}")print(f"First document, id: {documents[0].doc_id}")print(f"First document, hash: {documents[0].hash}")print( "First document, text" f" ({len(documents[0].text)} characters):\n{'='*20}\n{documents[0].text[:360]} ...")embed_model = HuggingFaceEmbedding( model_name="sentence-transformers/all-MiniLM-L6-v2", normalize=True,)根据Hnswlib.Index参数创建Hnswlib向量存储对象
Section titled “Create Hnswlib Vector Store object from Hnswlib.Index parameters”hnswlib_vector_store = HnswlibVectorStore.from_params( space="ip", dimension=embed_model._model.get_sentence_embedding_dimension(), max_elements=1000,)或者,您可以自行创建一个 Hnswlib.Index 对象。
import hnswlib
index = hnswlib.Index( "ip", embed_model._model.get_sentence_embedding_dimension())index.init_index(max_elements=1000)
hnswlib_vector_store = HnswlibVectorStore(index)hnswlib_storage_context = StorageContext.from_defaults( vector_store=hnswlib_vector_store)hnswlib_index = VectorStoreIndex.from_documents( documents, storage_context=hnswlib_storage_context, embed_model=embed_model, show_progress=True,)k = 5query = "Before college I wrote what begginers should write."hnswlib_vector_retriever = hnswlib_index.as_retriever(similarity_top_k=k)nodes_with_scores = nodes_with_scores = hnswlib_vector_retriever.retrieve( query)for node in nodes_with_scores: print(f"Node {node.id_} | Score: {node.score:.3f} - {node.text[:120]}...")