
    hx              %       F   U d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
mZmZ d dlZd dlZd dlmZmZmZmZ d dlmZ d dlmZ d dlm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" d dl#m$Z$ d dl%m&Z&  e$       Z'e$e(d<   d dl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 d dl1m2Z2 d dl3m4Z4 d dl5m6Z6 d dl7m8Z8 d dl9m:Z:m;Z;m<Z<m=Z= d dl>m?Z?m@Z@mAZAmBZB ddlCmDZD ede?fd       ZEe	 	 	 	 	 	 	 	 	 	 	 	 	 d9deFde	eF   de	eG   de	e
eFe6f      de	eF   de	eF   d e	eF   d!e	eF   d"e	eF   d#e	eF   d$e	eF   d%e	eF   d&ed'   deeee?f   fd(       ZHe	 	 	 	 	 	 	 	 	 	 	 	 	 d9d)d*deFde	eF   de	eG   de	e
eFe6f      de	eF   de	eF   d e	eF   d!e	eF   d"e	eF   d#e	eF   d$e	eF   d%e	eF   d&ed)   de?fd+       ZHe	 	 	 	 	 	 	 	 	 	 	 	 	 d9deFde	eF   de	eG   de	e
eFe6f      de	eF   de	eF   d e	eF   d!e	eF   d"e	eF   d#e	eF   d$e	eF   d%e	eF   de
e?eeee?f   f   fd,       ZHede?fd-       ZIe	 	 	 	 	 d:d.e;deFdeGded/   de	eF   d!e	eF   de?fd0       ZJe	 	 	 	 	 	 	 	 	 	 	 	 d;d.e;deFde	eF   d1e	eF   de	eG   de	e
eFe6f      de	eF   de	eF   d!e	eF   d2e	eeFef      d3e	eeFef      d4e	eeFef      d5e	e
eKej                  f      d6e	eF   de
e?eeee?f   f   fd7       ZMe	 	 	 	 	 	 	 	 	 	 	 d<d.e;deFdeFd1e	eF   de	eG   de	e
eFe6f      de	eF   de	eF   d!e	eF   d2e	eeFef      d3e	eeFef      d4e	eeFef      d5e	e
eKej                  f      d6e	eF   de?fd8       ZNy)=    N)partial)Any	CoroutineDictLiteralOptionalUnioncastoverload)Loggingclientexception_typeget_litellm_params)DEFAULT_IMAGE_ENDPOINT_MODEL)request_timeout)LiteLLMUnknownProvider)r   )mock_image_generation)BaseImageEditConfigBaseImageGenerationConfig)AsyncHTTPHandlerHTTPHandler)BaseLLMHTTPHandler)	CustomLLMllm_http_handler)azure_chat_completionsbase_llm_aiohttp_handlerbase_llm_http_handlerbedrock_image_generationopenai_chat_completionsopenai_image_variationsvertex_image_generation)get_secret_str)ImageEditOptionalRequestParams)ImageGenerationRequestQuality)GenericLiteLLMParams)!LITELLM_IMAGE_VARIATION_PROVIDERS	FileTypesLlmProvidersall_litellm_params)ImageResponseProviderConfigManagerget_llm_providerget_optional_params_image_gen   )ImageEditRequestUtilsreturnc                  x  K   t        j                         }t        |       dkD  r| d   n|d   }d|d<   d}	 t        t        g| i |}t        j                         }t        |j                  |      }t        ||j                  dd            \  }}}}|j                  d|       d{   }	d}
t        |	t              rt        di |	}
n2t        |	t              r|	}
nt        j                  |	      r
|	 d{   }
|
t        d      |
S 7 c7 # t         $ r}|xs d	}t#        |||| |
      d}~ww xY ww)a  
    Asynchronously calls the `image_generation` function with the given arguments and keyword arguments.

    Parameters:
    - `args` (tuple): Positional arguments to be passed to the `image_generation` function.
    - `kwargs` (dict): Keyword arguments to be passed to the `image_generation` function.

    Returns:
    - `response` (Any): The response returned by the `image_generation` function.
    r   modelTaimg_generationNapi_baser2   r4   z?Unable to get Image Response. Please pass a valid llm_provider.openair2   custom_llm_provideroriginal_exceptioncompletion_kwargsextra_kwargs )asyncioget_event_looplenr   image_generationcontextvarscopy_contextrunr,   getrun_in_executor
