forked from phoenix/litellm-mirror
fix(openai.py): drop invalid params if drop_params: true
for azure ai
Fixes https://github.com/BerriAI/litellm/issues/4800
This commit is contained in:
parent
576cccaade
commit
86c9e05c10
2 changed files with 78 additions and 43 deletions
|
@ -984,47 +984,79 @@ class OpenAIChatCompletion(BaseLLM):
|
||||||
headers=None,
|
headers=None,
|
||||||
):
|
):
|
||||||
response = None
|
response = None
|
||||||
try:
|
for _ in range(
|
||||||
openai_aclient = self._get_openai_client(
|
2
|
||||||
is_async=True,
|
): # if call fails due to alternating messages, retry with reformatted message
|
||||||
api_key=api_key,
|
try:
|
||||||
api_base=api_base,
|
openai_aclient = self._get_openai_client(
|
||||||
timeout=timeout,
|
is_async=True,
|
||||||
max_retries=max_retries,
|
api_key=api_key,
|
||||||
organization=organization,
|
api_base=api_base,
|
||||||
client=client,
|
timeout=timeout,
|
||||||
)
|
max_retries=max_retries,
|
||||||
|
organization=organization,
|
||||||
|
client=client,
|
||||||
|
)
|
||||||
|
|
||||||
## LOGGING
|
## LOGGING
|
||||||
logging_obj.pre_call(
|
logging_obj.pre_call(
|
||||||
input=data["messages"],
|
input=data["messages"],
|
||||||
api_key=openai_aclient.api_key,
|
api_key=openai_aclient.api_key,
|
||||||
additional_args={
|
additional_args={
|
||||||
"headers": {"Authorization": f"Bearer {openai_aclient.api_key}"},
|
"headers": {
|
||||||
"api_base": openai_aclient._base_url._uri_reference,
|
"Authorization": f"Bearer {openai_aclient.api_key}"
|
||||||
"acompletion": True,
|
},
|
||||||
"complete_input_dict": data,
|
"api_base": openai_aclient._base_url._uri_reference,
|
||||||
},
|
"acompletion": True,
|
||||||
)
|
"complete_input_dict": data,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
headers, response = await self.make_openai_chat_completion_request(
|
headers, response = await self.make_openai_chat_completion_request(
|
||||||
openai_aclient=openai_aclient, data=data, timeout=timeout
|
openai_aclient=openai_aclient, data=data, timeout=timeout
|
||||||
)
|
)
|
||||||
stringified_response = response.model_dump()
|
stringified_response = response.model_dump()
|
||||||
logging_obj.post_call(
|
logging_obj.post_call(
|
||||||
input=data["messages"],
|
input=data["messages"],
|
||||||
api_key=api_key,
|
api_key=api_key,
|
||||||
original_response=stringified_response,
|
original_response=stringified_response,
|
||||||
additional_args={"complete_input_dict": data},
|
additional_args={"complete_input_dict": data},
|
||||||
)
|
)
|
||||||
logging_obj.model_call_details["response_headers"] = headers
|
logging_obj.model_call_details["response_headers"] = headers
|
||||||
return convert_to_model_response_object(
|
return convert_to_model_response_object(
|
||||||
response_object=stringified_response,
|
response_object=stringified_response,
|
||||||
model_response_object=model_response,
|
model_response_object=model_response,
|
||||||
hidden_params={"headers": headers},
|
hidden_params={"headers": headers},
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except openai.UnprocessableEntityError as e:
|
||||||
raise e
|
## check if body contains unprocessable params - related issue https://github.com/BerriAI/litellm/issues/4800
|
||||||
|
if litellm.drop_params is True:
|
||||||
|
if e.body is not None and e.body.get("detail"): # type: ignore
|
||||||
|
detail = e.body.get("detail") # type: ignore
|
||||||
|
invalid_params: List[str] = []
|
||||||
|
if (
|
||||||
|
isinstance(detail, List)
|
||||||
|
and len(detail) > 0
|
||||||
|
and isinstance(detail[0], dict)
|
||||||
|
):
|
||||||
|
for error_dict in detail:
|
||||||
|
if (
|
||||||
|
error_dict.get("loc")
|
||||||
|
and isinstance(error_dict.get("loc"), list)
|
||||||
|
and len(error_dict.get("loc")) == 2
|
||||||
|
):
|
||||||
|
invalid_params.append(error_dict["loc"][1])
|
||||||
|
|
||||||
|
new_data = {}
|
||||||
|
for k, v in data.items():
|
||||||
|
if k not in invalid_params:
|
||||||
|
new_data[k] = v
|
||||||
|
data = new_data
|
||||||
|
else:
|
||||||
|
raise e
|
||||||
|
# e.message
|
||||||
|
except Exception as e:
|
||||||
|
raise e
|
||||||
|
|
||||||
def streaming(
|
def streaming(
|
||||||
self,
|
self,
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
model_list:
|
model_list:
|
||||||
- model_name: azure-chatgpt
|
- model_name: azure-mistral
|
||||||
litellm_params:
|
litellm_params:
|
||||||
model: azure/chatgpt-v-2
|
model: azure_ai/mistral
|
||||||
api_key: os.environ/AZURE_API_KEY
|
api_key: os.environ/AZURE_AI_MISTRAL_API_KEY
|
||||||
api_base: os.environ/AZURE_API_BASE
|
api_base: os.environ/AZURE_AI_MISTRAL_API_BASE
|
||||||
|
|
||||||
|
litellm_settings:
|
||||||
|
drop_params: true
|
Loading…
Add table
Add a link
Reference in a new issue