index.storage_context.persist(persist_dir="./storage") from llama_index import StorageContext, load_index_from_storage index = load_index_from_storage(StorageContext.from_defaults(persist_dir="./storage"))
vector_store = ... index = VectorStoreIndex.from_vector_store(vector_store)
def create_vectorstore(app): ### DATA and VECTORSTORE locations data_dir = 'data' vectorStore_dir = 'index' try: ### TRY LOADING PERSISTED INDEX ### storage_context = StorageContext.from_defaults(persist_dir=vectorStore_dir) vectorStore = load_index_from_storage(storage_context) logging.info("Loaded Vector Store OK.") return vectorStore except Exception as e: logging.info(f"Error loading Vector Store: {e}") try: ### READ and INDEX DOCS ### logging.info("Creating Embeddings and Vector Store...") documents = SimpleDirectoryReader(data_dir).load_data() vectorStore = VectorStoreIndex.from_documents(documents) logging.info("Vector Store created OK.") ### PERSIST INDEX TO STORAGE ### vectorStore.storage_context.persist(persist_dir=vectorStore_dir) logging.info("Vector Store persisted OK.") return vectorStore except Exception as e: logging.error(f"Error creating or storing Vector Store: {e}")