B
    fZ              	   @  sr  d dl mZ d dlZd dlmZmZmZ d dlmZm	Z	 d dl
Z
ddlmZmZmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZ dd	lmZmZ dd
lmZ ddl m!Z!m"Z" ddlm#Z# ddl$m%Z%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. dddddddddg	Z/G dd de+Z0G dd de,Z1G dd dZ2G dd dZ3G dd dZ4G d d! d!Z5e0Z6e1Z7dS )"    )annotationsN)AnyUnionMapping)Selfoverride   )	resources
_constants_exceptions)Querystring)		NOT_GIVENOmitHeadersTimeoutNotGiven	TransportProxiesTypesAsyncTransportRequestOptions)is_givenget_async_library)__version__)StreamAsyncStream)APIStatusError)TokenizerTypesync_get_tokenizerasync_get_tokenizer)DEFAULT_MAX_RETRIESDEFAULT_CONNECTION_LIMITSSyncAPIClientAsyncAPIClientSyncHttpxClientWrapperAsyncHttpxClientWrapperr   r   r   r   r	   	AnthropicAsyncAnthropicClientAsyncClientc                  s  e Zd ZU ded< ded< ded< ded< d	ed
< ded< ded< ejZejZdddeedddddddddddddddddddddd fddZ	e
edd d!d"Ze
ed#d d$d%Ze
d#d d&d'Ze
d#d d(d)Ze
ed*d  fd+d,Zed-d-dd.d/d0Zdddeddeddddi d1dddd2ddd3ddddd4d5d6d7d8ZeZd9dd:d;d<Zd=d d>d?Zed9d@dAdBdCdDdEZ  ZS )Fr%   zresources.Completionscompletionszresources.Messagesmessageszresources.BetabetaAnthropicWithRawResponsewith_raw_responseAnthropicWithStreamedResponsewith_streaming_responsez
str | Noneapi_key
auth_tokenNF)r0   r1   base_urltimeoutmax_retriesdefault_headersdefault_queryhttp_client	transportproxiesconnection_pool_limits_strict_response_validationzstr | httpx.URL | Nonez%Union[float, Timeout, None, NotGiven]intzMapping[str, str] | NonezMapping[str, object] | Nonezhttpx.Client | NonezTransport | NonezProxiesTypes | Nonezhttpx.Limits | NoneboolNone)r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   returnc              s   |dkrt jd}|| _|dkr.t jd}|| _|dkrHt jd}|dkrTd}t jt|||||	|
||||d t| _	t
| | _t
| | _t
| | _t| | _t| | _dS )a#  Construct a new synchronous anthropic client instance.

        This automatically infers the following arguments from their corresponding environment variables if they are not provided:
        - `api_key` from `ANTHROPIC_API_KEY`
        - `auth_token` from `ANTHROPIC_AUTH_TOKEN`
        NANTHROPIC_API_KEYANTHROPIC_AUTH_TOKENANTHROPIC_BASE_URLzhttps://api.anthropic.com)versionr2   r4   r3   r7   r8   r9   limitscustom_headerscustom_queryr;   )osenvirongetr0   r1   super__init__r   r   _default_stream_clsr	   ZCompletionsr)   ZMessagesr*   Betar+   r,   r-   r.   r/   )selfr0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   )	__class__ T/home/ankuromar296_gmail_com/.local/lib/python3.7/site-packages/anthropic/_client.pyrK   I   s8    $
zAnthropic.__init__r   )r?   c             C  s
   t ddS )Ncomma)array_format)r   )rN   rP   rP   rQ   qs   s    zAnthropic.qszdict[str, str]c             C  s   | j r| j S | jr| jS i S )N)_api_key_auth_bearer_auth)rN   rP   rP   rQ   auth_headers   s
    zAnthropic.auth_headersc             C  s   | j }|d kri S d|iS )Nz	X-Api-Key)r0   )rN   r0   rP   rP   rQ   rU      s    zAnthropic._api_key_authc             C  s    | j }|d kri S dd| iS )NAuthorizationzBearer )r1   )rN   r1   rP   rP   rQ   rV      s    zAnthropic._bearer_authzdict[str, str | Omit]c               s   t  jddd| jS )Nfalsez
