Fast reload path for instant restarts

A RAG tutor that takes a minute to boot is a RAG tutor nobody uses. The reload path is cheap to implement and transforms the developer experience. On startup, check for a saved index. If it exists, load it. If not, run the indexing pipeline and save.

booktutor.py
python
import os
from langchain_community.vectorstores import FAISS

index_path = f'{pdf_path}_faiss_index'

if os.path.exists(index_path):
    print('Loading existing vector store')
    vectorstore = FAISS.load_local(
        index_path,
        embeddings,
        allow_dangerous_deserialization=True,
    )
else:
    print('Building new vector store')
    vectorstore = build_and_save_index(pdf_path, embeddings)

allow_dangerous_deserialization=True is required because the pickle file contains arbitrary Python objects. Only set it when you trust the source of the index, which you do when you built it yourself.

Python pickle can execute code during unpickling, so loading an untrusted pickle is equivalent to running untrusted code. LangChain surfaces that risk with the explicit flag. When the pickle is one you produced locally, it is perfectly safe. When it came from the internet, it is not.

Quiz: Quiz

Loading practice…