-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
140 lines (127 loc) · 4.62 KB
/
utils.py
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
import os
import json
import tiktoken
import re
import difflib
OJ_STATUSES = [
"WT0",
"WT1",
"CI",
"RI",
"AC",
"PE",
"WA",
"TL",
"ML",
"OL",
"RE",
"CE",
"CO",
"TF",
"JE",
"UE",
"RE_SEGV",
"RE_FPE",
"RE_BUS",
"RE_ABRT",
"RE_SYS",
"CTL",
]
def similarity(s1, s2):
normalized1 = s1.replace('\n', ' ').replace('\r', '')
normalized2 = s2.replace('\n', ' ').replace('\r', '')
matcher = difflib.SequenceMatcher(None, normalized1, normalized2)
return matcher.ratio()
def read_file_contents(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
return content
def all_whitespace(str):
for i in str:
if i not in ['\n', '\r', '\t', ' ']:
return False
return True
def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0301"):
"""Returns the number of tokens used by a list of messages."""
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
print("Warning: model not found. Using cl100k_base encoding.")
encoding = tiktoken.get_encoding("cl100k_base")
if model == "gpt-3.5-turbo":
print("Warning: gpt-3.5-turbo may change over time. Returning num tokens assuming gpt-3.5-turbo-0301.")
return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0301")
elif model == "gpt-4":
print("Warning: gpt-4 may change over time. Returning num tokens assuming gpt-4-0314.")
return num_tokens_from_messages(messages, model="gpt-4-0314")
elif model == "gpt-3.5-turbo-0301":
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
tokens_per_name = -1 # if there's a name, the role is omitted
elif model == "gpt-4-0314":
tokens_per_message = 3
tokens_per_name = 1
else:
raise NotImplementedError(f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""")
num_tokens = 0
for message in messages:
num_tokens += tokens_per_message
for key, value in message.items():
num_tokens += len(encoding.encode(value))
if key == "name":
num_tokens += tokens_per_name
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
return num_tokens
def strings_are_same_except_blank_lines(s1, s2):
s1_no_blank_lines = ''.join(line for line in s1.splitlines() if line.strip())
s2_no_blank_lines = ''.join(line for line in s2.splitlines() if line.strip())
return s1_no_blank_lines == s2_no_blank_lines
def parse_warning(warning_json):
try:
warning_dicts = json.loads(warning_json)
except Exception as e:
return warning_json
res = ""
for item in warning_dicts:
if 'ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’' in item['message']:
continue
if bool(re.search('comparison of integer expressions of different signedness: ‘int’ and ‘[a-zA-Z_:<>]*size_type’', item['message'])):
continue
if 'suggest' in item['message']:
continue
if item['kind'] != 'error':
continue
location = item['locations'][0]
res += "%s in line %d column %d, " % (item['kind'].capitalize(), location['caret']['line'], location['caret']['column']) + item['message'] + '\n'
return res
def add_eoln(s):
if len(s) == 0 or s[-1] != '\n':
s += '\n'
return s
def contains_chinese(s):
if re.search(r'[\u4e00-\u9fff]+', s):
return True
else:
return False
def extract_code(response):
response = response[:response.rfind('```')]
response = re.sub(r'```[a-zA-Z+]*', '', response).strip()
response = response[:response.rfind('\n}')+2]
response = response[response.find('#'):]
return response
def extract_last_cpp_code(s: str) -> str:
matches = re.findall(r'```c\+\+(.*?)```', s, re.DOTALL)
return matches[-1].strip() if matches else ""
def format_extra(ret, case_cnt):
extra = {}
times = []
statuses = []
memory = []
for item in ret['extra']:
times.append(item['runTime'])
statuses.append(item['statusCode'])
memory.append(item['memory'])
extra = {'testcase': {'total': case_cnt, 'passed': ret['passed']},
'time': times, 'statuses': statuses, 'memory': memory}
if 'debug_info' in ret:
extra['debug_info'] = ret['debug_info']
return extra