
    )#hcy                     L   d Z ddlZddlZddlZddlmZ ddl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 ddlmZ ddlmZ ddlmZ  G d	 d
e      Z G d d      Z G d d      Z G d dee      Z G d d      Z G d d      Z G d dee      Z G d dee      Z G d de      Z G d dee      Z  G d dee      Z!d Z"	 	 	 	 	 	 	 	 	 	 	 d0d e	e#   d!e	e#   d"e	e#   d#e	e#   d$e	e#   d%e	e#   d&e	e#   d'e	e#   d(e	e$   d)e	e
e%ejL                  f      fd*Z' G d+ d,      Z(d-e#d.e#fd/Z)y)1zF
Common utilities used across bedrock chat/embedding/image generation
    N)Enum)AnyListOptionalUnion)
BaseConfigBaseLLMExceptionLiteLLMLoggingObj)
get_secret)AllMessageValues)ModelResponsec                       e Zd Zy)BedrockErrorN)__name__
__module____qualname__     ^/var/www/html/sandstorm/venv/lib/python3.12/site-packages/litellm/llms/bedrock/common_utils.pyr   r      s    r   r   c                       e Zd Zd ZdefdZdedefdZdee   fdZ	dee   fdZ
dee   fd	Zdee   fd
Zdee   fdZdee   fdZy)AmazonBedrockGlobalConfigc                      y Nr   selfs    r   __init__z"AmazonBedrockGlobalConfig.__init__   s    r   returnc                 
    ddiS )zS
        Mapping of common auth params across bedrock/vertex/azure/watsonx
        region_nameaws_region_namer   r   s    r   get_mapped_special_auth_paramsz8AmazonBedrockGlobalConfig.get_mapped_special_auth_params    s     011r   non_default_paramsoptional_paramsc                 p    | j                         }|j                         D ]  \  }}||v s||||   <    |S r   )r!   items)r   r"   r#   mapped_paramsparamvalues         r   map_special_auth_paramsz1AmazonBedrockGlobalConfig.map_special_auth_params&   sK    ;;=.446 	>LE5%8=e 45	> r   c                     | j                         | j                         z   | j                         z   | j                         z   | j	                         z   S r   )get_us_regionsget_eu_regionsget_ap_regionsget_ca_regionsget_sa_regionsr   s    r   get_all_regionsz)AmazonBedrockGlobalConfig.get_all_regions-   s[    !!!#$!!#$ !!#$ !!#	$	
r   c                 
    g dS )N)zap-northeast-1zap-northeast-2zap-northeast-3z
ap-south-1r   r   s    r   r-   z(AmazonBedrockGlobalConfig.get_ap_regions6   s    SSr   c                     dgS )Nz	sa-east-1r   r   s    r   r/   z(AmazonBedrockGlobalConfig.get_sa_regions9   s
    }r   c                 
    g dS )D
        Source: https://www.aws-services.info/bedrock.html
        )z	eu-west-1z	eu-west-2z	eu-west-3zeu-central-1r   r   s    r   r,   z(AmazonBedrockGlobalConfig.get_eu_regions<   s    
 	
r   c                     dgS )Nzca-central-1r   r   s    r   r.   z(AmazonBedrockGlobalConfig.get_ca_regionsG   s    r   c                 
    g dS )r4   )z	us-east-2z	us-east-1z	us-west-1z	us-west-2zus-gov-west-1r   r   s    r   r+   z(AmazonBedrockGlobalConfig.get_us_regionsJ   s    
 	
r   N)r   r   r   r   dictr!   r)   r   strr0   r-   r/   r,   r.   r+   r   r   r   r   r      s    2 2$ QU 
c 
TS	 TS	 	
S	 	
 S	  

S	 

r   r   c                      e Zd ZdZdededeeej                  f   de
fdZdedee   d	ed
ededefdZ	 	 ddedej                  dedededee   d	ed
ededee   dee   defdZ	 	 ddededee   d	edee   dee   defdZy)AmazonInvokeMixinzG
    Base class for bedrock models going through invoke_handler.py
    error_messagestatus_codeheadersr   c                     t        |||      S )N)messager<   r=   )r   )r   r;   r<   r=   s       r   get_error_classz!AmazonInvokeMixin.get_error_class\   s     !#
 	
r   modelmessagesr#   litellm_paramsc                     t        d      )NzGtransform_request not implemented for config. Done in invoke_handler.pyNotImplementedError)r   rA   rB   r#   rC   r=   s         r   transform_requestz#AmazonInvokeMixin.transform_requeste   s     "U
 	
r   Nraw_responsemodel_responselogging_objrequest_dataencodingapi_key	json_modec                     t        d      )NzHtransform_response not implemented for config. Done in invoke_handler.pyrE   )r   rA   rH   rI   rJ   rK   rB   r#   rC   rL   rM   rN   s               r   transform_responsez$AmazonInvokeMixin.transform_responseq   s     "V
 	
r   api_basec                     t        d      )NzJvalidate_environment not implemented for config. Done in invoke_handler.pyrE   )r   r=   rA   rB   r#   rM   rQ   s          r   validate_environmentz&AmazonInvokeMixin.validate_environment   s     "X
 	
r   NN)r   r   r   __doc__r8   intr   r7   httpxHeadersr	   r@   r   r   rG   Responser   r
   r   r   boolrP   rS   r   r   r   r:   r:   W   ss   
 
/2
=B4CV=W
	




 '(

 	


 

 

 


. "&$(

 nn
 &	

 '
 
 '(
 
 
 
 #
 D>
 

0 "&"&

 
 '(	

 
 #
 3-
 

r   r:   c                   
   e Zd ZU dZdZee   ed<   dZee	   ed<   dZ
ee   ed<   dZee   ed<   	 	 	 	 ddee   dee	   dee   dee   ddf
dZed	        Zd
edededeee   ef   fdZdedee   fdZdededededef
dZy)AmazonTitanConfigaw  
    Reference: https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/providers?model=titan-text-express-v1

    Supported Params for the Amazon Titan models:

    - `maxTokenCount` (integer) max tokens,
    - `stopSequences` (string[]) list of stop sequence strings
    - `temperature` (float) temperature for model,
    - `topP` (int) top p for model
    NmaxTokenCountstopSequencestemperaturetopPr   c                     t               }|j                         D ]%  \  }}|dk7  s|t        | j                  ||       ' y Nr   localsr%   setattr	__class__)r   r]   r^   r_   r`   locals_keyr(   s           r   r   zAmazonTitanConfig.__init__   s@     (!--/ 	4JCf}!2U3	4r   c                    | j                   j                         D ci c]`  \  }}|j                  d      sJ|j                  d      s9t        |t        j
                  t        j                  t        t        f      s|||b c}}S c c}}w N___abc	__dict__r%   
startswith
isinstancetypesFunctionTypeBuiltinFunctionTypeclassmethodstaticmethodclskvs      r   
get_configzAmazonTitanConfig.get_config        **,
1<<%LL(&&-- 	  qD
 	
 
   A%Bsupported_paramsproviderrA   stopc                     d}d|v rZt         j                  rJ|dk(  rEd|v rAg }t        |t              r/|D ]*  }t	        j
                  d|      s|j                  |       , |||d<   |S )z
        filter params to fit the required provider format, drop those that don't fit if user sets `litellm.drop_params = True`.
        Nr   bedrockamazonz^(\|+|User:)$)litellmdrop_paramsrp   listrematchappend)r   r}   r~   rA   r   filtered_stopss          r   _map_and_modify_argz%AmazonTitanConfig._map_and_modify_arg   s}     %%'*=*=9$U): "dD)! 488$4a8)0034 $'4V$r   c                 
    g dS )N)
max_tokensmax_completion_tokensr   r_   top_pstreamr   r   rA   s     r   get_supported_openai_paramsz-AmazonTitanConfig.get_supported_openai_params   s    
 	
