
    hX                     &   d dl Z d dlZd dlZd dl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 d dlmZmZmZ d dlmZ d dlZd dlmZ d dlmZ d dlmZ d d	l erd d
lmZ d dl m!Z" d dl#m$Z$ neZeZ"eZ$	 d dl%m&Z& dde& iZ( ejR                  dd      Z*d!dee+   dee,e-ej\                  f   fdZ/d Z0 G d dejb                        Z2 G d d      Z3 G d d      Z4	 d!deee5f   dee6   de3fdZ7d!dee6   de4fd Z8y# e'$ r dZ&Y w xY w)"    N)TYPE_CHECKINGAnyCallableDictListMappingOptionalUnionClientSessionTCPConnector)USE_CLIENT_DEFAULTAsyncHTTPTransportHTTPTransport)RequestFiles)verbose_logger)_DEFAULT_TTL_FOR_HTTPX_CLIENTS)track_llm_api_timing)*)LlmProviders)LoggingLiteLLMAiohttpTransport)versionz0.0.0z
User-Agentzlitellm/      @timeoutconnect
ssl_verifyreturnc                    ddl m} t        | t        j                        r| S | Bt        j                  dt        j                        } t        | t              r ||       n| }||} t        j                  dt        j                        }d}t        | t              r!t
        j                  j                  |       r| }|sMt        j                  d      }|r"t
        j                  j                  |      r|}nt        j                         }| dur;t        j                  |      }|r!t        |t              r|j!                  |       |S | S )	a  
    Unified SSL configuration function that handles ssl_context and ssl_verify logic.

    SSL Configuration Priority:
    1. If ssl_verify is provided -> is a SSL context use the custom SSL context
    2. If ssl_verify is False -> disable SSL verification (ssl=False)
    3. If ssl_verify is a string -> use it as a path to CA bundle file
    4. If SSL_CERT_FILE environment variable is set and exists -> use it as CA bundle file
    5. Else will use default SSL context with certifi CA bundle

    If ssl_security_level is set, it will apply the security level to the SSL context.

    Args:
        ssl_verify: SSL verification setting. Can be:
            - None: Use default from environment/litellm settings
            - False: Disable SSL verification
            - True: Enable SSL verification
            - str: Path to CA bundle file
    
    Returns:
        Union[bool, str, ssl.SSLContext]: Appropriate SSL configuration
    r   str_to_boolN
