liveBook · Manning

HNSWLib

As explained in Build AI-Enhanced Web Apps

HNSWLib vector store is a portable vector-storage implementation that persists indexes in a filesystem and supports similarity search.

HNSWLib is a portable, filesystem-backed vector store for semantic retrieval. In a LangChain.js RAG application, it stores embedded document chunks so the index can be loaded later instead of rebuilt for every request. section 7.2.4 section 7.2.3

A typical HNSWLib workflow connects document processing, indexing, and query-time retrieval:

  • Collect documents from a directory, split them into context-preserving chunks, and generate embeddings for each chunk. section 7.2.4
  • Create the store with HNSWLib.fromDocuments(documents, embeddings), producing a searchable vector index. section 7.2.4
  • Persist the index to the filesystem, load it later with HNSWLib.load, and expose it through asRetriever({ k: 6 }). section 7.2.4
  • Run indexing offline when processing a large document collection, keeping request-time work focused on retrieval and response generation. section 7.2.4

Index PDF documents

A directory-indexing routine filters for PDF files, processes each file into document chunks, and builds the store with `await HNSWLib.fromDocuments(documents, this.embeddings)`. The resulting store can support semantic search for a RAG response.

How Build AI-Enhanced Web Apps covers it

How deep it goes
Explains chunk indexing, embedding, filesystem loading, retriever configuration, and offline processing without detailing HNSW internals.
Examples
Uses PDF files from a directory, HNSWLib.fromDocuments, HNSWLib.load, and a six-result retriever.
What it assumes
Assumes document chunking and embedding components, while treating HNSWLib as the persistence and search layer.
What you can do after
Apply it via LangChain.js to build persistent indexes and retrieve relevant document chunks for RAG responses.
How it connects
Connects document chunks, embeddings, semantic search, vector stores, retrievers, and retrieval-augmented generation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.