o
    ưi                     @   sh   d Z ddl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 )
zn
AWS Bedrock CountTokens API handler.

Simplified handler leveraging existing LiteLLM Bedrock infrastructure.
    )AnyDictN)verbose_logger)BedrockError)BedrockCountTokensConfig)get_async_httpx_clientc                
   @   sB   e Zd ZdZdeeef deeef dedeeef fddZdS )	BedrockCountTokensHandlerz
    Simplified handler for AWS Bedrock CountTokens API requests.

    Uses existing LiteLLM infrastructure for authentication and request handling.
    request_datalitellm_paramsresolved_modelreturnc              
      s  z|  | td|  | j||dd}td|  | j|d}td|  | ||}td|  |dd}d	d
i}| jd||||||d\}	}
tt	j
jd}|j||	|
ddI dH }td|j  |jdkr|j}td|  t|j|d| }t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 existing LiteLLM patterns.

        Args:
            request_data: The incoming request payload
            litellm_params: LiteLLM configuration parameters
            resolved_model: The actual model ID resolved from router

        Returns:
            Dictionary containing token count response
        z3Processing CountTokens request for resolved model: N)optional_paramsmodelZmodel_idzRetrieved AWS region: )r	   zTransformed request: zMaking request to: api_keyzContent-Typezapplication/jsonZbedrock)Zservice_nameheadersr   r	   Zapi_baser   r   )Zllm_providerg      >@)r   datatimeoutzResponse status:    zAWS Bedrock error: )status_codemessagezBedrock response: zFinal response: z#HTTP error in CountTokens handler: zError in CountTokens handler: i  zCountTokens processing error: )Zvalidate_count_tokens_requestr   debugZ_get_aws_region_nameZ+transform_anthropic_to_bedrock_count_tokensZ!get_bedrock_count_tokens_endpointgetZ_sign_requestr   litellmZLlmProvidersZBEDROCKpostr   texterrorr   jsonZ'transform_bedrock_response_to_anthropichttpxZHTTPStatusErrorstrresponse	Exception)selfr	   r
   r   Zaws_region_nameZbedrock_requestZendpoint_urlr   r   Zsigned_headersZsigned_bodyZasync_clientr   Z
error_textZbedrock_responseZfinal_responsee r#   `/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/bedrock/count_tokens/handler.pyhandle_count_tokens_request   s   



z5BedrockCountTokensHandler.handle_count_tokens_requestN)__name__
__module____qualname____doc__r   r   r   r%   r#   r#   r#   r$   r      s    


r   )r)   typingr   r   r   r   Zlitellm._loggingr   Z!litellm.llms.bedrock.common_utilsr   Z0litellm.llms.bedrock.count_tokens.transformationr   Z&litellm.llms.custom_httpx.http_handlerr   r   r#   r#   r#   r$   <module>   s    