code: import os
  import uvicorn
  import asyncio
  from fastapi import FastAPI
  from fastapi.responses import StreamingResponse
  from pydantic import BaseModel
  from llama_index import load_index_from_storage, StorageContext, ServiceContext, LLMPredictor, StorageContext
  from fastapi.middleware.cors import CORSMiddleware
  from langchain.chat_models import ChatOpenAI
  
  llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, streaming=True)
  llm_predictor = LLMPredictor(llm=llm)
  service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
  
  app = FastAPI()
  app.add_middleware(
      CORSMiddleware,
      allow_origins=["*"],
      allow_credentials=True,
      allow_methods=["*"],
      allow_headers=["*"],
  )
  
  
  class QueryModel(BaseModel):
      index: str
      query: str
  
  
  async def astreamer(generator):
      try:
          for i in generator:
              yield (i)
              await asyncio.sleep(.1)
      except asyncio.CancelledError as e:
          print('cancelled')
  
  
  @app.post("/generate")
  async def query_index(query_model: QueryModel):
      storage_context = StorageContext.from_defaults(persist_dir=os.path.dirname(__file__)+'/3-index/' + query_model.index)
      index = load_index_from_storage(storage_context, service_context=service_context)
  
      query_engine = index.as_query_engine(streaming=True, similarity_top_k=1)
      response = query_engine.query(query_model.query)
      return StreamingResponse(astreamer(response.response_gen), media_type="text/event-stream")
  
  
  def start():
      uvicorn.run("server:app", host="0.0.0.0", port=8000)
  
  
  if __name__ == "__main__":
      start()
  
apis: llama_index.ServiceContext.from_defaults llama_index.load_index_from_storage llama_index.LLMPredictor
extract_api: [((360, 429), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {'model_name': '"""gpt-3.5-turbo"""', 'temperature': '(0)', 'streaming': '(True)'}), "(model_name='gpt-3.5-turbo', temperature=0, streaming=True)\n", (370, 429), False, 'from langchain.chat_models import ChatOpenAI\n'), ((446, 467), 'llama_index.LLMPredictor', 'LLMPredictor', ([], {'llm': 'llm'}), '(llm=llm)\n', (458, 467), False, 'from llama_index import load_index_from_storage, StorageContext, ServiceContext, LLMPredictor, StorageContext\n'), ((486, 543), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm_predictor': 'llm_predictor'}), '(llm_predictor=llm_predictor)\n', (514, 543), False, 'from llama_index import load_index_from_storage, StorageContext, ServiceContext, LLMPredictor, StorageContext\n'), ((551, 560), 'fastapi.FastAPI', 'FastAPI', ([], {}), '()\n', (558, 560), False, 'from fastapi import FastAPI\n'), ((1170, 1243), 'llama_index.load_index_from_storage', 'load_index_from_storage', (['storage_context'], {'service_context': 'service_context'}), '(storage_context, service_context=service_context)\n', (1193, 1243), False, 'from llama_index import load_index_from_storage, StorageContext, ServiceContext, LLMPredictor, StorageContext\n'), ((1489, 1541), 'uvicorn.run', 'uvicorn.run', (['"""server:app"""'], {'host': '"""0.0.0.0"""', 'port': '(8000)'}), "('server:app', host='0.0.0.0', port=8000)\n", (1500, 1541), False, 'import uvicorn\n'), ((878, 896), 'asyncio.sleep', 'asyncio.sleep', (['(0.1)'], {}), '(0.1)\n', (891, 896), False, 'import asyncio\n'), ((1099, 1124), 'os.path.dirname', 'os.path.dirname', (['__file__'], {}), '(__file__)\n', (1114, 1124), False, 'import os\n')]