2023-06-01)zX-Stainless-Asynczanthropic-version)rJ   r5   _custom_headers)rN   )rO   rP   rQ   r5      s    zAnthropic.default_headersr   )headersrE   r?   c             C  s\   | j r|drd S t|dtr(d S | jr<|dr<d S t|dtrPd S tdd S )Nz	X-Api-KeyrX   z"Could not resolve authentication method. Expected either api_key or auth_token to be set. Or for one of the `X-Api-Key` or `Authorization` headers to be explicitly omitted")r0   rI   
isinstancer   r1   	TypeError)rN   r[   rE   rP   rP   rQ   _validate_headers   s    zAnthropic._validate_headers)r0   r1   r2   r3   r7   r:   r4   r5   set_default_headersr6   set_default_query_extra_kwargsz!float | Timeout | None | NotGivenzint | NotGivenzMapping[str, Any]r   )r0   r1   r2   r3   r7   r:   r4   r5   r_   r6   r`   ra   r?   c            C  s   |dk	r|	dk	rt d|
dk	r0|dk	r0t d| j}|dk	rH||}n|	dk	rT|	}| j}|
dk	rl||
}n|dk	rx|}|dk	r|dk	rt dt| jtst dd}n | jtk	r| j}nd}|p| j}| jf |p| j	|p| j
|p| jt|tr| jn|||t|r|n| j||d	|S )z~
        Create a new client instance re-using the same options given to the current client with optional overriding.
        NzPThe `default_headers` and `set_default_headers` arguments are mutually exclusivezLThe `default_query` and `set_default_query` arguments are mutually exclusivezNThe 'http_client' argument is mutually exclusive with 'connection_pool_limits'zfA custom HTTP client has been set and is mutually exclusive with the 'connection_pool_limits' argument)	r0   r1   r2   r3   r7   r:   r4   r5   r6   )
ValueErrorrZ   _custom_queryr\   _clientr#   _limitsr    rO   r0   r1   r2   r   r3   r   r4   )rN   r0   r1   r2   r3   r7   r:   r4   r5   r_   r6   r`   ra   r[   paramsrP   rP   rQ   copy   sH    



zAnthropic.copystr)textr?   c             C  s   |   }||}t|jS )a)  Count the number of tokens in a given string.

        Note that this is only accurate for older models, e.g. `claude-2.1`. For newer
        models this can only be used as a _very_ rough estimate, instead you should rely
        on the `usage` property in the response for exact counts.
        )get_tokenizerencodelenids)rN   ri   	tokenizerencoded_textrP   rP   rQ   count_tokens  s    
zAnthropic.count_tokensr   c             C  s   t  S )N)r   )rN   rP   rP   rQ   rj     s    zAnthropic.get_tokenizerobjectzhttpx.Responser   )err_msgbodyresponser?   c            C  s   |j dkrtj|||dS |j dkr4tj|||dS |j dkrNtj|||dS |j dkrhtj|||dS |j dkrtj|||dS |j dkrtj|||dS |j dkrtj|||dS |j d	krtj	|||dS t
|||dS )
Ni  )rt   rs   i  i  i  i  i  i  i  )status_coder   BadRequestErrorAuthenticationErrorPermissionDeniedErrorNotFoundErrorConflictErrorUnprocessableEntityErrorRateLimitErrorInternalServerErrorr   )rN   rr   rs   rt   rP   rP   rQ   _make_status_error   s"    







zAnthropic._make_status_error)__name__
__module____qualname____annotations__r
   HUMAN_PROMPT	AI_PROMPTr   r   rK   propertyr   rT   rW   rU   rV   r5   r^   rg   with_optionsrp   rj   r~   __classcell__rP   rP   )rO   rQ   r%   :   s\   
	0+	,8c                  s  e Zd ZU ded< ded< ded< ded< d	ed
