diff --git a/scripts/data_overlap/common/object_spec.py b/scripts/data_overlap/common/object_spec.py index f4b3f2d6913..353b77ba4cd 100644 --- a/scripts/data_overlap/common/object_spec.py +++ b/scripts/data_overlap/common/object_spec.py @@ -1,4 +1,5 @@ from dataclasses import dataclass +import importlib from typing import Any, Dict, Tuple @@ -18,12 +19,11 @@ def __hash__(self): def create_object(spec: ObjectSpec): """Create the actual object given the `spec`.""" - # Adapted from https://stackoverflow.com/questions/547829/how-to-dynamically-load-a-python-class components = spec.class_name.split(".") - module: Any = __import__(components[0]) - for component in components[1:]: - module = getattr(module, component) - return module(**spec.args) + class_name = components[-1] + module_name = ".".join(components[:-1]) + cls = getattr(importlib.import_module(module_name), class_name) + return cls(**spec.args) def parse_object_spec(description: str) -> ObjectSpec: diff --git a/src/helm/benchmark/run_specs.py b/src/helm/benchmark/run_specs.py index 01daa12cc1f..eac6f561e4b 100644 --- a/src/helm/benchmark/run_specs.py +++ b/src/helm/benchmark/run_specs.py @@ -1,3 +1,4 @@ +import importlib import itertools from typing import Any, Callable, List, Dict, Optional, Set, TypeVar @@ -2290,10 +2291,10 @@ def alter_run_spec(run_spec: RunSpec) -> RunSpec: increase_max_tokens_expander = IncreaseMaxTokensRunExpander(value=AnthropicClient.ADDITIONAL_TOKENS) # Get scenario tags components = run_spec.scenario_spec.class_name.split(".") - module: Any = __import__(components[0]) - for component in components[1:]: - module = getattr(module, component) - scenario_tags: List[str] = module.tags + class_name = components[-1] + module_name = ".".join(components[:-1]) + cls = getattr(importlib.import_module(module_name), class_name) + scenario_tags: List[str] = cls.tags # If the scenario is instruction, do not use PROMPT_ANSWER_START if "instructions" in scenario_tags: format_expander = FormatPromptRunExpander(