o
    ưi                     @   st   d Z ddlmZmZmZmZmZ ddl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 G dd	 d	eZdS )
z`
Anthropic CountTokens API handler.

Uses httpx for HTTP requests instead of the Anthropic SDK.
    )AnyDictListOptionalUnionN)verbose_logger)AnthropicError)AnthropicCountTokensConfig)get_async_httpx_clientc                   @   s~   e Zd ZdZ				ddedeeeef  dedee dee	e
ejf  deeeeef   d	ee d
eeef fddZdS )AnthropicCountTokensHandlerz
    Handler for Anthropic CountTokens API requests.

    Uses httpx for HTTP requests, following the same pattern as BedrockCountTokensHandler.
    Nmodelmessagesapi_keyapi_basetimeouttoolssystemreturnc              
      s  z}|  || td|  | j||||d}td|  |p&|  }	td|	  | |}
ttjj	d}|durA|ntj
}|j|	|
||dI dH }td|j  |jd	krp|j}td
|  t|j|d| }td|  |W S  ty     tjy } ztdt|  t|jj|jjdd}~w ty } ztdt|  tddt| dd}~ww )a  
        Handle a CountTokens request using httpx.

        Args:
            model: The model identifier (e.g., "claude-3-5-sonnet-20241022")
            messages: The messages to count tokens for
            api_key: The Anthropic API key
            api_base: Optional custom API base URL
            timeout: Optional timeout for the request (defaults to litellm.request_timeout)

        Returns:
            Dictionary containing token count response

        Raises:
            AnthropicError: If the API request fails
        z4Processing Anthropic CountTokens request for model: )r   r   r   r   zTransformed request: zMaking request to: )Zllm_providerN)headersjsonr   zResponse status:    zAnthropic API error: )status_codemessagezAnthropic response: z#HTTP error in CountTokens handler: zError in CountTokens handler: i  zCountTokens processing error: )Zvalidate_requestr   debugZ!transform_request_to_count_tokensZ#get_anthropic_count_tokens_endpointZget_required_headersr
   litellmZLlmProvidersZ	ANTHROPICrequest_timeoutpostr   texterrorr   r   httpxZHTTPStatusErrorstrresponse	Exception)selfr   r   r   r   r   r   r   request_bodyZendpoint_urlr   Zasync_clientr   r!   Z
error_textZanthropic_responsee r&   b/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/anthropic/count_tokens/handler.pyhandle_count_tokens_request   sl   

z7AnthropicCountTokensHandler.handle_count_tokens_request)NNNN)__name__
__module____qualname____doc__r    r   r   r   r   r   floatr   Timeoutr(   r&   r&   r&   r'   r      s.    
	r   )r,   typingr   r   r   r   r   r   r   Zlitellm._loggingr   Z#litellm.llms.anthropic.common_utilsr   Z2litellm.llms.anthropic.count_tokens.transformationr	   Z&litellm.llms.custom_httpx.http_handlerr
   r   r&   r&   r&   r'   <module>   s    