SSL_VERIFYSSL_SECURITY_LEVELSSL_CERT_FILEF)cafile)litellm.secret_managers.mainr#   
isinstancessl
SSLContextosgetenvlitellmr   strssl_security_levelpathexistscertifiwherecreate_default_contextset_ciphers)r   r#   ssl_verify_boolr0   r'   ssl_cert_filecustom_ssl_contexts          b/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/custom_httpx/http_handler.pyget_ssl_configurationr;   +   s   . 9*cnn- YY|W-?-?@
5?
C5P+j1V`&(J#79S9STF*c"rww~~j'A		/2RWW^^M:"F]]_F 77

 -s3 **+=> "!    c                     t        | t              r| j                  d      }n| S |dk7  r7| j                  d|      }|dk(  r| d |dz    dz   }|S | d |dz    dz   | |d  z   }|S | S )Nzkey=&   z[REDACTED_API_KEY])r)   r/   find)error_message	key_index
next_parammasked_messages       r:   mask_sensitive_inforF   m   s    -%!&&v.	 B"''Y7
*?Y];>RRN  o	A.&'
,-  r<   c                   8     e Zd Z	 ddee   dee   f fdZ xZS )MaskedHTTPStatusErrormessagetextc           	         t        t        |j                  j                              }t        |   |j                  t        j                  |j                  j                  ||j                  j                  |j                  j                        t        j                  |j                  j                  |j                  j                  |j                  j                               || _        || _        y )N)methodurlheaderscontent)status_coderO   rN   )rI   requestresponse)rF   r/   rQ   rM   super__init__rI   httpxRequestrL   rN   rO   ResponserR   rP   rJ   )selforiginal_errorrI   rJ   
masked_url	__class__s        r:   rT   zMaskedHTTPStatusError.__init__   s     )^-C-C-G-G)HI
 	"**MM%--44&..66&..66	 ^^*33??&//77&//77 	 	
 	r<   NN)__name__
__module____qualname__r	   r/   rT   __classcell__)r[   s   @r:   rH   rH      s(    SW'/}CKC= r<   rH   c                      e Zd Z	 	 	 	 	 d&deeeej                  f      deee	e
edef      f      dee	   dee   fdZ	 d'deeeej                  f      dedeee	e
edef      f      dee   d	ej                   f
d
Zd Zd Zd Z	 	 	 d(de	dee   dee   dee   fdZ e       	 	 	 	 	 	 	 	 	 d)de	deeee	ef      dee   dee   dee   deeeej                  f      dedee   dee   defd       Z	 	 	 	 	 	 d*de	deeee	f      dee   dee   dee   deeeej                  f      defdZ	 	 	 	 	 	 d*de	deeee	f      dee   dee   dee   deeeej                  f      defdZ	 	 	 	 	 	 d*de	deeee	f      dee   dee   dee   deeeej                  f      defdZ	 	 	 	 	 	 d+de	dej                   deeee	ef      dee   dee   dee   dedefdZ d,dZ!e"	 d-d ee#jH                     dee   d	eee%e&f      fd!       Z'e"d	efd"       Z(e"	 	 d-dee   d ee#jH                     d	e)e	ef   fd#       Z*e"	 	 d-dee   d ee#jH                     d	e%fd$       Z+e"d	ee&   fd%       Z,y).AsyncHTTPHandlerNr   event_hooks.client_aliasr   c                 b    || _         || _        | j                  ||||      | _        || _        y )N)r   concurrent_limitrc   r   )r   rc   create_clientclientrd   )rX   r   rc   rf   rd   r   s         r:   rT   zAsyncHTTPHandler.__init__   s@     &((-#!	 ) 
 )r<   rf   r    c           	      d   t        |      }t        j                  dt        j                        }|t
        }t        j                  t        |t        j                        r|nd t        |t              r|nd       }t        j                  |||t        j                  ||      ||t              S )NSSL_CERTIFICATEssl_contextr   max_connectionsmax_keepalive_connections)	transportrc   r   limitsverifycertrN   )r;   r,   r-   r.   ssl_certificate_DEFAULT_TIMEOUTrb   _create_async_transportr)   r*   r+   boolrU   AsyncClientLimitsrN   )rX   r   rf   rc   r   
ssl_configrs   rp   s           r:   rg   zAsyncHTTPHandler.create_client   s     +:6
 yy*G,C,CD?&G %<<&0S^^&L
RV%/
D%Azt = 
	
   #<< 0*: 
 	
r<   c                 T   K   | j                   j                          d {    y 7 wNrh   acloserX   s    r:   closezAsyncHTTPHandler.close        kk  """   (&(c                 "   K   | j                   S wr|   rh   r   s    r:   
__aenter__zAsyncHTTPHandler.__aenter__   s     {{s   c                 T   K   | j                   j                          d {    y 7 wr|   r}   r   s    r:   	__aexit__zAsyncHTTPHandler.__aexit__   r   r   rM   paramsrN   follow_redirectsc                    K   ||nt         }|xs i }|j                  t        j                  |             | j                  j                  ||||       d {   }|S 7 wN)r   rN   r   )r   updateHTTPHandlerextract_query_paramsrh   getrX   rM   r   rN   r   _follow_redirectsrR   s          r:   r   zAsyncHTTPHandler.get   sm      !1 <BT 	 2k66s;<BS ) 
 
 
s   AA"A A"datajsonstreamlogging_objfilesrO   c                   K   t        j                          }	 || j                  }| j                  j                  d|||||||	|
	      }| j                  j	                  ||       d {   }|j                          |S 7 # t        j                  t        j                  f$ rz | j                  |d| j                        }	 | j                  |||||||       d {  7  |j                          d {  7   cY S # |j                          d {  7   w xY wt        j                  $ r}t        j                          }t        ||z
  d      }i }t        |dd       }|6|j                   j#                         D ]  \  }}||d	j%                  |      <    t'        j(                  d
| d| ddd|      d }~wt        j*                  $ r}|du r]t-        |d|j.                  j1                          d {  7         t-        |d|j.                  j1                          d {  7         nTt-        |dt3        |j.                  j4                               t-        |dt3        |j.                  j4                               t-        |d|j.                  j6                         |d }~wt8        $ r}|d }~ww xY ww)NPOSTr   r   r   rN   r   r   rO   r      r   rf   rc   rM   rh   r   r   r   rN   r      rR   response_headers-{}z%Connection timed out. Timeout passed=z, time taken=z secondsdefault-model-namelitellm-httpx-handlerrI   modelllm_providerrN   TrI   rJ   rP   )timer   rh   build_requestsendraise_for_statusrU   RemoteProtocolErrorConnectErrorrg   rc   single_connection_post_requestr~   TimeoutExceptionroundgetattrrN   itemsformatr.   TimeoutHTTPStatusErrorsetattrrR   areadrF   rJ   rP   	Exception)rX   rM   r   r   r   rN   r   r   r   r   rO   
start_timereqrR   
new_clienteend_time
time_deltaerror_responsekeyvalues                        r:   postzAsyncHTTPHandler.post   s     YY[
>	,,++++ , 
C "[[--c&-AAH%%'O B ))5+=+=> 	*++!AQAQ , J*!@@%!#! A    !'')))j'')))%% 	yy{Hx*4a8JG$Q
D9N)"0"8"8">">"@JCAFG188=> #A //?yV`Uaaij*4	  $$ 
	~9AJJ,<,<,>&>&>?6)9)9);#;#;<9&9!**//&JK6#6qzz#GHA}ajj&<&<=G 	G	s   J?AB *B+B  J?B AJ<D C#!D%J<8C;9J<?J?DD
DJ<1B
F;;J<(J)9G<:-J)'H*(BJ))J<5J77J<<J?c           
      F  K   	 || j                   }| j                  j                  d||||||      }| j                  j                  |       d {   }	|	j	                          |	S 7 # t
        j                  t
        j                  f$ rz | j                  |d| j                        }
	 | j                  ||
|||||       d {  7  |
j                          d {  7   cY S # |
j                          d {  7   w xY wt
        j                  $ ri}i }t        |dd       }|6|j                  j                         D ]  \  }}||dj!                  |      <    t#        j$                  d| d	d
d|      d }~wt
        j&                  $ r}}t)        |d|j*                  j,                         |du r0t)        |d|j*                  j/                          d {  7         |t)        |d|j*                  j0                         |d }~wt2        $ r}|d }~ww xY ww)NPUTr   r   r   rN   r   r   r   r   rR   r   Connection timed out after 	 seconds.r   r   r   rP   TrI   r   rh   r   r   r   rU   r   r   rg   rc   r   r~   r   r   rN   r   r   r.   r   r   r   rR   rP   r   rJ   r   rX   rM   r   r   r   rN   r   r   r   rR   r   r   r   r   r   s                  r:   putzAsyncHTTPHandler.putH  s    0	,,++++sDZa , C "[[--c22H%%'O 3 ))5+=+=> 	*++!AQAQ , J*!@@%!#! A    !'')))j'')))%% 	G$Q
D9N)"0"8"8">">"@JCAFG188=> #A //5gYiH*4	  $$ 	A}ajj&<&<=~9AJJ,<,<,>&>&>? G 9ajjoo6G 	G	   H!AA+ A)A+ (H!)A+ +AH-C)C	C)H C#!H'H!)D=D 
>DHA$E==HA	HG.HHHHH!c           
      F  K   	 || j                   }| j                  j                  d||||||      }| j                  j                  |       d {   }	|	j	                          |	S 7 # t
        j                  t
        j                  f$ rz | j                  |d| j                        }
	 | j                  ||
|||||       d {  7  |
j                          d {  7   cY S # |
j                          d {  7   w xY wt
        j                  $ ri}i }t        |dd       }|6|j                  j                         D ]  \  }}||dj!                  |      <    t#        j$                  d| d	d
d|      d }~wt
        j&                  $ r}}t)        |d|j*                  j,                         |du r0t)        |d|j*                  j/                          d {  7         |t)        |d|j*                  j0                         |d }~wt2        $ r}|d }~ww xY ww)NPATCHr   r   r   r   rR   r   r   r   r   r   r   rP   TrI   r   r   s                  r:   patchzAsyncHTTPHandler.patch  s    0	,,++++4d67\c , C "[[--c22H%%'O 3 ))5+=+=> 	*++!AQAQ , J*!@@%!#! A    !'')))j'')))%% 	G$Q
D9N)"0"8"8">">"@JCAFG188=> #A //5gYiH*4	  $$ 	A}ajj&<&<=~9AJJ,<,<,>&>&>? G 9ajjoo6G 	G	r   c           
      V  K   	 || j                   }| j                  j                  d||||||      }| j                  j                  ||       d {   }	|	j	                          |	S 7 # t
        j                  t
        j                  f$ rz | j                  |d| j                        }
	 | j                  ||
|||||       d {  7  |
j                          d {  7   cY S # |
j                          d {  7   w xY wt
        j                  $ r}}t        |d|j                  j                         |du r0t        |d	|j                  j!                          d {  7         |t        |d	|j                  j"                         |d }~wt$        $ r}|d }~ww xY ww)
NDELETEr   r   r   r   r   rP   TrI   )r   rh   r   r   r   rU   r   r   rg   rc   r   r~   r   r   rR   rP   r   rJ   r   )rX   rM   r   r   r   rN   r   r   r   rR   r   r   s               r:   deletezAsyncHTTPHandler.delete  s    "	,,++++#DtFG]d , C "[[--c&-AAH%%'O B ))5+=+=> 	*++!AQAQ , J*!@@%!#! A    !'')))j'')))$$ 	A}ajj&<&<=~9AJJ,<,<,>&>&>? G 9ajjoo6G 	G	s   F)AA- A+A- *F)+A- -AF&/C+
CC+F&"C%#F&)F)+D?D
 DF&A	F$E'%.FF&F!!F&&F)rh   c	           	         K   |j                  d||||||      }	|j                  |	|       d{   }
|
j                          |
S 7 w)zz
        Making POST request for a single connection client.

        Used for retrying connection client errors.
        r   )r   r   r   rN   rO   r   N)r   r   r   )rX   rM   rh   r   r   r   rN   r   rO   r   rR   s              r:   r   z/AsyncHTTPHandler.single_connection_post_request  sZ       ""CdfgW^ # 
  S88!!# 9s   /A
AA
c                     	 t        j                         j                  | j                                y # t        $ r Y y w xY wr|   )asyncioget_running_loopcreate_taskr   r   r   s    r:   __del__zAsyncHTTPHandler.__del__  s6    	$$&224::<@ 		s   14 	A A rl   c                     t         j                         rt         j                  | |      S t         j                         S )a  
        - Creates a transport for httpx.AsyncClient
            - if litellm.force_ipv4 is True, it will return AsyncHTTPTransport with local_address="0.0.0.0"
            - [Default] It will return AiohttpTransport
            - Users can opt out of using AiohttpTransport by setting litellm.use_aiohttp_transport to False


        Notes on this handler:
        - Why AiohttpTransport?
            - By default, we use AiohttpTransport since it offers much higher throughput and lower latency than httpx.

        - Why force ipv4?
            - Some users have seen httpx ConnectionError when using ipv6 - forcing ipv4 resolves the issue for them
        rk   )rb   _should_use_aiohttp_transport_create_aiohttp_transport_create_httpx_transportrk   s     r:   rv   z(AsyncHTTPHandler._create_async_transport  s>    * 99;#=='J >    7799r<   c                      ddl } ddlm} t        j                  du s | | j
                  dd            du ryt        j                  d       y)	z
        AiohttpTransport is the default transport for litellm.

        Httpx can be used by the following
            - litellm.disable_aiohttp_transport = True
            - os.getenv("DISABLE_AIOHTTP_TRANSPORT") = "True"
        r   Nr"   TDISABLE_AIOHTTP_TRANSPORTFalseFzUsing AiohttpTransport...)r,   r(   r#   r.   disable_aiohttp_transportr-   r   debug)r,   r#   s     r:   r   z.AsyncHTTPHandler._should_use_aiohttp_transport*  sL     	< --59299%@'JKtS
 	89r<   c                 V    dt         j                  rdndi}|||d<   |S | du rd|d<   |S )ay  
        Helper method to get SSL connector initialization arguments for aiohttp TCPConnector.
        
        SSL Configuration Priority:
        1. If ssl_context is provided -> use the custom SSL context
        2. If ssl_verify is False -> disable SSL verification (ssl=False)

        Returns:
            Dict with appropriate SSL configuration for TCPConnector
        
