From 2a04ee83cbee3100b42cc5f29fe89396bfeb424d Mon Sep 17 00:00:00 2001 From: Roger Date: Wed, 22 Jan 2025 14:41:59 -0800 Subject: [PATCH] blow up in case of error so the operator gets more information about what's wrong (#813) --- src/modelgauge/tests/safe_v1.py | 63 ++++++++++++++++----------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/src/modelgauge/tests/safe_v1.py b/src/modelgauge/tests/safe_v1.py index e7487747..7b33839a 100644 --- a/src/modelgauge/tests/safe_v1.py +++ b/src/modelgauge/tests/safe_v1.py @@ -152,39 +152,36 @@ def make_test_items(self, dependency_helper: DependencyHelper) -> List[TestItem] # We're not using a context manager here for better exception handling, # so the operator has some insight into why things aren't working. - try: - csvfile = open(data_file, "r") - reader = csv.DictReader(csvfile) - for row in reader: - text = row["prompt_text"].strip() - if not text: - continue - - # Check that prompt is for correct hazard/persona/locale. - hazard = row["hazard"].split("_")[0] - persona = SafePersonasVersion1(row["persona"]) - locale = row["locale"].lower() - if not hazard == self.hazard: - continue - if persona not in self.persona_types: - continue - if locale != self.locale: - continue - - prompt = PromptWithContext( - prompt=TextPrompt(text=text, options=sut_options), - source_id=row["release_prompt_id"], - ) - test_items.append( - TestItem( - prompts=[prompt], - context=SafeTestItemContext(persona_type=persona), - ), - ) - if len(test_items) == 0: - raise RuntimeError(f"No test items created from {data_file}") - except Exception as exc: - raise RuntimeError(f"Error making test items from {data_file}: {exc}") + csvfile = open(data_file, "r") + reader = csv.DictReader(csvfile) + for row in reader: + text = row["prompt_text"].strip() + if not text: + continue + + # Check that prompt is for correct hazard/persona/locale. + hazard = row["hazard"].split("_")[0] + persona = SafePersonasVersion1(row["persona"]) + locale = row["locale"].lower() + if not hazard == self.hazard: + continue + if persona not in self.persona_types: + continue + if locale != self.locale: + continue + + prompt = PromptWithContext( + prompt=TextPrompt(text=text, options=sut_options), + source_id=row["release_prompt_id"], + ) + test_items.append( + TestItem( + prompts=[prompt], + context=SafeTestItemContext(persona_type=persona), + ), + ) + if len(test_items) == 0: + raise RuntimeError(f"No test items created from {data_file}") return test_items