Update amazonmix.py
Browse files- amazonmix.py +23 -13
amazonmix.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
import re
|
| 2 |
from typing import Dict, Tuple, Optional
|
| 3 |
|
| 4 |
-
def extract_solution(
|
| 5 |
# Split response to isolate assistant output
|
| 6 |
-
if "Assistant:" in solution_str:
|
| 7 |
-
|
| 8 |
-
elif "<|im_start|>assistant" in solution_str:
|
| 9 |
-
|
| 10 |
-
else:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
# Extract final answer using XML-style tags
|
| 15 |
answer_pattern = r'<answer>(.*?)</answer>'
|
|
@@ -21,7 +21,7 @@ def extract_solution(solution_str, method):
|
|
| 21 |
|
| 22 |
final_answer = matches[-1].group(1).strip()
|
| 23 |
|
| 24 |
-
format1_match = re.search(r"([
|
| 25 |
if format1_match:
|
| 26 |
return format1_match.group(1).strip(), processed_str
|
| 27 |
|
|
@@ -42,7 +42,6 @@ def validate_response_structure(processed_str: str) -> bool:
|
|
| 42 |
|
| 43 |
# Check required tags
|
| 44 |
tags = {
|
| 45 |
-
'think_start': ('<think>', 1),
|
| 46 |
'think_end': ('</think>', 1),
|
| 47 |
'answer_start': ('<answer>', 1),
|
| 48 |
'answer_end': ('</answer>', 1)
|
|
@@ -60,8 +59,8 @@ def validate_response_structure(processed_str: str) -> bool:
|
|
| 60 |
validation_passed = False
|
| 61 |
|
| 62 |
# Verify tag order
|
| 63 |
-
if (positions['
|
| 64 |
-
positions['think_end'] > positions['
|
| 65 |
positions['answer_start'] > positions['answer_end']):
|
| 66 |
print(" [Error] Incorrect tag order: Expected <think>...</think><answer>...</answer>")
|
| 67 |
validation_passed = False
|
|
@@ -70,6 +69,16 @@ def validate_response_structure(processed_str: str) -> bool:
|
|
| 70 |
|
| 71 |
return validation_passed
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
def compute_score(solution_str: str, ground_truth: str, method='strict', format_reward: int = 1, answer_reward: float = 1.0):
|
| 74 |
"""Computes comprehensive score for model response.
|
| 75 |
|
|
@@ -86,6 +95,7 @@ def compute_score(solution_str: str, ground_truth: str, method='strict', format_
|
|
| 86 |
print("\n" + "="*80)
|
| 87 |
print(" Processing New Sample ".center(80, '='))
|
| 88 |
print(f"[Ground Truth]: {ground_truth}")
|
|
|
|
| 89 |
|
| 90 |
# Extract model answer
|
| 91 |
answer_text, processed_str= extract_solution(solution_str=solution_str, method=method)
|
|
@@ -104,7 +114,7 @@ def compute_score(solution_str: str, ground_truth: str, method='strict', format_
|
|
| 104 |
print(f"\n[Content Validation]")
|
| 105 |
print(f" Expected: {ground_truth}")
|
| 106 |
print(f" Predicted: {answer_text}")
|
| 107 |
-
if answer_text == ground_truth:
|
| 108 |
answer_score = 2
|
| 109 |
print(" Content validation: FULL MATCH")
|
| 110 |
else:
|
|
|
|
| 1 |
import re
|
| 2 |
from typing import Dict, Tuple, Optional
|
| 3 |
|
| 4 |
+
def extract_solution(processed_str, method):
|
| 5 |
# Split response to isolate assistant output
|
| 6 |
+
# if "Assistant:" in solution_str:
|
| 7 |
+
# processed_str = solution_str.split("Assistant:", 1)[1]
|
| 8 |
+
# elif "<|im_start|>assistant" in solution_str:
|
| 9 |
+
# processed_str = solution_str.split("<|im_start|>assistant", 1)[1]
|
| 10 |
+
# else:
|
| 11 |
+
# print("[Error] Failed to locate model response header")
|
| 12 |
+
# return None, solution_str
|
| 13 |
|
| 14 |
# Extract final answer using XML-style tags
|
| 15 |
answer_pattern = r'<answer>(.*?)</answer>'
|
|
|
|
| 21 |
|
| 22 |
final_answer = matches[-1].group(1).strip()
|
| 23 |
|
| 24 |
+
format1_match = re.search(r"([a-zA-Z]):", final_answer)
|
| 25 |
if format1_match:
|
| 26 |
return format1_match.group(1).strip(), processed_str
|
| 27 |
|
|
|
|
| 42 |
|
| 43 |
# Check required tags
|
| 44 |
tags = {
|
|
|
|
| 45 |
'think_end': ('</think>', 1),
|
| 46 |
'answer_start': ('<answer>', 1),
|
| 47 |
'answer_end': ('</answer>', 1)
|
|
|
|
| 59 |
validation_passed = False
|
| 60 |
|
| 61 |
# Verify tag order
|
| 62 |
+
if (positions['think_end'] > positions['answer_start'] or
|
| 63 |
+
positions['think_end'] > positions['answer_end'] or
|
| 64 |
positions['answer_start'] > positions['answer_end']):
|
| 65 |
print(" [Error] Incorrect tag order: Expected <think>...</think><answer>...</answer>")
|
| 66 |
validation_passed = False
|
|
|
|
| 69 |
|
| 70 |
return validation_passed
|
| 71 |
|
| 72 |
+
def parse_ground_truth_text_format(ground_truth):
|
| 73 |
+
if ":" in ground_truth and "[{" in ground_truth:
|
| 74 |
+
return ground_truth
|
| 75 |
+
elif ":" in ground_truth:
|
| 76 |
+
format1_match = re.search(r"([a-zA-Z]):", ground_truth)
|
| 77 |
+
if format1_match:
|
| 78 |
+
return format1_match.group(1).strip()
|
| 79 |
+
else:
|
| 80 |
+
return ground_truth
|
| 81 |
+
|
| 82 |
def compute_score(solution_str: str, ground_truth: str, method='strict', format_reward: int = 1, answer_reward: float = 1.0):
|
| 83 |
"""Computes comprehensive score for model response.
|
| 84 |
|
|
|
|
| 95 |
print("\n" + "="*80)
|
| 96 |
print(" Processing New Sample ".center(80, '='))
|
| 97 |
print(f"[Ground Truth]: {ground_truth}")
|
| 98 |
+
ground_truth = parse_ground_truth_text_format(ground_truth)
|
| 99 |
|
| 100 |
# Extract model answer
|
| 101 |
answer_text, processed_str= extract_solution(solution_str=solution_str, method=method)
|
|
|
|
| 114 |
print(f"\n[Content Validation]")
|
| 115 |
print(f" Expected: {ground_truth}")
|
| 116 |
print(f" Predicted: {answer_text}")
|
| 117 |
+
if answer_text.casefold() == ground_truth.casefold():
|
| 118 |
answer_score = 2
|
| 119 |
print(" Content validation: FULL MATCH")
|
| 120 |
else:
|