local_addr)0.0.0.0r   Nr*   F
verify_ssl)r.   
force_ipv4)r   rl   connector_kwargss      r:   _get_ssl_connector_kwargsz*AsyncHTTPHandler._get_ssl_connector_kwargsF  sQ      G,>,>.D,
 "&1U#
  	 5 -2\*r<   c                     ddl m} ddlm} t        j                  | |      t        j                   |t        j                  dd            du rdt        j                  d        |fd	
      S )z
        Creates an AiohttpTransport with RequestNotRead error handling

        Note: aiohttp TCPConnector ssl parameter accepts:
        - SSLContext: custom SSL context
        - False: disable SSL verification
        r   r   r"   )r   rl   AIOHTTP_TRUST_ENVr   TzCreating AiohttpTransport...c                  0    t        t        di        S )N)	connector	trust_env r   )r   r   s   r:   <lambda>z<AsyncHTTPHandler._create_aiohttp_transport.<locals>.<lambda>~  s    =&:)9:#r<   r   )+litellm.llms.custom_httpx.aiohttp_transportr   r(   r#   rb   r   r.   aiohttp_trust_envr,   r-   r   r   )r   rl   r   r#   r   r   s       @@r:   r   z*AsyncHTTPHandler._create_aiohttp_transportb  st     	X<+EE!{ F 
 "33	ryy!4g>?4GI;<&
 	
