Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redundant implementations of get_chat_response #16

Open
mattmazzola opened this issue Feb 12, 2024 · 0 comments · May be fixed by #22
Open

Redundant implementations of get_chat_response #16

mattmazzola opened this issue Feb 12, 2024 · 0 comments · May be fixed by #22

Comments

@mattmazzola
Copy link
Contributor

There is an implementation in utilities#get_chat_response and models/gpt#get_response.
These could be unified

MathVista/utilities.py

Lines 159 to 199 in 82f68d0

def get_chat_response(promot, api_key, model="gpt-3.5-turbo", temperature=0, max_tokens=256, n=1, patience=10000000,
sleep_time=0):
messages = [
{"role": "user", "content": promot},
]
# print("I am here")
while patience > 0:
patience -= 1
try:
response = openai.ChatCompletion.create(model=model,
messages=messages,
api_key=api_key,
temperature=temperature,
max_tokens=max_tokens,
n=n)
if n == 1:
prediction = response['choices'][0]['message']['content'].strip()
if prediction != "" and prediction != None:
return prediction
else:
prediction = [choice['message']['content'].strip() for choice in response['choices']]
if prediction[0] != "" and prediction[0] != None:
return prediction
except Exception as e:
if "Rate limit" not in str(e):
print(e)
if "Please reduce the length of the messages" in str(e):
print("!!Reduce promot size")
# reduce input prompt and keep the tail
new_size = int(len(promot) * 0.9)
new_start = len(promot) - new_size
promot = promot[new_start:]
messages = [
{"role": "user", "content": promot},
]
if sleep_time > 0:
time.sleep(sleep_time)
return ""

MathVista/models/gpt.py

Lines 16 to 55 in 82f68d0

def get_response(self, image_path, user_prompt):
patience = self.patience
max_tokens = self.max_tokens
messages = [
{"role": "user", "content": user_prompt},
]
while patience > 0:
patience -= 1
try:
# print("self.model", self.model)
response = openai.ChatCompletion.create(model=self.model,
messages=messages,
api_key=self.api_key,
temperature=self.temperature,
max_tokens=max_tokens,
n=self.n
)
if self.n == 1:
prediction = response['choices'][0]['message']['content'].strip()
if prediction != "" and prediction != None:
return prediction
else:
prediction = [choice['message']['content'].strip() for choice in response['choices']]
if prediction[0] != "" and prediction[0] != None:
return prediction
except Exception as e:
if "limit" not in str(e):
print(e)
if "Please reduce the length of the messages or completion" in str(e):
max_tokens = int(max_tokens * 0.9)
print("!!Reduce max_tokens to", max_tokens)
if max_tokens < 8:
return ""
if "Please reduce the length of the messages." in str(e):
print("!!Reduce user_prompt to", user_prompt[:-1])
return ""
if self.sleep_time > 0:
time.sleep(self.sleep_time)
return ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant