-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
262 lines (227 loc) · 9.52 KB
/
utils.py
File metadata and controls
262 lines (227 loc) · 9.52 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from tracemalloc import start
from unittest import result
from transformers import AutoTokenizer, AutoModel
from vllm import LLM
from collections import Counter
import re
model2path = {
'qwen2-7b-instruct': '/path/to/Qwen2-7B-Instruct',
'glm-4-9b-chat': "/path/to/models/ZhipuAI/glm-4-9b-chat",
'mistral-7b-instruct': '/path/to/Mistral-7B-Instruct-v0.3',
'llama2-7b-chat': '/path/to/llama-2-7b-chat-hf',
'llama3-8b-instruct': '/path/to/Meta-Llama-3-8B-Instruct',
'qwen2.5-7b-instruct': '/path/to/Qwen2.5-7B-Instruct',
'llama3.1-8b-instruct': '/path/to/Llama-3.1-8B-Instruct',
'llama3.2-1b-instruct': '/path/to/LLM-Research/Llama-3___2-1B-Instruct',
'llama3.2-3b-instruct': '/path/to/LLM-Research/Llama-3___2-3B-Instruct',
'llama3.3-70b-instruct': '/path/to/llama3.3-70B-instruct',
'qwen2.5-1.5b-instruct': '/path/to/Qwen/Qwen2___5-1___5B-Instruct',
'qwen2.5-0.5b-instruct': '/path/to/Qwen/Qwen2___5-0___5B-Instruct',
'deepseek-llm-7b-chat': '/path/to/deepseek-ai/deepseek-llm-7b-chat',
'baichuan2-7b-chat': '/path/to/baichuan-inc/Baichuan2-7B-Chat',
'qwen2.5-3b-instruct': '/path/to/Qwen/Qwen2___5-3B-Instruct',
'openelm-3b-instruct': '/path/to/LLM-Research/OpenELM-3B-Instruct',
'qwen2.5-3b': '/path/to/Qwen/Qwen2___5-3B',
'gemma-7b-it': '/path/to/LLM-Research/gemma-7b-it',
'gemma-2-2b-it': '/path/to/LLM-Research/gemma-2-2b-it',
'gemma-2b-it': '/path/to/AI-ModelScope/gemma-2b-it',
'qwen2.5-14b-instruct': '/path/to/Qwen/Qwen2___5-14B-Instruct',
'qwen2.5-32b-instruct': '/path/to/Qwen/Qwen2___5-32B-Instruct',
'qwen2.5-72b-instruct': '/path/to/qwen2.5-72B-Instruct',
'qwen3-8b': '/path/to/START/models/Qwen/Qwen3-8B',
'e5': "/path/to/e5-base-v2",
}
def load_model(config):
if 'e5' in config['model_path'].lower():
model = AutoModel.from_pretrained(config['model_path'], trust_remote_code=True)
else:
model = LLM(
config['model_path'],
dtype=config['type'],
enforce_eager=True,
trust_remote_code=True,
max_model_len=config['max_input_len'],
gpu_memory_utilization=config['gpu_use'],
tensor_parallel_size=config['gpu_num'],
)
tokenizer = AutoTokenizer.from_pretrained(config['model_path'], trust_remote_code=True)
return model, tokenizer
def cal_tool_use(input):
count = 0
python_start = 0
while True:
python_start = input.find("<python>", python_start)
if python_start == -1:
break
python_end = input.find("</python>", python_start)
if python_end == -1:
break
count += 1
python_start = python_end + len('</python>')
search_start = 0
while True:
search_start = input.find("<search>", search_start)
if search_start == -1:
break
search_end = input.find("</search>", search_start)
if search_end == -1:
break
count += 1
search_start = search_end + len('</search>')
code_start = 0
while True:
code_start = input.find("```python", code_start)
if code_start == -1:
break
code_end = input.find("```", code_start + len('```python'))
if code_end == -1:
break
count += 1
code_start = code_end + len('```')
return count
def cal_f1_score(string1, string2):
if string1 is None or string2 is None:
return 0.0
string1 = string1.lower().split()
string2 = string2.lower().split()
counter1 = Counter(string1)
counter2 = Counter(string2)
common = counter1 & counter2
if not common:
return 0.0
precision = sum(common.values()) / sum(counter1.values())
recall = sum(common.values()) / sum(counter2.values())
if precision + recall == 0:
return 0.0
f1_score = 2 * (precision * recall) / (precision + recall)
return f1_score
def remove_tool_use(input):
result = input
start_tag = "<result>"
end_tag = "</result>"
while start_tag in result:
# Find the index of the next <result> tag
start_idx = result.find(start_tag)
if start_idx == -1:
break # No more start tags found
# Find the index of the corresponding </result> tag
end_idx = result.find(end_tag, start_idx)
if end_idx == -1:
break # No matching end tag, stop to avoid infinite loop
# Include the length of </result> to remove the end tag as well
end_idx += len(end_tag)
# Remove the substring from start_idx to end_idx
result = result[:start_idx] + result[end_idx:]
return result
def remove_tool_use_torl(input):
result = input
start_tag = "```output"
end_tag = "```"
while start_tag in result:
# Find the index of the next ```output tag
start_idx = result.find(start_tag)
if start_idx == -1:
break # No more start tags found
# Find the index of the corresponding ``` tag
end_idx = result.find(end_tag, start_idx)
if end_idx == -1:
break # No matching end tag, stop to avoid infinite loop
# Include the length of ``` to remove the end tag as well
end_idx += len(end_tag)
# Remove the substring from start_idx to end_idx
result = result[:start_idx] + result[end_idx:]
return result
def validate_format(text: str):
# check if <think></think>, <answer></answer> is paired
if text.count('<think>') != text.count('</think>'):
return False
if text.count('<think>') == 0 or text.count('</think>') == 0:
return False
if text.count('<answer>') != 1 or text.count('</answer>') != 1:
return False
# check the order of search/result and new logic for </think> before <search>
current_pos = 0
while True:
search_pos = text.find('<search>', current_pos)
if search_pos == -1:
break
result_pos = text.find('<result>', search_pos)
search_end_pos = text.find('</search>', search_pos)
result_end_pos = text.find('</result>', result_pos)
if -1 in (result_pos, search_end_pos, result_end_pos):
return False
if not (search_pos < search_end_pos < result_pos < result_end_pos):
return False
# New logic: check if </think> is immediately before <search>
text_before_search = text[:search_pos].rstrip()
if not text_before_search.endswith('</think>'):
return False
# New logic: check if </result> is followed by <think> or <answer>
text_after_result = text[result_end_pos + len('</result>'):].lstrip()
if not (text_after_result.startswith('<think>') or text_after_result.startswith('<answer>')):
return False
current_pos = result_end_pos
# check the order of python/result and new logic for </think> before <python>
current_pos = 0
while True:
python_pos = text.find('<python>', current_pos)
if python_pos == -1:
break
result_pos = text.find('<result>', python_pos)
python_end_pos = text.find('</python>', python_pos)
result_end_pos = text.find('</result>', result_pos)
if -1 in (result_pos, python_end_pos, result_end_pos):
return False
if not (python_pos < python_end_pos < result_pos < result_end_pos):
return False
# New logic: check if </think> is immediately before <python>
text_before_python = text[:python_pos].rstrip()
if not text_before_python.endswith('</think>'):
return False
# New logic: check if </result> is followed by <think> or <answer>
text_after_result = text[result_end_pos + len('</result>'):].lstrip()
if not (text_after_result.startswith('<think>') or text_after_result.startswith('<answer>')):
return False
current_pos = result_end_pos
# check if \boxed{} is in the answer
answer_start = text.find('<answer>')
answer_end = text.find('</answer>')
if answer_start > answer_end:
return False
answer_content = text[answer_start:answer_end]
if '\\boxed{' not in answer_content or '}' not in answer_content:
return False
# check if any of the special tags appear inside the answer content
special_tags = ['<think>', '</think>', '<python>', '</python>', '<search>', '</search>']
for tag in special_tags:
if tag in answer_content:
return False
return True
def last_boxed_only_string(string):
try:
idx = string.rfind("\\boxed")
except:
import pdb; pdb.set_trace()
if idx < 0:
idx = string.rfind("\\fbox")
if idx < 0:
return None
i = idx
right_brace_idx = None
num_left_braces_open = 0
while i < len(string):
if string[i] == "{":
num_left_braces_open += 1
if string[i] == "}":
num_left_braces_open -= 1
if num_left_braces_open == 0:
right_brace_idx = i
break
i += 1
if right_brace_idx is None:
retval = string[idx:]
else:
retval = string[idx:right_brace_idx + 1]
return retval
def contains_chinese(text):
return bool(re.search('[\u4e00-\u9fff]', text))