r<   c                  <    t         j                  rt        d      S y)z
        Creates an AsyncHTTPTransport

        - If force_ipv4 is True, it will create an AsyncHTTPTransport with local_address set to "0.0.0.0"
        - [Default] If force_ipv4 is False, it will return None
        r   local_addressN)r.   r   r   r   r<   r:   r   z(AsyncHTTPHandler._create_httpx_transport  s     %I>>r<   )NN  NNr|   NNN)	NNNNNFNNNNNNNNFNNNNFNr    Nr\   )-r]   r^   r_   r	   r
   floatrU   r   r   r/   r   r   r   VerifyTypesrT   intrx   rg   r   r   r   dictrw   r   r   bytesLiteLLMLoggingObjectr   r   r   r   r   r   r   staticmethodr*   r+   r   r   rv   r   r   r   r   r   r   r<   r:   rb   rb      s`    :>HL&*,0)%u}} 456) gc4c0B+C&CDE)
 sm) [)). -1"
%u}} 456"
 "
 gc4c0B+C&CDE	"

 [)"
 
		"
H## "&"&+/  $	
 #4.(  37#!%"&9=6:(,LL uT3-./L tn	L
 L $L %u}} 456L L 23L %L L Lb ,0#!%"&9=:: uT3Y'(: tn	:
 : $: %u}} 456: :~ ,0#!%"&9=:: uT3Y'(: tn	:
 : $: %u}} 456: :~ ,0#!%"&9=,, uT3Y'(, tn	,
 , $, %u}} 456, ,d 37#!%"& !! uT3-./	
 tn  $  . SW:cnn-:BJ4.:	%/1CCD	E: :< 4  6 %)04 TN cnn-  
c3h   6 %)04
TN
cnn-
 
!
 
B 
X.@%A 
 
r<   rb   c                      e Zd Z	 	 	 	 ddeeeej                  f      deej                     deee	e
f      fdZd Z	 	 	 dde
dee   d	ee   d
ee	   fdZede
dee
e
f   fd       Z	 	 	 	 	 	 	 	 	 dde
deeee
ef      deeee
ef      dee   d	ee   de	deeeej                  f      deeeef      dedee   fdZ	 	 	 	 	 	 dde
deeee
f      deeee
f      dee   d	ee   de	deeeej                  f      fdZ	 	 	 	 	 	 dde
deeee
f      deeee
f      dee   d	ee   de	deeeej                  f      fdZ	 	 	 	 	 	 dde
deeee
f      dee   dee   d	ee   deeeej                  f      de	fdZddZdee   fdZy) r   Nr   rh   r   c                    |t         }t        |      }t        j                  dt        j
                        }|J| j                         }t        j                  ||t        j                  ||      ||t              | _        y || _        y )Nrj   rm   )rp   r   rq   rr   rs   rN   )ru   r;   r,   r-   r.   rt   _create_sync_transportrU   Clientry   rN   rh   )rX   r   rf   rh   r   rz   rs   rp   s           r:   rT   zHTTPHandler.__init__  s     ?&G +:6
 yy*G,C,CD>335I  ,,#||$4.> "
DK !DKr<   c                 8    | j                   j                          y r|   )rh   r   r   s    r:   r   zHTTPHandler.close  s    r<   rM   r   rN   r   c                     ||nt         }|xs i }|j                  | j                  |             | j                  j	                  ||||      }|S r   )r   r   r   rh   r   r   s          r:   r   zHTTPHandler.get  s_     !1 <BT 	 2d//45;;??BS # 
 r<   r    c                 X    ddl m}m}  ||       }t         ||j                              S )u   
        Parse a URL’s query-string into a dict.

        :param url: full URL, e.g. "https://.../path?foo=1&bar=2"
        :return: {"foo": "1", "bar": "2"}
        r   )	parse_qslurlsplit)urllib.parser  r  r   query)rM   r  r  partss       r:   r   z HTTPHandler.extract_query_params  s$     	5Iekk*++r<   r   r   r   r   rO   r   c                    	 |%| j                   j                  d||||||||		      }n#| j                   j                  d|||||||	      }| j                   j                  ||      }|j                          |S # t        j
                  $ r t        j                  d| ddd	      t        j                  $ r}|d
u r]t        |dt        |j                  j                                      t        |dt        |j                  j                                      n9t        |j                  j                        }t        |d|       t        |d|       t        |d|j                  j                         |d }~wt        $ r}|d }~ww xY w)Nr   r   )r   r   r   rN   r   rO   r   r   r   r   r   rI   r   r   TrI   rJ   rP   rh   r   r   r   rU   r   r.   r   r   r   rF   rR   readrJ   rP   r   )rX   rM   r   r   r   rN   r   r   r   rO   r   r   rR   r   
error_texts                  r:   r   zHTTPHandler.post  sx   &	"kk//!### 0 
 kk//Cdfg]bls 0  {{''F';H%%'O%% 	//5gYiH*4 
 $$ 
	~9&9!**//:K&LM6#6qzz7H#IJ0A
9j16:.A}ajj&<&<=G 	G	s&   A8A; ;AF<B=E99FFFc           	         	 |#| j                   j                  d||||||      }n!| j                   j                  d|||||      }| j                   j                  ||      }	|	j                          |	S # t        j
                  $ r t        j                  d| ddd	      t        j                  $ r}
|d
u r]t        |
dt        |
j                  j                                      t        |
dt        |
j                  j                                      n9t        |
j                  j                        }t        |
d|       t        |
d|       t        |
d|
j                  j                         |
d }
~
wt        $ r}
|
d }
~
ww xY w)Nr   r   r   r   r   rN   r   r   r   r   r   r
  TrI   rJ   rP   r  )rX   rM   r   r   r   rN   r   r   r   rR   r   r  s               r:   r   zHTTPHandler.patch  sg   	"kk//St$vw`g 0  kk//St$vw 0  {{''F';H%%'O%% 	//5gYiH*4 
 $$ 	~9&9!**//:K&LM6#6qzz7H#IJ0A
9j16:.A}ajj&<&<=G 	G	&   A4A7 7AF8B=E55FFFc           	      P   	 |#| j                   j                  d||||||      }n!| j                   j                  d|||||      }| j                   j                  ||      }	|	S # t        j                  $ r t        j                  d| ddd	      t        $ r}
|
d }
~
ww xY w)
Nr   r   r  r   r   r   r   r   r
  )rh   r   r   rU   r   r.   r   r   )rX   rM   r   r   r   rN   r   r   r   rR   r   s              r:   r   zHTTPHandler.put;  s    	"kk//3TVW^e 0  kk//3TVW 0  {{''F';HO%% 	//5gYiH*4 
  	G	s   A$A' '7B%B  B%c           	         	 |#| j                   j                  d||||||      }n!| j                   j                  d|||||      }| j                   j                  ||      }	|	j                          |	S # t        j
                  $ r t        j                  d| ddd	      t        j                  $ r}
