|
| 1 | +from os.path import abspath, basename, join as pjoin |
| 2 | +import os |
| 3 | + |
| 4 | +import orjson |
| 5 | +from pydantic import BaseModel |
| 6 | +from openai import OpenAI |
| 7 | +import fire |
| 8 | +from tqdm import tqdm |
| 9 | + |
| 10 | +from tfbench import ( |
| 11 | + load_tfb_from_hf, |
| 12 | + load_gen_results_jsonl, |
| 13 | + LMAnswer, |
| 14 | +) |
| 15 | +from tfbench.evaluation import get_incorrect |
| 16 | +from tfbench.common import get_prompt as get_task_prompt, BenchmarkTask |
| 17 | + |
| 18 | + |
| 19 | +PROMPT_TEMPLATE = """ |
| 20 | +The Haskell type inference task is as follows: |
| 21 | +{task} |
| 22 | +
|
| 23 | +The ground-truth correct answer is: |
| 24 | +{correct_answer} |
| 25 | +
|
| 26 | +My incorrect answer is: |
| 27 | +{wrong_answer} |
| 28 | +
|
| 29 | +My reasoning behind my answer is: |
| 30 | +{reasoning} |
| 31 | +
|
| 32 | +What mistake did I make? |
| 33 | +""" |
| 34 | + |
| 35 | +INSTRUCTION = """ |
| 36 | +You are a programming language and logic expert. |
| 37 | +You will be shown a Haskell type inference task, |
| 38 | +an incorrect answer, and the reasoning behind it. |
| 39 | +Your job is to identify the mistake in the answer, |
| 40 | +and classify the mistake in one word. |
| 41 | +The current error categories are: |
| 42 | +{categories}. |
| 43 | +Choose one category, or construct a new one if you are sure that |
| 44 | +none of the current categories fit. |
| 45 | +Only output the one-word classification and a short definition of the class. |
| 46 | +NOTE that the short definition should be generalized to other tasks that fall in the same category. |
| 47 | +""" |
| 48 | + |
| 49 | + |
| 50 | +class ClsResponse(BaseModel): |
| 51 | + category: str |
| 52 | + definition: str |
| 53 | + |
| 54 | + def __hash__(self): |
| 55 | + return hash(self.category) |
| 56 | + |
| 57 | + |
| 58 | +def get_prompt(task: BenchmarkTask, answer: LMAnswer) -> str: |
| 59 | + prompt = PROMPT_TEMPLATE.format( |
| 60 | + task=get_task_prompt(task), |
| 61 | + correct_answer=task.signature, |
| 62 | + wrong_answer=answer.answer, |
| 63 | + reasoning=answer.reasoning_steps, |
| 64 | + ) |
| 65 | + return prompt |
| 66 | + |
| 67 | + |
| 68 | +def categories_str(categories: set[ClsResponse]) -> str: |
| 69 | + """dump all categories in jsonl format string""" |
| 70 | + return "\n".join(orjson.dumps(c.__dict__).decode() for c in categories) |
| 71 | + |
| 72 | + |
| 73 | +def classify_run( |
| 74 | + client: OpenAI, |
| 75 | + categories: set[ClsResponse], |
| 76 | + tasks: list[BenchmarkTask], |
| 77 | + run_result: list[LMAnswer | None], |
| 78 | +) -> set[ClsResponse]: |
| 79 | + incorrect = get_incorrect(tasks, run_result) |
| 80 | + |
| 81 | + categories_: set[ClsResponse] = categories.copy() |
| 82 | + for task, answer in tqdm(incorrect): |
| 83 | + assert answer is not None |
| 84 | + response = client.responses.parse( |
| 85 | + model="gpt-5", |
| 86 | + instructions=INSTRUCTION.format(categories=categories_str(categories_)), |
| 87 | + input=get_prompt(task, answer), |
| 88 | + reasoning={"effort": "medium"}, |
| 89 | + text_format=ClsResponse, |
| 90 | + ) |
| 91 | + assert response.output_parsed is not None |
| 92 | + categories_.add(response.output_parsed) |
| 93 | + return categories_ |
| 94 | + |
| 95 | + |
| 96 | +def main(result_file_dir: str): |
| 97 | + |
| 98 | + client = OpenAI() |
| 99 | + categories: set[ClsResponse] = set() |
| 100 | + |
| 101 | + split = basename(abspath(result_file_dir)) |
| 102 | + print(split) |
| 103 | + base = load_tfb_from_hf(split) |
| 104 | + |
| 105 | + for file in os.listdir(result_file_dir): |
| 106 | + if not file.endswith(".jsonl"): |
| 107 | + continue |
| 108 | + result_file_path = pjoin(result_file_dir, file) |
| 109 | + run_result = load_gen_results_jsonl(result_file_path) |
| 110 | + print(f"Processing {result_file_path}") |
| 111 | + run_categories = classify_run( |
| 112 | + client, |
| 113 | + categories, |
| 114 | + base, |
| 115 | + run_result, |
| 116 | + ) |
| 117 | + categories.update(run_categories) |
| 118 | + |
| 119 | + with open("error_categories.json", "wb") as f: |
| 120 | + f.write( |
| 121 | + orjson.dumps( |
| 122 | + [c.model_dump(mode="json") for c in categories], |
| 123 | + option=orjson.OPT_INDENT_2, |
| 124 | + ) |
| 125 | + ) |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + fire.Fire(main) |
0 commit comments