< ded< ded< ejZejZdddeedddddddddddddddddddddd fddZ	e
edd d!d"Ze
ed#d d$d%Ze
d#d d&d'Ze
d#d d(d)Ze
ed*d  fd+d,Zed-d-dd.d/d0Zdddeddeddddi d1dddd2ddd3ddddd4d5d6d7d8ZeZd9dd:d;d<Zd=d d>d?Zed9d@dAdBdCdDdEZ  ZS )Fr&   zresources.AsyncCompletionsr)   zresources.AsyncMessagesr*   zresources.AsyncBetar+   AsyncAnthropicWithRawResponser-   "AsyncAnthropicWithStreamedResponser/   z
str | Noner0   r1   NF)r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   zstr | httpx.URL | Nonez%Union[float, Timeout, None, NotGiven]r<   zMapping[str, str] | NonezMapping[str, object] | Nonezhttpx.AsyncClient | NonezAsyncTransport | NonezProxiesTypes | Nonezhttpx.Limits | Noner=   r>   )r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r?   c              s   |dkrt jd}|| _|dkr.t jd}|| _|dkrHt jd}|dkrTd}t jt|||||	|
||||d t| _	t
| | _t
| | _t
| | _t| | _t| | _dS )a  Construct a new async anthropic client instance.

        This automatically infers the following arguments from their corresponding environment variables if they are not provided:
        - `api_key` from `ANTHROPIC_API_KEY`
        - `auth_token` from `ANTHROPIC_AUTH_TOKEN`
        Nr@   rA   rB   zhttps://api.anthropic.com)rC   r2   r4   r3   r7   r8   r9   rD   rE   rF   r;   )rG   rH   rI   r0   r1   rJ   rK   r   r   rL   r	   ZAsyncCompletionsr)   ZAsyncMessagesr*   Z	AsyncBetar+   r   r-   r   r/   )rN   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   )rO   rP   rQ   rK   Q  s8    $
zAsyncAnthropic.__init__r   )r?   c             C  s
   t ddS )NrR   )rS   )r   )rN   rP   rP   rQ   rT     s    zAsyncAnthropic.qszdict[str, str]c             C  s   | j r| j S | jr| jS i S )N)rU   rV   )rN   rP   rP   rQ   rW     s
    zAsyncAnthropic.auth_headersc             C  s   | j }|d kri S d|iS )Nz	X-Api-Key)r0   )rN   r0   rP   rP   rQ   rU     s    zAsyncAnthropic._api_key_authc             C  s    | j }|d kri S dd| iS )NrX   zBearer )r1   )rN   r1   rP   rP   rQ   rV     s    zAsyncAnthropic._bearer_authzdict[str, str | Omit]c               s   t  jdt  dd| jS )Nzasync:z
