Repository navigation
Expand file tree
/
Copy pathrun_eval.py
More file actions
86 lines (68 loc) · 2.68 KB
/
Copy pathrun_eval.py
File metadata and controls
86 lines (68 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# coding=utf-8
# Copyright 2025 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Binary of evaluating instruction following. See README.md."""
import os
from absl import app
from absl import flags
from absl import logging
import evaluation_lib
_INPUT_DATA = flags.DEFINE_string(
"input_data", None, "path to input data", required=True
)
_INPUT_RESPONSE_DATA = flags.DEFINE_string(
"input_response_data", None, "path to input response data", required=False
)
_OUTPUT_DIR = flags.DEFINE_string(
"output_dir",
None,
"Output directory for inference and eval results.",
required=True,
)
def main(argv):
if len(argv) > 1:
raise app.UsageError("Too many command-line arguments.")
inputs = evaluation_lib.read_prompt_list(_INPUT_DATA.value)
prompt_to_response = evaluation_lib.read_prompt_to_response_dict(
_INPUT_RESPONSE_DATA.value)
os.makedirs(_OUTPUT_DIR.value, exist_ok=True)
# get instruction following results
for func, output_file_name in [
(evaluation_lib.test_instruction_following_strict, "eval_results_strict"),
(evaluation_lib.test_instruction_following_loose, "eval_results_loose"),
]:
logging.info("Generating %s...", output_file_name)
outputs = []
for inp in inputs:
outputs.append(func(inp, prompt_to_response))
follow_all_instructions = [o.follow_all_instructions for o in outputs]
accuracy = sum(follow_all_instructions) / len(outputs)
logging.info("Accuracy: %f", accuracy)
input_basename = os.path.basename(_INPUT_RESPONSE_DATA.value)
if "-responses.jsonl" in input_basename:
model_name = input_basename.replace("-responses.jsonl", "")
else:
model_name = os.path.splitext(input_basename)[0]
output_filename = f"{model_name}-{output_file_name}.jsonl"
output_file_name = os.path.join(
_OUTPUT_DIR.value, output_filename
)
evaluation_lib.write_outputs(output_file_name, outputs)
logging.info("Generated: %s", output_file_name)
# Prints instruction following accuracy report.
print("=" * 64)
print(f"{output_file_name} Accuracy Scores:")
evaluation_lib.print_report(outputs)
if __name__ == "__main__":
app.run(main)