Build and persist a FAISS index on disk

FAISS is a battle-tested nearest-neighbor search library from Meta. For a single-book tutor, it is perfect: no server, no network hop, just a file on disk you can load instantly. LangChain wraps it with a familiar vector-store interface.

Indexing and persistence flow

Build once, save once, reload forever.

booktutor.py
python
from langchain_community.vectorstores import FAISS

index_path = f'{pdf_path}_faiss_index'

vectorstore = FAISS.from_documents(splits, embeddings)
vectorstore.save_local(index_path)
print(f'Index saved to {index_path}')

FAISS.from_documents embeds every split and builds an in-memory index. save_local writes two files: the vector index and a pickle of the document metadata. Together they let you reconstruct the vector store without reprocessing anything.

FAISS only stores vectors and IDs. It does not know what text those vectors came from. The pickle file holds the LangChain Document objects, including page_content and metadata, keyed by ID. When you search, FAISS returns IDs, and the wrapper looks up the actual documents from the pickle to return to your code.

Quiz: Quiz

Loading practice…