code: import os
  import openai
  from fastapi import FastAPI, HTTPException
  from llama_index import StorageContext, load_index_from_storage, ServiceContext, set_global_service_context
  from llama_index.indices.postprocessor import SentenceEmbeddingOptimizer
  from llama_index.embeddings import OpenAIEmbedding
  
  from pydantic import BaseModel
  
  openai.api_key = os.environ["OPENAI_API_KEY"]
  
  app = FastAPI()
  
  class QueryRequest(BaseModel):
      question: str
  
  class QueryResponse(BaseModel):
      answer: str
  
  
  embed_model = OpenAIEmbedding(embed_batch_size=10)
  service_context = ServiceContext.from_defaults(embed_model=embed_model)
  set_global_service_context(service_context)
  
  storage_context = StorageContext.from_defaults(persist_dir="./storage")
  index = load_index_from_storage(storage_context)
  query_engine = index.as_query_engine(
      node_postprocessors=[SentenceEmbeddingOptimizer(percentile_cutoff=0.5)],    
      response_mode="compact",
      similarity_cutoff=0.7
  )
  
  @app.get("/")
  def read_root():
      return {"Hello": "World"}
  
  @app.post("/chat")
  def query_data(request: QueryRequest):
      response = query_engine.query(request.question)
      if not response:
          raise HTTPException(status_code=404, detail="No results found")
  
      return QueryResponse(answer=str(response))
  
apis: llama_index.ServiceContext.from_defaults llama_index.StorageContext.from_defaults llama_index.load_index_from_storage llama_index.indices.postprocessor.SentenceEmbeddingOptimizer llama_index.set_global_service_context llama_index.embeddings.OpenAIEmbedding
extract_api: [((385, 394), 'fastapi.FastAPI', 'FastAPI', ([], {}), '()\n', (392, 394), False, 'from fastapi import FastAPI, HTTPException\n'), ((510, 546), 'llama_index.embeddings.OpenAIEmbedding', 'OpenAIEmbedding', ([], {'embed_batch_size': '(10)'}), '(embed_batch_size=10)\n', (525, 546), False, 'from llama_index.embeddings import OpenAIEmbedding\n'), ((565, 618), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'embed_model': 'embed_model'}), '(embed_model=embed_model)\n', (593, 618), False, 'from llama_index import StorageContext, load_index_from_storage, ServiceContext, set_global_service_context\n'), ((619, 662), 'llama_index.set_global_service_context', 'set_global_service_context', (['service_context'], {}), '(service_context)\n', (645, 662), False, 'from llama_index import StorageContext, load_index_from_storage, ServiceContext, set_global_service_context\n'), ((682, 735), 'llama_index.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'persist_dir': '"""./storage"""'}), "(persist_dir='./storage')\n", (710, 735), False, 'from llama_index import StorageContext, load_index_from_storage, ServiceContext, set_global_service_context\n'), ((744, 784), 'llama_index.load_index_from_storage', 'load_index_from_storage', (['storage_context'], {}), '(storage_context)\n', (767, 784), False, 'from llama_index import StorageContext, load_index_from_storage, ServiceContext, set_global_service_context\n'), ((1169, 1226), 'fastapi.HTTPException', 'HTTPException', ([], {'status_code': '(404)', 'detail': '"""No results found"""'}), "(status_code=404, detail='No results found')\n", (1182, 1226), False, 'from fastapi import FastAPI, HTTPException\n'), ((848, 897), 'llama_index.indices.postprocessor.SentenceEmbeddingOptimizer', 'SentenceEmbeddingOptimizer', ([], {'percentile_cutoff': '(0.5)'}), '(percentile_cutoff=0.5)\n', (874, 897), False, 'from llama_index.indices.postprocessor import SentenceEmbeddingOptimizer\n')]