isinstancedictr*   iscoroutine
ValueError	Exceptionr   argskwargsloopr2   r8   funcctxfunc_with_context_init_responseresponsees               O/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/images/main.pyaimage_generationrW   6   sY     !!#D4y1}DG&/E $F%
'9$9&9 &&(#CGGT2'7&**Z">(
$1
 #2249JKK,0mT*$5}5H}5$H  /**HQ   L +  
1=X 3 "
 	

sN   4D:A4D +D,AD ;D<D D:D D 	D7D22D77D:promptr2   nqualityresponse_formatsizestyleuserinput_fidelityapi_keyr4   api_versionr3   Tc                     y Nr<   rX   r2   rY   rZ   r[   r\   r]   r^   r_   timeoutr`   r4   ra   r8   r3   rM   s                   rV   r@   r@   p       (     F)r3   c                     y rc   r<   rd   s                   rV   r@   r@      rf   rg   c                    	 t               }|j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  d	d      }|j                  d
i       }|j                  d      }|j                  dd      }|j                  dd      }|j                  dd      xs i }|j                  dd      }||j                  |       t        j                  j                         }d}||t        |||      \  }}}}nd}d}||j                  d<   g d}t        } || z   }!|j                         D "#ci c]  \  }"}#|"|!vs|"|# }$}"}#d}%|D|t        j                  j                         v r$t        j                  |xs |t        |            }%t        d?|xs ||||||||||%d
|$}&t!        d?i |}'|}(|(j#                  |||&|	d|||||di d	|       d|(j$                  vr||(j$                  d<   |t'        ||      S |dk(  rt)        d      xs d})|xs t        j*                  xs t)        d      }|xs t        j,                  xs t)        d      }|
xs< t        j.                  xs* t        j0                  xs t)        d      xs t)        d       }
|&j3                  d!d      xs t)        d"      }*d#|
d$}+|+j                         D ]  \  }"}#|"|vs|#||"<    t5        j6                  || |	|
||*|||&||||||'%      }|S |dk(  s|t        j8                  v r%t;        j6                  || |	|
xs ||||&|||&
      }|S |d'k(  r.|t=        d(      t?        j6                  || |	||&|||||
)
      }|S |d*k(  r|&j3                  d+d      xs1 |&j3                  d,d      xs t        j@                  xs t)        d-      },|&j3                  d.d      xs1 |&j3                  d/d      xs t        jB                  xs t)        d0      }-|&j3                  d1d      xs |&j3                  d2d      xs t)        d3      }.|xs* t        j*                  xs t)        d4      xs t)        d5      }tE        j6                  || |	||&||,|-|.|||6      }|S |t        j                  jF                  fv r.|%tI        d7|       tJ        jM                  || |%|&||'||	|8	      S |t        jN                  v rd}/t        jP                  D ]  }0|0d9   |k(  s|0d:   }/ |/tS        ||;      |d<u r2d}1|tU        |tV              r|}1|/jY                  || |
|||&||	|1=	      }|S d}2|tU        |tZ              r|}2|/j7                  || |
|||&||	|2=	      }|S c c}#}"w # t<        $ r}3t]        |||3t               |>      d}3~3ww xY w)@zv
    Maps the https://api.openai.com/v1/images/generations endpoint.

    Currently supports just Azure + OpenAI.
    r3   Flitellm_call_idN	logger_fnmock_responseproxy_server_requestazure_ad_token_provider
model_infometadatalitellm_logging_objr   extra_headersheaders
base_model)r2   r8   r4   dall-e-2r6   r2   )r^   r   r4   ra   r`   deployment_idorganizationbase_urldefault_headersre   max_retriesrY   rZ   r\   r]   r_   r2   provider)
r2   rY   rZ   r[   r\   r]   r^   r_   r8   provider_config)	re   azurerj   rk   rm   ro   rp   preset_cache_keystream_responser2   r^   optional_paramslitellm_paramsr8   r8   )r2   rl   r~   AZURE_API_TYPEAZURE_API_BASEAZURE_API_VERSIONAZURE_OPENAI_API_KEYAZURE_API_KEYazure_ad_tokenAZURE_AD_TOKENzapplication/json;)zContent-Typezapi-key)r2   rX   re   r`   r4   r   rn   logging_objr   model_responsera   r3   r   rs   r   )
r2   rX   re   r`   r4   r   r   r   r3   r   bedrockz!Model needs to be set for bedrock)
r2   rX   re   r   r   r   r3   r   r4   r`   	vertex_aivertex_projectvertex_ai_projectVERTEXAI_PROJECTvertex_locationvertex_ai_locationVERTEXAI_LOCATIONvertex_credentialsvertex_ai_credentialsVERTEXAI_CREDENTIALSVERTEXAI_API_BASEVERTEX_API_BASE)r2   rX   re   r   r   r   r   r   r   r3   r4   r   z-image generation config is not supported for )	r2   rX    image_generation_provider_config(image_generation_optional_request_paramsr8   r   r   re   r   r|   custom_handlerr2   r8   T)	r2   rX   r`   r4   r   r   r   re   r   r7   r<   )/localsrD   updatelitellmutilsr*   r,   _hidden_paramsr)   itemsr(   _member_map_valuesr+   $get_provider_image_generation_configr-   r   update_environment_variablesmodel_call_detailsr   r"   r4   ra   r`   	azure_keypopr   r@   openai_compatible_providersr   rJ   r   r   r   r!   RECRAFTrI   r   image_generation_handler_custom_providerscustom_provider_mapr   rF   r   rW   r   r   )4rX   r2   rY   rZ   r[   r\   r]   r^   r_   re   r`   r4   ra   r8   rM   rL   r3   rj   rk   rl   rm   rn   ro   rp   rq   r   rr   rs   rt   r   dynamic_api_keyopenai_paramsr   default_paramskvnon_default_paramsimage_generation_configr   litellm_params_dictloggingapi_typer   ry   r   r   r   r   itemasync_custom_clientcustom_clientrU   s4                                                       rV   r@   r@      s   4o
x **%6> **%6=JJ{D1	'-zz/4'H%zz*@$G"(**-F"MZZd3
::j"-17<Q1RHd+

?D9

9d39rZZd3
$NN=)(/(C(C(E)- 3 ?DT$7!EAE& E"*16%%g.
$ ,&7#\\^
Qq/FAqD
 
 HL+#|'@'@'G'G'II &JJ$-)*=> $ 8 
%+) 33
 !
 1:6:.,,+"#2&(<($$(#%
 !4 	- 	
" !(B(BB@SG&&'<=$(uMRR')%&67B7HW7#3#3W~FV7WH  7&&7!"56   3??3$$3 ""893 "/2  -00 $ 2 01 
 !4"O (--/1G#!"GAJ 0 4DD!-(?/ /-' /2N| Y  8+"g&I&II4EE2?!/ /- /NR { !I-} CDD5FF/ /- /!Nt ] !K/##$4d; 6"&&':DA6))6 ""45	   ##$5t< 7"&&';TB7**7 ""56	   ##$8$? :"&&'>E:!"89   5##5!"565 ""34	  5EE/ /-0 2#5 /!Nh G !  ((%
 
 '. #PQdPe!fgg#<<1H9H$72/ = 
 
  7#<#<< 37N33
#'::%)*:%;N 4 %,5H 
 $&BF#%*V=M*N*0' "0!A!A!#%#1$3 3#. "B 
": # 8<%*V[*I$*M "0!@!@!#%#1$3 3#( "A 
" k
l  
 3 $h
 	

sk   E7W 9WWB=W 	CW ,W ;W 2W :DW A
W /W 	AW 1W W 	W3W..W3c                    K   t        j                         }|j                  dd      }|j                  dd      }d|d<   	 t        t        g| i |}t        j                         }t        |j                  |      }|$|"t        ||j                  dd            \  }}}}|j                  d|       d{   }	t        |	t              st        |	t              rt        |	t              rt        d
i |	}	|	}
|
S t        j                  |	      r|	 d{   }
|
S |j                  d|       d{   }
|
S 7 7 $7 
# t        $ r}|xs d}t        |||| |	      d}~ww xY ww)a  
    Asynchronously calls the `image_variation` function with the given arguments and keyword arguments.

    Parameters:
    - `args` (tuple): Positional arguments to be passed to the `image_variation` function.
    - `kwargs` (dict): Keyword arguments to be passed to the `image_variation` function.

    Returns:
    - `response` (Any): The response returned by the `image_variation` function.
    r2   Nr8   T
