o
    i[!                  
   @  sv  d dl 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
mZmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ 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' d dl(m)Z) W n e*y Z+ ze*de+dZ+[+ww eddG dd deZ,d%ddZ-d%dd Z.G d!d" d"ee! Z/G d#d$ d$e'Z0dS )&    )annotationsN)Callable)	dataclass)AnyLiteraloverload)ModelProfile)	UserError)amazon_model_profile)anthropic_model_profile)cohere_model_profile)deepseek_model_profile)meta_model_profile)mistral_model_profile)Provider)
BaseClient)Config)NoRegionError)Session)FrozenAuthTokenu   Please install the `boto3` package to use the Bedrock provider, you can use the `bedrock` optional group — `pip install "pydantic-ai-slim[bedrock]"`T)kw_onlyc                   @  s6   e Zd ZU dZdZded< dZded< dZded< d	S )
BedrockModelProfilezProfile for models used with BedrockModel.

    ALL FIELDS MUST BE `bedrock_` PREFIXED SO YOU CAN MERGE THEM WITH OTHER MODELS.
    Fboolbedrock_supports_tool_choicetextzLiteral['text', 'json']bedrock_tool_result_format bedrock_send_back_thinking_partsN)__name__
__module____qualname____doc__r   __annotations__r   r    r"   r"   c/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_ai/providers/bedrock.pyr   !   s
   
 r   
model_namestrreturnModelProfile | Nonec                 C  $   t | }d| v rtdd|S |S )z;Get the model profile for an Amazon model used via Bedrock.novaT)r   )r
   r   updater$   profiler"   r"   r#   bedrock_amazon_model_profile-      r-   c                 C  r(   )z<Get the model profile for a DeepSeek model used via Bedrock.r1T)r   )r   r   r*   r+   r"   r"   r#   bedrock_deepseek_model_profile5   r.   r0   c                   @  s   e Zd ZdZed&ddZed&ddZed'd	d
Zd(ddZe	d)ddZ
e	ddddddd*ddZ
e	dddddddddd+d"dZ
ddddddddddd#
d,d%dZ
dS )-BedrockProviderzProvider for AWS Bedrock.r&   r%   c                 C  s   dS )Nbedrockr"   selfr"   r"   r#   name@   s   zBedrockProvider.namec                 C  s
   | j jjS N)_clientmetaendpoint_urlr3   r"   r"   r#   base_urlD   s   
zBedrockProvider.base_urlr   c                 C  s   | j S r6   )r7   r3   r"   r"   r#   clientH   s   zBedrockProvider.clientr$   r'   c                 C  s   dd dd t tttd}|dd}t|dkr't|d dkr'|dd  }t|dk r/d S |d }|d }td	|}|rE|d}n|}||v rQ|| |S d S )
Nc                 S  s   t dddt| S )NT)r   r   )r   r*   r   r$   r"   r"   r#   <lambda>N   s    z/BedrockProvider.model_profile.<locals>.<lambda>c                 S  s   t ddt| S )Njson)r   )r   r*   r   r<   r"   r"   r#   r=   Q   s   
 )	anthropicmistralcohereamazonr8   deepseek.   r      z(.+)-v\d+(?::\d+)?$)	r   r-   r   r0   splitlenrematchgroup)r4   r$   provider_to_profilepartsprovidermodel_name_with_versionversion_matchr"   r"   r#   model_profileL   s*   zBedrockProvider.model_profilebedrock_clientNonec                C     d S r6   r"   )r4   rR   r"   r"   r#   __init__s      zBedrockProvider.__init__N)r:   region_nameprofile_nameaws_read_timeoutaws_connect_timeoutapi_keyr:   
