code: import os
  import hashlib
  from threading import Thread
  from pathlib import Path
  #import llama_index
  from openai import OpenAI
  import constants as c
  from llama_index import StorageContext, VectorStoreIndex, Document
  from llama_index.node_parser import SimpleNodeParser
  from llama_index import SimpleDirectoryReader
  
  c.Get_API()
  client = OpenAI()
  
  newdocspath = ""
  masterpath = ""
  basepath = ""
  persistpath = ""
  indexpath = ""
  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')
  
      # Check if index path directory is empty
      main_dir = "."
      indexes_dir = os.path.join(main_dir, "Indexes")
      chkindexpath = os.path.join(indexes_dir, tool)
      print('ckindexpath', chkindexpath)
      index_dir = Path(chkindexpath)
      print('index_dir',index_dir)
      is_empty =len(os.listdir(index_dir)) == 0
      print('is empty', is_empty)
      if is_empty:
          print('Running creating index function')
          print(basepath, masterdocs, newdocs, index_dir, tool)
          Create_Index(basepath, masterdocs, newdocs, index_dir, tool )
      else:
          print('Running updating index function')
          Update_Index(basepath, masterdocs, newdocs, index_dir)
      # print('Running creating index function')
      # print(basepath, masterdocs, newdocs, index_dir, tool)
      # Create_Index(basepath, masterdocs, newdocs, index_dir, tool )
  
  def Create_Index(basepath: str, masterdocs: str, newdocs: str, indexpath: str, tool: str):
  
      print('Creating index')
  
      # Load documents
      docpath = masterdocs
      documents = SimpleDirectoryReader(input_dir=docpath).load_data()
  
      # Parse documents into nodes
      parser = SimpleNodeParser.from_defaults()
      nodes = parser.get_nodes_from_documents(documents)
  
      # Create index using nodes
      index = VectorStoreIndex(nodes=nodes)
      for doc in documents:
          index.insert(doc)
  
      # Persist index
      persist_path = os.path.join(basepath, indexpath)
      print('persist_path= ', persist_path)
      saveindexpath = persist_path
      index.storage_context.persist(saveindexpath)
  
      print('Index created and saved')
  
  # def Update_Index(basepath: str, masterdocs: str, newdocs: str, indexpath: str):
  #     print("update index reached")
  #     from llama_index import load_index_from_storage, Document
  #     print('update_index indexpath', indexpath)
  #
  #     try:
  #         storage_context = StorageContext.from_defaults(persist_dir=indexpath)
  #         new_index = load_index_from_storage(storage_context)
  #         new_docs_dir = os.path.join(basepath, newdocs)
  #         is_empty = len(os.listdir(newdocs)) == 0
  #         if not is_empty:
  #             for filename in os.listdir(new_docs_dir):
  #                 path = os.path.join(new_docs_dir, filename)
  #                 with open(path) as f:
  #                     # Create document
  #                     text = f.read()
  #                     doc = Document(text, filename)
  #                     new_index.insert(doc)
  #                 storage_context.persist(new_index)
  #         print("Update index completed")
  #     except Exception as e:
  #         print(e)
  
  def Update_Index(basepath: str, masterdocs: str, newdocs: str, indexpath: str):
      # Loading from disk
      from llama_index import StorageContext, load_index_from_storage
      from llama_index import PromptHelper, LLMPredictor, ServiceContext
      import openai
      openai.api_key = c.Get_API()
  
      is_empty =len(os.listdir(newdocs)) == 0
  
      if not is_empty:
          storage_context = StorageContext.from_defaults(persist_dir=indexpath)
          index = load_index_from_storage(storage_context)
          new_docs_dir = os.path.join(basepath, newdocs)
          llm_predictor =LLMPredictor(llm=openai)
          max_input_size = 4096
          num_outputs = 5000
          max_chunk_overlap = 0.5
          chunk_size_limit = 3900
          prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
  
          service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
  
          reader = SimpleDirectoryReader(new_docs_dir)
          documents = reader.load_data()
          persist_path = persist_path = os.path.join(basepath, indexpath)
          for d in documents:
              index.insert(document = d, service_context = service_context)
          print(persist_path)
          storage_context.persist(persist_dir = persist_path)
      else:
          print('no new docs')
  
  
  
  def AskBuild(tool, choice):
      print("AskBuild reached : ", tool, choice)
      if choice == 'build':
          print("Askbuild build reached")
          main_dir = "."
          #train_dir = os.path.join(main_dir, "MyAI_Training_Docs")
          train_dir = ".//MyAI_Training_Docs//"
          train_path = os.path.join(train_dir, tool)
          master_dir = os.path.join(train_path, "Master")
          persistpath = 'Indexes//' + tool + '//'
          if tool == 'ai':
              doc_path = "ai"
          elif tool == 'gn':
              doc_path = "gn"
          newdocspath = train_path + "//Docs"
          masterpath = train_path + "//Master"
          print(tool, choice)
          print("PP: ", persistpath)
          print("nd: ", newdocspath)
          print("mp: ", masterpath)
          #print("bp: ", basepath)
          basepath = ""
          CreateUpdate_Index(basepath, masterpath, newdocspath, persistpath, choice, tool)
          print("Askbuild gn complete")
      elif choice == 'ask':
          print("Askbuild ask reached")
          persistpath = 'Indexes//'
          newdocspath = 'Docs'
          masterpath = 'Master'
          main_dir = "."
          basepath = os.path.join(main_dir, tool)
          indexpath = main_dir + '//Indexes//' + tool + '//'
          AskQuestion(indexpath, persistpath)
          print("Ask build ask complete")
      else:
          pass
  
  
  def AskQuestion(topic: str, action: str, question: str):
      from llama_index import load_index_from_storage
      print(topic)
      print("Ask question reached")
      indexpath = './/Indexes//' + topic + '//'
      print('indexpath= ', indexpath)
      print(os.listdir(indexpath))
      storage_context = StorageContext.from_defaults(persist_dir=indexpath)
      new_index = load_index_from_storage(storage_context)
      new_query_engine = new_index.as_query_engine()
  
      while True:
          if question.lower() == "exit":
              break
          response = new_query_engine.query(question)
          print(response)
  
          print("AskQuestion complete")
          return response
  
  #AskBuild('gn', 'build')
  