async_callr4   r5   r6   r7   r<   )r=   r>   rD   r   image_variationrA   rB   rC   r,   rE   rF   rG   r*   rH   rJ   r   rK   s               rV   aimage_variationr     s     !!#DJJw%E **%:DAF<#
888 &&(#CGGT2&5+<+;fjjT&B,(A"Aq
 #2249JKKmT*j=/
 -. - > >$H    /**H  "11$8IJJH L + K 
1=X 3 "
 	

sy   >E'A8E  9D::AE  <E'=E  D<E  E'E  3D>4E  9E':E  <E  >E   	E$	EE$$E'image)urlb64_jsonc                 *   |j                  dd       }t        t        |j                  d            }t        di |}	t	        ||	j                  dd       |	j                  dd       |	j                  dd             \  }}
}}	 t        |
      }t        |      }t               }d }t        j                  |xs d	|
      }|t        d|
 dt               |j                  |	j                  dd             }|j                  |	j                  dd             }|t        j                  k(  rJ|t        d      |t        d      t        j                  ||||| |	j                  dd       |
|i |	
      }ne|t        j                   k(  rR|t        d      |t        d      t#        j                  ||||| |	j                  dd       xs t$        |
|i |	|      }|t        d|
 dt               |S # t        $ r t        d|
 dt               w xY w)Nr   rq   r8   r4   r`   )r2   r8   r4   r`   z"Invalid image variation provider: z. Supported providers are:  r{   z_image variation provider has no known model info config - required for getting api keys, etc.: z/API key is required for OpenAI image variationsz0API base is required for OpenAI image variationsre   )
r   r`   r4   r2   r   re   r8   r   r   r   z.API key is required for Topaz image variationsz/API base is required for Topaz image variations)r   r`   r4   r2   r   re   r8   r   r   r   r   r<   )rD   r
   LiteLLMLoggingObjr   r,   r(   r&   rI   r*   r+   get_provider_model_infoget_api_keyget_api_baseOPENAIr    image_variationsTOPAZr   DEFAULT_REQUEST_TIMEOUT)r   r2   rY   r[   r\   r^   rM   r   rq   r   r8   r   r4   llm_providerimage_variation_providerr   rT   r}   r`   s                      rV   r   r   "  s    ZZ$'F0&**=R2ST (1&1N<L*../DdK##J5""9d3	=9E
#$78#D\#R 
 #_N(,H+CCkrO
 m  oB  nC  C^  _@  ^A  B
 	
 )).*<*<Y*MNG++N,>,>z4,PQH#D#K#KK?NOOOPP*;;)"&&y$7 3+)
 
"%F%L%L	L?MNNNOO+<<)"&&y$7R;R 3+)
 01D0EE`  bC  aD  E
 	
 OA  
01D0EE`  bC  aD  E
 	

s   G3 3Hmaskrr   extra_query
extra_bodyre   r8   c                    t               }	 |j                  d      }|j                  dd      }|j                  dd      du }t        di |}t	        |xs t
        |      \  }}}}t        j                  |t        j                  |            }|t        d	|       |j                  |       t        j                  |      }t        j                  |||
      }|j                  ||t!        |      d|i||       t#        j$                  || |||||||	||xs t&        ||j                  d            S # t(        $ r}t        j*                  |||||      d}~ww xY w)zW
    Maps the image edit functionality, similar to OpenAI's images/edits endpoint.
    rq   rj   Nr   FTr   r{   z image edit is not supported for )r2   image_edit_provider_configimage_edit_optional_paramsr   r   )r2   r   rX   r   "image_edit_optional_request_paramsr8   r   r   rr   r   re   	_is_asyncr   r7   r<   )r   rD   r   r%   r,   r   r+   get_provider_image_edit_configr   r(   rI   r   r/   'get_requested_image_edit_optional_paramget_optional_params_image_editr   rG   r   image_edit_handlerr   rJ   r   )r   rX   r2   r   rY   rZ   r[   r\   r^   rr   r   r   re   r8   rM   
local_varsrq   rj   r   r   rR   r   r   image_edit_request_paramsrU   s                            rV   
image_editr     s   0 JJ
17<Q1R)/4Et)LJJ|U3t;	 .77+;77 3,
("Aq "@@))*=>
 	# &-?@S?TUVV&! "II*U 	# "@@+E+E 	" 	88 !:;!?+ !4 	9 		
 %77'A/H 3)+'!66::h'
 	
   
$$ 3 (
 	

s   D/D< <	E$EE$c                   K   t               }	 t        j                         }d|d<   |,t        j                  ||j                  dd            \  }}}}t        t        f| ||||||||||d|}t        j                         }t        |j                  |      }|j                  d|       d{   }t        j                  |      r| d{   }|S |}|S 7 )7 # t        $ r}t        j                  |||||      d}~ww xY ww)a}  
    Asynchronously calls the `image_edit` function with the given arguments and keyword arguments.

    Parameters:
    - `args` (tuple): Positional arguments to be passed to the `image_edit` function.
    - `kwargs` (dict): Keyword arguments to be passed to the `image_edit` function.

    Returns:
    - `response` (Any): The response returned by the `image_edit` function.
    Tr   Nrx   r5   )r   rX   r   r2   rY   rZ   r[   r\   r^   re   r8   r7   )r   r=   r>   r   r,   rD   r   r   rA   rB   rC   rE   rH   rJ   r   )r   r2   rX   r   rY   rZ   r[   r\   r^   rr   r   r   re   r8   rM   r   rN   rR   rO   rP   rQ   rS   rT   rU   s                           rV   aimage_editr     s<    > J+
%%'#| &+2+C+CjnnZ&F,(A"Aq 
+ 3
 
  &&(#CGGT2"2249JKK}-**H  %H L +
  
$$ 3 (
 	

sY   D	B"C 0C1C CC D	C D	C C 	D'DDD	)NNNNNNNNiX  NNNN)ru   r.   r   NN)NNNNNNNNNNNN)NNNNNNNNNNN)Or=   rA   	functoolsr   typingr   r   r   r   r   r	   r
   r   httpxr   r   r   r   r   litellm.constantsr   r   r   litellm.exceptionsr   *litellm.litellm_core_utils.litellm_loggingr   )litellm.litellm_core_utils.mock_functionsr   litellm.llms.base_llmr   r   &litellm.llms.custom_httpx.http_handlerr   r   *litellm.llms.custom_httpx.llm_http_handlerr   litellm.llms.custom_llmr   r   __annotations__litellm.mainr   r   r   r   r   r    r!   litellm.secret_managers.mainr"   litellm.types.images.mainr#   litellm.types.llms.openair$   litellm.types.routerr%   litellm.types.utilsr&   r'   r(   r)   litellm.utilsr*   r+   r,   r-   r   r/   rW   strintr@   r   r   floatTimeoutr   r   r<   rg   rV   <module>r      s      Q Q Q   G G : H 5 S K P P I - (:'; $ ;   8 D C 5   ) 5
 5
 5
r 
  CG%)$(!"!%C= } eC!>>?@	
 c] 3- C= 3- SM c] sm #  T]!$ sC&'% 
. 
  CG%)$(!"!%  ',!C= } eC!>>?@	
 c] 3- C= 3- SM c] sm #  U^!$ % 
,   CG%)$(!"!%H
H
C=H
 }H
 eC!>>?@	H

 c]H
 3-H
 C=H
 3-H
 SMH
 c]H
 smH
 #H
  #sM)*	,!H
 H
V
 3
} 3
 3
l  27\\\ \ ./	\
 3-\ 3-\ \ \~   CG%) /3,0+/59)-#b
b
b
 C=b
 3-	b

 }b
 eC!>>?@b
 c]b
 3-b
 3-b
 DcN+b
 $sCx.)b
 c3h(b
 eE5==012b
" "##b
& =)Cm$;<<='b
 b
J 
 CG%) /3,0+/59)-#J
J
J
 J
 3-	J

 }J
 eC!>>?@J
 c]J
 3-J
 3-J
 DcN+J
 $sCx.)J
 c3h(J
 eE5==012J
" "##J
& 'J
 J
rg   