Spaces:
Runtime error
Runtime error
| import time | |
| import random | |
| from openai import OpenAI | |
| def get_response(prompt: str, ak: str) -> str: | |
| ''' | |
| prompt: | |
| ''' | |
| client = OpenAI(api_key=ak) | |
| completion = client.chat.completions.create( | |
| model="gpt-4-1106-preview", | |
| messages=[ | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| return completion.choices[0].message.content | |
| def check_judge(answer: str) -> str: | |
| assert len(answer.split('\n')[0])>=1 , "Answer has no analysis." | |
| assert answer.split('\n')[-1] in ['0', '1'], "Judge output is not 0 or 1." + answer.split('\n')[-1] | |
| assert not (answer.split('\n')[0] in ['0', '1']), "Answer has no analysis." | |
| return answer.split('\n')[-1] | |
| def judge(rule: str, answer: str, ak: str): | |
| return get_response(rule.replace("[Answer to be judged]:", "[Answer to be judged]: " + answer + '\n'), ak) | |
| def alternate_judge(rule, answer, ak): | |
| maxtry = 10 | |
| while True: | |
| try: | |
| out = judge(rule, answer, ak) | |
| bitout = check_judge(out) | |
| return out, bitout | |
| except Exception as e: | |
| if maxtry <= 0: | |
| return None, "0" | |
| if not isinstance(e, KeyError): | |
| maxtry -= 1 | |
| print(e) | |
| else: | |
| print("Request Error: " + str(e)) | |
| print("Retrying...") | |
| time.sleep(random.uniform(1, 2)) | |
| continue | |
| def count_lines(file_path: str): | |
| with open(file_path, 'r') as f: | |
| return sum(1 for _ in f) |