
    -:jS                    d   d dl mZ d dlZd dlZd dlmZmZ d dlmZ d dl	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mZmZmZmZmZ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%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+ g dZ, G d de*      Z- G d de+      Z. G d d      Z/ G d d      Z0e-Z1e.Z2y)    )annotationsN)UnionMapping)override)	Tokenizer   )	resources
_constants_exceptionsQuerystring)		NOT_GIVENOmitHeadersTimeoutNotGiven	TransportProxiesTypesAsyncTransportRequestOptions)is_given)__version__)Stream)AsyncStream)APIStatusError)sync_get_tokenizerasync_get_tokenizer)DEFAULT_LIMITSDEFAULT_MAX_RETRIESSyncAPIClientAsyncAPIClient)	r   r   r   r   r	   	AnthropicAsyncAnthropicClientAsyncClientc                      e Zd ZU 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 fdZ	e
edd              Ze
edd              Ze
dd       Ze
dd       Ze
ed fd              Zedd       Zdddeddeddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZeZddZ	 	 	 	 ddZd dZe	 	 	 	 	 	 	 	 d!d       Z xZS )"r"   zresources.CompletionscompletionsAnthropicWithRawResponsewith_raw_response
str | Noneapi_key
auth_tokenNFr+   r,   base_urltimeoutmax_retriesdefault_headersdefault_queryhttp_client	transportproxiesconnection_pool_limits_strict_response_validationc                  |t         j                  j                  d      }|| _        |t         j                  j                  d      }|| _        |t         j                  j                  d      }|d}t
        |   t        |||||	|
||||       t        | _	        t        j                  |       | _        t        |       | _        y)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_URLhttps://api.anthropic.comversionr.   r0   r/   r3   r4   r5   limitscustom_headerscustom_queryr7   )osenvirongetr+   r,   super__init__r   r   _default_stream_clsr	   Completionsr'   r(   r)   selfr+   r,   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   	__class__s                S/var/www/html/tokenscope/api/venv/lib/python3.12/site-packages/anthropic/_client.pyrF   zAnthropic.__init__@   s    D ?jjnn%89G(>?J$zz~~&:;H2H##)*&(C 	 	
 $* $006!9$!?    c                    t        d      S Ncomma)array_formatr   rJ   s    rL   qszAnthropic.qs        00rM   c                f    | j                   r| j                   S | j                  r| j                  S i S N_api_key_auth_bearer_authrR   s    rL   auth_headerszAnthropic.auth_headers   3     %%%$$$	rM   c                *    | j                   }|i S d|iS N	X-Api-Keyr+   rJ   r+   s     rL   rX   zAnthropic._api_key_auth        ,,?IW%%rM   c                0    | j                   }|i S dd| iS NAuthorizationzBearer r,   rJ   r,   s     rL   rY   zAnthropic._bearer_auth   (    __
I7:,!788rM   c                <    i t         |   ddi| j                  S Nzanthropic-versionz
2023-06-01rE   r1   _custom_headersrJ   rK   s    rL   r1   zAnthropic.default_headers   4    
g%

 ""
 	
rM   c                   | j                   r|j                  d      ry t        |j                  d      t              ry | j                  r|j                  d      ry t        |j                  d      t              ry t        d      Nr^   rd   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"r+   rD   
isinstancer   r,   	TypeErrorrJ   headersr@   s      rL   _validate_headerszAnthropic._validate_headers   o    <<GKK4n((5t<??w{{?;n((94@ |
 	
rM   r+   r,   r.   r/   r3   r6   r0   r1   set_default_headersr2   set_default_queryc               V   ||	t        d      |
|t        d      | j                  }|i ||}n|	|	}| j                  }|
i ||
}n||}|'|t        d      | j                  rt        d      d}n1| j                  t
        ur| j                  }nd}|xs | j                  }| j                  |xs | j                  |xs | j                  |xs t        | j                        t        |t              r| j                  n|||t        |      r|n| j                   ||	      S a  
        Create a new client instance re-using the same options given to the current client with optional overriding.

        It should be noted that this does not share the underlying httpx client class which may lead
        to performance issues.
        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)	r+   r,   r.   r/   r3   r6   r0   r1   r2   
ValueErrorrk   _custom_query_has_custom_http_client_limitsr   _clientrK   r+   r,   strr.   rq   r   r/   r   r0   rJ   r+   r,   r.   r/   r3   r6   r0   r1   rx   r2   ry   rt   paramss                 rL   copyzAnthropic.copy   [   * &+>+Jopp$):)Fkll&&&44O4G ,)G##$00-0F*&F!-& !qrr++ |  K||>1)-&)-&%5K~~+t||!4T__3T]]!3$.w$ADLLw##9'/'<$BRBR#   

 
	
