
    i                     n    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	e      Zy)
zn
AWS Bedrock CountTokens API handler.

Simplified handler leveraging existing LiteLLM Bedrock infrastructure.
    )AnyDictN)verbose_logger)BedrockError)BedrockCountTokensConfig)get_async_httpx_clientc            
       F    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Zy)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           	        K   	 | j                  |       t        j                  d|        | j                  ||d      }t        j                  d|        | j	                  |      }t        j                  d|        |j                  dd      }|j                  dd      }| j                  ||||	      }t        j                  d
|        |j                  dd      }	ddi}
| j                  d|
|||||	      \  }}t        t        j                  j                        }|j                  |||d       d{   }t        j                  d|j                          |j                  dk7  r;|j                  }t        j                  d|        t!        |j                  |      |j#                         }t        j                  d|        | j%                  |      }t        j                  d|        |S 7 # t         $ r  t&        j(                  $ r[}t        j                  dt+        |              t!        |j,                  j                  |j,                  j                        d}~wt.        $ r?}t        j                  dt+        |              t!        ddt+        |             d}~ww xY 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modelmodel_idzRetrieved AWS region: )r   zTransformed request: api_baseaws_bedrock_runtime_endpoint)r   aws_region_namer   r   zMaking request to: api_keyzContent-Typezapplication/jsonbedrock)service_nameheadersr   r   r   r   r   )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: )validate_count_tokens_requestr   debug_get_aws_region_name+transform_anthropic_to_bedrock_count_tokensget!get_bedrock_count_tokens_endpoint_sign_requestr   litellmLlmProvidersBEDROCKpostr   texterrorr   json'transform_bedrock_response_to_anthropichttpxHTTPStatusErrorstrresponse	Exception)selfr   r   r   r   bedrock_requestr   r   endpoint_urlr   r   signed_headerssigned_bodyasync_clientr2   
error_textbedrock_responsefinal_responsees                      z/Users/manta/Documents/Projects/TheRoad-I1/.venv/lib/python3.12/site-packages/litellm/llms/bedrock/count_tokens/handler.pyhandle_count_tokens_requestz5BedrockCountTokensHandler.handle_count_tokens_request   s    "e	..|<  EnEUV
 #77 .$ 8 O   #9/9J!KL #NN) O O   #88I!JK &))*d;H+9+=+=.,(  AA$ /!-I	 B L   #6|n!EF %((D9G%'9:G*.*<*<& .,%$ += +'NK 2$1199L *..& 	 /  H   #4X5I5I4J!KL##s*%]]
$$'::,%GH" ( 4 4& 
  (}}  #56F5G!HI "II N   #3N3C!DE!!9<  	$$ 	  #Fs1vh!OPJJ22

   	  #A#a&!JK8QA 	sI   KD=H HCH KH K#AI99K:J??KKN)__name__
__module____qualname____doc__r   r1   r   r?        r>   r
   r
      sO    v38nv S#Xv 	v
 
c3hvrE   r
   )rC   typingr   r   r/   r'   litellm._loggingr   !litellm.llms.bedrock.common_utilsr   0litellm.llms.bedrock.count_tokens.transformationr   &litellm.llms.custom_httpx.http_handlerr   r
   rD   rE   r>   <module>rK      s0       + : U I} 8 }rE   