[docs]classDeepEvalCallbackHandler(BaseCallbackHandler):"""Callback Handler that logs into deepeval. Args: implementation_name: name of the `implementation` in deepeval metrics: A list of metrics Raises: ImportError: if the `deepeval` package is not installed. Examples: >>> from langchain_community.llms import OpenAI >>> from langchain_community.callbacks import DeepEvalCallbackHandler >>> from deepeval.metrics import AnswerRelevancy >>> metric = AnswerRelevancy(minimum_score=0.3) >>> deepeval_callback = DeepEvalCallbackHandler( ... implementation_name="exampleImplementation", ... metrics=[metric], ... ) >>> llm = OpenAI( ... temperature=0, ... callbacks=[deepeval_callback], ... verbose=True, ... openai_api_key="API_KEY_HERE", ... ) >>> llm.generate([ ... "What is the best evaluation tool out there? (no bias at all)", ... ]) "Deepeval, no doubt about it." """REPO_URL:str="https://github.com/confident-ai/deepeval"ISSUES_URL:str=f"{REPO_URL}/issues"BLOG_URL:str="https://docs.confident-ai.com"# noqa: E501
[docs]def__init__(self,metrics:List[Any],implementation_name:Optional[str]=None,)->None:"""Initializes the `deepevalCallbackHandler`. Args: implementation_name: Name of the implementation you want. metrics: What metrics do you want to track? Raises: ImportError: if the `deepeval` package is not installed. ConnectionError: if the connection to deepeval fails. """super().__init__()# Import deepeval (not via `import_deepeval` to keep hints in IDEs)try:importdeepeval# ignore: F401,I001exceptImportError:raiseImportError("""To use the deepeval callback manager you need to have the `deepeval` Python package installed. Please install it with `pip install deepeval`""")ifos.path.exists(".deepeval"):warnings.warn("""You are currently not logging anything to the dashboard, we recommend using `deepeval login`.""")# Set the deepeval variablesself.implementation_name=implementation_nameself.metrics=metricswarnings.warn(("The `DeepEvalCallbackHandler` is currently in beta and is subject to"" change based on updates to `langchain`. Please report any issues to"f" {self.ISSUES_URL} as an `integration` issue."),)
[docs]defon_llm_start(self,serialized:Dict[str,Any],prompts:List[str],**kwargs:Any)->None:"""Store the prompts"""self.prompts=prompts
[docs]defon_llm_new_token(self,token:str,**kwargs:Any)->None:"""Do nothing when a new token is generated."""pass
[docs]defon_llm_end(self,response:LLMResult,**kwargs:Any)->None:"""Log records to deepeval when an LLM ends."""fromdeepeval.metrics.answer_relevancyimportAnswerRelevancyfromdeepeval.metrics.bias_classifierimportUnBiasedMetricfromdeepeval.metrics.metricimportMetricfromdeepeval.metrics.toxic_classifierimportNonToxicMetricformetricinself.metrics:fori,generationinenumerate(response.generations):# Here, we only measure the first generation's outputoutput=generation[0].textquery=self.prompts[i]ifisinstance(metric,AnswerRelevancy):result=metric.measure(output=output,query=query,)print(f"Answer Relevancy: {result}")# noqa: T201elifisinstance(metric,UnBiasedMetric):score=metric.measure(output)print(f"Bias Score: {score}")# noqa: T201elifisinstance(metric,NonToxicMetric):score=metric.measure(output)print(f"Toxic Score: {score}")# noqa: T201else:raiseValueError(f"""Metric {metric.__name__} is not supported by deepeval callbacks.""")
[docs]defon_llm_error(self,error:BaseException,**kwargs:Any)->None:"""Do nothing when LLM outputs an error."""pass
[docs]defon_chain_start(self,serialized:Dict[str,Any],inputs:Dict[str,Any],**kwargs:Any)->None:"""Do nothing when chain starts"""pass
[docs]defon_chain_end(self,outputs:Dict[str,Any],**kwargs:Any)->None:"""Do nothing when chain ends."""pass
[docs]defon_chain_error(self,error:BaseException,**kwargs:Any)->None:"""Do nothing when LLM chain outputs an error."""pass
[docs]defon_tool_start(self,serialized:Dict[str,Any],input_str:str,**kwargs:Any,)->None:"""Do nothing when tool starts."""pass
[docs]defon_agent_action(self,action:AgentAction,**kwargs:Any)->Any:"""Do nothing when agent takes a specific action."""pass
[docs]defon_tool_end(self,output:Any,observation_prefix:Optional[str]=None,llm_prefix:Optional[str]=None,**kwargs:Any,)->None:"""Do nothing when tool ends."""pass
[docs]defon_tool_error(self,error:BaseException,**kwargs:Any)->None:"""Do nothing when tool outputs an error."""pass