-
Notifications
You must be signed in to change notification settings - Fork 21
refactor(SimplifyEvaluatorCreation): simplify evaluator creation #1245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+21
−152
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,6 +74,20 @@ | |
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| EVALUATOR_SCHEMA_TO_EVALUATOR_CLASS = { | ||
| ContainsEvaluatorConfig: ContainsEvaluator, | ||
| ExactMatchEvaluatorConfig: ExactMatchEvaluator, | ||
| JsonSimilarityEvaluatorConfig: JsonSimilarityEvaluator, | ||
| LLMJudgeOutputEvaluatorConfig: LLMJudgeOutputEvaluator, | ||
| LLMJudgeStrictJSONSimilarityOutputEvaluatorConfig: LLMJudgeStrictJSONSimilarityOutputEvaluator, | ||
| LLMJudgeTrajectoryEvaluatorConfig: LLMJudgeTrajectoryEvaluator, | ||
| LLMJudgeTrajectorySimulationEvaluatorConfig: LLMJudgeTrajectorySimulationEvaluator, | ||
| ToolCallArgsEvaluatorConfig: ToolCallArgsEvaluator, | ||
| ToolCallCountEvaluatorConfig: ToolCallCountEvaluator, | ||
| ToolCallOrderEvaluatorConfig: ToolCallOrderEvaluator, | ||
| ToolCallOutputEvaluatorConfig: ToolCallOutputEvaluator, | ||
| } | ||
|
|
||
|
|
||
| class EvaluatorFactory: | ||
| """Factory class for creating evaluator instances based on configuration.""" | ||
|
|
@@ -137,50 +151,15 @@ def _create_evaluator_internal( | |
| data, file_path, class_name, evaluators_dir | ||
| ) | ||
|
|
||
| # use built-in evaluators | ||
| config: BaseEvaluatorConfig[Any] = TypeAdapter(EvaluatorConfig).validate_python( | ||
| data | ||
| ) | ||
| match config: | ||
| case ContainsEvaluatorConfig(): | ||
| return EvaluatorFactory._create_contains_evaluator(data) | ||
| case ExactMatchEvaluatorConfig(): | ||
| return EvaluatorFactory._create_exact_match_evaluator(data) | ||
| case JsonSimilarityEvaluatorConfig(): | ||
| return EvaluatorFactory._create_json_similarity_evaluator(data) | ||
| case LLMJudgeOutputEvaluatorConfig(): | ||
| return EvaluatorFactory._create_llm_judge_output_evaluator(data) | ||
| case LLMJudgeStrictJSONSimilarityOutputEvaluatorConfig(): | ||
| return EvaluatorFactory._create_llm_judge_strict_json_similarity_output_evaluator( | ||
| data | ||
| ) | ||
| case LLMJudgeTrajectoryEvaluatorConfig(): | ||
| return EvaluatorFactory._create_trajectory_evaluator(data) | ||
| case ToolCallArgsEvaluatorConfig(): | ||
| return EvaluatorFactory._create_tool_call_args_evaluator(data) | ||
| case ToolCallCountEvaluatorConfig(): | ||
| return EvaluatorFactory._create_tool_call_count_evaluator(data) | ||
| case ToolCallOrderEvaluatorConfig(): | ||
| return EvaluatorFactory._create_tool_call_order_evaluator(data) | ||
| case ToolCallOutputEvaluatorConfig(): | ||
| return EvaluatorFactory._create_tool_call_output_evaluator(data) | ||
| case LLMJudgeTrajectorySimulationEvaluatorConfig(): | ||
| return ( | ||
| EvaluatorFactory._create_llm_judge_simulation_trajectory_evaluator( | ||
| data | ||
| ) | ||
| ) | ||
| case _: | ||
| raise ValueError(f"Unknown evaluator configuration: {config}") | ||
|
|
||
| @staticmethod | ||
| def _create_contains_evaluator(data: dict[str, Any]) -> ContainsEvaluator: | ||
| evaluator_id = data.get("id") | ||
| if not evaluator_id or not isinstance(evaluator_id, str): | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is redundant as evaluators have "id" as a required str field |
||
| raise ValueError("Evaluator 'id' must be a non-empty string") | ||
| return TypeAdapter(ContainsEvaluator).validate_python( | ||
| evaluator_class = EVALUATOR_SCHEMA_TO_EVALUATOR_CLASS.get(type(config)) | ||
| if not evaluator_class: | ||
| raise ValueError(f"Unknown evaluator configuration: {config}") | ||
| return TypeAdapter(evaluator_class).validate_python( | ||
| { | ||
| "id": evaluator_id, | ||
| "id": data.get("id"), | ||
| "config": EvaluatorFactory._prepare_evaluator_config(data), | ||
| } | ||
| ) | ||
|
|
@@ -270,116 +249,6 @@ def _create_coded_evaluator_internal( | |
| } | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _create_exact_match_evaluator( | ||
| data: dict[str, Any], | ||
| ) -> ExactMatchEvaluator: | ||
| return TypeAdapter(ExactMatchEvaluator).validate_python( | ||
| { | ||
| "id": data.get("id"), | ||
| "config": EvaluatorFactory._prepare_evaluator_config(data), | ||
| } | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _create_json_similarity_evaluator( | ||
| data: dict[str, Any], | ||
| ) -> JsonSimilarityEvaluator: | ||
| return TypeAdapter(JsonSimilarityEvaluator).validate_python( | ||
| { | ||
| "id": data.get("id"), | ||
| "config": EvaluatorFactory._prepare_evaluator_config(data), | ||
| } | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _create_llm_judge_output_evaluator( | ||
| data: dict[str, Any], | ||
| ) -> LLMJudgeOutputEvaluator: | ||
| return TypeAdapter(LLMJudgeOutputEvaluator).validate_python( | ||
| { | ||
| "id": data.get("id"), | ||
| "config": EvaluatorFactory._prepare_evaluator_config(data), | ||
| } | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _create_llm_judge_strict_json_similarity_output_evaluator( | ||
| data: dict[str, Any], | ||
| ) -> LLMJudgeStrictJSONSimilarityOutputEvaluator: | ||
| return TypeAdapter(LLMJudgeStrictJSONSimilarityOutputEvaluator).validate_python( | ||
| { | ||
| "id": data.get("id"), | ||
| "config": EvaluatorFactory._prepare_evaluator_config(data), | ||
| } | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _create_trajectory_evaluator( | ||
| data: dict[str, Any], | ||
| ) -> LLMJudgeTrajectoryEvaluator: | ||
| return TypeAdapter(LLMJudgeTrajectoryEvaluator).validate_python( | ||
| { | ||
| "id": data.get("id"), | ||
| "config": EvaluatorFactory._prepare_evaluator_config(data), | ||
| } | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _create_tool_call_args_evaluator( | ||
| data: dict[str, Any], | ||
| ) -> ToolCallArgsEvaluator: | ||
| return TypeAdapter(ToolCallArgsEvaluator).validate_python( | ||
| { | ||
| "id": data.get("id"), | ||
| "config": EvaluatorFactory._prepare_evaluator_config(data), | ||
| } | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _create_tool_call_count_evaluator( | ||
| data: dict[str, Any], | ||
| ) -> ToolCallCountEvaluator: | ||
| return TypeAdapter(ToolCallCountEvaluator).validate_python( | ||
| { | ||
| "id": data.get("id"), | ||
| "config": EvaluatorFactory._prepare_evaluator_config(data), | ||
| } | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _create_tool_call_order_evaluator( | ||
| data: dict[str, Any], | ||
| ) -> ToolCallOrderEvaluator: | ||
| return TypeAdapter(ToolCallOrderEvaluator).validate_python( | ||
| { | ||
| "id": data.get("id"), | ||
| "config": EvaluatorFactory._prepare_evaluator_config(data), | ||
| } | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _create_tool_call_output_evaluator( | ||
| data: dict[str, Any], | ||
| ) -> ToolCallOutputEvaluator: | ||
| return TypeAdapter(ToolCallOutputEvaluator).validate_python( | ||
| { | ||
| "id": data.get("id"), | ||
| "config": EvaluatorFactory._prepare_evaluator_config(data), | ||
| } | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _create_llm_judge_simulation_trajectory_evaluator( | ||
| data: dict[str, Any], | ||
| ) -> LLMJudgeTrajectorySimulationEvaluator: | ||
| return TypeAdapter(LLMJudgeTrajectorySimulationEvaluator).validate_python( | ||
| { | ||
| "id": data.get("id"), | ||
| "config": EvaluatorFactory._prepare_evaluator_config(data), | ||
| } | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _create_legacy_evaluator_internal( | ||
| data: dict[str, Any], | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.