-
Notifications
You must be signed in to change notification settings - Fork 8
/
call_agent.py
54 lines (48 loc) · 1.62 KB
/
call_agent.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
import argparse
import openai
import pandas as pd
from tqdm import tqdm
from IPython import embed
import time
from random import random, uniform
import warnings
import os
import requests
import ast
warnings.filterwarnings("ignore")
# User inputs:
# Load your API key from an environment variable or secret management service
# openai.api_key = os.getenv("OPENAI_API_KEY")
# OR
# Load your API key manually:
# openai.api_key = API_KEY
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.organization = os.getenv("OPENAI_ORGANIZATION")
def ask_agent(model,history):
count = 0
try:
count+=1
if model in ['gpt-4-vision-preview','gpt-4-turbo']:
params = {
"model": model,
"messages": history,
"max_tokens": 4096,
}
r = openai.ChatCompletion.create(**params)
resp = r['choices'][0]['message']['content']
else:
print(f"Unrecognize model name: {model}")
return 0
except Exception as e:
print('openai-api error: ',e)
if ((e.http_status==429) or (e.http_status==502) or (e.http_status==500)) :
time.sleep(60+10*random()) # wait for 60 seconds and try again
if count < 25:
resp = ask_model(model,history)
else: return e
elif (e.http_status==400): # images cannot be process due to safty filter
if (len(history) == 4) or (len(history) == 2): # if dataset exemplars are unsafe
return e
else: # else, remove the lase experiment and try again
resp = ask_model(model,history[:-2])
return resp