defliteralai_callback_handler(batch_size:int=5,api_key:Optional[str]=None,url:Optional[str]=None,environment:Optional[str]=None,disabled:bool=False,)->BaseCallbackHandler:try:fromliteralaiimportLiteralClientfromliteralai.my_typesimportEnvironmentliteralai_client=LiteralClient(batch_size=batch_size,api_key=api_key,url=url,environment=cast(Environment,environment),disabled=disabled,)literalai_client.instrument_llamaindex()classQueryEndEventHandler(BaseEventHandler):"""This handler will flush the Literal Client cache to Literal AI at the end of each query."""@classmethoddefclass_name(cls)->str:"""Class name."""return"QueryEndEventHandler"defhandle(self,event:BaseEvent,**kwargs)->None:"""Flushes the Literal cache when receiving the QueryEnd event."""try:ifisinstance(event,QueryEndEvent):literalai_client.flush()exceptExceptionase:logging.error("Error in Literal AI global handler : %s",str(e),exc_info=True,)dispatcher=get_dispatcher()event_handler=QueryEndEventHandler()dispatcher.add_event_handler(event_handler)exceptImportError:raiseImportError("Please install the Literal AI Python SDK with `pip install -U literalai`")