code: import os
  from configparser import ConfigParser, SectionProxy
  from typing import Any, Type
  
  from llama_index import (
      LLMPredictor,
      ServiceContext,
      VectorStoreIndex,
  )
  from llama_index.embeddings.base import BaseEmbedding
  from llama_index.embeddings.openai import OpenAIEmbedding
  from llama_index.indices import SimpleKeywordTableIndex
  from llama_index.indices.base import BaseIndex
  from llama_index.indices.loading import load_index_from_storage
  from llama_index.llm_predictor import StructuredLLMPredictor
  from llama_index.llms.llm import LLM
  from llama_index.llms.openai import OpenAI
  from llama_index.storage.storage_context import StorageContext
  
  CONFIG_FILE_NAME = "config.ini"
  DEFAULT_PERSIST_DIR = "./storage"
  DEFAULT_CONFIG = {
      "store": {"persist_dir": DEFAULT_PERSIST_DIR},
      "index": {"type": "default"},
      "embed_model": {"type": "default"},
      "llm_predictor": {"type": "default"},
  }
  
  
  def load_config(root: str = ".") -> ConfigParser:
      """Load configuration from file."""
      config = ConfigParser()
      config.read_dict(DEFAULT_CONFIG)
      config.read(os.path.join(root, CONFIG_FILE_NAME))
      return config
  
  
  def save_config(config: ConfigParser, root: str = ".") -> None:
      """Load configuration to file."""
      with open(os.path.join(root, CONFIG_FILE_NAME), "w") as fd:
          config.write(fd)
  
  
  def load_index(root: str = ".") -> BaseIndex[Any]:
      """Load existing index file."""
      config = load_config(root)
      service_context = _load_service_context(config)
  
      # Index type
      index_type: Type
      if config["index"]["type"] == "default" or config["index"]["type"] == "vector":
          index_type = VectorStoreIndex
      elif config["index"]["type"] == "keyword":
          index_type = SimpleKeywordTableIndex
      else:
          raise KeyError(f"Unknown index.type {config['index']['type']}")
  
      try:
          # try loading index
          storage_context = _load_storage_context(config)
          index = load_index_from_storage(storage_context)
      except ValueError:
          # build index
          storage_context = StorageContext.from_defaults()
          index = index_type(
              nodes=[], service_context=service_context, storage_context=storage_context
          )
      return index
  
  
  def save_index(index: BaseIndex[Any], root: str = ".") -> None:
      """Save index to file."""
      config = load_config(root)
      persist_dir = config["store"]["persist_dir"]
      index.storage_context.persist(persist_dir=persist_dir)
  
  
  def _load_service_context(config: ConfigParser) -> ServiceContext:
      """Internal function to load service context based on configuration."""
      embed_model = _load_embed_model(config)
      llm_predictor = _load_llm_predictor(config)
      return ServiceContext.from_defaults(
          llm_predictor=llm_predictor, embed_model=embed_model
      )
  
  
  def _load_storage_context(config: ConfigParser) -> StorageContext:
      persist_dir = config["store"]["persist_dir"]
      return StorageContext.from_defaults(persist_dir=persist_dir)
  
  
  def _load_llm_predictor(config: ConfigParser) -> LLMPredictor:
      """Internal function to load LLM predictor based on configuration."""
      model_type = config["llm_predictor"]["type"].lower()
      if model_type == "default":
          llm = _load_llm(config["llm_predictor"])
          return LLMPredictor(llm=llm)
      elif model_type == "structured":
          llm = _load_llm(config["llm_predictor"])
          return StructuredLLMPredictor(llm=llm)
      else:
          raise KeyError("llm_predictor.type")
  
  
  def _load_llm(section: SectionProxy) -> LLM:
      if "engine" in section:
          return OpenAI(engine=section["engine"])
      else:
          return OpenAI()
  
  
  def _load_embed_model(config: ConfigParser) -> BaseEmbedding:
      """Internal function to load embedding model based on configuration."""
      model_type = config["embed_model"]["type"]
      if model_type == "default":
          return OpenAIEmbedding()
      else:
          raise KeyError("embed_model.type")
  
apis: llama_index.LLMPredictor llama_index.ServiceContext.from_defaults llama_index.storage.storage_context.StorageContext.from_defaults llama_index.llm_predictor.StructuredLLMPredictor llama_index.indices.loading.load_index_from_storage llama_index.llms.openai.OpenAI llama_index.embeddings.openai.OpenAIEmbedding
extract_api: [((1023, 1037), 'configparser.ConfigParser', 'ConfigParser', ([], {}), '()\n', (1035, 1037), False, 'from configparser import ConfigParser, SectionProxy\n'), ((2725, 2812), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm_predictor': 'llm_predictor', 'embed_model': 'embed_model'}), '(llm_predictor=llm_predictor, embed_model=\n    embed_model)\n', (2753, 2812), False, 'from llama_index import LLMPredictor, ServiceContext, VectorStoreIndex\n'), ((2951, 3004), 'llama_index.storage.storage_context.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'persist_dir': 'persist_dir'}), '(persist_dir=persist_dir)\n', (2979, 3004), False, 'from llama_index.storage.storage_context import StorageContext\n'), ((1091, 1127), 'os.path.join', 'os.path.join', (['root', 'CONFIG_FILE_NAME'], {}), '(root, CONFIG_FILE_NAME)\n', (1103, 1127), False, 'import os\n'), ((1957, 1997), 'llama_index.indices.loading.load_index_from_storage', 'load_index_from_storage', (['storage_context'], {}), '(storage_context)\n', (1980, 1997), False, 'from llama_index.indices.loading import load_index_from_storage\n'), ((3297, 3318), 'llama_index.LLMPredictor', 'LLMPredictor', ([], {'llm': 'llm'}), '(llm=llm)\n', (3309, 3318), False, 'from llama_index import LLMPredictor, ServiceContext, VectorStoreIndex\n'), ((3597, 3629), 'llama_index.llms.openai.OpenAI', 'OpenAI', ([], {'engine': "section['engine']"}), "(engine=section['engine'])\n", (3603, 3629), False, 'from llama_index.llms.openai import OpenAI\n'), ((3655, 3663), 'llama_index.llms.openai.OpenAI', 'OpenAI', ([], {}), '()\n', (3661, 3663), False, 'from llama_index.llms.openai import OpenAI\n'), ((3898, 3915), 'llama_index.embeddings.openai.OpenAIEmbedding', 'OpenAIEmbedding', ([], {}), '()\n', (3913, 3915), False, 'from llama_index.embeddings.openai import OpenAIEmbedding\n'), ((1265, 1301), 'os.path.join', 'os.path.join', (['root', 'CONFIG_FILE_NAME'], {}), '(root, CONFIG_FILE_NAME)\n', (1277, 1301), False, 'import os\n'), ((2069, 2099), 'llama_index.storage.storage_context.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {}), '()\n', (2097, 2099), False, 'from llama_index.storage.storage_context import StorageContext\n'), ((3420, 3451), 'llama_index.llm_predictor.StructuredLLMPredictor', 'StructuredLLMPredictor', ([], {'llm': 'llm'}), '(llm=llm)\n', (3442, 3451), False, 'from llama_index.llm_predictor import StructuredLLMPredictor\n')]