r   r"   r#   r   c                     |j                         D ]W  \  }}|dk(  s|dk(  r||d<   |dk(  r||d<   |dk(  r| j                  d|id||      }|d   |d<   |d	k(  r||d
<   |dk(  sS||d<   Y |S )Nr   r   r]   r_   r   r   )r~   rA   r   r^   r   r`   r   )r%   r   )r   r"   r#   rA   r   rx   ry   r   s           r   map_openai_paramsz#AmazonTitanConfig.map_openai_params   s     ',,. 	.DAqL A)@$@340M!12.F{ $ 8 8QK)5q !9 ! 4A3H0G|*+'H},-)	. r   )NNNN)r   r   r   rU   r]   r   rV   __annotations__r^   r   r_   floatr`   r   rt   rz   r7   r8   r   r   r   r   rZ   r   r   r   r   r\   r\      s,   	 $(M8C='$(M8D>(#'K%'D(3- (,(,'+"
4}
4  ~
4 e_	
4
 sm
4 

4 
 
$     	 
 DIsN# .
 
c 
   	
  
r   r\   c                       e Zd ZU dZdZee   ed<   dZee	   ed<   dZ
ee	   ed<   dZee   ed<   dZee   ed	<   dZee   ed
<   dZeee	      ed<   	 	 ddee   dee	   ddfdZed        Zd ZdedefdZy)AmazonAnthropicClaude3Configa~  
    Reference:
        https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/providers?model=claude
        https://docs.anthropic.com/claude/docs/models-overview#model-comparison

    Supported Params for the Amazon / Anthropic Claude 3 models:

    - `max_tokens` Required (integer) max tokens. Default is 4096
    - `anthropic_version` Required (string) version of anthropic for bedrock - e.g. "bedrock-2023-05-31"
    - `system` Optional (string) the system prompt, conversion from openai format to this is handled in factory.py
    - `temperature` Optional (float) The amount of randomness injected into the response
    - `top_p` Optional (float) Use nucleus sampling.
    - `top_k` Optional (int) Only sample from the top K options for each subsequent token
    - `stop_sequences` Optional (List[str]) Custom text sequences that cause the model to stop generating
    i   r   zbedrock-2023-05-31anthropic_versionNsystemr_   r   top_kstop_sequencesr   c                     t               }|j                         D ]%  \  }}|dk7  s|t        | j                  ||       ' y rb   rc   )r   r   r   rg   rh   r(   s         r   r   z%AmazonAnthropicClaude3Config.__init__  s@    
 (!--/ 	4JCf}!2U3	4r   c                     | j                   j                         D ci c]O  \  }}|j                  d      s9t        |t        j
                  t        j                  t        t        f      s|||Q c}}S c c}}w Nrk   rm   rv   s      r   rz   z'AmazonAnthropicClaude3Config.get_config  t     **,
1<<%&&-- 	  qD
 	
 
   AA6c                 
    g dS )N)	r   r   toolstool_choicer   r   r_   r   extra_headersr   r   s    r   r   z8AmazonAnthropicClaude3Config.get_supported_openai_params/  s    

 
	
r   r"   r#   c                     |j                         D ]G  \  }}|dk(  s|dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  sC||d<   I |S )	Nr   r   r   r   r   r   r_   r   r%   r   r"   r#   r'   r(   s        r   r   z.AmazonAnthropicClaude3Config.map_openai_params<  s    .446 	1LE5$1H(H05-+0( ,1)49 01%16.+0(	1 r   rT   )r   r   r   rU   r   r   rV   r   r   r8   r   r_   r   r   r   r   r   r   rt   rz   r   r7   r   r   r   r   r   r      s      !%J$';x}; FHSM #'K%'!E8E?!E8C=*.NHT#Y'. %)+/4SM4 $C=4 
	4 
 
"
D 4 r   r   c                      e Zd ZU dZej
                  Zee   e	d<   dZ
ee   e	d<   dZee   e	d<   dZee   e	d<   dZee   e	d<   dZee   e	d<   	 	 	 	 	 	 ddee   dee   dee   dee   dee   dee   d	dfd
Zed        Zd ZdedefdZy)AmazonAnthropicConfiga  
    Reference: https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/providers?model=claude

    Supported Params for the Amazon / Anthropic models:

    - `max_tokens_to_sample` (integer) max tokens,
    - `temperature` (float) model temperature,
    - `top_k` (integer) top k,
    - `top_p` (integer) top p,
    - `stop_sequences` (string[]) list of stop sequences - e.g. ["\n\nHuman:"],
    - `anthropic_version` (string) version of anthropic for bedrock - e.g. "bedrock-2023-05-31"
    max_tokens_to_sampleNr   r_   r   r   r   r   c                     t               }|j                         D ]%  \  }}	|dk7  s|	t        | j                  ||	       ' y rb   rc   )