rM   c                p    t        | d      rt        | d      sy | j                  ry | j                          y Nr   close)hasattrr   r   rR   s    rL   __del__zAnthropic.__del__   s.    t67wtW?U''

rM   c                n    | j                         }|j                  |      }t        |j                        S ),Count the number of tokens in a given stringget_tokenizerencodelenidsrJ   text	tokenizerencoded_texts       rL   count_tokenszAnthropic.count_tokens
  s3     &&(	 ''-<##$$rM   c                    t               S rV   )r   rR   s    rL   r   zAnthropic.get_tokenizer  s    !##rM   c                  |j                   dk(  rt        j                  |||      S |j                   dk(  rt        j                  |||      S |j                   dk(  rt        j                  |||      S |j                   dk(  rt        j
                  |||      S |j                   dk(  rt        j                  |||      S |j                   dk(  rt        j                  |||      S |j                   dk(  rt        j                  |||      S |j                   d	k\  rt        j                  |||      S t        |||      S 
Ni  )responsebodyi  i  i  i  i  i  i  status_coder   BadRequestErrorAuthenticationErrorPermissionDeniedErrorNotFoundErrorConflictErrorUnprocessableEntityErrorRateLimitErrorInternalServerErrorr   rJ   err_msgr   r   s       rL   _make_status_errorzAnthropic._make_status_error  0    3&..wPTUU3&227XTXYY3&44WxVZ[[3&,,WxdSS3&,,WxdSS3&77(Y]^^3&--gtTT3&227XTXYYgtDDrM   )r+   r*   r,   r*   r.   str | httpx.URL | Noner/   %Union[float, Timeout, None, NotGiven]r0   intr1   Mapping[str, str] | Noner2   Mapping[str, object] | Noner3   httpx.Client | Noner4   zTransport | Noner5   ProxiesTypes | Noner6   httpx.Limits | Noner7   boolreturnNoner   r   r   zdict[str, str]r   zdict[str, str | Omit]rt   r   r@   r   r   r   )r+   r*   r,   r*   r.   r   r/   !float | Timeout | None | NotGivenr3   r   r6   r   r0   int | NotGivenr1   r   rx   r   r2   r   ry   r   r   r"   r   r   r   r   r   r   r   r   r   r   r   objectr   zhttpx.Responser   r   __name__
__module____qualname____annotations__r
   HUMAN_PROMPT	AI_PROMPTr   r   rF   propertyr   rS   rZ   rX   rY   r1   ru   r   with_optionsr   r   r   r   __classcell__rK   s   @rL   r"   r"   4   s   &&//  **L$$I
 #!%+/9B.4859+/&*'+6: -25@@ @@ 	@@
 )@@ 7@@ @@ 2@@ 3@@ )@@ $@@ %@@" !4#@@4 &*5@@6 
7@@D 1  1    & & 9 9 
  
 
 
$ #!%+/5>+/6:&/488<599=C
 C
 	C

 )C
 3C
 )C
 !4C
 $C
 2C
 6C
 3C
 7C
 
C
N L%% 
%$ EE 	E
 !E 
E ErM   r"   c                      e Zd ZU 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 fdZ	e
edd              Ze
edd              Ze
dd       Ze
dd       Ze
ed fd              Zedd       Zdddeddeddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZeZddZ	 	 	 	 ddZd dZe	 	 	 	 	 	 	 	 d!d       Z xZS )"r#   zresources.AsyncCompletionsr'   AsyncAnthropicWithRawResponser)   r*   r+   r,   NFr-   c                  |t         j                  j                  d      }|| _        |t         j                  j                  d      }|| _        |t         j                  j                  d      }|d}t
        |   t        |||||	|
