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.
