code: import uvicorn
  import os
  import logging
  import llama_index
  from typing import cast
  from pathlib import Path
  from fastapi.middleware.cors import CORSMiddleware
  from fastapi import FastAPI
  from dotenv import load_dotenv
  from contextlib import asynccontextmanager
  from firebase_admin import credentials, initialize_app
  
  from app.db.pg_vector import CustomPGVectorStore, get_vector_store_singleton
  from app.db.wait_for_db import check_database_connection
  from app.api.api import api_router
  from app.setup.service_context import initialize_llamaindex_service_context
  from app.setup.tracing import initialize_tracing_service
  
  load_dotenv()
  
  cwd = Path.cwd()
  
  # Default to 'development' if not set
  environment = os.getenv("ENVIRONMENT", "dev")
  
  
  @asynccontextmanager
  async def lifespan(app: FastAPI):
      # First wait for DB to be connectable.
      await check_database_connection()
  
      # Initialize pg vector store singleton.
      vector_store = await get_vector_store_singleton()
      vector_store = cast(CustomPGVectorStore, vector_store)
      await vector_store.run_setup()
  
      # Initialize firebase admin for authentication.
      cred = credentials.Certificate(cwd / 'firebase_creds.json')
      initialize_app(cred)
  
      # if environment == "dev":
      #     # Initialize observability service.
      #     initialize_tracing_service("wandb", "talking-resume")
  
      # Set global ServiceContext for LlamaIndex.
      initialize_llamaindex_service_context(environment)
  
      yield
  
      # This section is run on app shutdown.
      await vector_store.close()
  
  app = FastAPI(lifespan=lifespan)
  
  if environment == "dev":
      # LLM debug.
      llama_index.set_global_handler("simple")
  
      logger = logging.getLogger("uvicorn")
      logger.warning(
          "Running in development mode - allowing CORS for all origins")
      app.add_middleware(
          CORSMiddleware,
          allow_origins=["*"],
          allow_credentials=True,
          allow_methods=["*"],
          allow_headers=["*"],
      )
  
  app.include_router(api_router, prefix="/api")
  
  
  if __name__ == "__main__":
      uvicorn.run(app="main:app", host="0.0.0.0", reload=True)
  
apis: llama_index.set_global_handler
extract_api: [((620, 633), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (631, 633), False, 'from dotenv import load_dotenv\n'), ((641, 651), 'pathlib.Path.cwd', 'Path.cwd', ([], {}), '()\n', (649, 651), False, 'from pathlib import Path\n'), ((705, 736), 'os.getenv', 'os.getenv', (['"""ENVIRONMENT"""', '"""dev"""'], {}), "('ENVIRONMENT', 'dev')\n", (714, 736), False, 'import os\n'), ((1549, 1575), 'fastapi.FastAPI', 'FastAPI', ([], {'lifespan': 'lifespan'}), '(lifespan=lifespan)\n', (1556, 1575), False, 'from fastapi import FastAPI\n'), ((993, 1032), 'typing.cast', 'cast', (['CustomPGVectorStore', 'vector_store'], {}), '(CustomPGVectorStore, vector_store)\n', (997, 1032), False, 'from typing import cast\n'), ((1132, 1184), 'firebase_admin.credentials.Certificate', 'credentials.Certificate', (["(cwd / 'firebase_creds.json')"], {}), "(cwd / 'firebase_creds.json')\n", (1155, 1184), False, 'from firebase_admin import credentials, initialize_app\n'), ((1189, 1209), 'firebase_admin.initialize_app', 'initialize_app', (['cred'], {}), '(cred)\n', (1203, 1209), False, 'from firebase_admin import credentials, initialize_app\n'), ((1405, 1455), 'app.setup.service_context.initialize_llamaindex_service_context', 'initialize_llamaindex_service_context', (['environment'], {}), '(environment)\n', (1442, 1455), False, 'from app.setup.service_context import initialize_llamaindex_service_context\n'), ((1623, 1663), 'llama_index.set_global_handler', 'llama_index.set_global_handler', (['"""simple"""'], {}), "('simple')\n", (1653, 1663), False, 'import llama_index\n'), ((1678, 1706), 'logging.getLogger', 'logging.getLogger', (['"""uvicorn"""'], {}), "('uvicorn')\n", (1695, 1706), False, 'import logging\n'), ((2051, 2107), 'uvicorn.run', 'uvicorn.run', ([], {'app': '"""main:app"""', 'host': '"""0.0.0.0"""', 'reload': '(True)'}), "(app='main:app', host='0.0.0.0', reload=True)\n", (2062, 2107), False, 'import uvicorn\n'), ((847, 874), 'app.db.wait_for_db.check_database_connection', 'check_database_connection', ([], {}), '()\n', (872, 874), False, 'from app.db.wait_for_db import check_database_connection\n'), ((945, 973), 'app.db.pg_vector.get_vector_store_singleton', 'get_vector_store_singleton', ([], {}), '()\n', (971, 973), False, 'from app.db.pg_vector import CustomPGVectorStore, get_vector_store_singleton\n')]