r   r   r   r_   r   r   r   rg   rh   r(   s
             r   r   zAmazonAnthropicConfig.__init__b  s@     (!--/ 	4JCf}!2U3	4r   c                     | j                   j                         D ci c]O  \  }}|j                  d      s9t        |t        j
                  t        j                  t        t        f      s|||Q c}}S c c}}w r   rm   rv   s      r   rz   z AmazonAnthropicConfig.get_configp  r   r   c                 
    g dS )N)r   r   r_   r   r   r   r   r   s    r   r   z1AmazonAnthropicConfig.get_supported_openai_params  s    
 	
r   r"   r#   c                     |j                         D ]B  \  }}|dk(  s|dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  s9|d	u s>||d<   D |S )
Nr   r   r   r_   r   r   r   r   Tr   r   s        r   r   z'AmazonAnthropicConfig.map_openai_params  s    .446 
	2LE5$1H(H:? 67%16.+0(49 01 Ud],1)
	2 r   )NNNNNN)r   r   r   rU   r   r   r   r   rV   r   r   r   r_   r   r   r   r   r8   r   rt   rz   r   r7   r   r   r   r   r   r   M  s    +2*<*<(3-<%)NHTN)#'K%'E8C=E8C='+x}+ /3)-'+##+/4&sm4 !4 e_	4
 }4 }4 $C=4 
4 
 
"

D 4 r   r   c            
           e Zd ZU dZdZee   ed<   dZee	   ed<   dZ
ee   ed<   	 	 	 ddee   dee	   dee   ddfdZed        Zd	edee   fd
Zdeded	ededef
dZy)AmazonCohereConfiga*  
    Reference: https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/providers?model=command

    Supported Params for the Amazon / Cohere models:

    - `max_tokens` (integer) max tokens,
    - `temperature` (float) model temperature,
    - `return_likelihood` (string) n/a
    Nr   r_   return_likelihoodr   c                     t               }|j                         D ]%  \  }}|dk7  s|t        | j                  ||       ' y rb   rc   )r   r   r_   r   rg   rh   r(   s          r   r   zAmazonCohereConfig.__init__  @     (!--/ 	4JCf}!2U3	4r   c                    | j                   j                         D ci c]`  \  }}|j                  d      sJ|j                  d      s9t        |t        j
                  t        j                  t        t        f      s|||b c}}S c c}}w rj   rm   rv   s      r   rz   zAmazonCohereConfig.get_config  r{   r|   rA   c                 
    g dS )N)r   r_   r   r   r   s     r   r   z.AmazonCohereConfig.get_supported_openai_params  s    
 	
r   r"   r#   r   c                 t    |j                         D ]$  \  }}|dk(  r||d<   |dk(  r||d<   |dk(  s ||d<   & |S )Nr   r_   r   r   r   r"   r#   rA   r   rx   ry   s          r   r   z$AmazonCohereConfig.map_openai_params  s]     ',,. 	2DAqH},-)M!12.L 01-	2 r   NNN)r   r   r   rU   r   r   rV   r   r_   r   r   r8   r   rt   rz   r   r   r7   rZ   r   r   r   r   r   r     s     !%J$#'K%''+x}+ %)'++/		4SM	4 e_	4 $C=		4
 
	4 
 
$
 
