Spaces:
Runtime error
Runtime error
Create tool.py
Browse files
tool.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def get_response(prompt: str, ak: str) -> str:
|
| 2 |
+
'''
|
| 3 |
+
prompt:
|
| 4 |
+
'''
|
| 5 |
+
client = OpenAI(api_key=ak)
|
| 6 |
+
|
| 7 |
+
completion = client.chat.completions.create(
|
| 8 |
+
model="gpt-4-1106-preview",
|
| 9 |
+
messages=[
|
| 10 |
+
{"role": "user", "content": prompt}
|
| 11 |
+
]
|
| 12 |
+
)
|
| 13 |
+
return completion.choices[0].message.content
|
| 14 |
+
|
| 15 |
+
def check_judge(answer: str) -> str:
|
| 16 |
+
|
| 17 |
+
assert len(answer.split('\n')[0])>=1 , "Answer has no analysis."
|
| 18 |
+
assert answer.split('\n')[-1] in ['0', '1'], "Judge output is not 0 or 1." + answer.split('\n')[-1]
|
| 19 |
+
assert not (answer.split('\n')[0] in ['0', '1']), "Answer has no analysis."
|
| 20 |
+
|
| 21 |
+
return answer.split('\n')[-1]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def judge(rule: str, answer: str, ak: str):
|
| 25 |
+
return get_response(rule.replace("[Answer to be judged]:", "[Answer to be judged]: " + answer + '\n'), ak)
|
| 26 |
+
|
| 27 |
+
def alternate_judge(rule, answer, ak):
|
| 28 |
+
maxtry = 10
|
| 29 |
+
while True:
|
| 30 |
+
try:
|
| 31 |
+
out = judge(rule, answer, ak)
|
| 32 |
+
bitout = check_judge(out)
|
| 33 |
+
return out, bitout
|
| 34 |
+
except Exception as e:
|
| 35 |
+
if maxtry <= 0:
|
| 36 |
+
return None, "0"
|
| 37 |
+
if not isinstance(e, KeyError):
|
| 38 |
+
maxtry -= 1
|
| 39 |
+
print(e)
|
| 40 |
+
else:
|
| 41 |
+
print("Request Error: " + str(e))
|
| 42 |
+
print("Retrying...")
|
| 43 |
+
time.sleep(random.uniform(1, 2))
|
| 44 |
+
continue
|
| 45 |
+
|
| 46 |
+
def count_lines(file_path: str):
|
| 47 |
+
with open(file_path, 'r') as f:
|
| 48 |
+
return sum(1 for _ in f)
|