Ë
    uPêjŽ"  ã                   ó¨   — d Z ddlmZmZmZ ddlZddlmZ ddlm	Z	 ddl
mZmZ ddlmZmZmZ ddlmZmZ erddlZdd	lmZ eZneZ G d
„ de«      Zy)z½
Stability AI Image Generation Config

Handles transformation between OpenAI-compatible format and Stability AI API format.

API Reference: https://platform.stability.ai/docs/api-reference
é    )ÚTYPE_CHECKINGÚAnyÚFinalN)ÚBaseImageGenerationConfig)Úget_secret_str)ÚAllMessageValuesÚ#OpenAIImageGenerationOptionalParams)Ú%OPENAI_SIZE_TO_STABILITY_ASPECT_RATIOÚSTABILITY_GENERATION_MODELSÚStabilityImageGenerationRequest)ÚImageObjectÚImageResponse)ÚLoggingc                   ób  — e Zd ZU dZdZeed<   dedee   fd„Z	de
de
ded	ede
f
d
„Zdedefd„Z	 d dedz  dedz  dede
de
dedz  defd„Z	 	 d!de
dedee   de
de
dedz  dedz  de
fd„Zdedede
de
de
de
fd„Z	 	 d!dedej&                  dedede
de
de
dddedz  dedz  defd„Zdefd„Zy)"ÚStabilityImageGenerationConfigz 
    Configuration for Stability AI image generation.

    Supports:
    - Stable Diffusion 3 (SD3, SD3.5)
    - Stable Image Ultra
    - Stable Image Core
    zhttps://api.stability.aiÚDEFAULT_BASE_URLÚmodelÚreturnc                 ó
   — g d¢S )zƒ
        Return list of OpenAI params supported by Stability AI.

        https://platform.stability.ai/docs/api-reference
        )ÚnÚsizeÚresponse_format© )Úselfr   s     ú{/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/llms/stability/image_generation/transformation.pyÚget_supported_openai_paramsz:StabilityImageGenerationConfig.get_supported_openai_params2   s   € ò
ð 	
ó    Únon_default_paramsÚoptional_paramsÚdrop_paramsc           
      ó  — | j                  |«      }|j                  «       D ][  \  }}||vsŒ||v r6|dk(  r|t        v rt        |   |d<   Œ)|dk(  r||d<   Œ4|dk(  r||d<   Œ?|||<   ŒE|rŒHt        d|› d|› d	|› d
�«      ‚ |S )zÔ
        Map OpenAI parameters to Stability AI parameters.

        OpenAI -> Stability mappings:
        - size -> aspect_ratio
        - n -> (handled separately, Stability returns 1 image per request)
        r   Úaspect_ratior   Ú_nr   Ú_response_formatz
Parameter z is not supported for model z. Supported parameters are z6. Set drop_params=True to drop unsupported parameters.)r   Úitemsr
   Ú
ValueError)r   r   r   r   r    Úsupported_paramsÚkÚvs           r   Úmap_openai_paramsz0StabilityImageGenerationConfig.map_openai_params>   sÒ   € ð #'×"BÑ"BÀ5Ó"IÐà&×,Ñ,Ó.ò 	‰DˆAˆqØ˜Ò'ØÐ(Ñ(à˜F’{ qÔ,QÑ'QÜ:_Ð`aÑ:b˜¨Ò7Ø˜cšà01˜¨Ò-ØÐ/Ò/à>?˜Ð(:Ò;à-.˜¨Ò*Ù Øä$Ø$ Q CÐ'CÀEÀ7ð K4Ø4DÐ3Eð FOðPóð ð#	ð. Ðr   c                 óŽ   — |j                  «       }|j                  d«      }t        j                  «       D ]  \  }}||v sŒ|c S  y)z9
        Get the API endpoint for a given model.
        z
stability/z!/v2beta/stable-image/generate/sd3)ÚlowerÚremoveprefixr   r%   )r   r   Ú
model_nameÚkeyÚendpoints        r   Ú_get_model_endpointz2StabilityImageGenerationConfig._get_model_endpointg   sO   € ð
 —[‘[“]ˆ
Ø×,Ñ,¨\Ó:ˆ
ô 9×>Ñ>Ó@ò 	 ‰MˆC�Ø�jÒ Ø’ð	 ð
 3r   NÚapi_baseÚapi_keyÚlitellm_paramsÚstreamc                 óŒ   — |xs t        d«      xs | j                  }|j                  d«      }| j                  |«      }|› |› �S )zH
        Get the complete URL for the Stability AI API request.
        ÚSTABILITY_API_BASEú/)r   r   Úrstripr1   )	r   r2   r3   r   r   r4   r5   Úbase_urlr0   s	            r   Úget_complete_urlz/StabilityImageGenerationConfig.get_complete_urlw   sL   € ð !Òa¤NÐ3GÓ$HÒaÈD×LaÑLaˆØ—?‘? 3Ó'ˆà×2Ñ2°5Ó9ˆØ�˜H˜:Ð&Ð&r   ÚheadersÚmessagesc                 óX   — |xs t        d«      }|st        d«      ‚d|› �|d<   d|d<   |S )zK
        Validate environment and set up headers for Stability AI.
        ÚSTABILITY_API_KEYz_STABILITY_API_KEY is not set. Please set it via environment variable or pass api_key parameter.zBearer ÚAuthorizationzapplication/jsonÚAccept)r   r&   )	r   r<   r   r=   r   r4   r3   r2   Úfinal_api_keys	            r   Úvalidate_environmentz3StabilityImageGenerationConfig.validate_environment‰   sI   € ð ,3Ò+Y´nÐEXÓ6YˆáÜØqóð ð &-¨]¨OÐ#<ˆ�Ñ Ø.ˆ�ÑØˆr   Úpromptc                 óŠ   — |ddœ}|j                  «       D ]!  \  }}|j                  d«      rŒ|dv sŒ|||<   Œ# t        |«      S )zÖ
        Transform OpenAI-style request to Stability AI request format.

        Note: Stability AI uses multipart/form-data, but the HTTP handler
        will handle the conversion from dict to form data.
        Úpng)rD   Úoutput_formatÚ_)Únegative_promptr"   ÚseedrG   r   ÚmodeÚstrengthÚstyle_preset)r%   Ú
