| 16 | GPT4_API_KEY = '' |
| 17 | GPT_MODEL = 'gpt-4o-2024-05-13' |
| 18 | def get_response_gpt4(prompt, max_new_tokens=1024, temperature=1.0, stop=None): |
| 19 | tries = 0 |
| 20 | while tries < 10: |
| 21 | tries += 1 |
| 22 | try: |
| 23 | headers = { |
| 24 | 'Authorization': "Bearer {}".format(GPT4_API_KEY), |
| 25 | } |
| 26 | messages = [ |
| 27 | {'role': 'user', 'content': prompt}, |
| 28 | ] |
| 29 | resp = requests.post("https://api.openai.com/v1/chat/completions", json = { |
| 30 | "model": GPT_MODEL, |
| 31 | "messages": messages, |
| 32 | "temperature": temperature, |
| 33 | "max_tokens": max_new_tokens, |
| 34 | "stop": stop, |
| 35 | }, headers=headers, timeout=600) |
| 36 | if resp.status_code != 200: |
| 37 | raise Exception(resp.text) |
| 38 | resp = resp.json() |
| 39 | break |
| 40 | except KeyboardInterrupt as e: |
| 41 | raise e |
| 42 | except Exception as e: |
| 43 | if "maximum context length" in str(e): |
| 44 | raise e |
| 45 | elif "triggering" in str(e): |
| 46 | return 'Trigger OpenAI\'s content management policy' |
| 47 | print("Error Occurs: \"%s\" Retry ..."%(str(e))) |
| 48 | else: |
| 49 | print("Max tries. Failed.") |
| 50 | return "Max tries. Failed." |
| 51 | try: |
| 52 | return resp["choices"][0]["message"]["content"] |
| 53 | except: |
| 54 | return '' |
| 55 | |
| 56 | def get_pred(rank, world_size, data, max_new_tokens, fout, template, cache_fout, cache_dict): |
| 57 | for item in tqdm(data): |