o
    ưi8                  .   @   s   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rod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j*eeef f f d 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j*e	eeej*f e	eeeej*eeef f f e
eeef eeef f f"d$d%Z-deeef d&ej.d'e,d(d)d*d+deej*eeef f fd,d-Z/d.ej*d(d)d*d+fd/d0Z0d.e	eeej*f d(d)d*d+fd1d2Z1dS )3z?
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                    s  zt  }d|d< t||||d\}}}}ddlm} ddlm} ttd |	dp4|j
|||d	}|d
u rAtd| dttf| |||||||||	|
||||d|}t }t|j|}|d
|I d
H }t |r|I d
H }t|tjr|  |W S td tjy } z|d
}~w ty } zHddlm} ddlm} t||||d\}}}}d
}|rzttd |	dp|j
|||d	}W n	 ty   Y nw |d
u r|tj||dd
}~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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
isinstancehttpxResponseraise_for_statusZ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-   r.   funcctxZfunc_with_contextZinit_responseresponser4   _Zresolved_custom_llm_provider rN   O/home/app/Keep/.python/lib/python3.10/site-packages/litellm/passthrough/main.pyr(   &   s   

r(   F)r   r   r   r   r   r(   r   r   r    r!   r"   r#   r   c           $      K   s  ddl m} ddlm} ddlm} |}|du r!|rtj}ntj}t	d|
d}t||||d\}}}}|d i |}d	|v rF|d	 |d	< |j||i |||
rQ|
n|d
 t	td |
dpg|j|||d}|du rttd| d|j||||||d\}}|dkrd|v rtt|}t|}||}|ji |g i |||d}tj|pi |dd}|j|||
r|
n|t||d\}}|rt|trd|v r||d< |jj| ||dur|n|	|du r|	du r|
nd||du r|	du r|nd|||d	}|j||
p|pi d} | |_ |
r|
n|}!|j!|!||!t||dd z,|r-t"||| ||dW S |jj#|| d}"|"$  t%|"drI| rIt&|"||W S |"W S  tyf }# z|du r[|#t'j(|#|dd}#~#w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)   Zmodel_id)r&   litellm_paramsoptional_paramsr%   r   request_datar   r.   r/   r1   r2   )r   r   r&   r%   r   rS   Zbedrockzapplication-inference-profile)headersr&   messagesrT   rS   r   r   F)r   rV   Zforward_headers)rV   rS   rU   r   r&   r&   )	r$   urlr   r   r    r!   r"   rV   r#   )r%   rU   )Zcomplete_input_dictr   rV   )inputr   Zadditional_args)r   requestis_streaming_requestrR   r.   rZ   stream
iter_bytesr3   rN   ))Z-litellm.litellm_core_utils.get_litellm_paramsrP   r7   r+   r8   r-   litellmZmodule_level_aclientZmodule_level_clientr   r9   r   Zupdate_environment_variablesr	   r:   r;   Zget_complete_urlr   Z"encode_bedrock_runtime_modelid_arnstrrC   URLZget_api_keyZvalidate_environmentr   Zforward_headers_from_requestZsign_requestrB   dictr   Zbuild_requestr[   r]   Zpre_call_async_passthrough_requestsendrE   hasattr_sync_streamingrF   rG   )$r$   r%   r&   r   r   r   r   r   r(   r   r   r    r!   r"   r#   r   rH   rP   r+   r-   Z	_is_asyncrR   Zlitellm_params_dictr.   Zupdated_urlZbase_target_urlZencoded_url_strZprovider_api_keyZauth_headersrV   Zsigned_json_bodyrZ   r[   rU   rL   r4   rN   rN   rO   r<      s   "
	






	
	
r<   rZ   r[   rR   rQ   r.   r   c                    sZ   | j j||d}t|r)|rt|||dS |I dH }| I dH  |  |S td)zr
    Handle async passthrough requests.
    Uses async client to send request and properly handles streaming.
    r\   )rL   rR   r.   Nz$Expected coroutine from async client)r   rd   r5   rA   _async_streamingZareadrE   r;   )r   rZ   r[   rR   r.   Zresponse_resultrL   rN   rN   rO   rc   a  s   

rc   rL   c              
   c   sf    ddl m} zg }|  D ]
}|| |V  q|j|j||d W d S  ty2 } z|d }~ww )Nr   )executor	raw_bytesr.   )r8   rh   r^   appendsubmitZ"flush_passthrough_collected_chunksr;   )rL   rR   r.   rh   rj   chunkr4   rN   rN   rO   rf     s    
rf   c                 C  s   | I d H }z'|   g }| 2 z3 d H W }|| |V  q6 t|j||d W d S  tyG   z	| I d H  W   tyF   Y  w w )Nri   )rE   Zaiter_bytesrk   r5   create_taskZ(async_flush_passthrough_collected_chunksr;   aclose)rL   rR   r.   Ziter_responserj   rm   rN   rN   rO   rg     s0   


rg   )2__doc__r5   r=   	functoolsr   typingr   r   r   r   r   r   r	   r
   r   rC   Zhttpx._typesr   r   r   r_   Z1litellm.litellm_core_utils.get_llm_provider_logicr   Z&litellm.llms.custom_httpx.http_handlerr   r   Z*litellm.llms.custom_httpx.llm_http_handlerr   Zlitellm.passthrough.utilsr   r8   r   rF   utilsr   Z*litellm.litellm_core_utils.litellm_loggingr   rQ   Z0litellm.llms.base_llm.passthrough.transformationr   r`   rb   rD   r(   boolr<   Requestrc   rf   rg   rN   rN   rN   rO   <module>   s(   ,	
|	

 >

!
