
    jx              %       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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/m0Z0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z;m<Z<m=Z=m>Z> d dl?m@Z@mAZAmBZBmCZC ddlDmEZE ede@fd       ZFe	 	 	 	 	 	 	 	 	 	 	 	 d8deGde
eG   de
eH   de
eeGe7f      de
eG   de
eG   d e
eG   d!e
eG   d"e
eG   d#e
eG   d$e
eG   d%e	d&   deeee@f   fd'       ZIe	 	 	 	 	 	 	 	 	 	 	 	 d8d(d)deGde
eG   de
eH   de
eeGe7f      de
eG   de
eG   d e
eG   d!e
eG   d"e
eG   d#e
eG   d$e
eG   d%e	d(   de@fd*       ZIe	 	 	 	 	 	 	 	 	 	 	 	 d8deGde
eG   de
eH   de
eeGe7f      de
eG   de
eG   d e
eG   d!e
eG   d"e
eG   d#e
eG   d$e
eG   dee@eeee@f   f   fd+       ZIede@fd,       ZJe	 	 	 	 	 d9d-e<deGdeHde	d.   de
eG   d!e
eG   de@fd/       ZKe	 	 	 	 	 	 	 	 	 	 	 	 d:d-ee<ee<   f   deGde
eG   d0e
eG   de
eH   de
eeGe7f      de
eG   de
eG   d!e
eG   d1e
eeGef      d2e
eeGef      d3e
eeGef      d4e
eeLej                  f      d5e
eG   dee@eeee@f   f   fd6       ZNe	 	 	 	 	 	 	 	 	 	 	 d;d-ee<ee<   f   deGdeGd0e
eG   de
eH   de
eeGe7f      de
eG   de
eG   d!e
eG   d1e
eeGef      d2e
eeGef      d3e
eeGef      d4e
eeLej                  f      d5e
eG   de@fd7       ZOy)<    N)partial)	Any	CoroutineDictList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r3   r5   z?Unable to get Image Response. Please pass a valid llm_provider.openair3   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loopr3   r9   funcctxfunc_with_context_init_responseresponsees               l/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/litellm/images/main.pyaimage_generationrX   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:promptr3   nqualityresponse_formatsizestyleuserapi_keyr5   api_versionr4   Tc                     y Nr=   rY   r3   rZ   r[   r\   r]   r^   r_   timeoutr`   r5   ra   r9   r4   rN   s                  rW   rA   rA   r       &     F)r4   c                     y rc   r=   rd   s                  rW   rA   rA      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        dC|xs |||||||||$d	|#}%t!        dCi |}&|}'|'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 |t        j                  j8                  t        j                  j:                  t        j                  j<                  t        j                  j>                  t        j                  j@                  fv r/|$tC        d&|       tD        jG                  |	|| |$|%||&|||'
      S |d(k(  r|d)d*l$m%}+ |+jM                  |
      }
|+jO                  |	      }	|||%d<   d#|	d$}*|*j                         D ]  \  }!}"|!|vs|"||!<    t5        j6                  || ||	|
d|||%||||||&%      }|S |dk(  s/|t        jP                  jR                  k(  s|t        jT                  v r8|j                  d+d      },tW        j6                  || ||	xs ||
||%||,||,      }|S |d-k(  r.|tY        d.      t[        j6                  || |||%||||
|	/
      }|S |d0k(  r|%j3                  d1d      xs1 |%j3                  d2d      xs t        j\                  xs t)        d3      }-|%j3                  d4d      xs1 |%j3                  d5d      xs t        j^                  xs t)        d6      }.|%j3                  d7d      xs |%j3                  d8d      xs t)        d9      }/|
xs* t        j*                  xs t)        d:      xs t)        d;      }
ta        j6                  || |||%||-|.|/||
|<      }|S |t        jb                  v rd}0t        jd                  D ]  }1|1d=   |k(  s|1d>   }0 |0tg        ||?      |d@u r2d}2|ti        |tj              r|}2|0jm                  || |	|
||%|||2A	      }|S d}3|ti        |tn              r|}3|0j7                  || |	|
||%|||3A	      }|S c c}"}!w # tX        $ r}4tq        |||4t               |B      d}4~4ww xY w)Dzv
    Maps the https://api.openai.com/v1/images/generations endpoint.

    Currently supports just Azure + OpenAI.
    r4   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)r3   r9   r5   dall-e-2r7   r3   )r_   r   r5   ra   r`   deployment_idorganizationbase_urldefault_headersre   max_retriesrZ   r[   r]   r^   r3   provider)	r3   rZ   r[   r\   r]   r^   r_   r9   provider_config)	re   azurerj   rk   rm   ro   rp   preset_cache_keystream_responser3   r_   optional_paramslitellm_paramsr9   r9   )r3   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)r3   rY   re   r`   r5   r   rn   logging_objr   model_responsera   r4   r   rs   r   z-image generation config is not supported for )
r`   r3   rY    image_generation_provider_config(image_generation_optional_request_paramsr9   r   r   re   r   azure_air   )AzureFoundryModelInforw   )r3   rY   re   r`   r5   r   r   r   rw   r4   r   bedrockz!Model needs to be set for bedrock)
r3   rY   re   r   r   r   r4   r   r5   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)r3   rY   re   r   r   r   r   r   r   r4   r5   r   r|   custom_handlerr3   r9   T)	r3   rY   r`   r5   r   r   r   re   r   r8   r=   )9localsrE   updatelitellmutilsr+   r-   _hidden_paramsr*   itemsr)   _member_map_valuesr,   $get_provider_image_generation_configr.   r   update_environment_variablesmodel_call_detailsr   r#   r5   ra   r`   	azure_keypopr   rA   RECRAFTAIMLGEMINIFAL_AIRUNWAYMLrJ   r   image_generation_handler"litellm.llms.azure_ai.common_utilsr   get_api_baseget_api_keyLITELLM_PROXYvalueopenai_compatible_providersr    rK   r   r   r   r"   _custom_providerscustom_provider_mapr   rG   r   rX   r   r   )5rY   r3   rZ   r[   r\   r]   r^   r_   re   r`   r5   ra   r9   rN   rM   r4   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   rw   r   r   r   r   itemasync_custom_clientcustom_clientrV   s5                                                        rW   rA   rA      s   2Y
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#\\^
+TQ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 
 !3"O (--/1G#!"GAJ 0 4DD!-(?/ /-' /2NT m !  ((  %%  ''  ''  ))%
 
 '. CDWCXY  $<<1H9H$72/ =   !J.P,99(CH+77@G(3@0 !3"O (--/1G#!"GAJ 0 4DD!#(?/ /-' /2N\ y  8+"l&@&@&F&FF"g&I&II +1**^T*JL4EE2?!/ /-) /Nl S !I-} CDD5FF/ /- /!NL u !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 /!N@ c  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 
