mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 03:04:13 +00:00
(fix) completion gpt-4 vision check finish_details or finish_reason
This commit is contained in:
parent
fd6064b571
commit
4d67cee135
1 changed files with 10 additions and 6 deletions
|
@ -1121,7 +1121,7 @@ def openai_token_counter(messages, model="gpt-3.5-turbo-0613"):
|
||||||
try:
|
try:
|
||||||
encoding = tiktoken.encoding_for_model(model)
|
encoding = tiktoken.encoding_for_model(model)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print("Warning: model not found. Using cl100k_base encoding.")
|
print_verbose("Warning: model not found. Using cl100k_base encoding.")
|
||||||
encoding = tiktoken.get_encoding("cl100k_base")
|
encoding = tiktoken.get_encoding("cl100k_base")
|
||||||
if model in {
|
if model in {
|
||||||
"gpt-3.5-turbo-0613",
|
"gpt-3.5-turbo-0613",
|
||||||
|
@ -1137,10 +1137,10 @@ def openai_token_counter(messages, model="gpt-3.5-turbo-0613"):
|
||||||
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
|
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
|
||||||
tokens_per_name = -1 # if there's a name, the role is omitted
|
tokens_per_name = -1 # if there's a name, the role is omitted
|
||||||
elif "gpt-3.5-turbo" in model:
|
elif "gpt-3.5-turbo" in model:
|
||||||
print("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.")
|
print_verbose("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.")
|
||||||
return openai_token_counter(messages, model="gpt-3.5-turbo-0613")
|
return openai_token_counter(messages, model="gpt-3.5-turbo-0613")
|
||||||
elif "gpt-4" in model:
|
elif "gpt-4" in model:
|
||||||
print("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.")
|
print_verbose("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.")
|
||||||
return openai_token_counter(messages, model="gpt-4-0613")
|
return openai_token_counter(messages, model="gpt-4-0613")
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
|
@ -2618,7 +2618,11 @@ def convert_to_model_response_object(response_object: Optional[dict]=None, model
|
||||||
choice_list=[]
|
choice_list=[]
|
||||||
for idx, choice in enumerate(response_object["choices"]):
|
for idx, choice in enumerate(response_object["choices"]):
|
||||||
message = Message(content=choice["message"]["content"], role=choice["message"]["role"], function_call=choice["message"].get("function_call", None))
|
message = Message(content=choice["message"]["content"], role=choice["message"]["role"], function_call=choice["message"].get("function_call", None))
|
||||||
choice = Choices(finish_reason=choice["finish_reason"], index=idx, message=message)
|
finish_reason = choice.get("finish_reason", None)
|
||||||
|
if finish_reason == None:
|
||||||
|
# gpt-4 vision can return 'finish_reason' or 'finish_details'
|
||||||
|
finish_reason = choice.get("finish_details")
|
||||||
|
choice = Choices(finish_reason=finish_reason, index=idx, message=message)
|
||||||
choice_list.append(choice)
|
choice_list.append(choice)
|
||||||
model_response_object.choices = choice_list
|
model_response_object.choices = choice_list
|
||||||
|
|
||||||
|
@ -2631,8 +2635,8 @@ def convert_to_model_response_object(response_object: Optional[dict]=None, model
|
||||||
if "model" in response_object:
|
if "model" in response_object:
|
||||||
model_response_object.model = response_object["model"]
|
model_response_object.model = response_object["model"]
|
||||||
return model_response_object
|
return model_response_object
|
||||||
except:
|
except Exception as e:
|
||||||
Exception("Invalid response object.")
|
raise Exception(f"Invalid response object {e}")
|
||||||
|
|
||||||
|
|
||||||
# NOTE: DEPRECATING this in favor of using success_handler() in Logging:
|
# NOTE: DEPRECATING this in favor of using success_handler() in Logging:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue