code: import logging
  import os
  from llama_index import (
      StorageContext,
      load_index_from_storage,
  )
  
  from app.engine.constants import STORAGE_DIR
  from app.engine.context import create_service_context
  
  
  def get_chat_engine():
      service_context = create_service_context()
      # check if storage already exists
      if not os.path.exists(STORAGE_DIR):
          raise Exception(
              "StorageContext is empty - call 'python app/engine/generate.py' to generate the storage first"
          )
      logger = logging.getLogger("uvicorn")
      # load the existing index
      logger.info(f"Loading index from {STORAGE_DIR}...")
      storage_context = StorageContext.from_defaults(persist_dir=STORAGE_DIR)
      index = load_index_from_storage(storage_context, service_context=service_context)
      logger.info(f"Finished loading index from {STORAGE_DIR}")
      return index.as_chat_engine()
  
apis: llama_index.StorageContext.from_defaults llama_index.load_index_from_storage
extract_api: [((249, 273), 'app.engine.context.create_service_context', 'create_service_context', ([], {}), '()\n', (271, 273), False, 'from app.engine.context import create_service_context\n'), ((507, 535), 'logging.getLogger', 'logging.getLogger', (['"""uvicorn"""'], {}), "('uvicorn')\n", (524, 535), False, 'import logging\n'), ((644, 697), 'llama_index.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'persist_dir': 'STORAGE_DIR'}), '(persist_dir=STORAGE_DIR)\n', (672, 697), False, 'from llama_index import StorageContext, load_index_from_storage\n'), ((710, 783), 'llama_index.load_index_from_storage', 'load_index_from_storage', (['storage_context'], {'service_context': 'service_context'}), '(storage_context, service_context=service_context)\n', (733, 783), False, 'from llama_index import StorageContext, load_index_from_storage\n'), ((323, 350), 'os.path.exists', 'os.path.exists', (['STORAGE_DIR'], {}), '(STORAGE_DIR)\n', (337, 350), False, 'import os\n')]