code: import os
  import hashlib
  from threading import Thread
  from pathlib import Path
  #import llama_index
  from openai import OpenAI
  import constants as c
  c.Get_API()
  client = OpenAI()
  
  newdocspath = ""
  masterpath = ""
  basepath = ""
  persistpath = ""
  # test
  class Document:
      __slots__ = ['text', 'doc_id', 'id_', 'hash']
  
      def __init__(self, text: str, doc_id: str):
          self.text = text
          self.doc_id = doc_id
          self.id_ = doc_id
          self.hash = self.generate_hash(text)
  
      def generate_hash(self, text: str) -> str:
          return hashlib.sha256(text.encode()).hexdigest()
  
      def get_metadata_str(self, mode=None) -> str:
          return f"{self.doc_id}-{self.hash}"
  
      def get_content(self, metadata_mode=None) -> str:
          return self.text
  
  
  def index_document(doc: Document):
      print("index_document reached")
      index = VectorStoreIndex()
      index.add_document(doc)
      print("index doscument complete")
  
  def CreateUpdate_Index(basepath, masterdocs, newdocs, indexpath, action, tool ):
      print('Create/Update function running')
  
      # Ask questions until user exits
      while True:
          # Check if index path directory is empty
          chkindexpath = "Z:\\MyChatBot_v1.0\\"+ tool + "\\index\\"
          print(chkindexpath)
          index_dir = Path(chkindexpath)
          is_empty = len(os.listdir(index_dir)) == 0
  
          if is_empty:
              print('Running creating index function')
              Create_Index(basepath, masterdocs, newdocs, indexpath, tool )
          else:
              print('Running updating index function')
              Update_Index(basepath, masterdocs, newdocs, indexpath)
  
  
  def Create_Index(basepath: str, masterdocs: str, newdocs: str, indexpath: str, tool):
      print('Creating index')
      from llama_index import StorageContext, VectorStoreIndex, SimpleDirectoryReader, load_index_from_storage, Document
  
      # Specify the input_dir path
      docpath = masterdocs
      documents = SimpleDirectoryReader(input_dir=docpath).load_data()
  
      # Create an index from the documents
      index = VectorStoreIndex.from_documents(documents)
  
      # Persist index to disk
      saveindexpath = basepath + indexpath
      index.storage_context.persist(saveindexpath)
  
      print('Index created and saved')
  
      docs_dir = os.path.join("Z:\\MyAI_Training_Docs\\", tool, "_Training_Docs\\docs")
      doc_paths = Path(docs_dir).glob("*")
  
      num_nodes = 8
      nodes = [BaseNode() for _ in range(num_nodes)]
      index = VectorStoreIndex(nodes=nodes)
  
      threads = []
      for path in doc_paths:
          with open(path) as f:
              text = f.read()
          doc = Document(text, path.name)
          thread = Thread(target=index_document, args=(doc,))
          threads.append(thread)
          thread.start()
  
      for thread in threads:
          thread.join()
  
      storage_context = StorageContext(indexdir=indexpath)
      storage_context.persist(index)
      print("Create index complete")
  
  def Update_Index(basepath: str, masterdocs: str, newdocs: str, indexpath: str):
      print("update index reached")
      import os
      from llama_index import load_index_from_storage
  
      storage_context = StorageContext.from_defaults(indexpath)
      index = load_index_from_storage(storage_context)
  
      new_docs_dir = os.path.join(basepath, newdocs)
      for filename in os.listdir(new_docs_dir):
          path = os.path.join(new_docs_dir, filename)
          with open(path) as f:
              text = f.read()
          doc = Document(text, filename)
          index.add_document(doc)
  
      storage_context.persist(index)
      print("Update index completed")
  
  def AskBuild(tool, choice):
      print("AskBuild reached : ", tool, choice)
      if choice == 'build':
          print("Askbuild build reached")
          basepath = 'Z:\\MyAI_Training_Docs\\'
          persistpath = 'Index\\Index\\'
          if tool == 'ai':
              doc_path = "AI"
          elif tool == 'gn':
              doc_path = "GN"
          newdocspath = basepath + doc_path + "_Training_Docs\\Docs"
          masterpath = basepath + doc_path + "_Training_Docs\\Master"
          print(tool, choice)
          print("PP: ", persistpath)
          print("nd: ", newdocspath)
          print("mp: ", masterpath)
          print("bp: ", basepath)
          CreateUpdate_Index(basepath, masterpath, newdocspath, persistpath, choice, tool)
          print("Askbuild GN complete")
      elif choice == 'ask':
          print("Askbuild ask reached")
          persistpath = 'Index\\Index\\'
          newdocspath = 'Docs'
          masterpath = 'Master'
          basepath = 'Z:\\MyChatBot_v1.0\\' + tool + '\\'
          AskQuestion(basepath, persistpath)
          print("Ask build ask complete")
      else:
          pass
  
  
  def AskQuestion(indexpath: str):
      print("Ask question reached")
      storage_context = StorageContext.from_defaults(indexpath)
      index = load_index_from_storage(storage_context)
  
      while True:
          question = input("Enter question: ")
          if question.lower() == "exit":
              break
  
          response = index.query(question)
          print(response)
      print("AskQuestion complete")
