Last active
May 5, 2025 04:41
-
-
Save lorenzejay/37d245e3f7b67e37a611e7af3026614b to your computer and use it in GitHub Desktop.
Custom Event Listener Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from crewai.utilities.events import ( | |
| TaskCompletedEvent, | |
| ) | |
| from crewai.utilities.events.base_event_listener import BaseEventListener | |
| from crewai.task import Task | |
| from crewai.utilities.evaluators.task_evaluator import TaskEvaluator | |
| from crewai.utilities.events.crewai_event_bus import CrewAIEventsBus | |
| from opik.evaluation.metrics import Hallucination | |
| class EvalListenerSetup(BaseEventListener): | |
| def __init__(self): | |
| super().__init__() | |
| def setup_listeners(self, crewai_event_bus: CrewAIEventsBus): | |
| @crewai_event_bus.on(TaskCompletedEvent) | |
| def on_task_completed(source: Task, event: TaskCompletedEvent): | |
| if source.name == "conflicts_of_interest_task": | |
| evaluator = TaskEvaluator(source.agent) | |
| evaluation = evaluator.evaluate(source, event.output) | |
| hallucination_eval = Hallucination() | |
| hallucination_result = hallucination_eval.score( | |
| input=source.prompt() + "\n\n" + source.prompt_context, | |
| output=event.output, | |
| ) | |
| print("evaluation", evaluation.quality) | |
| print("evaluation", evaluation.suggestions) | |
| print("------") | |
| print(hallucination_result.value) | |
| print(hallucination_result.reason) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment