code: """Utilities for Spider module."""
  
  import json
  import os
  from typing import Dict, Tuple
  
  from llama_index import LLMPredictor, SQLDatabase
  from llama_index.indices import SQLStructStoreIndex
  from llama_index.llms.openai import OpenAI
  from sqlalchemy import create_engine, text
  
  
  def load_examples(spider_dir: str) -> Tuple[list, list]:
      """Load examples."""
      with open(os.path.join(spider_dir, "train_spider.json")) as f:
          train_spider = json.load(f)
      with open(os.path.join(spider_dir, "train_others.json")) as f:
          train_others = json.load(f)
      with open(os.path.join(spider_dir, "dev.json")) as f:
          dev = json.load(f)
      return train_spider + train_others, dev
  
  
  def create_indexes(spider_dir: str, llm: OpenAI) -> Dict[str, SQLStructStoreIndex]:
      """Create indexes for all databases."""
      # Create all necessary SQL database objects.
      databases = {}
      for db_name in os.listdir(os.path.join(spider_dir, "database")):
          db_path = os.path.join(spider_dir, "database", db_name, db_name + ".sqlite")
          if not os.path.exists(db_path):
              continue
          engine = create_engine("sqlite:///" + db_path)
          databases[db_name] = SQLDatabase(engine=engine)
          # Test connection.
          with engine.connect() as connection:
              connection.execute(
                  text("select name from sqlite_master where type = 'table'")
              ).fetchone()
  
      llm_predictor = LLMPredictor(llm=llm)
      llm_indexes = {}
      for db_name, db in databases.items():
          llm_indexes[db_name] = SQLStructStoreIndex(
              llm_predictor=llm_predictor,
              sql_database=db,
          )
      return llm_indexes
  
apis: llama_index.indices.SQLStructStoreIndex llama_index.SQLDatabase llama_index.LLMPredictor
extract_api: [((1447, 1468), 'llama_index.LLMPredictor', 'LLMPredictor', ([], {'llm': 'llm'}), '(llm=llm)\n', (1459, 1468), False, 'from llama_index import LLMPredictor, SQLDatabase\n'), ((452, 464), 'json.load', 'json.load', (['f'], {}), '(f)\n', (461, 464), False, 'import json\n'), ((555, 567), 'json.load', 'json.load', (['f'], {}), '(f)\n', (564, 567), False, 'import json\n'), ((640, 652), 'json.load', 'json.load', (['f'], {}), '(f)\n', (649, 652), False, 'import json\n'), ((925, 961), 'os.path.join', 'os.path.join', (['spider_dir', '"""database"""'], {}), "(spider_dir, 'database')\n", (937, 961), False, 'import os\n'), ((982, 1048), 'os.path.join', 'os.path.join', (['spider_dir', '"""database"""', 'db_name', "(db_name + '.sqlite')"], {}), "(spider_dir, 'database', db_name, db_name + '.sqlite')\n", (994, 1048), False, 'import os\n'), ((1127, 1164), 'sqlalchemy.create_engine', 'create_engine', (["('sqlite:///' + db_path)"], {}), "('sqlite:///' + db_path)\n", (1140, 1164), False, 'from sqlalchemy import create_engine, text\n'), ((1194, 1220), 'llama_index.SQLDatabase', 'SQLDatabase', ([], {'engine': 'engine'}), '(engine=engine)\n', (1205, 1220), False, 'from llama_index import LLMPredictor, SQLDatabase\n'), ((1563, 1628), 'llama_index.indices.SQLStructStoreIndex', 'SQLStructStoreIndex', ([], {'llm_predictor': 'llm_predictor', 'sql_database': 'db'}), '(llm_predictor=llm_predictor, sql_database=db)\n', (1582, 1628), False, 'from llama_index.indices import SQLStructStoreIndex\n'), ((376, 421), 'os.path.join', 'os.path.join', (['spider_dir', '"""train_spider.json"""'], {}), "(spider_dir, 'train_spider.json')\n", (388, 421), False, 'import os\n'), ((479, 524), 'os.path.join', 'os.path.join', (['spider_dir', '"""train_others.json"""'], {}), "(spider_dir, 'train_others.json')\n", (491, 524), False, 'import os\n'), ((582, 618), 'os.path.join', 'os.path.join', (['spider_dir', '"""dev.json"""'], {}), "(spider_dir, 'dev.json')\n", (594, 618), False, 'import os\n'), ((1064, 1087), 'os.path.exists', 'os.path.exists', (['db_path'], {}), '(db_path)\n', (1078, 1087), False, 'import os\n'), ((1341, 1400), 'sqlalchemy.text', 'text', (['"""select name from sqlite_master where type = \'table\'"""'], {}), '("select name from sqlite_master where type = \'table\'")\n', (1345, 1400), False, 'from sqlalchemy import create_engine, text\n')]