o
    ưip%                  	   @   s  U d Z ddlZ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mZ ddlZddlZddlmZmZmZmZ erIddlmZ ddlZddlmZ ddlmZmZmZ ded	ee d
f fddZ!e!eZ"ee d
f e#d< e!eZ$ee d
f e#d< G dd deZ%deej&ej'f de	e ef d	e	e ef fddZ(G dd dZ)G dd deZ*			ddee  dee  dee  d	e*fddZ+dS )z3
Common helpers / utils across al OpenAI endpoints
    N)	TYPE_CHECKINGAnyDictListLiteral
NamedTupleOptionalTupleUnion)AsyncAzureOpenAIAsyncOpenAIAzureOpenAIOpenAI)ClientSession)BaseLLMException)_DEFAULT_TTL_FOR_HTTPX_CLIENTSAsyncHTTPHandlerget_ssl_configurationclsreturn.c                 C   s   t dd t| jjD S )zAExtract __init__ parameter names (excluding 'self') from a class.c                 s   s    | ]	}|d kr|V  qdS )selfN ).0pr   r   W/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/openai/common_utils.py	<genexpr>   s    z*_get_client_init_params.<locals>.<genexpr>)tupleinspect	signature__init__
parameters)r   r   r   r   _get_client_init_params   s   r!   _OPENAI_INIT_PARAMS_AZURE_OPENAI_INIT_PARAMSc                       s^   e Zd Z				d
dededeej deej dee	e
ejf  dee
 f fdd	Z  ZS )OpenAIErrorNstatus_codemessagerequestresponseheadersbodyc                    sp   || _ || _|| _|r|| _ntjddd| _|r|| _n	tj|| jd| _t j	|| j| j| j| j|d d S )NPOSThttps://api.openai.com/v1)methodurl)r%   r'   )r%   r&   r)   r'   r(   r*   )
r%   r&   r)   r'   httpxRequestr(   Responsesuperr   )r   r%   r&   r'   r(   r)   r*   	__class__r   r   r   &   s&   	
zOpenAIError.__init__)NNNN)__name__
__module____qualname__intstrr   r/   r0   r1   r
   dictZHeadersr   __classcell__r   r   r3   r   r$   %   s$    r$   edatac           	         s   g  t | tjr| j }|di }|}n| j}|durt |tr|dr|di }t |trHzt	|}W n tj
yG   d|i}Y nw |d}t |trt|dkrt |d tr|D ]!}|drt |dtrt|ddkr |d d  qa fd	d
| D }|S )a{  
    Helper function to read OpenAI UnprocessableEntityError and drop the params that raised an error from the error message.

    Args:
    e (UnprocessableEntityError): The UnprocessableEntityError exception
    data (Dict[str, Any]): The original data dictionary containing all parameters

    Returns:
    Dict[str, Any]: A new dictionary with invalid parameters removed
    errorNr&   detailr   loc      c                    s   i | ]\}}| vr||qS r   r   )r   kvZinvalid_paramsr   r   
<dictcomp>s   s    z?drop_params_from_unprocessable_entity_error.<locals>.<dictcomp>)
isinstancer/   HTTPStatusErrorr(   jsongetr*   r:   r9   loadsJSONDecodeErrorr   lenlistappenditems)	r<   r=   Z
error_jsonerror_messageZ
error_bodyr&   r?   Z
error_dictZnew_datar   rE   r   +drop_params_from_unprocessable_entity_errorH   s<   


$rR   c                   @   s   e Zd ZdZededed deee	e
eef  fddZedee	e
eef ded defd	d
Zededed defddZeded deedf fddZe	dded deej fddZedeej fddZdS )BaseOpenAILLMzb
    Base class for OpenAI LLMs for getting their httpx clients and SSL verification settings
    client_initialization_paramsclient_type)openaiZazurer   c                 C   s   t j| |d}tj|}|S )zbRetrieves the OpenAI client from the in-memory cache based on the client initialization parametersrT   rU   )rS   get_openai_client_cache_keylitellmin_memory_llm_clients_cacheZ	get_cache)rT   rU   
