code: import logging
  import os
  import sys
  from pathlib import Path
  
  from dotenv import load_dotenv
  
  from langchain.llms.octoai_endpoint import OctoAIEndpoint as OctoAiCloudLLM
  from langchain.embeddings.octoai_embeddings import OctoAIEmbeddings
  from llama_index import (
      LLMPredictor,
      ServiceContext,
      download_loader,
      GPTVectorStoreIndex,
      LangchainEmbedding,
  )
  
  import time
  from termios import tcflush, TCIFLUSH
  
  # Get the current file's directory
  current_dir = os.path.dirname(os.path.abspath(__file__))
  # Change the current working directory
  os.chdir(current_dir)
  # Set logging level to CRITICAL
  logging.basicConfig(level=logging.CRITICAL)
  
  # Load environment variables
  load_dotenv()
  
  # Set the file storage directory
  FILES = "./files"
  
  
  def init():
      """
      Initialize the files directory.
      """
      if not os.path.exists(FILES):
          os.mkdir(FILES)
  
  
  def handle_exit():
      """
      Handle exit gracefully.
      """
      print("\nGoodbye!\n")
      sys.exit(1)
  
  
  def ask(file):
      """
      Load the file, create the query engine and interactively answer user questions about the document.
      """
      print("Loading...")
      # Load the PDFReader
      PDFReader = download_loader("PDFReader")
      loader = PDFReader()
      documents = loader.load_data(file=Path(file))
  
      # Initialize the OctoAiCloudLLM
      endpoint_url = os.getenv("ENDPOINT_URL")
      # Set up the language model and predictor
      llm = OctoAiCloudLLM(
          endpoint_url=endpoint_url,
          model_kwargs={
          "model": "llama-2-7b-chat",
          "messages": [
              {
                  "role": "system",
                  "content": "Below is an instruction that describes a task. Write a response that appropriately completes the request."
              }
          ],
          "stream": False,
          "max_tokens": 256
          }
      )
  
  
      llm_predictor = LLMPredictor(llm=llm)
  
      # Create the LangchainEmbedding
      embeddings = LangchainEmbedding(
          OctoAIEmbeddings(
              endpoint_url="https://instructor-large-f1kzsig6xes9.octoai.run/predict"
          )
      )
  
      # Create the ServiceContext
      service_context = ServiceContext.from_defaults(
          llm_predictor=llm_predictor, chunk_size_limit=512, embed_model=embeddings
      )
  
      # Create the index from documents
      index = GPTVectorStoreIndex.from_documents(
          documents, service_context=service_context
      )
  
      # Create the query engine
      query_engine = index.as_query_engine(verbose=True, llm_predictor=llm_predictor)
  
      # Clear the screen
      os.system("clear")
  
      print("Ready! Ask anything about the document")
      print("")
      print("Press Ctrl+C to exit")
  
      try:
          tcflush(sys.stdin, TCIFLUSH)
          while True:
              prompt = input("\nPrompt: ")
              if prompt is None:
                  continue
              if prompt == "exit":
                  handle_exit()
  
              start_time = time.time()
              response = query_engine.query(prompt)
              end_time = time.time()
              elapsed_time = end_time - start_time
              print()
  
              # Transform response to string and remove leading newline character if present
              response = str(response).lstrip("\n")
  
              print(f"Response({round(elapsed_time, 1)} sec): {response}")
      except KeyboardInterrupt:
          handle_exit()
  
  
  def select_file():
      """
      Select a file for processing.
      """
      os.system("clear")
      files = [file for file in os.listdir(FILES) if file.endswith(".pdf")]
  
      if not files:
          return "file.pdf" if os.path.exists("file.pdf") else None
  
      print("Select a file")
      for i, file in enumerate(files):
          print(f"{i+1}. {file}")
      print()
  
      try:
          possible_selections = list(range(len(files) + 1))
          selection = int(input("Enter a number, or 0 to exit: "))
  
          if selection == 0:
              handle_exit()
          elif selection not in possible_selections:
              select_file()
          else:
              file_path = os.path.abspath(os.path.join(FILES, files[selection - 1]))
  
          return file_path
      except ValueError:
          return select_file()
  
  
  if __name__ == "__main__":
      # Initialize the file directory
      init()
      if file := select_file():
          # Start the interactive query session
          ask(file)
      else:
          print("No files found")
          handle_exit()
  
