code: #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  
  '''
  This script allows you to ask questions to the Alice in Wonderland book.
  It uses the GPT-3 model to create a vector index of the book, and then
  allows you to ask questions to the index.
  '''
  
  import os
  import yaml
  import openai
  from llama_index import (
      GPTVectorStoreIndex,
      StorageContext,
      SimpleDirectoryReader,
      download_loader,
      load_index_from_storage
  )
  from llama_index.storage.docstore import SimpleDocumentStore
  from llama_index.vector_stores import SimpleVectorStore
  from llama_index.storage.index_store import SimpleIndexStore
  from argparse import ArgumentParser
  
  # script configuration
  persist_dir = "./indices/alice/"
  pdf_file = "alice.pdf"
  
  openai_config = "projects/infrastructure/charts/secrets/values/integration/openai-configuration/openai.yml"
  credentials_path = os.path.join(os.path.expanduser('~'), openai_config)
  credentials = yaml.safe_load(open(credentials_path, "r"))
  
  os.environ["OPENAI_API_KEY"] = credentials["access_token"]
  os.environ["OPENAI_ORGANIZATION"] = credentials["organization_id"]
  
  
  # Save the index in .JSON file for repeated use. Saves money on ADA API calls
  def create_index_from_pdf(persist_dir):
      # This example uses PDF reader, there are many options at https://llamahub.ai/
      # Use SimpleDirectoryReader to read all the txt files in a folder
      PDFReader = download_loader("PDFReader")
      loader = PDFReader()
      documents = loader.load_data(file=pdf_file)
  
      # Chunking and Embedding of the chunks.
      index = GPTVectorStoreIndex.from_documents(documents)
      index.storage_context.persist(persist_dir=persist_dir)
      return index
  
  def load_index(persist_dir):
      storage_context = StorageContext.from_defaults(
          docstore=SimpleDocumentStore.from_persist_dir(persist_dir=persist_dir),
          vector_store=SimpleVectorStore.from_persist_dir(persist_dir=persist_dir),
          index_store=SimpleIndexStore.from_persist_dir(persist_dir=persist_dir),
      )
  
      index = load_index_from_storage(storage_context)
      return index
  
  
  def main(args):
      print(args.question)
  
      if args.create_index:
          index = create_index_from_pdf(persist_dir)
      else:
          index = load_index(persist_dir)
  
      # Retrieval, node poseprocessing, response synthesis. 
      query_engine = index.as_query_engine()
  
      # Run the query engine on a user question.
      response = query_engine.query(args.question)
      print(response)
  
  
  if __name__ == '__main__':
      parser = ArgumentParser(description=__doc__, prog='alice.py', epilog='Have fun!')
      parser.add_argument('-c', '--create-index', help='(re)create the index', action='store_true')
      parser.add_argument('question', help='question string to ask the index')
      args = parser.parse_args()
      main(args)
  
  
apis: llama_index.storage.docstore.SimpleDocumentStore.from_persist_dir llama_index.GPTVectorStoreIndex.from_documents llama_index.storage.index_store.SimpleIndexStore.from_persist_dir llama_index.download_loader llama_index.load_index_from_storage llama_index.vector_stores.SimpleVectorStore.from_persist_dir
extract_api: [((862, 885), 'os.path.expanduser', 'os.path.expanduser', (['"""~"""'], {}), "('~')\n", (880, 885), False, 'import os\n'), ((1376, 1404), 'llama_index.download_loader', 'download_loader', (['"""PDFReader"""'], {}), "('PDFReader')\n", (1391, 1404), False, 'from llama_index import GPTVectorStoreIndex, StorageContext, SimpleDirectoryReader, download_loader, load_index_from_storage\n'), ((1535, 1580), 'llama_index.GPTVectorStoreIndex.from_documents', 'GPTVectorStoreIndex.from_documents', (['documents'], {}), '(documents)\n', (1569, 1580), False, 'from llama_index import GPTVectorStoreIndex, StorageContext, SimpleDirectoryReader, download_loader, load_index_from_storage\n'), ((2000, 2040), 'llama_index.load_index_from_storage', 'load_index_from_storage', (['storage_context'], {}), '(storage_context)\n', (2023, 2040), False, 'from llama_index import GPTVectorStoreIndex, StorageContext, SimpleDirectoryReader, download_loader, load_index_from_storage\n'), ((2491, 2563), 'argparse.ArgumentParser', 'ArgumentParser', ([], {'description': '__doc__', 'prog': '"""alice.py"""', 'epilog': '"""Have fun!"""'}), "(description=__doc__, prog='alice.py', epilog='Have fun!')\n", (2505, 2563), False, 'from argparse import ArgumentParser\n'), ((1756, 1817), 'llama_index.storage.docstore.SimpleDocumentStore.from_persist_dir', 'SimpleDocumentStore.from_persist_dir', ([], {'persist_dir': 'persist_dir'}), '(persist_dir=persist_dir)\n', (1792, 1817), False, 'from llama_index.storage.docstore import SimpleDocumentStore\n'), ((1840, 1899), 'llama_index.vector_stores.SimpleVectorStore.from_persist_dir', 'SimpleVectorStore.from_persist_dir', ([], {'persist_dir': 'persist_dir'}), '(persist_dir=persist_dir)\n', (1874, 1899), False, 'from llama_index.vector_stores import SimpleVectorStore\n'), ((1921, 1979), 'llama_index.storage.index_store.SimpleIndexStore.from_persist_dir', 'SimpleIndexStore.from_persist_dir', ([], {'persist_dir': 'persist_dir'}), '(persist_dir=persist_dir)\n', (1954, 1979), False, 'from llama_index.storage.index_store import SimpleIndexStore\n')]