A local vector store lets developers test retrieval without operating a database service. FAISS gives LlamaIndex an in-process similarity index whose vectors can be persisted with the document and index metadata needed to return source text after a restart.
The llama-index-vector-stores-faiss integration wraps a faiss.Index object and connects it to VectorStoreIndex through StorageContext. An eight-dimensional keyword embedding keeps the build, persistence, reload, and retrieval path independent of an API key.
The dimension passed to faiss.IndexFlatL2() must equal the embedding model's output length. FAISS stores the vectors, while LlamaIndex persists the document text and index structure alongside them; move or back up the entire storage directory rather than the vector-store file alone.
Steps to build a FAISS vector store in LlamaIndex:
- Install LlamaIndex core, its FAISS integration, and faiss-cpu in the project environment.
$ python3 -m pip install llama-index-core llama-index-vector-stores-faiss faiss-cpu
A project virtual environment avoids changing a shared system Python environment.
Related: How to install LlamaIndex with pip - Create faiss_vector_store_build.py with the imports and deterministic embedding class.
- faiss_vector_store_build.py
from pathlib import Path import faiss from llama_index.core import ( Document, StorageContext, VectorStoreIndex, load_index_from_storage, ) from llama_index.core.embeddings import BaseEmbedding from llama_index.vector_stores.faiss import FaissVectorStore class RunbookEmbedding(BaseEmbedding): def _vector(self, text: str) -> list[float]: terms = [ "billing", "refund", "escalation", "ticket", "inventory", "restart", "warehouse", "faiss", ] lowered = text.lower() return [1.0 if term in lowered else 0.0 for term in terms] def _get_text_embedding(self, text: str) -> list[float]: return self._vector(text) def _get_query_embedding(self, query: str) -> list[float]: return self._vector(query) async def _aget_query_embedding(self, query: str) -> list[float]: return self._vector(query)
- Append the storage guard and support documents below the embedding class.
store_dir = Path("faiss_support_store") if store_dir.exists(): raise SystemExit(f"Refusing to replace existing store: {store_dir}") documents = [ Document( text=( "Billing runbook: ticket 7421 covers refund escalation. " "Send the case to the accounts-review queue." ) ), Document( text=( "Inventory runbook: ticket 8804 covers warehouse sync. " "Restart the importer before opening an incident." ) ), ]
An existing faiss_support_store directory causes an immediate stop, preventing accidental replacement of persisted vectors and metadata.
- Append the FAISS index build and persistence operations below the document list.
embedding_model = RunbookEmbedding() faiss_index = faiss.IndexFlatL2(8) vector_store = FaissVectorStore(faiss_index=faiss_index) storage_context = StorageContext.from_defaults(vector_store=vector_store) index = VectorStoreIndex.from_documents( documents, storage_context=storage_context, embed_model=embedding_model, ) index.storage_context.persist(persist_dir=str(store_dir))
The eight IndexFlatL2 dimensions match the eight terms returned by RunbookEmbedding.
- Append the persisted-store reload and retrieval check at the end of the file.
reloaded_store = FaissVectorStore.from_persist_dir(str(store_dir)) reloaded_context = StorageContext.from_defaults( vector_store=reloaded_store, persist_dir=str(store_dir), ) reloaded_index = load_index_from_storage( storage_context=reloaded_context, embed_model=embedding_model, ) retriever = reloaded_index.as_retriever(similarity_top_k=1) question = "Which runbook covers refund escalation for ticket 7421?" nodes = retriever.retrieve(question) print("stored vectors:", reloaded_store.client.ntotal) print("embedding dimensions:", reloaded_store.client.d) print("query:", question) print("top match:", nodes[0].node.get_content(metadata_mode="none"))
- Run the completed script to verify that the reopened FAISS store returns the billing runbook.
$ python3 faiss_vector_store_build.py stored vectors: 2 embedding dimensions: 8 query: Which runbook covers refund escalation for ticket 7421? top match: Billing runbook: ticket 7421 covers refund escalation. Send the case to the accounts-review queue.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.