2023-06-01)zX-Stainless-Asynczanthropic-version)rJ   r5   r   rZ   )rN   )rO   rP   rQ   r5     s    
zAsyncAnthropic.default_headersr   )r[   rE   r?   c             C  s\   | j r|drd S t|dtr(d S | jr<|dr<d S t|dtrPd S tdd S )Nz	X-Api-KeyrX   z"Could not resolve authentication method. Expected either api_key or auth_token to be set. Or for one of the `X-Api-Key` or `Authorization` headers to be explicitly omitted")r0   rI   r\   r   r1   r]   )rN   r[   rE   rP   rP   rQ   r^     s    z AsyncAnthropic._validate_headers)r0   r1   r2   r3   r7   r:   r4   r5   r_   r6   r`   ra   z!float | Timeout | None | NotGivenzint | NotGivenzMapping[str, Any]r   )r0   r1   r2   r3   r7   r:   r4   r5   r_   r6   r`   ra   r?   c            C  s   |dk	r|	dk	rt d|
dk	r0|dk	r0t d| j}|dk	rH||}n|	dk	rT|	}| j}|
dk	rl||
}n|dk	rx|}|dk	r|dk	rt dt| jtst dd}n | jtk	r| j}nd}|p| j}| jf |p| j	|p| j
|p| jt|tr| jn|||t|r|n| j||d	|S )z~
        Create a new client instance re-using the same options given to the current client with optional overriding.
        NzPThe `default_headers` and `set_default_headers` arguments are mutually exclusivezLThe `default_query` and `set_default_query` arguments are mutually exclusivezNThe 'http_client' argument is mutually exclusive with 'connection_pool_limits'zfA custom HTTP client has been set and is mutually exclusive with the 'connection_pool_limits' argument)	r0   r1   r2   r3   r7   r:   r4   r5   r6   )rb   rZ   rc   r\   rd   r$   re   r    rO   r0   r1   r2   r   r3   r   r4   )rN   r0   r1   r2   r3   r7   r:   r4   r5   r_   r6   r`   ra   r[   rf   rP   rP   rQ   rg     sH    



zAsyncAnthropic.copyrh   )ri   r?   c               s"   |   I dH }||}t|jS )a)  Count the number of tokens in a given string.

        Note that this is only accurate for older models, e.g. `claude-2.1`. For newer
        models this can only be used as a _very_ rough estimate, instead you should rely
        on the `usage` property in the response for exact counts.
        N)rj   rk   rl   rm   )rN   ri   rn   ro   rP   rP   rQ   rp     s    
zAsyncAnthropic.count_tokensr   c               s   t  I d H S )N)r   )rN   rP   rP   rQ   rj   %  s    zAsyncAnthropic.get_tokenizerrq   zhttpx.Responser   )rr   rs   rt   r?   c            C  s   |j dkrtj|||dS |j dkr4tj|||dS |j dkrNtj|||dS |j dkrhtj|||dS |j dkrtj|||dS |j dkrtj|||dS |j dkrtj|||dS |j d	krtj	|||dS t
|||dS )
Ni  )rt   rs   i  i  i  i  i  i  i  )ru   r   rv   rw   rx   ry   rz   r{   r|   r}   r   )rN   rr   rs   rt   rP   rP   rQ   r~   (  s"    







z!AsyncAnthropic._make_status_error)r   r   r   r   r
   r   r   r   r   rK   r   r   rT   rW   rU   rV   r5   r^   rg   r   rp   rj   r~   r   rP   rP   )rO   rQ   r&   B  s\   
	0+	,8c               @  s   e Zd ZdddddZdS )r,   r%   r>   )clientr?   c             C  s.   t |j| _t |j| _t |j| _d S )N)r	   ZCompletionsWithRawResponser)   ZMessagesWithRawResponser*   ZBetaWithRawResponser+   )rN   r   rP   rP   rQ   rK   K  s    z!AnthropicWithRawResponse.__init__N)r   r   r   rK   rP   rP   rP   rQ   r,   J  s   r,   c               @  s   e Zd ZdddddZdS )r   r&   r>   )r   r?   c             C  s.   t |j| _t |j| _t |j| _d S )N)r	   ZAsyncCompletionsWithRawResponser)   ZAsyncMessagesWithRawResponser*   ZAsyncBetaWithRawResponser+   )rN   r   rP   rP   rQ   rK   R  s    z&AsyncAnthropicWithRawResponse.__init__N)r   r   r   rK   rP   rP   rP   rQ   r   Q  s   r   c               @  s   e Zd ZdddddZdS )r.   r%   r>   )r   r?   c             C  s.   t |j| _t |j| _t |j| _d S )N)r	   Z CompletionsWithStreamingResponser)   ZMessagesWithStreamingResponser*   ZBetaWithStreamingResponser+   )rN   r   rP   rP   rQ   rK   Y  s    z&AnthropicWithStreamedResponse.__init__N)r   r   r   rK   rP   rP   rP   rQ   r.   X  s   r.   c               @  s   e Zd ZdddddZdS )r   r&   r>   )r   r?   c             C  s.   t |j| _t |j| _t |j| _d S )N)r	   Z%AsyncCompletionsWithStreamingResponser)   Z"AsyncMessagesWithStreamingResponser*   ZAsyncBetaWithStreamingResponser+   )rN   r   rP   rP   rQ   rK   `  s    z+AsyncAnthropicWithStreamedResponse.__init__N)r   r   r   rK   rP   rP   rP   rQ   r   _  s   r   )8
__future__r   rG   typingr   r   r   typing_extensionsr   r   Zhttpx r	   r
   r   Z_qsr   _typesr   r   r   r   r   r   r   r   r   _utilsr   r   _versionr   Z
_streamingr   r   r   Z_tokenizersr   r   r   Z_base_clientr   r    r!   r"   r#   r$   __all__r%   r&   r,   r   r.   r   r'   r(   rP   rP   rP   rQ   <module>   sD   , 
  
  