c 
   	
  
r   r   c                   <   e Zd ZU dZdZee   ed<   dZee	   ed<   dZ
ee	   ed<   dZee   ed<   dZee   ed<   dZee   ed<   dZee   ed	<   	 	 	 	 	 	 	 ddee   dee	   dee	   dee   dee   dee   d	ee   d
dfdZed        Zded
efdZdedededed
ef
dZy)AmazonAI21ConfigaK  
    Reference: https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/providers?model=j2-ultra

    Supported Params for the Amazon / AI21 models:

    - `maxTokens` (int32): The maximum number of tokens to generate per result. Optional, default is 16. If no `stopSequences` are given, generation stops after producing `maxTokens`.

    - `temperature` (float): Modifies the distribution from which tokens are sampled. Optional, default is 0.7. A value of 0 essentially disables sampling and results in greedy decoding.

    - `topP` (float): Used for sampling tokens from the corresponding top percentile of probability mass. Optional, default is 1. For instance, a value of 0.9 considers only tokens comprising the top 90% probability mass.

    - `stopSequences` (array of strings): Stops decoding if any of the input strings is generated. Optional.

    - `frequencyPenalty` (object): Placeholder for frequency penalty object.

    - `presencePenalty` (object): Placeholder for presence penalty object.

    - `countPenalty` (object): Placeholder for count penalty object.
    N	maxTokensr_   r`   r^   frequencePenaltypresencePenaltycountPenaltyr   c                     t               }|j                         D ]%  \  }	}
|	dk7  s|
t        | j                  |	|
       ' y rb   rc   )r   r   r_   r`   r^   r   r   r   rg   rh   r(   s              r   r   zAmazonAI21Config.__init__  s@     (!--/ 	4JCf}!2U3	4r   c                    | j                   j                         D ci c]`  \  }}|j                  d      sJ|j                  d      s9t        |t        j
                  t        j                  t        t        f      s|||b c}}S c c}}w rj   rm   rv   s      r   rz   zAmazonAI21Config.get_config  r{   r|   rA   c                 
    g dS N)r   r_   r   r   r   r   s     r   r   z,AmazonAI21Config.get_supported_openai_params!      
 	
r   r"   r#   r   c                     |j                         D ].  \  }}|dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  s*||d<   0 |S )Nr   r   r_   r   r`   r   r   r   s          r   r   z"AmazonAI21Config.map_openai_params)  sn     ',,. 	.DAqL /0,M!12.G|*+'H},-)	. r   )NNNNNNN)r   r   r   rU   r   r   rV   r   r_   r   r`   r^   r   r   r7   r   r   r   rt   rz   r8   r   r   rZ   r   r   r   r   r   r     sN   (  $Ix}##'K%' D(5/ $(M8D>('+htn+&*OXd^*#'L(4.' $('+ $(,+/*.'+4C=4 e_4 uo	4
  ~4 #4.4 "$4 tn4 
4 
 
$
 
 
   	
  
r   r   c                       e Zd ZdZdZy)AnthropicConstantsz	

Human: z

Assistant: N)r   r   r   HUMAN_PROMPT	AI_PROMPTr   r   r   r   r   <  s     L!Ir   r   c            
           e Zd ZU dZdZee   ed<   dZee	   ed<   dZ
ee	   ed<   	 	 	 ddee   dee	   dee   ddfdZed	        Zd
edefdZdeded
ededef
dZy)AmazonLlamaConfigaB  
    Reference: https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/providers?model=meta.llama2-13b-chat-v1

    Supported Params for the Amazon / Meta Llama models:

    - `max_gen_len` (integer) max tokens,
    - `temperature` (float) temperature for model,
    - `top_p` (float) top p for model
    Nmax_gen_lenr_   r`   r]   r   c                     t               }|j                         D ]%  \  }}|dk7  s|t        | j                  ||       ' y rb   rc   )r   r]   r_   r`   rg   rh   r(   s          r   r   zAmazonLlamaConfig.__init__P  r   r   c                    | j                   j                         D ci c]`  \  }}|j                  d      sJ|j                  d      s9t        |t        j
                  t        j                  t        t        f      s|||b c}}S c c}}w rj   rm   rv   s      r   rz   zAmazonLlamaConfig.get_config[  r{   r|   rA   c                 
    g dS r   r   r   s     r   r   z-AmazonLlamaConfig.get_supported_openai_paramsn  r   r   r"   r#   r   c                     |j                         D ].  \  }}|dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  s*||d<   0 |S )Nr   r   r_   r   r   r   r   s          r   r   z#AmazonLlamaConfig.map_openai_paramsv  sn     ',,. 	.DAqL 12.M!12.G|+,(H},-)	. r   r   )r   r   r   rU   r   r   rV   r   r_   r   r`   r   rt   rz   r8   r   r   r7   rZ   r   r   r   r   r   r   A  s     "&K#%#'K%' D(5/  (,'+"		4}	4 e_	4 sm		4
 
	4 
 
