mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-25 18:54:30 +00:00
All checks were successful
Read Version from pyproject.toml / read-version (push) Successful in 44s
* feat(base_llm): initial commit for common base config class Addresses code qa critique https://github.com/andrewyng/aisuite/issues/113#issuecomment-2512369132 * feat(base_llm/): add transform request/response abstract methods to base config class * feat(cohere-+-clarifai): refactor integrations to use common base config class * fix: fix linting errors * refactor(anthropic/): move anthropic + vertex anthropic to use base config * test: fix xai test * test: fix tests * fix: fix linting errors * test: comment out WIP test * fix(transformation.py): fix is pdf used check * fix: fix linting error
79 lines
2.6 KiB
Python
79 lines
2.6 KiB
Python
"""
|
|
Common helpers / utils across al OpenAI endpoints
|
|
"""
|
|
|
|
import json
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
import httpx
|
|
import openai
|
|
|
|
from litellm.llms.base_llm.transformation import BaseLLMException
|
|
|
|
|
|
class OpenAIError(BaseLLMException):
|
|
def __init__(
|
|
self,
|
|
status_code: int,
|
|
message: str,
|
|
request: Optional[httpx.Request] = None,
|
|
response: Optional[httpx.Response] = None,
|
|
headers: Optional[httpx.Headers] = None,
|
|
):
|
|
self.status_code = status_code
|
|
self.message = message
|
|
self.headers = headers
|
|
if request:
|
|
self.request = request
|
|
else:
|
|
self.request = httpx.Request(method="POST", url="https://api.openai.com/v1")
|
|
if response:
|
|
self.response = response
|
|
else:
|
|
self.response = httpx.Response(
|
|
status_code=status_code, request=self.request
|
|
)
|
|
super().__init__(
|
|
status_code=status_code,
|
|
message=self.message,
|
|
headers=self.headers,
|
|
request=self.request,
|
|
response=self.response,
|
|
)
|
|
|
|
|
|
####### Error Handling Utils for OpenAI API #######################
|
|
###################################################################
|
|
def drop_params_from_unprocessable_entity_error(
|
|
e: openai.UnprocessableEntityError, data: Dict[str, Any]
|
|
) -> Dict[str, Any]:
|
|
"""
|
|
Helper function to read OpenAI UnprocessableEntityError and drop the params that raised an error from the error message.
|
|
|
|
Args:
|
|
e (UnprocessableEntityError): The UnprocessableEntityError exception
|
|
data (Dict[str, Any]): The original data dictionary containing all parameters
|
|
|
|
Returns:
|
|
Dict[str, Any]: A new dictionary with invalid parameters removed
|
|
"""
|
|
invalid_params: List[str] = []
|
|
if e.body is not None and isinstance(e.body, dict) and e.body.get("message"):
|
|
message = e.body.get("message", {})
|
|
if isinstance(message, str):
|
|
try:
|
|
message = json.loads(message)
|
|
except json.JSONDecodeError:
|
|
message = {"detail": message}
|
|
detail = message.get("detail")
|
|
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 = {k: v for k, v in data.items() if k not in invalid_params}
|
|
return new_data
|