|
| 1 | +import re |
| 2 | +from typing import List, Tuple |
| 3 | + |
| 4 | +import datasets |
| 5 | + |
| 6 | + |
| 7 | +def load_dataset(**kwargs): |
| 8 | + """ |
| 9 | + Load the graphwalks dataset with specific data file. |
| 10 | +
|
| 11 | + Args: |
| 12 | + kwargs: Must contain 'data_file' key specifying which parquet file to load |
| 13 | +
|
| 14 | + Returns: |
| 15 | + Dictionary with 'train' split containing the dataset |
| 16 | + """ |
| 17 | + data_file = kwargs.get("data_file") |
| 18 | + if not data_file: |
| 19 | + raise ValueError("data_file must be specified in dataset_kwargs") |
| 20 | + |
| 21 | + dataset = datasets.load_dataset( |
| 22 | + "openai/graphwalks", data_files=data_file, split="train" |
| 23 | + ) |
| 24 | + return {"train": dataset} |
| 25 | + |
| 26 | + |
| 27 | +def extract_answer_list(response: str) -> Tuple[List[str], bool]: |
| 28 | + """ |
| 29 | + Extract the answer list from a model response. |
| 30 | +
|
| 31 | + Args: |
| 32 | + response: The model's generated response |
| 33 | +
|
| 34 | + Returns: |
| 35 | + Tuple of (list of nodes, is_error) |
| 36 | + - list of nodes: extracted node IDs |
| 37 | + - is_error: True if parsing failed, False otherwise |
| 38 | + """ |
| 39 | + # Get the very last line of the response (strip trailing newlines first) |
| 40 | + line = response.rstrip("\n").split("\n")[-1] |
| 41 | + |
| 42 | + # Check if formatted correctly |
| 43 | + if "Final Answer:" not in line: |
| 44 | + return [], True |
| 45 | + |
| 46 | + # Extract the list part using regex with capturing group |
| 47 | + match = re.search(r"Final Answer:\s*\[(.*)\]", line) |
| 48 | + if match: |
| 49 | + # Extract content between brackets using group(1) |
| 50 | + bracket_content = match.group(1) |
| 51 | + # Handle empty list case |
| 52 | + if not bracket_content.strip(): |
| 53 | + return [], False |
| 54 | + # Split by comma and clean up whitespace and quotes |
| 55 | + result_list = [ |
| 56 | + item.strip().strip("'\"") |
| 57 | + for item in bracket_content.split(",") |
| 58 | + if item.strip() |
| 59 | + ] |
| 60 | + return result_list, False |
| 61 | + else: |
| 62 | + return [], True |
| 63 | + |
| 64 | + |
| 65 | +def extract_answer_list_flexible(response: str) -> Tuple[List[str], bool]: |
| 66 | + """ |
| 67 | + Extract the answer list from a model response (flexible version). |
| 68 | + Searches backwards through all lines to find "Final Answer:" pattern. |
| 69 | + More lenient than extract_answer_list which only checks the last line. |
| 70 | +
|
| 71 | + Args: |
| 72 | + response: The model's generated response |
| 73 | +
|
| 74 | + Returns: |
| 75 | + Tuple of (list of nodes, is_error) |
| 76 | + - list of nodes: extracted node IDs |
| 77 | + - is_error: True if parsing failed, False otherwise |
| 78 | + """ |
| 79 | + lines = response.rstrip("\n").split("\n") |
| 80 | + for line in reversed(lines): |
| 81 | + match = re.search(r"Final Answer:\s*\[(.*)\]", line) |
| 82 | + if match: |
| 83 | + # Extract content between brackets using group(1) |
| 84 | + bracket_content = match.group(1) |
| 85 | + # Handle empty list case |
| 86 | + if not bracket_content.strip(): |
| 87 | + return [], False |
| 88 | + # Split by comma and clean up whitespace and quotes |
| 89 | + result_list = [ |
| 90 | + item.strip().strip("'\"") |
| 91 | + for item in bracket_content.split(",") |
| 92 | + if item.strip() |
| 93 | + ] |
| 94 | + return result_list, False |
| 95 | + |
| 96 | + # No "Final Answer:" found anywhere |
| 97 | + return [], True |
| 98 | + |
| 99 | + |
| 100 | +def process_results(doc, results): |
| 101 | + """ |
| 102 | + Process results and compute set-based F1 scores. |
| 103 | + Returns both strict F1 (last line only) and flexible F1 (search all lines). |
| 104 | +
|
| 105 | + Args: |
| 106 | + doc: Document containing ground truth answer_nodes |
| 107 | + results: List containing model generation |
| 108 | +
|
| 109 | + Returns: |
| 110 | + Dictionary with f1 and flexible_f1 scores |
| 111 | + """ |
| 112 | + # Extract model response (first element of results) |
| 113 | + response = results[0] |
| 114 | + |
| 115 | + # Get ground truth nodes |
| 116 | + gold_nodes = doc["answer_nodes"] |
| 117 | + |
| 118 | + # Parse the response using strict extraction |
| 119 | + predicted_nodes_strict, _ = extract_answer_list(response) |
| 120 | + sampled_set_strict = set(predicted_nodes_strict) |
| 121 | + truth_set = set(gold_nodes) |
| 122 | + |
| 123 | + # Calculate strict F1 |
| 124 | + n_overlap_strict = len(sampled_set_strict & truth_set) |
| 125 | + n_sampled_strict = len(sampled_set_strict) |
| 126 | + n_golden = len(truth_set) |
| 127 | + |
| 128 | + recall_strict = n_overlap_strict / n_golden if n_golden > 0 else 0.0 |
| 129 | + precision_strict = ( |
| 130 | + n_overlap_strict / n_sampled_strict if n_sampled_strict > 0 else 0.0 |
| 131 | + ) |
| 132 | + f1_strict = ( |
| 133 | + 2 * (recall_strict * precision_strict) / (recall_strict + precision_strict) |
| 134 | + if (recall_strict + precision_strict) > 0 |
| 135 | + else 0.0 |
| 136 | + ) |
| 137 | + |
| 138 | + # Parse the response using flexible extraction |
| 139 | + predicted_nodes_flexible, _ = extract_answer_list_flexible(response) |
| 140 | + sampled_set_flexible = set(predicted_nodes_flexible) |
| 141 | + |
| 142 | + # Calculate flexible F1 |
| 143 | + n_overlap_flexible = len(sampled_set_flexible & truth_set) |
| 144 | + n_sampled_flexible = len(sampled_set_flexible) |
| 145 | + |
| 146 | + recall_flexible = n_overlap_flexible / n_golden if n_golden > 0 else 0.0 |
| 147 | + precision_flexible = ( |
| 148 | + n_overlap_flexible / n_sampled_flexible if n_sampled_flexible > 0 else 0.0 |
| 149 | + ) |
| 150 | + f1_flexible = ( |
| 151 | + 2 |
| 152 | + * (recall_flexible * precision_flexible) |
| 153 | + / (recall_flexible + precision_flexible) |
| 154 | + if (recall_flexible + precision_flexible) > 0 |
| 155 | + else 0.0 |
| 156 | + ) |
| 157 | + |
| 158 | + return { |
| 159 | + "f1": f1_strict, |
| 160 | + "flexible_f1": f1_flexible, |
| 161 | + } |
0 commit comments