apis: llama_index.GPTVectorStoreIndex.from_documents llama_index.ServiceContext.from_defaults llama_index.download_loader llama_index.LLMPredictor
extract_api: [((557, 578), 'os.chdir', 'os.chdir', (['current_dir'], {}), '(current_dir)\n', (565, 578), False, 'import os\n'), ((611, 654), 'logging.basicConfig', 'logging.basicConfig', ([], {'level': 'logging.CRITICAL'}), '(level=logging.CRITICAL)\n', (630, 654), False, 'import logging\n'), ((685, 698), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (696, 698), False, 'from dotenv import load_dotenv\n'), ((491, 516), 'os.path.abspath', 'os.path.abspath', (['__file__'], {}), '(__file__)\n', (506, 516), False, 'import os\n'), ((970, 981), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (978, 981), False, 'import sys\n'), ((1183, 1211), 'llama_index.download_loader', 'download_loader', (['"""PDFReader"""'], {}), "('PDFReader')\n", (1198, 1211), False, 'from llama_index import LLMPredictor, ServiceContext, download_loader, GPTVectorStoreIndex, LangchainEmbedding\n'), ((1343, 1368), 'os.getenv', 'os.getenv', (['"""ENDPOINT_URL"""'], {}), "('ENDPOINT_URL')\n", (1352, 1368), False, 'import os\n'), ((1425, 1712), 'langchain.llms.octoai_endpoint.OctoAIEndpoint', 'OctoAiCloudLLM', ([], {'endpoint_url': 'endpoint_url', 'model_kwargs': "{'model': 'llama-2-7b-chat', 'messages': [{'role': 'system', 'content':\n    'Below is an instruction that describes a task. Write a response that appropriately completes the request.'\n    }], 'stream': False, 'max_tokens': 256}"}), "(endpoint_url=endpoint_url, model_kwargs={'model':\n    'llama-2-7b-chat', 'messages': [{'role': 'system', 'content':\n    'Below is an instruction that describes a task. Write a response that appropriately completes the request.'\n    }], 'stream': False, 'max_tokens': 256})\n", (1439, 1712), True, 'from langchain.llms.octoai_endpoint import OctoAIEndpoint as OctoAiCloudLLM\n'), ((1854, 1875), 'llama_index.LLMPredictor', 'LLMPredictor', ([], {'llm': 'llm'}), '(llm=llm)\n', (1866, 1875), False, 'from llama_index import LLMPredictor, ServiceContext, download_loader, GPTVectorStoreIndex, LangchainEmbedding\n'), ((2131, 2239), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm_predictor': 'llm_predictor', 'chunk_size_limit': '(512)', 'embed_model': 'embeddings'}), '(llm_predictor=llm_predictor, chunk_size_limit=\n    512, embed_model=embeddings)\n', (2159, 2239), False, 'from llama_index import LLMPredictor, ServiceContext, download_loader, GPTVectorStoreIndex, LangchainEmbedding\n'), ((2300, 2378), 'llama_index.GPTVectorStoreIndex.from_documents', 'GPTVectorStoreIndex.from_documents', (['documents'], {'service_context': 'service_context'}), '(documents, service_context=service_context)\n', (2334, 2378), False, 'from llama_index import LLMPredictor, ServiceContext, download_loader, GPTVectorStoreIndex, LangchainEmbedding\n'), ((2536, 2554), 'os.system', 'os.system', (['"""clear"""'], {}), "('clear')\n", (2545, 2554), False, 'import os\n'), ((3418, 3436), 'os.system', 'os.system', (['"""clear"""'], {}), "('clear')\n", (3427, 3436), False, 'import os\n'), ((828, 849), 'os.path.exists', 'os.path.exists', (['FILES'], {}), '(FILES)\n', (842, 849), False, 'import os\n'), ((859, 874), 'os.mkdir', 'os.mkdir', (['FILES'], {}), '(FILES)\n', (867, 874), False, 'import os\n'), ((1958, 2052), 'langchain.embeddings.octoai_embeddings.OctoAIEmbeddings', 'OctoAIEmbeddings', ([], {'endpoint_url': '"""https://instructor-large-f1kzsig6xes9.octoai.run/predict"""'}), "(endpoint_url=\n    'https://instructor-large-f1kzsig6xes9.octoai.run/predict')\n", (1974, 2052), False, 'from langchain.embeddings.octoai_embeddings import OctoAIEmbeddings\n'), ((2674, 2702), 'termios.tcflush', 'tcflush', (['sys.stdin', 'TCIFLUSH'], {}), '(sys.stdin, TCIFLUSH)\n', (2681, 2702), False, 'from termios import tcflush, TCIFLUSH\n'), ((1275, 1285), 'pathlib.Path', 'Path', (['file'], {}), '(file)\n', (1279, 1285), False, 'from pathlib import Path\n'), ((2909, 2920), 'time.time', 'time.time', ([], {}), '()\n', (2918, 2920), False, 'import time\n'), ((2994, 3005), 'time.time', 'time.time', ([], {}), '()\n', (3003, 3005), False, 'import time\n'), ((3467, 3484), 'os.listdir', 'os.listdir', (['FILES'], {}), '(FILES)\n', (3477, 3484), False, 'import os\n'), ((3559, 3585), 'os.path.exists', 'os.path.exists', (['"""file.pdf"""'], {}), "('file.pdf')\n", (3573, 3585), False, 'import os\n'), ((4023, 4064), 'os.path.join', 'os.path.join', (['FILES', 'files[selection - 1]'], {}), '(FILES, files[selection - 1])\n', (4035, 4064), False, 'import os\n')]