startswithÚdict)	r   r   rD   r   r4   r<   Ústability_requestr/   Úvalues	            r   Ú"transform_image_generation_requestzAStabilityImageGenerationConfig.transform_image_generation_request¡   sh   € ð  Ø"ñE
Ðð *×/Ñ/Ó1ò 	/‰JˆC�à�~‰~˜cÔ"Øàð 	ò 	ð */Ð! #Ò&ð	/ô" Ð%Ó&Ð&r   Úraw_responseÚmodel_responseÚlogging_objÚrequest_dataÚencodingztiktoken.Encoding | NoneÚ	json_modec                 ó  — 	 |j                  «       }d|v r.| j                  d|d   › �|j                  |j                  ¬«      ‚|j                  dd«      }|dk(  r| j                  d	d
|j                  ¬«      ‚|j                  sg |_        |j                  d«      }|r'|j                  j                  t        |dd¬«      «       |S # t        $ r0}| j                  d|› �|j                  |j                  ¬«      ‚d}~ww xY w)a  
        Transform Stability AI response to OpenAI-compatible ImageResponse.

        Stability returns: {"image": "base64...", "finish_reason": "SUCCESS", "seed": 123}
        OpenAI expects: {"data": [{"b64_json": "base64..."}], "created": timestamp}
        z%Error parsing Stability AI response: )Úerror_messageÚstatus_coder<   NÚerrorszStability AI error: Úfinish_reasonÚ ÚCONTENT_FILTEREDz3Content was filtered by Stability AI safety systemsi�  Úimage)Úb64_jsonÚurlÚrevised_prompt)	ÚjsonÚ	ExceptionÚget_error_classr[   r<   ÚgetÚdataÚappendr   )r   r   rS   rT   rU   rV   r   r4   rW   r3   rX   Úresponse_dataÚer]   Ú	image_b64s                  r   Ú#transform_image_generation_responsezBStabilityImageGenerationConfig.transform_image_generation_responseÉ   s9  € ð&	Ø#/×#4Ñ#4Ó#6ˆMð �}Ñ$Ø×&Ñ&Ø 4°]À8Ñ5LÐ4MÐNØ(×4Ñ4Ø$×,Ñ,ð 'ó ð ð  -×0Ñ0°À"ÓEˆØÐ.Ò.Ø×&Ñ&ØSØØ$×,Ñ,ð 'ó ð ð ×"Ò"Ø"$ˆNÔð )×,Ñ,¨WÓ5ˆ	ÙØ×Ñ×&Ñ&ÜØ&ØØ#'ôôð ÐøôM ò 	Ø×&Ñ&Ø EÀaÀSÐIØ(×4Ñ4Ø$×,Ñ,ð 'ó ð ûð	ús   ‚C Ã	DÃ+C<Ã<Dc                  ó   — y)zQ
        Stability AI requires multipart/form-data for image generation.
        Tr   )r   s    r   Úuse_multipart_form_dataz6StabilityImageGenerationConfig.use_multipart_form_data  s   € ð r   )N)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚstrÚ__annotations__Úlistr	   r   rO   Úboolr*   r1   r;   r   rC   rR   ÚhttpxÚResponser   ÚLiteLLMLoggingObjrm   ro   r   r   r   r   r   &   s  … ñð 7Ð�cÓ6ð

°ð 

¸Ð>aÑ9bó 

ð'à ð'ð ð'ð ð	'ð
 ð'ð 
ó'ðR3¨ð 3°ó 3ð. #ñ'à˜‘*ð'ð �t‘ð'ð ð	'ð
 ð'ð ð'ð �t‘ð'ð 
ó'ð2 #Ø#ñàðð ðð Ð'Ñ(ð	ð
 ðð ðð �t‘ðð ˜‘*ðð 
óð0&'àð&'ð ð&'ð ð	&'ð
 ð&'ð ð&'ð 
ó&'ðd #Ø!%ñ;àð;ð —n‘nð;ð &ð	;ð
 'ð;ð ð;ð ð;ð ð;ð -ð;ð �t‘ð;ð ˜$‘;ð;ð 
ó;ðz¨ô r   r   )rs   Útypingr   r   r   rx   Ú5litellm.llms.base_llm.image_generation.transformationr   Úlitellm.secret_managers.mainr   Úlitellm.types.llms.openair   r	   Úlitellm.types.llms.stabilityr
   r   r   Úlitellm.types.utilsr   r   ÚtiktokenÚ*litellm.litellm_core_utils.litellm_loggingr   Ú_LiteLLMLoggingObjrz   r   r   r   r   ú<module>r„      sW   ðñ÷ -Ñ ,ã õõ 8÷÷ñ ÷
 ;áÛåXà*ÑàÐôdÐ%>õ dr   