apis: llama_index.VectorStoreIndex.from_documents llama_index.StorageContext llama_index.StorageContext.from_defaults llama_index.VectorStoreIndex llama_index.SimpleDirectoryReader llama_index.load_index_from_storage llama_index.Document
extract_api: [((147, 158), 'constants.Get_API', 'c.Get_API', ([], {}), '()\n', (156, 158), True, 'import constants as c\n'), ((168, 176), 'openai.OpenAI', 'OpenAI', ([], {}), '()\n', (174, 176), False, 'from openai import OpenAI\n'), ((854, 872), 'llama_index.VectorStoreIndex', 'VectorStoreIndex', ([], {}), '()\n', (870, 872), False, 'from llama_index import StorageContext, VectorStoreIndex, SimpleDirectoryReader, load_index_from_storage, Document\n'), ((2052, 2094), 'llama_index.VectorStoreIndex.from_documents', 'VectorStoreIndex.from_documents', (['documents'], {}), '(documents)\n', (2083, 2094), False, 'from llama_index import StorageContext, VectorStoreIndex, SimpleDirectoryReader, load_index_from_storage, Document\n'), ((2268, 2338), 'os.path.join', 'os.path.join', (['"""Z:\\\\MyAI_Training_Docs\\\\"""', 'tool', '"""_Training_Docs\\\\docs"""'], {}), "('Z:\\\\MyAI_Training_Docs\\\\', tool, '_Training_Docs\\\\docs')\n", (2280, 2338), False, 'import os\n'), ((2462, 2491), 'llama_index.VectorStoreIndex', 'VectorStoreIndex', ([], {'nodes': 'nodes'}), '(nodes=nodes)\n', (2478, 2491), False, 'from llama_index import StorageContext, VectorStoreIndex, SimpleDirectoryReader, load_index_from_storage, Document\n'), ((2822, 2856), 'llama_index.StorageContext', 'StorageContext', ([], {'indexdir': 'indexpath'}), '(indexdir=indexpath)\n', (2836, 2856), False, 'from llama_index import StorageContext, VectorStoreIndex, SimpleDirectoryReader, load_index_from_storage, Document\n'), ((3131, 3170), 'llama_index.StorageContext.from_defaults', 'StorageContext.from_defaults', (['indexpath'], {}), '(indexpath)\n', (3159, 3170), False, 'from llama_index import StorageContext, VectorStoreIndex, SimpleDirectoryReader, load_index_from_storage, Document\n'), ((3183, 3223), 'llama_index.load_index_from_storage', 'load_index_from_storage', (['storage_context'], {}), '(storage_context)\n', (3206, 3223), False, 'from llama_index import load_index_from_storage\n'), ((3244, 3275), 'os.path.join', 'os.path.join', (['basepath', 'newdocs'], {}), '(basepath, newdocs)\n', (3256, 3275), False, 'import os\n'), ((3296, 3320), 'os.listdir', 'os.listdir', (['new_docs_dir'], {}), '(new_docs_dir)\n', (3306, 3320), False, 'import os\n'), ((4751, 4790), 'llama_index.StorageContext.from_defaults', 'StorageContext.from_defaults', (['indexpath'], {}), '(indexpath)\n', (4779, 4790), False, 'from llama_index import StorageContext, VectorStoreIndex, SimpleDirectoryReader, load_index_from_storage, Document\n'), ((4803, 4843), 'llama_index.load_index_from_storage', 'load_index_from_storage', (['storage_context'], {}), '(storage_context)\n', (4826, 4843), False, 'from llama_index import load_index_from_storage\n'), ((1282, 1300), 'pathlib.Path', 'Path', (['chkindexpath'], {}), '(chkindexpath)\n', (1286, 1300), False, 'from pathlib import Path\n'), ((2609, 2634), 'llama_index.Document', 'Document', (['text', 'path.name'], {}), '(text, path.name)\n', (2617, 2634), False, 'from llama_index import StorageContext, VectorStoreIndex, SimpleDirectoryReader, load_index_from_storage, Document\n'), ((2652, 2694), 'threading.Thread', 'Thread', ([], {'target': 'index_document', 'args': '(doc,)'}), '(target=index_document, args=(doc,))\n', (2658, 2694), False, 'from threading import Thread\n'), ((3337, 3373), 'os.path.join', 'os.path.join', (['new_docs_dir', 'filename'], {}), '(new_docs_dir, filename)\n', (3349, 3373), False, 'import os\n'), ((3446, 3470), 'llama_index.Document', 'Document', (['text', 'filename'], {}), '(text, filename)\n', (3454, 3470), False, 'from llama_index import StorageContext, VectorStoreIndex, SimpleDirectoryReader, load_index_from_storage, Document\n'), ((1945, 1985), 'llama_index.SimpleDirectoryReader', 'SimpleDirectoryReader', ([], {'input_dir': 'docpath'}), '(input_dir=docpath)\n', (1966, 1985), False, 'from llama_index import StorageContext, VectorStoreIndex, SimpleDirectoryReader, load_index_from_storage, Document\n'), ((2355, 2369), 'pathlib.Path', 'Path', (['docs_dir'], {}), '(docs_dir)\n', (2359, 2369), False, 'from pathlib import Path\n'), ((1324, 1345), 'os.listdir', 'os.listdir', (['index_dir'], {}), '(index_dir)\n', (1334, 1345), False, 'import os\n')]