$
 
 
   	
  
r   r   c                      e Zd ZU dZdZee   ed<   dZee	   ed<   dZ
ee	   ed<   dZee	   ed<   dZeee      ed<   	 	 	 	 	 ddee   dee	   dee   dee	   deee      ddfd	Zed
        Zdedee   fdZdededededef
dZy)AmazonMistralConfiga  
    Reference: https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-mistral.html
    Supported Params for the Amazon / Mistral models:

    - `max_tokens` (integer) max tokens,
    - `temperature` (float) temperature for model,
    - `top_p` (float) top p for model
    - `stop` [string] A list of stop sequences that if generated by the model, stops the model from generating further output.
    - `top_k` (float) top k for model
    Nr   r_   r   r   r   r   c                     t               }|j                         D ]%  \  }}|dk7  s|t        | j                  ||       ' y rb   rc   )	r   r   r_   r   r   r   rg   rh   r(   s	            r   r   zAmazonMistralConfig.__init__  s@     (!--/ 	4JCf}!2U3	4r   c                    | j                   j                         D ci c]`  \  }}|j                  d      sJ|j                  d      s9t        |t        j
                  t        j                  t        t        f      s|||b c}}S c c}}w rj   rm   rv   s      r   rz   zAmazonMistralConfig.get_config  r{   r|   rA   c                 
    g dS )N)r   r_   r   r   r   r   r   s     r   r   z/AmazonMistralConfig.get_supported_openai_params  s    GGr   r"   r#   r   c                     |j                         D ]8  \  }}|dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  s4||d<   : |S )Nr   r_   r   r   r   r   r   s          r   r   z%AmazonMistralConfig.map_openai_params  s     ',,. 
	.DAqL 01-M!12.G|+,(F{*+'H},-)
	. r   )NNNNN)r   r   r   rU   r   r   rV   r   r_   r   r   r   r   r   r8   r   rt   rz   r   r7   rZ   r   r   r   r   r   r     s    	 !%J$#'K%'!E8E?!!E8E?! $D(49
$ %)'+#!%$(4SM4 e_4 }	4
 4 tCy!4 
4 
 
$H Hc H   	
  
