code: from fastapi import FastAPI
  from fastapi.responses import JSONResponse, StreamingResponse
  
  from pydantic import BaseModel
  import os.path
  import llama_index
  
  from llama_index import (
      VectorStoreIndex,
      SimpleDirectoryReader,
      StorageContext,
      ServiceContext,
      load_index_from_storage,
      set_global_service_context,
      PromptTemplate
  )
  from llama_index.embeddings import HuggingFaceEmbedding
  from fastapi.middleware.cors import CORSMiddleware
  
  
  import logging
  import sys
  
  
  # Set global handler for LLaMA index
  llama_index.set_global_handler("simple")
  
  # Initialize FastAPI app
  app = FastAPI()
  
  # Define directory for persisting index
  PERSIST_DIR = "./storage"
  
  # Initialize embedding model
  embed_model = HuggingFaceEmbedding(model_name="OrdalieTech/Solon-embeddings-large-0.1")
  
  # Create service context with embedding model
  service_context = ServiceContext.from_defaults(embed_model=embed_model)
  set_global_service_context(service_context)
  
  # Load or create the index
  if not os.path.exists(PERSIST_DIR):
      documents = SimpleDirectoryReader("data").load_data()
      index = VectorStoreIndex.from_documents(documents)
      index.storage_context.persist(persist_dir=PERSIST_DIR)
  else:
      storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
      index = load_index_from_storage(storage_context)
  
  # Initialize query engine from index
  query_engine = index.as_query_engine(streaming=True, similarity_top_k=2)
  
  # Define custom prompt template
  qa_prompt_tmpl_str = (
      "Context information is below.\n"
      "---------------------\n"
      "{context_str}\n"
      "---------------------\n"
      "Given the context information and not prior knowledge,Some rules to follow: 1. Avoid statements like 'Based on the context, ...' or 'The context information ...' or anything along those lines. "
      "answer the query in french and but remember you are chatbot trained on rh questions so always put that in perspective . you are named Rhym a chatbot created by the innovation team at BMCI  \n"
      "Query: {query_str}\n"
      "Answer: "
  )
  qa_prompt_tmpl = PromptTemplate(qa_prompt_tmpl_str)
  
  # Update query engine with custom prompt template
  query_engine.update_prompts(
      {"response_synthesizer:text_qa_template": qa_prompt_tmpl}
  )
  # Define Pydantic model for query requests
  class Query(BaseModel):
      text: str
  
  @app.get("/query")
  async def query_index(query: str ):
      try:
          response_stream = query_engine.query(query)
  
          async def event_stream():
              for text in response_stream.response_gen:
                  yield f"data: {text}\n\n"
              # Send a special message or marker to indicate the end of the stream
              yield "data: END_OF_STREAM\n\n"
  
          return StreamingResponse(event_stream(), media_type="text/event-stream")
          
      except Exception as e:
          logging.error(f"Error during query processing: {str(e)}")
          return JSONResponse(
              status_code=503,
              content={"message": "LLM API is currently unavailable.", "error": str(e)}
          )
  
  # Add CORS middleware to allow specific origins (or use '*' for all origins)
  origins = [
     
       "*", # Allow all origins
  ]
  
  app.add_middleware(
      CORSMiddleware,
      allow_origins=origins,
      allow_credentials=True,
      allow_methods=["*"],
      allow_headers=["*"],
  )
  
  # ... [rest of your code]
  
  # The main function remains unchanged
  if __name__ == "__main__":
      import uvicorn
      uvicorn.run(app, host="0.0.0.0", port=8000)
  
apis: llama_index.VectorStoreIndex.from_documents llama_index.ServiceContext.from_defaults llama_index.StorageContext.from_defaults llama_index.set_global_handler llama_index.SimpleDirectoryReader llama_index.embeddings.HuggingFaceEmbedding llama_index.load_index_from_storage llama_index.PromptTemplate llama_index.set_global_service_context
extract_api: [((528, 568), 'llama_index.set_global_handler', 'llama_index.set_global_handler', (['"""simple"""'], {}), "('simple')\n", (558, 568), False, 'import llama_index\n'), ((601, 610), 'fastapi.FastAPI', 'FastAPI', ([], {}), '()\n', (608, 610), False, 'from fastapi import FastAPI\n'), ((722, 795), 'llama_index.embeddings.HuggingFaceEmbedding', 'HuggingFaceEmbedding', ([], {'model_name': '"""OrdalieTech/Solon-embeddings-large-0.1"""'}), "(model_name='OrdalieTech/Solon-embeddings-large-0.1')\n", (742, 795), False, 'from llama_index.embeddings import HuggingFaceEmbedding\n'), ((861, 914), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'embed_model': 'embed_model'}), '(embed_model=embed_model)\n', (889, 914), False, 'from llama_index import VectorStoreIndex, SimpleDirectoryReader, StorageContext, ServiceContext, load_index_from_storage, set_global_service_context, PromptTemplate\n'), ((915, 958), 'llama_index.set_global_service_context', 'set_global_service_context', (['service_context'], {}), '(service_context)\n', (941, 958), False, 'from llama_index import VectorStoreIndex, SimpleDirectoryReader, StorageContext, ServiceContext, load_index_from_storage, set_global_service_context, PromptTemplate\n'), ((2076, 2110), 'llama_index.PromptTemplate', 'PromptTemplate', (['qa_prompt_tmpl_str'], {}), '(qa_prompt_tmpl_str)\n', (2090, 2110), False, 'from llama_index import VectorStoreIndex, SimpleDirectoryReader, StorageContext, ServiceContext, load_index_from_storage, set_global_service_context, PromptTemplate\n'), ((1093, 1135), 'llama_index.VectorStoreIndex.from_documents', 'VectorStoreIndex.from_documents', (['documents'], {}), '(documents)\n', (1124, 1135), False, 'from llama_index import VectorStoreIndex, SimpleDirectoryReader, StorageContext, ServiceContext, load_index_from_storage, set_global_service_context, PromptTemplate\n'), ((1223, 1276), 'llama_index.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'persist_dir': 'PERSIST_DIR'}), '(persist_dir=PERSIST_DIR)\n', (1251, 1276), False, 'from llama_index import VectorStoreIndex, SimpleDirectoryReader, StorageContext, ServiceContext, load_index_from_storage, set_global_service_context, PromptTemplate\n'), ((1289, 1329), 'llama_index.load_index_from_storage', 'load_index_from_storage', (['storage_context'], {}), '(storage_context)\n', (1312, 1329), False, 'from llama_index import VectorStoreIndex, SimpleDirectoryReader, StorageContext, ServiceContext, load_index_from_storage, set_global_service_context, PromptTemplate\n'), ((3437, 3480), 'uvicorn.run', 'uvicorn.run', (['app'], {'host': '"""0.0.0.0"""', 'port': '(8000)'}), "(app, host='0.0.0.0', port=8000)\n", (3448, 3480), False, 'import uvicorn\n'), ((1039, 1068), 'llama_index.SimpleDirectoryReader', 'SimpleDirectoryReader', (['"""data"""'], {}), "('data')\n", (1060, 1068), False, 'from llama_index import VectorStoreIndex, SimpleDirectoryReader, StorageContext, ServiceContext, load_index_from_storage, set_global_service_context, PromptTemplate\n')]