classAnswerRelevancyEvaluator(BaseEvaluator):""" Answer relevancy evaluator. Evaluates the relevancy of response to a query. This evaluator considers the query string and response string. Args: raise_error(Optional[bool]): Whether to raise an error if the response is invalid. Defaults to False. eval_template(Optional[Union[str, BasePromptTemplate]]): The template to use for evaluation. refine_template(Optional[Union[str, BasePromptTemplate]]): The template to use for refinement. """def__init__(self,llm:Optional[LLM]=None,raise_error:bool=False,eval_template:str|BasePromptTemplate|None=None,score_threshold:float=_DEFAULT_SCORE_THRESHOLD,parser_function:Callable[[str],Tuple[Optional[float],Optional[str]]]=_default_parser_function,)->None:"""Init params."""self._llm=llmorSettings.llmself._raise_error=raise_errorself._eval_template:BasePromptTemplateifisinstance(eval_template,str):self._eval_template=PromptTemplate(eval_template)else:self._eval_template=eval_templateorDEFAULT_EVAL_TEMPLATEself.parser_function=parser_functionself.score_threshold=score_thresholddef_get_prompts(self)->PromptDictType:"""Get prompts."""return{"eval_template":self._eval_template,"refine_template":self._refine_template,}def_update_prompts(self,prompts:PromptDictType)->None:"""Update prompts."""if"eval_template"inprompts:self._eval_template=prompts["eval_template"]if"refine_template"inprompts:self._refine_template=prompts["refine_template"]asyncdefaevaluate(self,query:str|None=None,response:str|None=None,contexts:Sequence[str]|None=None,sleep_time_in_seconds:int=0,**kwargs:Any,)->EvaluationResult:"""Evaluate whether the response is relevant to the query."""delkwargs# Unuseddelcontexts# UnusedifqueryisNoneorresponseisNone:raiseValueError("query and response must be provided")awaitasyncio.sleep(sleep_time_in_seconds)eval_response=awaitself._llm.apredict(prompt=self._eval_template,query=query,response=response,)score,reasoning=self.parser_function(eval_response)invalid_result,invalid_reason=False,NoneifscoreisNoneandreasoningisNone:ifself._raise_error:raiseValueError("The response is invalid")invalid_result=Trueinvalid_reason="Unable to parse the output string."ifscore:score/=self.score_thresholdreturnEvaluationResult(query=query,response=response,score=score,feedback=eval_response,invalid_result=invalid_result,invalid_reason=invalid_reason,)
asyncdefaevaluate(self,query:str|None=None,response:str|None=None,contexts:Sequence[str]|None=None,sleep_time_in_seconds:int=0,**kwargs:Any,)->EvaluationResult:"""Evaluate whether the response is relevant to the query."""delkwargs# Unuseddelcontexts# UnusedifqueryisNoneorresponseisNone:raiseValueError("query and response must be provided")awaitasyncio.sleep(sleep_time_in_seconds)eval_response=awaitself._llm.apredict(prompt=self._eval_template,query=query,response=response,)score,reasoning=self.parser_function(eval_response)invalid_result,invalid_reason=False,NoneifscoreisNoneandreasoningisNone:ifself._raise_error:raiseValueError("The response is invalid")invalid_result=Trueinvalid_reason="Unable to parse the output string."ifscore:score/=self.score_thresholdreturnEvaluationResult(query=query,response=response,score=score,feedback=eval_response,invalid_result=invalid_result,invalid_reason=invalid_reason,)