_cache_keyZ_cached_clientr   r   r   get_cached_openai_client}   s   z&BaseOpenAILLM.get_cached_openai_clientopenai_clientc                 C   s$   t j||d}tjj|| td dS )zZStores the OpenAI client in the in-memory cache for _DEFAULT_TTL_FOR_HTTPX_CLIENTS SECONDSrW   )keyvaluettlN)rS   rX   rY   rZ   Z	set_cacher   )r]   rU   rT   r[   r   r   r   set_cached_openai_client   s   
z&BaseOpenAILLM.set_cached_openai_clientc           	      C   s   d}|  ddurt|  dd }| }d| d|  d g}d}tj|d| }|D ]}|| d	|  |  q1d
|}|S )zWCreates a cache key for the OpenAI client based on the client initialization parametersNapi_key zhashed_api_key=z	is_async=is_async)timeoutmax_retriesorganizationapi_baserU   =,)	rJ   hashlibsha256encode	hexdigestrS   -get_openai_client_initialization_param_fieldsrO   join)	rT   rU   Zhashed_api_keyZhash_object	key_partsZLITELLM_CLIENT_SPECIFIC_PARAMSZopenai_client_fieldsparamr[   r   r   r   rX      s(   
z)BaseOpenAILLM.get_openai_client_cache_key.c                 C   s   | dkrt S tS )zGReturns a tuple of fields that are used to initialize the OpenAI clientrV   )r"   r#   ri   r   r   r   rp      s   z;BaseOpenAILLM.get_openai_client_initialization_param_fieldsNshared_sessionr   c                 C   sv   t jd urt jS tt ddrddlm} tj| dS t }tj|tj	t
|tjr+|nd t
|tr3|nd | dddS )	Nnetwork_mockFr   MockOpenAITransport	transport)ssl_contextZ
ssl_verifyrt   T)verifyry   follow_redirects)rY   Zaclient_sessiongetattr(litellm.llms.custom_httpx.mock_transportrw   r/   AsyncClientr   r   Z_create_async_transportrG   ssl
SSLContextbool)rt   rw   
ssl_configr   r   r   _get_async_http_client   s"   

z$BaseOpenAILLM._get_async_http_clientc                  C   sJ   t jd urt jS tt ddrddlm}  tj|  dS t }tj|ddS )Nru   Fr   rv   rx   T)r{   r|   )rY   Zclient_sessionr}   r~   rw   r/   Clientr   )rw   r   r   r   r   _get_sync_http_client   s   
z#BaseOpenAILLM._get_sync_http_client)N)r5   r6   r7   __doc__staticmethodr:   r   r   r
   r   r   r   r   r\   ra   r9   rX   r	   rp   r/   r   r   r   r   r   r   r   r   rS   x   sR    %
	rS   c                   @   s.   e Zd ZU eed< ee ed< ee ed< dS )OpenAICredentialsrh   rb   rg   N)r5   r6   r7   r9   __annotations__r   r   r   r   r   r      s   
 r   rh   rb   rg   c                 C   sd   | pt jptdptdpd}|pt jptddpd}|p*t jp*t jp*td}t|||dS )zFResolve OpenAI credentials from params, litellm globals, and env vars.ZOPENAI_BASE_URLZOPENAI_API_BASEr,   ZOPENAI_ORGANIZATIONNZOPENAI_API_KEY)rh   rb   rg   )rY   rh   osgetenvrg   rb   Z
openai_keyr   )rh   rb   rg   Zresolved_api_baseZresolved_organizationZresolved_api_keyr   r   r   get_openai_credentials   s8   
r   )NNN),r   rl   r   rI   r   r   typingr   r   r   r   r   r   r   r	   r
   r/   rV   r   r   r   r   Zaiohttpr   rY   Z)litellm.llms.base_llm.chat.transformationr   Z&litellm.llms.custom_httpx.http_handlerr   r   r   typer9   r!   r"   r   r#   r$   ZUnprocessableEntityErrorrH   rR   rS   r   r   r   r   r   r   <module>   sR    ,#


0 