
    i                         U d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZmZmZ ddl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mZ ddlm Z  ddl!m"Z"m#Z# eeed
Z$ee%ef   e&d<   defdZ'dede
fdZ(de
dede
fdZ)dee%e
f   fdZ*dee%   fdZ+dee%   fdZ, e*       Z-e-j]                  d      Z/e-j]                  d      Z0e-j]                  d      Z1e-j]                  d      Z2e-j]                  d      Z3e-j]                  d      Z4e-j]                  d      Z5e-j]                  d      Z6e-j]                  d      Z7e-j]                  d       Z8y)!z
Factory for generating container SDK functions from JSON config.

This module reads endpoints.json and dynamically generates SDK functions
that use the generic container handler.
    N)partial)Path)AnyCallableDictListLiteralOptionalType)request_timeout)Logging)BaseContainerConfig)generic_container_handler)ContainerFileListResponseContainerFileObjectDeleteContainerFileResponse)GenericLiteLLMParams)ProviderConfigManagerclientRESPONSE_TYPESreturnc                      t        t              j                  dz  } t        |       5 }t	        j
                  |      cddd       S # 1 sw Y   yxY w)z0Load the endpoints configuration from JSON file.zendpoints.jsonN)r   __file__parentopenjsonload)config_pathfs     t/Users/manta/Documents/Projects/TheRoad-I1/.venv/lib/python3.12/site-packages/litellm/containers/endpoint_factory.py_load_endpoints_configr!   $   s8    x.''*::K	k	ayy| 
		s   AAendpoint_configc                 R   | d   t         j                  | d         | j                  dg       t        	 	 	 	 	 ddt        dt        d   dt
        t        t        t        f      dt
        t        t        t        f      d	t
        t        t        t        f      f
fd
       }|S )z
    Create a sync SDK function from endpoint config.

    Uses the generic container handler instead of individual handler methods.
    nameresponse_typepath_paramsopenaitimeoutcustom_llm_providerextra_headersextra_query
extra_bodyc                    t               }	 |j                  d      }|j                  d      }|j                  dd      du }	|j                  d      }
|
1t        |
t              rt        j                  |
      }
r di |
S |
S t        di |}t        j                  t        j                  |            }|t        d|       D ci c]  }||v s||j                  |       }}|j                  |d	|d|i|
       t        j                  d|||||| xs t         |	d|S c c}w # t"        $ r}t        j$                  d	||||      d }~ww xY w)Nlitellm_logging_objlitellm_call_id
async_callFTmock_response)providerz)Container provider config not found for:  )kwargsmodeloptional_paramslitellm_paramsr)   )endpoint_namecontainer_provider_configr7   logging_objr*   r+   r(   	_is_asyncr5   r)   original_exceptioncompletion_kwargsextra_kwargs )localspopget
isinstancestrr   loadsr   r   get_provider_container_configlitellmLlmProviders
ValueErrorupdate_from_kwargsr   handleDEFAULT_REQUEST_TIMEOUT	Exceptionexception_type)r(   r)   r*   r+   r,   r4   
local_varsr.   r/   r;   r1   r7   r9   kr6   er8   r&   r%   s                   r    endpoint_funcz4create_sync_endpoint_function.<locals>.endpoint_func5   s    X
;	5;ZZ@U5V-3ZZ8I-JO

<74?I #JJ7M(mS1$(JJ}$=M (9=99$$ 2;F;N &CC --.AB & )0 ?@S?TU 
 :ETAVq&**Q-/OT  22 / 1?C$7 3  -33 
+*C-/+':#:#
 
 
 U0  	(($7#$",# 	s=   A7D2 D2 AD2 	D-D--?D2 -D2 2	E;EEiX  r'   NNN)	r   rC   r   intr	   r
   r   rE   r   )r"   rS   r8   r&   r%   s     @@@r    create_sync_endpoint_functionrV   +   s     $F+M"&&'GHM!%%mR8K192604/3DD$X.D  S#X/D d38n-	D
 T#s(^,D DL     	sync_funcc                      t         	 	 	 	 	 ddt        dt        d   dt        t        t
        t        f      dt        t        t
        t        f      dt        t        t
        t        f      f
 fd       }|S )	z:Create an async SDK function that wraps the sync function.r'   r(   r)   r*   r+   r,   c           	        K   t               }	 t        j                         }d|d<   t        f| ||||d|}t	        j
                         }	t        |	j                  |      }
|j                  d |
       d {   }t        j                  |      r| d {   }|S |}|S 7 )7 # t        $ r}t        j                  d||||      d }~ww xY ww)NTr0   )r(   r)   r*   r+   r,   r3   r<   )rA   asyncioget_event_loopr   contextvarscopy_contextrunrun_in_executoriscoroutinerN   rH   rO   )r(   r)   r*   r+   r,   r4   rP   loopfuncctxfunc_with_contextinit_responseresponserR   rX   s                 r    async_endpoint_funcz;create_async_endpoint_function.<locals>.async_endpoint_func   s      X
	))+D#'F< $7+'% D **,C ' 6"&"6"6t=N"OOM""=1!.. O )O P /
  	(($7#$",# 	sY   CA*B' 9B#:B' B%B' CB' "C#B' %B' '	C0C

CCrT   )r   rU   r	   r
   r   rE   r   )rX   r"   rh   s   `  r    create_async_endpoint_functionri      s     192604/3(($X.(  S#X/( d38n-	(
 T#s(^,( (T rW   c                      t               } i }| d   D ])  }t        |      }|||d   <   t        ||      }|||d   <   + |S )z
    Generate all container endpoint functions from the JSON config.

    Returns a dict mapping function names to their implementations.
    	endpointsr$   
async_name)r!   rV   ri   )configrk   r"   rX   
async_funcs        r    generate_container_endpointsro      s^     $%FI!+.1/B	-6	/&)* 4IO
3=	/,/0 / rW   c                      t               } g }| d   D ]*  }|j                  |d          |j                  |d          , |S )z4Get all endpoint names (sync and async) from config.rk   r$   rl   )r!   append)rm   namesendpoints      r    get_all_endpoint_namesrt      sG    #%FE;'Xf%&Xl+, ( LrW   c                  L    t               } | d   D cg c]  }|d   	 c}S c c}w )z5Get all async endpoint names for router registration.rk   rl   )r!   )rm   rs   s     r    get_async_endpoint_namesrv      s.    #%F39+3FG3FxH\"3FGGGs   !list_container_filesalist_container_filesupload_container_fileaupload_container_fileretrieve_container_filearetrieve_container_filedelete_container_fileadelete_container_fileretrieve_container_file_content aretrieve_container_file_content)9__doc__r[   r]   r   	functoolsr   pathlibr   typingr   r   r   r   r	   r
   r   rH   litellm.constantsr   rM   *litellm.litellm_core_utils.litellm_loggingr   LiteLLMLoggingObj/litellm.llms.base_llm.containers.transformationr   +litellm.llms.custom_httpx.container_handlerr   litellm.types.containers.mainr   r   r   litellm.types.routerr   litellm.utilsr   r   r   rE   __annotations__r!   rV   ri   ro   rt   rv   _generated_endpointsrC   rw   rx   ry   rz   r{   r|   r}   r~   r   r   r@   rW   r    <module>r      s        E E E  H S O Q 
 6 7 ";.#>#S$Y  Q4 QH Qh111 1hd3=&9 *S	 H$s) H 45  ,//0FG ,001HI ,001HI -112JK .223LM /334NO ,001HI -112JK "6":":%#  $8#;#;&$  rW   