o
    ưil                      @   s  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dZ'dede
fddZ(de
dede
fddZ)dee%e
f fddZ*dee% fddZ+dee% fddZ,e* Z-e-.dZ/e-.dZ0e-.dZ1e-.d Z2e-.d!Z3e-.d"Z4e-.d#Z5e-.d$Z6e-.d%Z7e-.d&Z8dS )'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                  C   sD   t tjd } t| }t|W  d   S 1 sw   Y  dS )z0Load the endpoints configuration from JSON file.zendpoints.jsonN)r   __file__parentopenjsonload)Zconfig_pathf r   Z/home/app/Keep/.python/lib/python3.10/site-packages/litellm/containers/endpoint_factory.py_load_endpoints_config$   s   
$r   endpoint_configc                    s   | d  t | d | 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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_paramsX  openaiNtimeoutcustom_llm_providerextra_headersextra_query
extra_bodyc                    s  t  }zu d} d} dddu }	 d}
|
d ur6t|
tr)t|
}
r3di |
W S |
W S tdi  }tj	t
|d}|d u rQtd|  fd	d
D }|jd|d|i|d tjd|||||| pqt|	d W S  ty } z
t
jd||| dd }~ww )Nlitellm_logging_objlitellm_call_id
async_callFTmock_response)providerz)Container provider config not found for: c                    s    i | ]}| v r|  |qS r   )get).0kkwargsr   r   
<dictcomp>Y   s     zHcreate_sync_endpoint_function.<locals>.endpoint_func.<locals>.<dictcomp> )modeloptional_paramslitellm_paramsr'   )endpoint_namecontainer_provider_configr9   Zlogging_objr(   r)   r&   	_is_asyncr7   r'   Zoriginal_exceptionZcompletion_kwargsextra_kwargsr   )localspopr0   
isinstancestrr   loadsr   r   Zget_provider_container_configlitellmZLlmProviders
ValueErrorZupdate_environment_variablesr   handleDEFAULT_REQUEST_TIMEOUT	Exceptionexception_type)r&   r'   r(   r)   r*   r4   
local_varsr+   r,   r<   r.   r9   r;   r8   er:   r#   r"   r3   r   endpoint_func5   s`   	




	z4create_sync_endpoint_function.<locals>.endpoint_funcr$   r%   NNN)	r   r0   r   intr   r	   r   rB   r   )r    rM   r   rL   r   create_sync_endpoint_function+   s*   CrP   	sync_funcc                    s^   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	d
}|S )z:Create an async SDK function that wraps the sync function.r$   r%   Nr&   r'   r(   r)   r*   c              
      s   t  }z;t }d|d< t f| ||||d|}t }	t|	j|}
|d |
I d H }t|r;|I d H }|W S |}|W S  t	yU } z
t
jd||||dd }~ww )NTr-   )r&   r'   r(   r)   r*   r6   r=   )r?   asyncioget_event_loopr   contextvarscopy_contextrunrun_in_executoriscoroutinerH   rD   rI   )r&   r'   r(   r)   r*   r4   rJ   loopfuncctxZfunc_with_contextZinit_responseresponserK   rQ   r   r   async_endpoint_func   sD   	


z;create_async_endpoint_function.<locals>.async_endpoint_funcrN   )r   rO   r   r	   r   rB   r   )rQ   r    r^   r   r]   r   create_async_endpoint_function|   s$   *r_   c                  C   sF   t  } i }| d D ]}t|}|||d < t||}|||d < q	|S )z
    Generate all container endpoint functions from the JSON config.
    
    Returns a dict mapping function names to their implementations.
    	endpointsr!   
async_name)r   rP   r_   )configr`   r    rQ   Z
async_funcr   r   r   generate_container_endpoints   s   
rc   c                  C   s8   t  } g }| d D ]}||d  ||d  q	|S )z4Get all endpoint names (sync and async) from config.r`   r!   ra   )r   append)rb   namesendpointr   r   r   get_all_endpoint_names   s   rg   c                  C   s   t  } dd | d D S )z5Get all async endpoint names for router registration.c                 S   s   g | ]}|d  qS )ra   r   )r1   rf   r   r   r   
<listcomp>   s    z,get_async_endpoint_names.<locals>.<listcomp>r`   )r   )rb   r   r   r   get_async_endpoint_names   s   ri   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__rR   rT   r   	functoolsr   pathlibr   typingr   r   r   r   r   r	   r
   rD   Zlitellm.constantsr   rG   Z*litellm.litellm_core_utils.litellm_loggingr   ZLiteLLMLoggingObjZ/litellm.llms.base_llm.containers.transformationr   Z+litellm.llms.custom_httpx.container_handlerr   Zlitellm.types.containers.mainr   r   r   Zlitellm.types.routerr   Zlitellm.utilsr   r   r   rB   __annotations__r   rP   r_   rc   rg   ri   Z_generated_endpointsr0   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   r   r   r   r   <module>   sT    $Q
4