str | NonerW   rX   rY   float | NonerZ   c                C  rT   r6   r"   )r4   r[   r:   rW   rX   rY   rZ   r"   r"   r#   rU   v   s   
)aws_access_key_idaws_secret_access_keyaws_session_tokenr:   rW   rX   rY   rZ   r^   r_   r`   c          	      C  rT   r6   r"   )	r4   r^   r_   r`   r:   rW   rX   rY   rZ   r"   r"   r#   rU      s   )
rR   r^   r_   r`   r:   rW   rX   r[   rY   rZ   BaseClient | Nonec       
      
   C  s   |dur	|| _ dS |	pttdd}|
pttdd}||d}z.|dur6tjt|||d}d|d	< n
tj|||||d
}|jdtdi ||d| _ W dS  t	ya } zt
d|d}~ww )a  Initialize the Bedrock provider.

        Args:
            bedrock_client: A boto3 client for Bedrock Runtime. If provided, other arguments are ignored.
            aws_access_key_id: The AWS access key ID. If not set, the `AWS_ACCESS_KEY_ID` environment variable will be used if available.
            aws_secret_access_key: The AWS secret access key. If not set, the `AWS_SECRET_ACCESS_KEY` environment variable will be used if available.
            aws_session_token: The AWS session token. If not set, the `AWS_SESSION_TOKEN` environment variable will be used if available.
            api_key: The API key for Bedrock client. Can be used instead of `aws_access_key_id`, `aws_secret_access_key`, and `aws_session_token`. If not set, the `AWS_BEARER_TOKEN_BEDROCK` environment variable will be used if available.
            base_url: The base URL for the Bedrock client.
            region_name: The AWS region name. If not set, the `AWS_DEFAULT_REGION` environment variable will be used if available.
            profile_name: The AWS profile name.
            aws_read_timeout: The read timeout for Bedrock client.
            aws_connect_timeout: The connect timeout for Bedrock client.
        NAWS_READ_TIMEOUTi,  AWS_CONNECT_TIMEOUT<   )read_timeoutconnect_timeout)botocore_sessionrW   rX   bearersignature_version)r^   r_   r`   rW   rX   zbedrock-runtime)configr9   zGYou must provide a `region_name` or a boto3 client for Bedrock Runtime.r"   )r7   floatosgetenvboto3r   _BearerTokenSessionr;   r   r   r	   )r4   rR   r^   r_   r`   r:   rW   rX   r[   rY   rZ   re   rf   rj   sessionexcr"   r"   r#   rU      s>   


)r&   r%   )r&   r   r$   r%   r&   r'   )rR   r   r&   rS   )r[   r%   r:   r\   rW   r\   rX   r\   rY   r]   rZ   r]   r&   rS   )r^   r\   r_   r\   r`   r\   r:   r\   rW   r\   rX   r\   rY   r]   rZ   r]   r&   rS   )rR   ra   r^   r\   r_   r\   r`   r\   r:   r\   rW   r\   rX   r\   r[   r\   rY   r]   rZ   r]   r&   rS   )r   r   r   r    propertyr5   r:   r;   rQ   r   rU   r"   r"   r"   r#   r1   =   sN    
'r1   c                      s2   e Zd Zd fddZdd	d
ZdddZ  ZS )ro   tokenr%   c                   s   t    || _d S r6   )superrU   rt   )r4   rt   	__class__r"   r#   rU      s   

z_BearerTokenSession.__init___kwargsr   r&   r   c                 K  s
   t | jS r6   )r   rt   )r4   rx   r"   r"   r#   get_auth_token   s   
z"_BearerTokenSession.get_auth_tokenrS   c                 C  rT   r6   r"   r3   r"   r"   r#   get_credentials   rV   z#_BearerTokenSession.get_credentials)rt   r%   )rx   r   r&   r   )r&   rS   )r   r   r   rU   ry   rz   __classcell__r"   r"   rv   r#   ro      s    
ro   rr   )1
__future__r   _annotationsrl   rI   collections.abcr   dataclassesr   typingr   r   r   pydantic_air   pydantic_ai.exceptionsr	   pydantic_ai.profiles.amazonr
   pydantic_ai.profiles.anthropicr   pydantic_ai.profiles.coherer   pydantic_ai.profiles.deepseekr   pydantic_ai.profiles.metar   pydantic_ai.profiles.mistralr   pydantic_ai.providersr   rn   botocore.clientr   botocore.configr   botocore.exceptionsr   botocore.sessionr   botocore.tokensr   ImportError_import_errorr   r-   r0   r1   ro   r"   r"   r"   r#   <module>   sJ    

 