o
    ưij                      @   s   d Z ddl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r;	 G d
d de	ZdS )zZ
Azure Anthropic handler - reuses AnthropicChatCompletion logic with Azure authentication
    N)TYPE_CHECKINGCallableUnion)AnthropicChatCompletion)AsyncHTTPHandlerHTTPHandler)ModelResponse)CustomStreamWrapper   )AzureAnthropicConfigc                       sh   e Zd ZdZd fddZddi dfdededed	ed
edede	dede
eejf defddZ  ZS )AzureAnthropicChatCompletionzp
    Azure Anthropic chat completion handler.
    Reuses all Anthropic logic but with Azure authentication.
    returnNc                    s   t    d S )N)super__init__)self	__class__ ^/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/azure_ai/anthropic/handler.pyr      s   z%AzureAnthropicChatCompletion.__init__modelmessagesapi_basecustom_llm_providercustom_prompt_dictmodel_responseprint_verboseoptional_paramstimeoutlitellm_paramsc           $      C   sT  t |}|dd}|dd}|dd}d}t |}t }|j|	|||i |d|i|d}|j|||||d}|
j||	|||dd	 |d
|  |du r|du r|d ||d< | jd/i d|d|d|d|d|d|d|d|d|	d|
d|d|d|d|d|d|d|d|d|durt|t	r|S dS | j
d/i d|d|d|d|d|d|d|d|d|	d|d|
d|d|d|d|d|d|d|d|d|S |du r||d< dd lm} ||||t||||
||d!	\}}dd"lm} t||d#|
||d$S |du s*t|ts8dd%lm} |d|id&}n|}z|j||t||d'}W nQ ty } zDdd(lm} t|d)d*} t|dd}!t|d+t|}"t|d,d}#|!du r|#rt|#dd}!|#rt|#d+rt|#d+|"}"||"| |!d-d}~ww |j||||
|	||||||d.S )0z
        Completion method that uses Azure authentication instead of Anthropic's x-api-key.
        All other logic is the same as AnthropicChatCompletion.
        streamN	json_modeFis_vertex_request)api_keyheadersr   r   r   r   )r   r   r   r   r#   )Zcomplete_input_dictr   r#   )inputr"   Zadditional_argsz_is_function_call: Tz2makes async azure anthropic streaming POST requestr   r   datar   r   r   r   encodingr"   logging_objr   _is_function_callr   	logger_fnr#   r   clientZprovider_configr   )make_sync_call)	r*   r   r#   r%   r   r   r'   r   r    )process_anthropic_headersZazure_ai)completion_streamr   r   r'   Z_response_headers)_get_httpx_client)params)r#   r%   r   )AnthropicErrorstatus_codei  textresponse)messager1   r#   )r   Zraw_responser   r'   r"   Zrequest_datar   r   r   r&   r    r   )copydeepcopypopr   Zvalidate_environmentZtransform_requestZpre_callZacompletion_stream_function
isinstancer   Zacompletion_function#litellm.llms.anthropic.chat.handlerr+   jsondumpsZ#litellm.llms.anthropic.common_utilsr,   r	   r   &litellm.llms.custom_httpx.http_handlerr.   post	Exceptionr0   getattrstrhasattrZtransform_response)$r   r   r   r   r   r   r   r   r&   r"   r'   r   r   r   Zacompletionr)   r#   r*   r   r    r!   r(   configr%   r+   r-   response_headersr,   r.   r3   er0   r1   Zerror_headersZ
error_textZerror_responser   r   r   
completion!   sj  

			
	

	


	
z'AzureAnthropicChatCompletion.completion)r   N)__name__
__module____qualname____doc__r   r@   listdictr   r   r   floathttpxTimeoutrE   __classcell__r   r   r   r   r      s8    r   )rI   r5   r:   typingr   r   r   rM   r9   r   r<   r   r   Zlitellm.types.utilsr   Zlitellm.utilsr	   Ztransformationr   r   r   r   r   r   <module>   s    