r   r   c                       fd}|S )z,Closure to capture the headers and add them.c                 n    j                         D ]!  \  }}| j                  j                  ||       # y)z.Actual callback function that Boto3 will call.N)r%   r=   
add_header)requestkwargsheader_nameheader_valuer=   s       r   callbackz#add_custom_header.<locals>.callback  s3    )0 	B%KOO&&{LA	Br   r   )r=   r   s   ` r   add_custom_headerr     s    B
 Or   aws_access_key_idaws_secret_access_keyr    aws_bedrock_runtime_endpointaws_session_nameaws_profile_nameaws_role_nameaws_web_identity_tokenr   timeoutc           
      `   t        dd       }t        dd       }||||||||g}t        |      D ](  \  }}|s	|j                  d      st        |      ||<   * |\  }}}}}}}}t        j                  dt
        j                        }| rn|r|} n|r|} n|r|} nt        dd      t        d      }|r|}n|r|}nd	|  d
}dd l}t        |
t              r|j                  j                  |
|
      }nft        |
t        j                        r2|j                  j                  |
j                  |
j                         }n|j                  j                         }|p|n|lt        |      }|t        dd      |j#                  d      }|j%                  |||d      }|j#                  d|d   d   |d   d   |d   d   | |||      }n|U|S|j#                  d||      }|j'                  ||      }|j#                  d|d   d   |d   d   |d   d   | |||      }nZ||j#                  d||| |||      }n?|'|j)                  |      j#                  d| |||      }n|j#                  d| |||      }|	r/|j*                  j,                  j/                  dt1        |	             |S )NAWS_REGION_NAME
AWS_REGIONzos.environ/
SSL_VERIFYzRAWS region not set: set AWS_REGION_NAME or AWS_REGION env variable or in .env filei  )r?   r<   AWS_BEDROCK_RUNTIME_ENDPOINTzhttps://bedrock-runtime.z.amazonaws.comr   )connect_timeoutread_timeoutz6OIDC token could not be retrieved from secret manager.stsi  )RoleArnRoleSessionNameWebIdentityTokenDurationSecondszbedrock-runtimeCredentialsAccessKeyIdSecretAccessKeySessionToken)service_namer   r   aws_session_tokenr   endpoint_urlconfigverify)r   r   )r   r   )r   r   r   r   r   r   r   )profile_name)r   r   r   r   r   zbefore-sign.bedrock-runtime.*)r   	enumeratero   osgetenvr   
ssl_verifyr   boto3rp   r   sessionConfigrW   Timeoutconnectreadclientassume_role_with_web_identityassume_roleSessionmetaeventsregisterr   )r   r   r   r    r   r   r   r   r   r   r   litellm_aws_region_namestandard_aws_region_nameparams_to_checkir'   r   env_aws_bedrock_runtime_endpointr   r  r   
oidc_token
sts_clientsts_responser  s                            r   init_bedrock_clientr    s    )):DA),= 	$	O o. 35U%%m4!+E!2OA3 		$ <););<J 	%	 -	!.h
 	
 (22P'Q$#3	)71+nM'5!%%gG%T	GU]]	+%%#OO',, & 
 %%' 	*%( 67
P 
 \\%(
 "??!,' 	 @ 
 **=9-H".}"=>O"P*=9.I#%  	
 
	"'7'C\\/"7 " 

 "--!3C . 
 **=9-H".}"=>O"P*=9.I#%  	
 
	& */"7#%  
 
	% ,<=DD*#% E 
 *#%  
 ##+->}-M	
 Mr   c                   *    e Zd Zd Zd Zd Zd Zd Zy)ModelResponseIteratorc                      || _         d| _        y )NF)rI   is_done)r   rI   s     r   r   zModelResponseIterator.__init__  s    ,r   c                     | S r   r   r   s    r   __iter__zModelResponseIterator.__iter__      r   c                 L    | j                   rt        d| _         | j                  S NT)r  StopIterationrI   r   s    r   __next__zModelResponseIterator.__next__  s"    <<"""r   c                     | S r   r   r   s    r   	__aiter__zModelResponseIterator.__aiter__  r  r   c                 T   K   | j                   rt        d| _         | j                  S wr   )r  StopAsyncIterationrI   r   s    r   	__anext__zModelResponseIterator.__anext__  s&     <<$$"""s   &(N)r   r   r   r   r  r"  r$  r'  r   r   r   r  r    s    
##r   r  response_tool_namer   c                 x    | t         j                  j                  v rt         j                  j                  |    } | S )z
    If litellm formatted the input tool name, we need to convert it back to the original name.

    Args:
        response_tool_name (str): The name of the tool as received from the response.

    Returns:
        str: The original name of the tool.
    )r   bedrock_tool_name_mappings
cache_dict)r(  s    r   get_bedrock_tool_namer,    s:     W??JJJ$??JJ
 r   )NNNNNNNNNNN)*rU   r   r   rq   enumr   typingr   r   r   r   rW   r   )litellm.llms.base_llm.chat.transformationr   r	   r
   litellm.secret_managers.mainr   litellm.types.llms.openair   litellm.types.utilsr   r   r   r:   r\   r   r   r   r   r   r   r   r   r8   r7   r   r  r  r  r,  r   r   r   <module>r3     s   
 	   - -   
 4 6 -	# 	8
 8
v7
 7
tf): fRP PfM M`B*J BJW(* Wt" "
E): EPG+Z GT '++/%)26&*&*#',0$(59s}s $C=s c]	s
 #+3-s sms sms C=s %SMs D>s eE5==012sl# #4c c r   