code: import os
  import requests
  
  from dotenv import load_dotenv
  from llama_index import SimpleDirectoryReader, StorageContext, VectorStoreIndex, load_index_from_storage
  from pathlib import Path
  
  import helper.data as dhelp
  
  
  load_dotenv()
  
  LOCAL_FILE_NAME = 'data/TR-61850.pdf'
  INDEX_STORAGE_DIR = './storage'
  
  
  def build_index() -> VectorStoreIndex:
      """
      Load data into a vector store and build the index.
  
      :return: The vector store index
      """
  
      # Using the previously identified optimal settings
      service_context = dhelp.get_service_context(chunk_size=512, chunk_overlap=75)
  
      try:
          print('Loading index from storage...')
          # rebuild storage context
          storage_context = StorageContext.from_defaults(persist_dir=INDEX_STORAGE_DIR)
          # load index
          index = load_index_from_storage(
              storage_context,
              service_context=service_context
          )
      except Exception as ex:
          print(f'Building index from scratch because of exception: {ex}')
  
          if not os.path.exists(LOCAL_FILE_NAME) or os.path.isfile(LOCAL_FILE_NAME):
              filename = Path(LOCAL_FILE_NAME)
              url = 'https://www.fit.vut.cz/research/publication-file/11832/TR-61850.pdf'
              response = requests.get(url)
              filename.write_bytes(response.content)
  
          documents = SimpleDirectoryReader(
              input_files=[LOCAL_FILE_NAME]
          ).load_data()
          index = VectorStoreIndex.from_documents(
              documents,
              service_context=service_context,
              show_progress=True
          )
          index.storage_context.persist(INDEX_STORAGE_DIR)
  
      return index
  
  
  if __name__ == '__main__':
      build_index()
  
apis: llama_index.VectorStoreIndex.from_documents llama_index.StorageContext.from_defaults llama_index.load_index_from_storage llama_index.SimpleDirectoryReader
extract_api: [((219, 232), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (230, 232), False, 'from dotenv import load_dotenv\n'), ((531, 590), 'helper.data.get_service_context', 'dhelp.get_service_context', ([], {'chunk_size': '(512)', 'chunk_overlap': '(75)'}), '(chunk_size=512, chunk_overlap=75)\n', (556, 590), True, 'import helper.data as dhelp\n'), ((708, 767), 'llama_index.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'persist_dir': 'INDEX_STORAGE_DIR'}), '(persist_dir=INDEX_STORAGE_DIR)\n', (736, 767), False, 'from llama_index import SimpleDirectoryReader, StorageContext, VectorStoreIndex, load_index_from_storage\n'), ((805, 878), 'llama_index.load_index_from_storage', 'load_index_from_storage', (['storage_context'], {'service_context': 'service_context'}), '(storage_context, service_context=service_context)\n', (828, 878), False, 'from llama_index import SimpleDirectoryReader, StorageContext, VectorStoreIndex, load_index_from_storage\n'), ((1447, 1546), 'llama_index.VectorStoreIndex.from_documents', 'VectorStoreIndex.from_documents', (['documents'], {'service_context': 'service_context', 'show_progress': '(True)'}), '(documents, service_context=service_context,\n    show_progress=True)\n', (1478, 1546), False, 'from llama_index import SimpleDirectoryReader, StorageContext, VectorStoreIndex, load_index_from_storage\n'), ((1065, 1096), 'os.path.isfile', 'os.path.isfile', (['LOCAL_FILE_NAME'], {}), '(LOCAL_FILE_NAME)\n', (1079, 1096), False, 'import os\n'), ((1121, 1142), 'pathlib.Path', 'Path', (['LOCAL_FILE_NAME'], {}), '(LOCAL_FILE_NAME)\n', (1125, 1142), False, 'from pathlib import Path\n'), ((1254, 1271), 'requests.get', 'requests.get', (['url'], {}), '(url)\n', (1266, 1271), False, 'import requests\n'), ((1030, 1061), 'os.path.exists', 'os.path.exists', (['LOCAL_FILE_NAME'], {}), '(LOCAL_FILE_NAME)\n', (1044, 1061), False, 'import os\n'), ((1344, 1396), 'llama_index.SimpleDirectoryReader', 'SimpleDirectoryReader', ([], {'input_files': '[LOCAL_FILE_NAME]'}), '(input_files=[LOCAL_FILE_NAME])\n', (1365, 1396), False, 'from llama_index import SimpleDirectoryReader, StorageContext, VectorStoreIndex, load_index_from_storage\n')]