||||       t        | _	        t        j                  |       | _        t        |       | _        y)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`
        Nr9   r:   r;   r<   r=   )rB   rC   rD   r+   r,   rE   rF   r   r   rG   r	   AsyncCompletionsr'   r   r)   rI   s                rL   rF   zAsyncAnthropic.__init__E  s    D ?jjnn%89G(>?J$zz~~&:;H2H##)*&(C 	 	
 $/ $55d;!>t!DrM   c                    t        d      S rO   r   rR   s    rL   rS   zAsyncAnthropic.qs  rT   rM   c                f    | j                   r| j                   S | j                  r| j                  S i S rV   rW   rR   s    rL   rZ   zAsyncAnthropic.auth_headers  r[   rM   c                *    | j                   }|i S d|iS r]   r_   r`   s     rL   rX   zAsyncAnthropic._api_key_auth  ra   rM   c                0    | j                   }|i S dd| iS rc   re   rf   s     rL   rY   zAsyncAnthropic._bearer_auth  rg   rM   c                <    i t         |   ddi| j                  S ri   rj   rl   s    rL   r1   zAsyncAnthropic.default_headers  rm   rM   c                   | j                   r|j                  d      ry t        |j                  d      t              ry | j                  r|j                  d      ry t        |j                  d      t              ry t        d      ro   rp   rs   s      rL   ru   z AsyncAnthropic._validate_headers  rv   rM   rw   c               V   ||	t        d      |
|t        d      | j                  }|i ||}n|	|	}| j                  }|
i ||
}n||}|'|t        d      | j                  rt        d      d}n1| j                  t
        ur| j                  }nd}|xs | j                  }| j                  |xs | j                  |xs | j                  |xs t        | j                        t        |t              r| j                  n|||t        |      r|n| j                   ||	      S r{   r|   r   s                 rL   r   zAsyncAnthropic.copy  r   rM   c                    t        | d      rt        | d      sy | j                  ry 	 t        j                         j	                  | j                                y # t        $ r Y y w xY wr   )r   r   asyncioget_running_loopcreate_taskr   	ExceptionrR   s    rL   r   zAsyncAnthropic.__del__  sY    t67wtW?U''	$$&224::<@ 		s   1A 	A&%A&c                   K   | j                          d{   }|j                  |      }t        |j                        S 7 *w)r   Nr   r   s       rL   r   zAsyncAnthropic.count_tokens  s?      ,,..	 ''-<##$$ /s   AA+Ac                2   K   t                d {   S 7 wrV   )r   rR   s    rL   r   zAsyncAnthropic.get_tokenizer  s     (****s   c                  |j                   dk(  rt        j                  |||      S |j                   dk(  rt        j                  |||      S |j                   dk(  rt        j                  |||      S |j                   dk(  rt        j
                  |||      S |j                   dk(  rt        j                  |||      S |j                   dk(  rt        j                  |||      S |j                   dk(  rt        j                  |||      S |j                   d	k\  rt        j                  |||      S t        |||      S r   r   r   s       rL   r   z!AsyncAnthropic._make_status_error  r   rM   )r+   r*   r,   r*   r.   r   r/   r   r0   r   r1   r   r2   r   r3   httpx.AsyncClient | Noner4   zAsyncTransport | Noner5   r   r6   r   r7   r   r   r   r   r   r   r   )r+   r*   r,   r*   r.   r   r/   r   r3   r   r6   r   r0   r   r1   r   rx   r   r2   r   ry   r   r   r#   r   r   r   r   r   r   s   @rL   r#   r#   9  s   ++44  **L$$I
 #!%+/9B.485904+/'+6: -25@E @E 	@E
 )@E 7@E @E 2@E 3@E .@E )@E %@E" !4#@E4 &*5@E6 
7@ED 1  1    & & 9 9 
  
 
 
$ #!%+/5>046:&/488<599=C
 C
 	C

 )C
 3C
 .C
 !4C
 $C
 2C
 6C
 3C
 7C
 
C
N L%% 
%+ EE 	E
 !E 
E ErM   r#   c                      e Zd ZddZy)r(   c                L    t        j                  |j                        | _        y rV   )r	   CompletionsWithRawResponser'   rJ   clients     rL   rF   z!AnthropicWithRawResponse.__init__B  s    $??@R@RSrM   N)r   r"   r   r   r   r   r   rF    rM   rL   r(   r(   A  s    TrM   r(   c                      e Zd ZddZy)r   c                L    t        j                  |j                        | _        y rV   )r	   AsyncCompletionsWithRawResponser'   r   s     rL   rF   z&AsyncAnthropicWithRawResponse.__init__G  s    $DDVEWEWXrM   N)r   r#   r   r   r   r   rM   rL   r   r   F  s    YrM   r   )3
__future__r   rB   r   typingr   r   typing_extensionsr   httpx
tokenizersr    r	   r
   r   _qsr   _typesr   r   r   r   r   r   r   r   r   _utilsr   _versionr   
_streamingr   r   r   _tokenizersr   r   _base_clientr   r   r    r!   __all__r"   r#   r(   r   r$   r%   r   rM   rL   <module>r      s    # 	  ! &    0 0 
 
 
  ! ( 2 ' @ 
BE BEJEE^ EEPT T
Y Y
 
rM   