
    AHj8              .          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 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jT                  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jT                  e	eeejT                  f   e	eeeejT                  eeef   f   f   e
eeef   eeef   f   f"d$       Z-deeef   d%ej\                  d&e,d'd(d)d*deejT                  eeef   f   fd+Z/d,ejT                  d'd(d)d*fd-Z0d,e	eeejT                  f   d'd(d)d*fd.Z1y)/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)CommonUtils)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                   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$                  |      r6| d
{   }t'        |t(        j*                        r|j-                          |S t        d      7 Z7 =# t(        j.                  $ r}|d
}~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||t0        j3                  ||      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/Expected coroutine from async passthrough routeer0   )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
isinstancehttpxResponseraise_for_statusHTTPStatusErrorbase_llm_http_handler_handle_error)r%   r&   r'   r   r   r   r   r   r   r    r!   r"   r#   r$   r   kwargsloopr-   r/   r0   funcctxfunc_with_contextinit_responseresponser6   _resolved_custom_llm_providers                               P/root/tools/cai/cai_env/lib/python3.12/site-packages/litellm/passthrough/main.pyr*   r*   &   sj    .c
%%'+/'(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 (ENN3))+O MNN L +      "
47 1A 3	1
-'A '	"&45vzzBS7T# #*JJ)*FG K      "G#11+ 2 
 	
?"
s}   G?C&E	 *E+E	 	E
/E	 9G?:E	 E	 	G<EG<*$G7=GG7	GG7GG77G<<G?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 |}d	|v r|d	   |d	<   |j                  ||i |||
r|
n|
       t        t        d   |j                  d            xs |j                   ||      |      }|t        d| d      |j!                  ||||||      \  }}|dk(  r7d|v r3t#        j$                  t'        |            }t)        j*                  |      }|j-                  |      }|j/                  i |g i |||      }t1        j2                  |xs i |d      }|j5                  |||
r|
n|t'        |      |      \  }}|rt7        |t8              r	d|v r||d<   |j:                  j=                  | |||n|	||	|
nd|||	|nd|||	      }|j?                  ||
xs |xs i       } | |_         |
r|
n|}!|jC                  |!||!t'        |      |d       	 |rtE        ||| ||      S |j:                  jG                  ||       }"|"jI                          tK        |"d      r| rtM        |"||      S |"S # t        $ r }#||#tN        jQ                  |#|      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+   model_id)r'   litellm_paramsoptional_paramsr&   r   request_datar   r0   r1   r3   r4   )r   r   r'   r&   r   rZ   bedrockzapplication-inference-profile)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\   )complete_input_dictr   r^   )inputr   additional_args)r   requestis_streaming_requestrX   r0   re   stream
iter_bytesr5    ))-litellm.litellm_core_utils.get_litellm_paramsrV   r9   r-   r:   r/   litellmmodule_level_aclientmodule_level_clientr   r;   r   update_environment_variablesr
   r<   r=   get_complete_urlr   "encode_bedrock_runtime_modelid_arnstrrE   URLget_api_keyvalidate_environmentr   forward_headers_from_requestsign_requestrD   dictr   build_requestrf   rh   pre_call_async_passthrough_requestsendrG   hasattr_sync_streamingrI   rJ   )$r%   r&   r'   r   r   r   r   r   r*   r   r    r!   r"   r#   r$   r   rK   rV   r-   r/   	_is_asyncrX   litellm_params_dictr0   updated_urlbase_target_urlencoded_url_strprovider_api_keyauth_headersr^   signed_json_bodyre   rf   r\   rQ   r6   s$                                       rT   r>   r>      s   D Q03&I~11F00F2FJJ?T4UV4D/	51E -6v6 V*0*<J'44*/!Tt 5  ()6::6G+H 		>	>12 
? 
  )$7#8
CDD#2#C#C1* $D $ K i',Kx,W%HH
 ii0 '227;"77*   8 L #??'-2G !0 < <*!Tt[! != !G 
4&7d?Wmm))$4$@ g&.7?T&.7?T * 
G +??'T'R @  "6  4TL   #/K(
 ! 
-%9$7 /  (.}}'9'9'Rf'9'gH%%' ,/4H&x1DoVV   
"G#11+ 2 
 	

s%    J AJ J 	K%K  Kre   rf   rX   rW   r0   r   c                   K   | j                   j                  ||      }t        j                  |      rD|rt	        |||      S | d{   }|j                          d{    |j                          |S t        d      7 97 #w)zr
    Handle async passthrough requests.
    Uses async client to send request and properly handles streaming.
    rg   )rQ   rX   r0   Nz$Expected coroutine from async client)r   r|   r7   rC   _async_streamingareadrG   r=   )r   re   rf   rX   r0   response_resultrQ   s          rT   r{   r{   a  s      mm((AU(VO ?+#($7 /  -,H.."""%%'O >?? -"s$   AB
BB"B#"BBrQ   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_bytesr0   )r:   r   ri   appendsubmit"flush_passthrough_collected_chunksr=   )rQ   rX   r0   r   r   chunkr6   s          rT   r~   r~     sx     
 '!#	((* 	EU#K	 	BB+ 	 	

  s)   A(A
A A(	A%A  A%%A(c                j  K   |  d {   }	 |j                          g }|j                         2 3 d {   }|j                  |       |  7 G7 6 t        j                  |j                  ||             y # t        $ r+ 	 |j                          d {  7    # t        $ r Y  w xY ww xY ww)Nr   )rG   aiter_bytesr   r7   create_task(async_flush_passthrough_collected_chunksr=   aclose)rQ   rX   r0   iter_responser   r   s         rT   r   r     s     
 #NM&&(!#	(446 	 	%U#K #
	6 	HH# / I 	
  	&&((( 	  		s|   B3AB3"A< AAAA< B3A'A< ;B3<	B0B BB B0 	B,)B0+B,,B00B3)2__doc__r7   r?   	functoolsr   typingr   r   r   r   r   r	   r
   r   r   rE   httpx._typesr   r   r   rl   1litellm.litellm_core_utils.get_llm_provider_logicr   &litellm.llms.custom_httpx.http_handlerr   r   *litellm.llms.custom_httpx.llm_http_handlerr   litellm.passthrough.utilsr   r:   r   rI   utilsr   *litellm.litellm_core_utils.litellm_loggingr   rW   0litellm.llms.base_llm.passthrough.transformationr   rr   rx   rF   r*   boolr>   Requestr{   r~   r   rj       rT   <module>r      s     
 
 
  C C  N P I 1  *,  'WV  *."!+/&*!$((,%)=A!y
y
 y
 	y

 "#y
 smy
 c]y
 #4.y
 d^y
 c]y
 4.y
 L!y
 3-y
 _%y
 k"y
  U;(889:!y
$ 5>>>#s(334%y
 y
x  *."!+/&*#(!$((,%)=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ennnS#X.FFGGHc3m38	'z
 z
z@+//0@]]@ @ -	@
 -@ 5>>>#s(334@Bnn, -,S%..01, -r   