" A	
B	  
 3 $h
 	

sy   E7[" 9[[B<[" C[" ,[" 
B/[" :A[" ,[" ;A+[" '2[" D[" ./[" A[" *1[" [" "	\+\\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.
    r3   Nr9   T
async_callr5   r6   r7   r8   r=   )r>   r?   rE   r   image_variationrB   rC   rD   r-   rF   rG   rH   r+   rI   rK   r   rL   s               rW   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   r9   r5   r`   )r3   r9   r5   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`   r5   r3   r   re   r9   r   r   r   z.API key is required for Topaz image variationsz/API base is required for Topaz image variations)r   r`   r5   r3   r   re   r9   r   r   r   r   r=   )rE   r   LiteLLMLoggingObjr   r-   r)   r'   rJ   r+   r,   get_provider_model_infor   r   OPENAIr!   image_variationsTOPAZr   DEFAULT_REQUEST_TIMEOUT)r   r3   rZ   r\   r]   r_   rN   r   rq   r   r9   r   r5   llm_providerimage_variation_providerr   rU   r}   r`   s                      rW   r   r   N  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   r9   c                    t               }	 |j                  d      }|j                  dd      }|j                  dd      du }t        | t              r| n| g}|j                  d      }i }t        |t
              r|j                  |       t        |	t
              r|j                  |	       |rt        |      }	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   FTrs   r   r{   z image edit is not supported for )r3   image_edit_provider_configimage_edit_optional_paramsr   r   )r3   r   rY   r   "image_edit_optional_request_paramsr9   r   r   rr   r   re   	_is_asyncr   r8   r=   )r   rE   r   rG   listrH   r   r&   r-   r   r,   get_provider_image_edit_configr   r)   rJ   r0   'get_requested_image_edit_optional_paramget_optional_params_image_editr   r   image_edit_handlerr   rK   r   )r   rY   r3   r   rZ   r[   r\   r]   r_   rr   r   r   re   r9   rN   
local_varsrq   rj   r   imagesheaders_from_kwargsmerged_extra_headersr   rS   r   r   image_edit_request_paramsrV   s                               rW   
image_editr     s   0 JW
17<Q1R)/4Et)LJJ|U3t;	 %UD1w$jj3/1)40 ''(;<mT* ''6 !56M .77+;77 3,
("Aq "@@ --.AB 	# &-?@S?TUVV&! "II*U 	# "@@+E+E 	" 	88 !:;!?+ !4 	9 		
 %77'A/H 3)+'!66::h'
 	
   
$$ 3 (
 	

s   F&F3 3	G<GGc                 @  K   t               }	 t        j                         }d|d<   |,t        j                  ||j                  dd            \  }}}}t        | t              r| n| g}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   r6   )r   rY   r   r3   rZ   r[   r\   r]   r_   re   r9   r8   )r   r>   r?   r   r-   rE   rG   r   r   r   rB   rC   rD   rF   rI   rK   r   )r   r3   rY   r   rZ   r[   r\   r]   r_   rr   r   r   re   r9   rN   r   rO   rS   r   rP   rQ   rR   rT   rU   rV   s                            rW   aimage_editr   !  sN    > J-
%%'#| &+2+C+CjnnZ&F,(A"Aq %UD1w
+ 3
 
  &&(#CGGT2"2249JKK}-**H  %H L +
  
$$ 3 (
 	

sY   DB7C3 C/C3 $C1%C3 *D+C3 .D/C3 1C3 3	D<DDD)NNNNNNNiX  NNNN)ru   r/   r   NN)NNNNNNNNNNNN)NNNNNNNNNNN)Pr>   rB   	functoolsr   typingr   r   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   r0   rX   strintrA   r   r   floatTimeoutr   r   r=   rg   rW   <module>r     s      W W W   G G : H 5 S K P P I - (:'; $ ;   8 D C 5   ) 5
 5
 5
v 
  CG%)!"!%C= } eC!>>?@	
 c] 3- C= 3- c] sm # T]" sC&'# 
. 
  CG%)!"!% ',C= } eC!>>?@	
 c] 3- C= 3- c] sm # U^" # 
.   CG%)!"!%q
q
C=q
 }q
 eC!>>?@	q

 c]q
 3-q
 C=q
 3-q
 c]q
 smq
 #q
 c3%&(q
 q
h 3
} 3
 3
l  27\\\ \ ./	\
 3-\ 3-\ \ \~   CG%) /3,0+/59)-#o
DO+,o
o
 C=o
 3-	o

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

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