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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import textwrap
|
| 3 |
+
|
| 4 |
+
# 1. 软约束列表(交由 LLM 判断)
|
| 5 |
+
SOFT_IDS = {
|
| 6 |
+
'soft_content:language', 'soft', 'soft_content:open_ended',
|
| 7 |
+
'situation:suggestion', 'situation:role_play',
|
| 8 |
+
'situation:story_generation', 'style:open_ended'
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# 辅助函数:处理比较关系的符号
|
| 13 |
+
def get_op(relation):
|
| 14 |
+
return "<" if relation == "less than" else ">="
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# 辅助函数:安全获取参数
|
| 18 |
+
def get_int(kwargs, key, default=1):
|
| 19 |
+
val = kwargs.get(key)
|
| 20 |
+
return int(val) if val is not None else default
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def get_str(kwargs, key, default=""):
|
| 24 |
+
val = kwargs.get(key)
|
| 25 |
+
return str(val) if val is not None else default
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# 2. 全量硬约束模板字典 (严格基于 instructions.py 源码提取)
|
| 29 |
+
CODE_TEMPLATES = {
|
| 30 |
+
"detectable_format:number_highlighted_sections": lambda kw: textwrap.dedent(f"""\
|
| 31 |
+
def check(response_text):
|
| 32 |
+
import re
|
| 33 |
+
num_highlights = 0
|
| 34 |
+
highlights = re.findall(r"\\*[^\\n\\*]*\\*", response_text)
|
| 35 |
+
double_highlights = re.findall(r"\\*\\*[^\\n\\*]*\\*\\*", response_text)
|
| 36 |
+
for highlight in highlights:
|
| 37 |
+
if highlight.strip("*").strip():
|
| 38 |
+
num_highlights += 1
|
| 39 |
+
for highlight in double_highlights:
|
| 40 |
+
if highlight.removeprefix("**").removesuffix("**").strip():
|
| 41 |
+
num_highlights += 1
|
| 42 |
+
return num_highlights >= {get_int(kw, 'num_highlights', 1)}"""),
|
| 43 |
+
|
| 44 |
+
"detectable_format:multiple_sections": lambda kw: textwrap.dedent(f"""\
|
| 45 |
+
def check(response_text):
|
| 46 |
+
import re
|
| 47 |
+
spliter = {repr(get_str(kw, 'section_spliter', 'SECTION'))}
|
| 48 |
+
num_sections = {get_int(kw, 'num_sections', 2)}
|
| 49 |
+
section_splitter_patten = r"\\s?" + spliter + r"\\s?\\d+\\s?"
|
| 50 |
+
sections = re.split(section_splitter_patten, response_text, flags=re.IGNORECASE)
|
| 51 |
+
return (len(sections) - 1) >= num_sections"""),
|
| 52 |
+
|
| 53 |
+
"punctuation:no_comma": lambda kw: textwrap.dedent("""\
|
| 54 |
+
def check(response_text):
|
| 55 |
+
import re
|
| 56 |
+
return not bool(re.search(r"\\,", response_text))"""),
|
| 57 |
+
|
| 58 |
+
"keywords:letter_frequency": lambda kw: textwrap.dedent(f"""\
|
| 59 |
+
def check(response_text):
|
| 60 |
+
import collections
|
| 61 |
+
letter = {repr(get_str(kw, 'letter', 'a').lower())}
|
| 62 |
+
frequency = {get_int(kw, 'let_frequency', 1)}
|
| 63 |
+
letters = collections.Counter(response_text.lower())
|
| 64 |
+
return letters[letter] {get_op(kw.get('let_relation'))} frequency"""),
|
| 65 |
+
|
| 66 |
+
"length_constraints:number_sentences": lambda kw: textwrap.dedent(f"""\
|
| 67 |
+
def check(response_text):
|
| 68 |
+
import re
|
| 69 |
+
sentences = [s for s in re.split(r'(?<=[.!?])\\s+', response_text) if s.strip()]
|
| 70 |
+
return len(sentences) {get_op(kw.get('relation'))} {get_int(kw, 'num_sentences', 1)}"""),
|
| 71 |
+
|
| 72 |
+
"detectable_format:number_placeholders": lambda kw: textwrap.dedent(f"""\
|
| 73 |
+
def check(response_text):
|
| 74 |
+
import re
|
| 75 |
+
placeholders = re.findall(r"\\[.*?\\]", response_text)
|
| 76 |
+
return len(placeholders) >= {get_int(kw, 'num_placeholders', 1)}"""),
|
| 77 |
+
|
| 78 |
+
"detectable_format:number_bullet_lists": lambda kw: textwrap.dedent(f"""\
|
| 79 |
+
def check(response_text):
|
| 80 |
+
import re
|
| 81 |
+
bullet_lists = re.findall(r"^\\s*\\*[^\\*].*$", response_text, flags=re.MULTILINE)
|
| 82 |
+
bullet_lists_2 = re.findall(r"^\\s*-.*$", response_text, flags=re.MULTILINE)
|
| 83 |
+
return (len(bullet_lists) + len(bullet_lists_2)) == {get_int(kw, 'num_bullets', 1)}"""),
|
| 84 |
+
|
| 85 |
+
"start_ending:constrained_response": lambda kw: textwrap.dedent("""\
|
| 86 |
+
def check(response_text):
|
| 87 |
+
options = ["My answer is yes.", "My answer is no.", "My answer is maybe."]
|
| 88 |
+
return any(opt in response_text.strip() for opt in options)"""),
|
| 89 |
+
|
| 90 |
+
"start_ending:constrained_start": lambda kw: textwrap.dedent(f"""\
|
| 91 |
+
def check(response_text):
|
| 92 |
+
import re
|
| 93 |
+
starter = {repr(get_str(kw, 'starter', '').strip())}
|
| 94 |
+
pattern = r"^\\s*" + re.escape(starter) + r".*$"
|
| 95 |
+
return bool(re.search(pattern, response_text, flags=re.MULTILINE))"""),
|
| 96 |
+
|
| 97 |
+
"length_constraints:number_paragraphs": lambda kw: textwrap.dedent(f"""\
|
| 98 |
+
def check(response_text):
|
| 99 |
+
import re
|
| 100 |
+
paragraphs = re.split(r"\\s?\\*\\*\\*\\s?", response_text)
|
| 101 |
+
num_paragraphs = len(paragraphs)
|
| 102 |
+
for index, paragraph in enumerate(paragraphs):
|
| 103 |
+
if not paragraph.strip():
|
| 104 |
+
if index == 0 or index == len(paragraphs) - 1:
|
| 105 |
+
num_paragraphs -= 1
|
| 106 |
+
else: return False
|
| 107 |
+
return num_paragraphs == {get_int(kw, 'num_paragraphs', 1)}"""),
|
| 108 |
+
|
| 109 |
+
"keywords:existence": lambda kw: textwrap.dedent(f"""\
|
| 110 |
+
def check(response_text):
|
| 111 |
+
import re
|
| 112 |
+
keywords = {kw.get('keywords', [])}
|
| 113 |
+
for keyword in keywords:
|
| 114 |
+
if not re.search(keyword, response_text, flags=re.IGNORECASE):
|
| 115 |
+
return False
|
| 116 |
+
return True"""),
|
| 117 |
+
|
| 118 |
+
"keywords:frequency": lambda kw: textwrap.dedent(f"""\
|
| 119 |
+
def check(response_text):
|
| 120 |
+
import re
|
| 121 |
+
keyword = {repr(get_str(kw, 'keyword', ''))}
|
| 122 |
+
actual_occurrences = len(re.findall(keyword, response_text, flags=re.IGNORECASE))
|
| 123 |
+
return actual_occurrences {get_op(kw.get('relation'))} {get_int(kw, 'frequency', 1)}"""),
|
| 124 |
+
|
| 125 |
+
"length_constraints:number_words": lambda kw: textwrap.dedent(f"""\
|
| 126 |
+
def check(response_text):
|
| 127 |
+
import re
|
| 128 |
+
num_words = len(re.findall(r"\\w+", response_text))
|
| 129 |
+
return num_words {get_op(kw.get('relation'))} {get_int(kw, 'num_words', 100)}"""),
|
| 130 |
+
|
| 131 |
+
"detectable_format:json_format": lambda kw: textwrap.dedent("""\
|
| 132 |
+
def check(response_text):
|
| 133 |
+
import json
|
| 134 |
+
value = response_text.strip().removeprefix("```json").removeprefix("```Json").removeprefix("```JSON").removeprefix("```").removesuffix("```").strip()
|
| 135 |
+
try:
|
| 136 |
+
json.loads(value)
|
| 137 |
+
return True
|
| 138 |
+
except ValueError:
|
| 139 |
+
return False"""),
|
| 140 |
+
|
| 141 |
+
"keywords:forbidden_words": lambda kw: textwrap.dedent(f"""\
|
| 142 |
+
def check(response_text):
|
| 143 |
+
import re
|
| 144 |
+
forbidden_words = {kw.get('forbidden_words', [])}
|
| 145 |
+
for word in forbidden_words:
|
| 146 |
+
if re.search(r"\\b" + word + r"\\b", response_text, flags=re.IGNORECASE):
|
| 147 |
+
return False
|
| 148 |
+
return True"""),
|
| 149 |
+
|
| 150 |
+
"start_ending:repeat_prompt": lambda kw: textwrap.dedent(f"""\
|
| 151 |
+
def check(response_text):
|
| 152 |
+
prompt_to_repeat = {repr(get_str(kw, 'prompt_to_repeat', ''))}
|
| 153 |
+
return response_text.strip().lower().startswith(prompt_to_repeat.strip().lower())"""),
|
| 154 |
+
|
| 155 |
+
"start_ending:end_phrase": lambda kw: textwrap.dedent(f"""\
|
| 156 |
+
def check(response_text):
|
| 157 |
+
end_phrase = {repr(get_str(kw, 'end_phrase', ''))}.strip().lower()
|
| 158 |
+
value = response_text.strip().strip('"').lower()
|
| 159 |
+
return value.endswith(end_phrase)"""),
|
| 160 |
+
|
| 161 |
+
"detectable_format:title": lambda kw: textwrap.dedent("""\
|
| 162 |
+
def check(response_text):
|
| 163 |
+
import re
|
| 164 |
+
titles = re.findall(r"<<[^\\n]+>>", response_text)
|
| 165 |
+
for title in titles:
|
| 166 |
+
if title.lstrip("<").rstrip(">").strip(): return True
|
| 167 |
+
return False"""),
|
| 168 |
+
|
| 169 |
+
"format:capital_letters": lambda kw: textwrap.dedent("""\
|
| 170 |
+
def check(response_text):
|
| 171 |
+
return response_text.isupper()"""),
|
| 172 |
+
|
| 173 |
+
"format:lowercase_letters": lambda kw: textwrap.dedent("""\
|
| 174 |
+
def check(response_text):
|
| 175 |
+
return response_text.islower()"""),
|
| 176 |
+
|
| 177 |
+
"detectable_format:quotation": lambda kw: textwrap.dedent("""\
|
| 178 |
+
def check(response_text):
|
| 179 |
+
val = response_text.strip()
|
| 180 |
+
return len(val) > 1 and val[0] == '"' and val[-1] == '"'"""),
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def convert_dataset(input_file, output_file):
|
| 185 |
+
with open(input_file, 'r', encoding='utf-8') as fin, \
|
| 186 |
+
open(output_file, 'w', encoding='utf-8') as fout:
|
| 187 |
+
|
| 188 |
+
for line_idx, line in enumerate(fin):
|
| 189 |
+
data = json.loads(line)
|
| 190 |
+
|
| 191 |
+
converted_data = {
|
| 192 |
+
"id": line_idx + 1,
|
| 193 |
+
"conversations": [
|
| 194 |
+
{"role": "user", "content": data['prompt']},
|
| 195 |
+
{"role": "assistant", "content": "no"}
|
| 196 |
+
],
|
| 197 |
+
"system": "",
|
| 198 |
+
"gemini-judge": {"constraints": []},
|
| 199 |
+
"new_query": data['prompt'],
|
| 200 |
+
"soft-constraint-fusion": {
|
| 201 |
+
"soft_constraints": [],
|
| 202 |
+
"final_query": data['prompt']
|
| 203 |
+
}
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
constraints_desc = data.get('constraint', [])
|
| 207 |
+
kwargs_list = data.get('kwargs', [])
|
| 208 |
+
id_list = data.get('instruction_id_list', [])
|
| 209 |
+
|
| 210 |
+
for i in range(len(id_list)):
|
| 211 |
+
inst_id = id_list[i]
|
| 212 |
+
desc = constraints_desc[i] if i < len(constraints_desc) else ""
|
| 213 |
+
kw = kwargs_list[i] if i < len(kwargs_list) else {}
|
| 214 |
+
|
| 215 |
+
if inst_id in SOFT_IDS:
|
| 216 |
+
converted_data["soft-constraint-fusion"]["soft_constraints"].append(desc)
|
| 217 |
+
else:
|
| 218 |
+
if inst_id in CODE_TEMPLATES:
|
| 219 |
+
# 动态生成特定参数的代码
|
| 220 |
+
code_str = CODE_TEMPLATES[inst_id](kw)
|
| 221 |
+
else:
|
| 222 |
+
# 极其罕见的 ID,安全回退
|
| 223 |
+
code_str = textwrap.dedent(f"""\
|
| 224 |
+
def check(response_text):
|
| 225 |
+
# UNMAPPED ID: {inst_id}
|
| 226 |
+
return False""")
|
| 227 |
+
|
| 228 |
+
converted_data["gemini-judge"]["constraints"].append({
|
| 229 |
+
"description": desc,
|
| 230 |
+
"checker_code": code_str
|
| 231 |
+
})
|
| 232 |
+
|
| 233 |
+
fout.write(json.dumps(converted_data, ensure_ascii=False) + '\n')
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
if __name__ == "__main__":
|
| 237 |
+
convert_dataset('train.jsonl', 'output.jsonl')
|
| 238 |
+
print("转换完成!所有参数已被安全硬编码进独立函数中。")
|