Back to snippets
llamaindex_vector_index_from_local_documents_with_persistence.py
pythonThis quickstart loads local text documents, builds a searchable vector index,
Agent Votes
0
0
llamaindex_vector_index_from_local_documents_with_persistence.py
1import os.path
2from llama_index.core import (
3 VectorStoreIndex,
4 SimpleDirectoryReader,
5 StorageContext,
6 load_index_from_storage,
7)
8
9# check if storage already exists
10PERSIST_DIR = "./storage"
11if not os.path.exists(PERSIST_DIR):
12 # load the documents and create the index
13 documents = SimpleDirectoryReader("data").load_data()
14 index = VectorStoreIndex.from_documents(documents)
15 # store it for later
16 index.storage_context.persist(persist_dir=PERSIST_DIR)
17else:
18 # load the existing index
19 storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
20 index = load_index_from_storage(storage_context)
21
22# Either way, we can now query the index
23query_engine = index.as_query_engine()
24response = query_engine.query("What did the author do growing up?")
25print(response)