|d
u r]t        |
dt        |
j                  j                                      t        |
dt        |
j                  j                                      n9t        |
j                  j                        }t        |
d|       t        |
d|       t        |
d|
j                  j                         |
d }
~
wt        $ r}
|
d }
~
ww xY w)Nr   r   r  r   r   r   r   r   r
  TrI   rJ   rP   r  )rX   rM   r   r   r   rN   r   r   r   rR   r   r  s               r:   r   zHTTPHandler.deleteY  si   	"kk//c4PWah 0  kk//c4PW 0  {{''F';H%%'O%% 	//5gYiH*4 
 $$ 	~9&9!**//:K&LM6#6qzz7H#IJ0A
9j16:.A}ajj&<&<=G 	G	r  c                 D    	 | j                          y # t        $ r Y y w xY wr|   )r   r   r   s    r:   r   zHTTPHandler.__del__  s"    	JJL 		s    	c                 <    t         j                  rt        d      S y)z
        Create an HTTP transport with IPv4 only if litellm.force_ipv4 is True.
        Otherwise, return None.

        Some users have seen httpx ConnectionError when using ipv6 - forcing ipv4 resolves the issue for them
        r   r   N)r.   r   r   r   s    r:   r   z"HTTPHandler._create_sync_transport  s      y99r<   )Nr   NNr   )	NNNNFNNNNr   r   r   )r]   r^   r_   r	   r
   r   rU   r   r   rw   r/   rT   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r<   r:   r   r     sn    :>)-15!!%u}} 456!! &	!!
 U49-.!!F "&"&+/  $	
 #4.( 
,# 
,$sCx. 
, 
, 3715!%"&9=596:33 uT3-./3 uT3_-.	3
 3 $3 3 %u}} 4563 dL0123 3 233p ,0+/!%"&9=)) uT3Y'() uT3Y'(	)
 ) $) ) %u}} 456)\ ,0+/!%"&9= uT3Y'( uT3Y'(	
  $  %u}} 456B ,0#!%"&9=)) uT3Y'() tn	)
 ) $) %u}} 456) )V
(? 
r<   r   r   r   c                 r   d}|#|j                         D ]  \  }}	 || d| z  } d|z   | z   }t        j                  j	                  |      }|r|S |t        d	i |}n!t        t        j                  dd            }t        j                  j                  ||t               |S # t        $ r Y w xY w)
z
    Retrieves the async HTTP client from the cache
    If not present, creates a new client

    Caches the new client and returns it.
     _async_httpx_client     @r   r   r   r   r   ttlr   )
r   r   r.   in_memory_llm_clients_cache	get_cacherb   rU   r   	set_cacher   )r   r   _params_key_namer   r   _cache_key_name_cached_client_new_clients           r:   get_async_httpx_clientr$    s      ,,.JC se1UG$44  ) +-==LO88BB?SN&00&MM%=
 ''11* 2 
 )  s   
B**	B65B6c                 l   d}| #| j                         D ]  \  }}	 || d| z  } d|z   }t        j                  j	                  |      }|r|S | t        d	i | }n!t        t        j                  dd            }t        j                  j                  ||t               |S # t        $ r Y w xY w)
z
    Retrieves the HTTP client from the cache
    If not present, creates a new client

    Caches the new client and returns it.
    r  r  httpx_clientr  r   r   r  r  r   )
r   r   r.   r  r  r   rU   r   r  r   )r   r   r   r   r!  r"  r#  s          r:   _get_httpx_clientr'    s      ,,.JC se1UG$44  ) %'77O88BB?SN!+F+!%--s*ST''11* 2 
 '  s   
B''	B32B3r|   )9r   r,   r*   r   typingr   r   r   r   r   r   r	   r
   r3   rU   aiohttpr   r   r   r   r   httpx._typesr   r.   litellm._loggingr   litellm.constantsr   (litellm.litellm_core_utils.logging_utilsr   litellm.types.llms.custom_httpr   *litellm.litellm_core_utils.litellm_loggingr   r   r   r   litellm._versionr   r   rN   r   ru   r   rw   r/   r+   r;   rF   r   rH   rb   r   httpxSpecialProviderr   r$  r'  r   r<   r:   <module>r2     sO    	 
  U U U   / G G %  + < I ,$ TL!(
 HWI&
 !5==c: ?h{&; ?uTSVX[XfXfMfGg ?D8E11 4l l^B BN "#&::;#TN# #Lhtn  {  Gs   <D DD