mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-12 23:01:41 +00:00
Add typing for litellm.moderation response obj
This commit is contained in:
parent
bb40f215e0
commit
40b3e43593
2 changed files with 25 additions and 8 deletions
|
|
@ -10,7 +10,10 @@ import litellm
|
|||
from litellm._logging import verbose_logger
|
||||
from litellm.constants import RESPONSE_FORMAT_TOOL_NAME
|
||||
from litellm.types.llms.databricks import DatabricksTool
|
||||
from litellm.types.llms.openai import ChatCompletionThinkingBlock
|
||||
from litellm.types.llms.openai import (
|
||||
ChatCompletionThinkingBlock,
|
||||
OpenAIModerationResponse,
|
||||
)
|
||||
from litellm.types.utils import (
|
||||
ChatCompletionDeltaToolCall,
|
||||
ChatCompletionMessageToolCall,
|
||||
|
|
@ -297,6 +300,12 @@ class LiteLLMResponseObjectHandler:
|
|||
model_response_object = ImageResponse(**model_response_dict)
|
||||
return model_response_object
|
||||
|
||||
@staticmethod
|
||||
def convert_to_moderation_response(
|
||||
response_object: dict,
|
||||
) -> OpenAIModerationResponse:
|
||||
return OpenAIModerationResponse(**response_object)
|
||||
|
||||
@staticmethod
|
||||
def convert_chat_to_text_completion(
|
||||
response: ModelResponse,
|
||||
|
|
@ -500,9 +509,9 @@ def convert_to_model_response_object( # noqa: PLR0915
|
|||
provider_specific_fields["thinking_blocks"] = thinking_blocks
|
||||
|
||||
if reasoning_content:
|
||||
provider_specific_fields[
|
||||
"reasoning_content"
|
||||
] = reasoning_content
|
||||
provider_specific_fields["reasoning_content"] = (
|
||||
reasoning_content
|
||||
)
|
||||
|
||||
message = Message(
|
||||
content=content,
|
||||
|
|
|
|||
|
|
@ -183,6 +183,7 @@ from .types.llms.openai import (
|
|||
ChatCompletionUserMessage,
|
||||
HttpxBinaryResponseContent,
|
||||
ImageGenerationRequestQuality,
|
||||
OpenAIModerationResponse,
|
||||
)
|
||||
from .types.utils import (
|
||||
LITELLM_IMAGE_VARIATION_PROVIDERS,
|
||||
|
|
@ -4436,7 +4437,7 @@ def adapter_completion(
|
|||
|
||||
def moderation(
|
||||
input: str, model: Optional[str] = None, api_key: Optional[str] = None, **kwargs
|
||||
):
|
||||
) -> OpenAIModerationResponse:
|
||||
# only supports open ai for now
|
||||
api_key = (
|
||||
api_key
|
||||
|
|
@ -4455,7 +4456,11 @@ def moderation(
|
|||
response = openai_client.moderations.create(input=input, model=model)
|
||||
else:
|
||||
response = openai_client.moderations.create(input=input)
|
||||
return response
|
||||
|
||||
response_dict: Dict = response.model_dump()
|
||||
return litellm.utils.LiteLLMResponseObjectHandler.convert_to_moderation_response(
|
||||
response_object=response_dict,
|
||||
)
|
||||
|
||||
|
||||
@client
|
||||
|
|
@ -4465,7 +4470,7 @@ async def amoderation(
|
|||
api_key: Optional[str] = None,
|
||||
custom_llm_provider: Optional[str] = None,
|
||||
**kwargs,
|
||||
):
|
||||
) -> OpenAIModerationResponse:
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
# only supports open ai for now
|
||||
|
|
@ -4506,7 +4511,10 @@ async def amoderation(
|
|||
response = await _openai_client.moderations.create(input=input, model=model)
|
||||
else:
|
||||
response = await _openai_client.moderations.create(input=input)
|
||||
return response
|
||||
response_dict: Dict = response.model_dump()
|
||||
return litellm.utils.LiteLLMResponseObjectHandler.convert_to_moderation_response(
|
||||
response_object=response_dict,
|
||||
)
|
||||
|
||||
|
||||
##### Image Generation #######################
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue