
    hT,              *       p   d Z 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mZmZmZ ddlZddlmZ ddlmZmZ ddlmZ dd	lmZ  e       Zd
dlm Z  erddl!m"Z# ddl$m%Z% edddddddddddddde&de&de&dee&   dee&   dee&   dee'   dee'   dee   dee'   dee   dee   dee   dee   deeeef      deejP                  e	eeejP                  f   e
eeef   eeef   f   f d       Z)edddddd dddddddd!de&de&de&dee&   dee&   dee&   dee'   dee'   d"e*dee   dee'   dee   dee   dee   dee   deeeef      deejP                  e	eeejP                  f   e
eeef   eeef   f   f"d#       Z+d$ejP                  d%d&d'd(fd)Z,d$e	eeejP                  f   d%d&d'd(fd*Z-y)+z?
This module is used to pass through requests to the LLM APIs.
    N)partial)	TYPE_CHECKINGAnyAsyncGenerator	Coroutine	GeneratorListOptionalUnioncast)CookieTypesQueryParamTypesRequestFiles)get_llm_provider)AsyncHTTPHandlerHTTPHandler)BaseLLMHTTPHandler)client   )BasePassthroughUtils)Logging)BasePassthroughConfig)custom_llm_providerapi_baseapi_keyrequest_query_paramsrequest_headerscontentdatafilesjsonparamscookiesr   methodendpointmodelr   r   r   r   r   r   r   r    r!   r"   r#   r   returnc                 F  K   	 t        j                         }d|d<   t        ||||      \  }}}}ddlm} ddlm} t        t        d   |j                  d            xs |j                   ||      |	      }|t        d| d      t        t        f| |||||||||	|
||||d|}t        j                         }t        |j                   |      }|j#                  d
|       d
{   }t        j$                  |      r| d
{   }	 |j'                          |S |}|S 7 :7 # t(        j*                  $ rD}|j,                  j/                          d
{  7  }|j1                  d      }t        |      d
}~ww xY w# t        $ r}ddlm} ddlm} t        ||||      \  }}}}d
}|rN	 t        t        d   |j                  d            xs |j                   ||      |	      }n# t        $ r Y nw xY w||t2        j5                  ||      d
}~ww xY ww)zR
    Async: Reranks a list of documents based on their relevance to the query
    Tallm_passthrough_router&   r   r   r   r   LlmProvidersProviderConfigManagerr   provider_configproviderr&   N	Provider 
 not found)r$   r%   r&   r   r   r   r   r   r   r   r    r!   r"   r#   r   zutf-8er/   )asyncioget_event_loopr   litellm.types.utilsr,   litellm.utilsr.   r   r
   getget_provider_passthrough_config	Exceptionr   llm_passthrough_routecontextvarscopy_contextrunrun_in_executoriscoroutineraise_for_statushttpxHTTPStatusErrorresponseareaddecodebase_llm_http_handler_handle_error)r$   r%   r&   r   r   r   r   r   r   r   r    r!   r"   r#   r   kwargsloopr,   r.   r/   funcctxfunc_with_contextinit_responserF   r5   
error_texterror_text_str_resolved_custom_llm_providers                                 T/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/passthrough/main.pyr)   r)   %   s    8a
%%'+/'(8H 3	9
5"GX 	57,-vzz:K/L
 
"BB!"56 C 
 	 "i(;'<JGHH!
 3!5+!
" #
( &&(#CGGT2"2249JKK}-**H0))+  %H L + (( 0#$::#3#3#555
!+!2!27!;//0  "
47 1A 3	1
-'A '	"&45vzzBS7T# #*JJ)*FG K      "G#11+ 2 
 	
?"
s   H!C&F *D%+F 	D'
F D) F  H!!F $H!%F 'F )F <E;E!E;;F  F 	H$H1=G/.H/	G;8H:G;;HHH!F)r   r   r   r   r   r)   r   r   r    r!   r"   r#   r   r)   c                    ddl m} ddlm} ddlm} |#|rt        j                  }nt        j                  }t        d|j                  d            }t        ||||      \  }}}} |di |}|j                  ||i |||
r|
n|	       t        t        d
   |j                  d            xs |j                   ||      |      }|t        d| d      |j!                  ||||||      \  }}|j#                  |      }|j%                  i |g i i ||      }t'        j(                  |xs i |d      }|j+                  |||
r|
n|t-        |      |      \  }}|rt/        |t0              r	d|v r||d<   |j2                  j5                  | ||||
nd|||nd|||	      }|j7                  ||
xs |xs i       }||_        	 |j2                  j;                  ||      }t=        j>                  |      r|rtA        |||      S |S |jC                          tE        |d      r|rtG        |||      S |S # t        $ r } || tH        jK                  | |      d} ~ ww xY w)z
    Pass through requests to the LLM APIs.

    Step 1. Build the request
    Step 2. Send the request
    Step 3. Return the response
    r   )get_litellm_paramsr+   r-   NLiteLLMLoggingObjlitellm_logging_objr*   )r&   litellm_paramsoptional_paramsr%   r   request_datar   r/   r0   r2   r3   )r   r   r&   r%   r   rZ   )headersr&   messagesr[   rZ   r   r   F)r   r]   forward_headers)r]   rZ   r\   r   r&   r&   )	r$   urlr   r   r    r!   r"   r]   r#   )r%   r\   )requeststream
iter_bytesr4    )&-litellm.litellm_core_utils.get_litellm_paramsrW   r8   r,   r9   r.   litellmmodule_level_aclientmodule_level_clientr   r:   r   update_environment_variablesr
   r;   r<   get_complete_urlget_api_keyvalidate_environmentr   forward_headers_from_requestsign_requeststr
isinstancedictr   build_requestis_streaming_requestrb   sendr6   rB   _async_streamingrC   hasattr_sync_streamingrI   rJ   )!r$   r%   r&   r   r   r   r   r   r)   r   r   r    r!   r"   r#   r   rK   rW   r,   r.   rY   litellm_params_dictr/   updated_urlbase_target_urlprovider_api_keyauth_headersr]   signed_json_bodyra   rs   rF   r5   s!                                    rU   r=   r=      s   B Q03~!11F00F2FJJ?T4UV4D/	51E -6v644*/!Tt 5  ()6::6G+H 		>	>12 
? 
  )$7#8
CDD#2#C#C1* $D $ K '227;"77   8 L #??'-2G !0 < <*!Tt[! != !G 
4&7d?Wmm)) %-T4%-T4 * 
G +??'T'R @  "6
==%%g>R%Sx(#'2EWW!!# Hl+0D #8-@/RR O 
"G#11+ 2 
 	

s+   A H7 H7 
*H7 5H7 7	I  II rF   rY   rX   r/   r   c              #      K   ddl m} 	 g }| j                         D ]  }|j                  |       |  |j	                  |j
                  ||       y # t        $ r}|d }~ww xY ww)Nr   )executor	raw_bytesr/   )r9   r   rc   appendsubmit"flush_passthrough_collected_chunksr<   )rF   rY   r/   r   r   chunkr5   s          rU   rw   rw   C  ss     
 '!#	((*EU#K + 	BB+ 	 	

  s)   A(A
A A(	A%A  A%%A(c                  K   	 |  d {   }g }|j                         2 3 d {   }|j                  |       |  7 67 6 t        j                  |j	                  ||             y # t
        $ r}|d }~ww xY ww)Nr   )aiter_bytesr   r6   create_task(async_flush_passthrough_collected_chunksr<   )rF   rY   r/   iter_responser   r   r5   s          rU   ru   ru   Y  s     
&!#	(446 	 	%U#K '	6
 	HH# / I 	
  sR   A?A, A A, AAAA, A'A, +A?,	A<5A77A<<A?).__doc__r6   r>   	functoolsr   typingr   r   r   r   r   r	   r
   r   r   rD   httpx._typesr   r   r   rf   1litellm.litellm_core_utils.get_llm_provider_logicr   &litellm.llms.custom_httpx.http_handlerr   r   *litellm.llms.custom_httpx.llm_http_handlerr   r9   r   rI   utilsr   *litellm.litellm_core_utils.litellm_loggingr   rX   0litellm.llms.base_llm.passthrough.transformationr   ro   rq   Responser)   boolr=   rw   ru   rd       rU   <module>r      s     
 
 
  C C  N P I  *,  'WV  *."!+/&*!$((,%)=A!|
|
 |
 	|

 "#|
 sm|
 c]|
 #4.|
 d^|
 c]|
 4.|
 L!|
 3-|
 _%|
 k"|
  U;(889:!|
$ 	NNc3&'c3m38%|
 |
~  *."!+/&*#(!$((,%)=A#Z
Z
 Z
 	Z

 "#Z
 smZ
 c]Z
 #4.Z
 d^Z
 !Z
 c]Z
 4.Z
 L!Z
 3-Z
 _%Z
  k"!Z
" U;(889:#Z
& 	NNc3&'c3m38'Z
 Z
znn, -,S%..01, -r   