File size: 892 Bytes
6855b1e
 
 
 
 
 
428be9a
6855b1e
0e9148c
6855b1e
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import logging
import os

from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, StorageContext, load_index_from_storage

logger = logging.getLogger(__name__)
DOCUMENT_PATH = 'search_data/'


# remember to delete stored vectors when new documents are added to the data so the storage is recreated
def read_write_index(path):
    if not os.path.exists(path):
        documents = SimpleDirectoryReader(DOCUMENT_PATH).load_data()
        logger.info(f'Indexing documents in {DOCUMENT_PATH}...')
        index = VectorStoreIndex.from_documents(documents)
        index.storage_context.persist(persist_dir=path)
        logger.info(f'{len(documents)} documents indexed.')
    else:
        logger.info(f'Loading index from {path}...')
        storage_context = StorageContext.from_defaults(persist_dir=path)
        index = load_index_from_storage(storage_context)
    return index