apis: llama_index.LLMPredictor llama_index.ServiceContext.from_defaults llama_index.StorageContext.from_defaults llama_index.VectorStoreIndex llama_index.SimpleDirectoryReader llama_index.load_index_from_storage llama_index.node_parser.SimpleNodeParser.from_defaults llama_index.PromptHelper
extract_api: [((314, 325), 'constants.Get_API', 'c.Get_API', ([], {}), '()\n', (323, 325), True, 'import constants as c\n'), ((335, 343), 'openai.OpenAI', 'OpenAI', ([], {}), '()\n', (341, 343), False, 'from openai import OpenAI\n'), ((1027, 1045), 'llama_index.VectorStoreIndex', 'VectorStoreIndex', ([], {}), '()\n', (1043, 1045), False, 'from llama_index import StorageContext, VectorStoreIndex, Document\n'), ((1321, 1354), 'os.path.join', 'os.path.join', (['main_dir', '"""Indexes"""'], {}), "(main_dir, 'Indexes')\n", (1333, 1354), False, 'import os\n'), ((1374, 1405), 'os.path.join', 'os.path.join', (['indexes_dir', 'tool'], {}), '(indexes_dir, tool)\n', (1386, 1405), False, 'import os\n'), ((1461, 1479), 'pathlib.Path', 'Path', (['chkindexpath'], {}), '(chkindexpath)\n', (1465, 1479), False, 'from pathlib import Path\n'), ((2370, 2402), 'llama_index.node_parser.SimpleNodeParser.from_defaults', 'SimpleNodeParser.from_defaults', ([], {}), '()\n', (2400, 2402), False, 'from llama_index.node_parser import SimpleNodeParser\n'), ((2502, 2531), 'llama_index.VectorStoreIndex', 'VectorStoreIndex', ([], {'nodes': 'nodes'}), '(nodes=nodes)\n', (2518, 2531), False, 'from llama_index import StorageContext, VectorStoreIndex, Document\n'), ((2624, 2657), 'os.path.join', 'os.path.join', (['basepath', 'indexpath'], {}), '(basepath, indexpath)\n', (2636, 2657), False, 'import os\n'), ((4102, 4113), 'constants.Get_API', 'c.Get_API', ([], {}), '()\n', (4111, 4113), True, 'import constants as c\n'), ((6806, 6857), 'llama_index.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'persist_dir': 'indexpath'}), '(persist_dir=indexpath)\n', (6834, 6857), False, 'from llama_index import StorageContext, load_index_from_storage\n'), ((6874, 6914), 'llama_index.load_index_from_storage', 'load_index_from_storage', (['storage_context'], {}), '(storage_context)\n', (6897, 6914), False, 'from llama_index import load_index_from_storage\n'), ((4207, 4258), 'llama_index.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'persist_dir': 'indexpath'}), '(persist_dir=indexpath)\n', (4235, 4258), False, 'from llama_index import StorageContext, load_index_from_storage\n'), ((4275, 4315), 'llama_index.load_index_from_storage', 'load_index_from_storage', (['storage_context'], {}), '(storage_context)\n', (4298, 4315), False, 'from llama_index import load_index_from_storage\n'), ((4339, 4370), 'os.path.join', 'os.path.join', (['basepath', 'newdocs'], {}), '(basepath, newdocs)\n', (4351, 4370), False, 'import os\n'), ((4394, 4418), 'llama_index.LLMPredictor', 'LLMPredictor', ([], {'llm': 'openai'}), '(llm=openai)\n', (4406, 4418), False, 'from llama_index import PromptHelper, LLMPredictor, ServiceContext\n'), ((4564, 4663), 'llama_index.PromptHelper', 'PromptHelper', (['max_input_size', 'num_outputs', 'max_chunk_overlap'], {'chunk_size_limit': 'chunk_size_limit'}), '(max_input_size, num_outputs, max_chunk_overlap,\n    chunk_size_limit=chunk_size_limit)\n', (4576, 4663), False, 'from llama_index import PromptHelper, LLMPredictor, ServiceContext\n'), ((4687, 4778), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm_predictor': 'llm_predictor', 'prompt_helper': 'prompt_helper'}), '(llm_predictor=llm_predictor, prompt_helper=\n    prompt_helper)\n', (4715, 4778), False, 'from llama_index import PromptHelper, LLMPredictor, ServiceContext\n'), ((4792, 4827), 'llama_index.SimpleDirectoryReader', 'SimpleDirectoryReader', (['new_docs_dir'], {}), '(new_docs_dir)\n', (4813, 4827), False, 'from llama_index import SimpleDirectoryReader\n'), ((4905, 4938), 'os.path.join', 'os.path.join', (['basepath', 'indexpath'], {}), '(basepath, indexpath)\n', (4917, 4938), False, 'import os\n'), ((5468, 5497), 'os.path.join', 'os.path.join', (['train_dir', 'tool'], {}), '(train_dir, tool)\n', (5480, 5497), False, 'import os\n'), ((5519, 5553), 'os.path.join', 'os.path.join', (['train_path', '"""Master"""'], {}), "(train_path, 'Master')\n", (5531, 5553), False, 'import os\n'), ((6761, 6782), 'os.listdir', 'os.listdir', (['indexpath'], {}), '(indexpath)\n', (6771, 6782), False, 'import os\n'), ((1531, 1552), 'os.listdir', 'os.listdir', (['index_dir'], {}), '(index_dir)\n', (1541, 1552), False, 'import os\n'), ((2270, 2310), 'llama_index.SimpleDirectoryReader', 'SimpleDirectoryReader', ([], {'input_dir': 'docpath'}), '(input_dir=docpath)\n', (2291, 2310), False, 'from llama_index import SimpleDirectoryReader\n'), ((4133, 4152), 'os.listdir', 'os.listdir', (['newdocs'], {}), '(newdocs)\n', (4143, 4152), False, 'import os\n'), ((6312, 6340), 'os.path.join', 'os.path.join', (['main_dir', 'tool'], {}), '(main_dir, tool)\n', (6324, 6340), False, 'import os\n')]