
    dhY1                        S SK r S SKrS SKJrJrJrJrJrJrJ	r	J
r
  S SKJr  S SKJrJr  S SKJr  S SKJr  S SKJr  S SKJr  S S	KJrJrJrJr  S S
KJrJr  S SK J!r!J"r"J#r#J$r$   " S S\5      r%\" SSSS9 " S S\\%5      5       r&g)    N)AnyAsyncIteratorCallableDictIteratorListMappingOptional)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseLanguageModel)LLM)GenerationChunk)PromptValue)check_package_versionget_from_dict_or_envget_pydantic_field_namespre_init)_build_model_kwargsconvert_to_secret_str)
ConfigDictField	SecretStrmodel_validatorc                   $   \ rS rSr% Sr\\S'   Sr\\S'   \" SSS9r	\
\S'    \" S	S
S9r\\S'    Sr\\   \S'    Sr\\   \S'    Sr\\   \S'    Sr\\S'    Sr\\   \S'    Sr\\S'    Sr\\
   \S'   Sr\\   \S'   Sr\\
   \S'   Sr\\
   \S'   Sr\\\
/\4      \S'   \" \S9r\\
\4   \S'   \ " SS9\!S\S\4S j5       5       r"\#S\S\4S  j5       r$\%S\&\
\4   4S! j5       r'\%S\&\
\4   4S" j5       r(S&S#\\)\
      S\)\
   4S$ jjr*S%r+g)'_AnthropicCommon!   Nclientasync_clientzclaude-2
model_name)defaultaliasmodel   
max_tokensmax_tokens_to_sampletemperaturetop_ktop_pF	streamingdefault_request_timeout   max_retriesanthropic_api_urlanthropic_api_keyHUMAN_PROMPT	AI_PROMPTcount_tokens)default_factorymodel_kwargsbefore)modevaluesreturnc                 2    [        U 5      n[        X5      nU$ N)r   r   )clsr8   all_required_field_namess      Z/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/anthropic.pybuild_extra_AnthropicCommon.build_extraE   s     $<C#@ $VF    c                    [        [        USS5      5      US'   [        USSSS9US'    SSKn[        S	S
S9  UR	                  US   US   R                  5       US   US   S9US'   UR                  US   US   R                  5       US   US   S9US'   UR                  US'   UR                  US'   US   R                  US'   U$ ! [         a    [        S5      ef = f)z?Validate that api key and python package exists in environment.r0   ANTHROPIC_API_KEYr/   ANTHROPIC_API_URLzhttps://api.anthropic.com)r"   r   N	anthropicz0.3)gte_versionr,   r.   )base_urlapi_keytimeoutr.   r   r    r1   r2   r3   z]Could not import anthropic python package. Please it install it with `pip install anthropic`.)r   r   rE   r   	Anthropicget_secret_valueAsyncAnthropicr1   r2   r3   ImportError)r<   r8   rE   s      r>   validate_environment%_AnthropicCommon.validate_environmentL   sB    '< )<>QR'
"# ';/	'
"#	!+5A(22 3423DDF89"=1	  3  F8 &/%=%= 3423DDF89"=1	 &> &F>" &/%;%;F>""+"5"5F;%+H%5%B%BF>"   	E 	s   BC Cc                     U R                   U R                  S.nU R                  b  U R                  US'   U R                  b  U R                  US'   U R                  b  U R                  US'   0 UEU R
                  E$ )z5Get the default parameters for calling Anthropic API.)r'   r$   r(   r)   r*   )r'   r$   r(   r)   r*   r5   )selfds     r>   _default_params _AnthropicCommon._default_paramsu   s}     %)$=$=ZZ
 '#//Am::!AgJ::!AgJ)!)t(())rA   c                 "    0 0 EU R                   E$ )zGet the identifying parameters.)rS   rQ   s    r>   _identifying_params$_AnthropicCommon._identifying_params   s     ."-,,--rA   stopc                     U R                   (       a  U R                  (       d  [        S5      eUc  / nUR                  U R                   /5        U$ )N-Please ensure the anthropic package is loaded)r1   r2   	NameErrorextend)rQ   rY   s     r>   _get_anthropic_stop$_AnthropicCommon._get_anthropic_stop   sC      KLL<D 	T&&'(rA    r;   ),__name__
__module____qualname____firstlineno__r   r   __annotations__r    r   r$   strr'   intr(   r
   floatr)   r*   r+   boolr,   r.   r/   r0   r   r1   r2   r3   r   dictr5   r   r   classmethodr?   r   rN   propertyr	   rS   rW   r   r^   __static_attributes__r`   rA   r>   r   r   !   s   FCL#z>E3> %c F#FA#'K%'QE8C=@!E8E?!DIt(/3Xe_3SKV'+x}+-1x	*1"&L(3-&#Ix}#37L(8SE3J/07#(#>L$sCx.>(# #   $
 &$ &4 & &P *c!2 * * .WS#X%6 . .
c(; 
tCy 
 
rA   r   z0.0.28z1.0z langchain_anthropic.AnthropicLLM)sinceremovalalternative_importc                   |   \ rS rSrSr\" SSS9r\S\S\4S j5       r	\
S\4S j5       rS	\S\4S
 jr  SS	\S\\\      S\\   S\S\4
S jjrS	\S\4S jr  SS	\S\\\      S\\   S\S\4
S jjr  SS	\S\\\      S\\   S\S\\   4
S jjr  SS	\S\\\      S\\   S\S\\   4
S jjrS\S\4S jrSrg)rJ      a  Anthropic large language models.

To use, you should have the ``anthropic`` python package installed, and the
environment variable ``ANTHROPIC_API_KEY`` set with your API key, or pass
it as a named parameter to the constructor.

Example:
    .. code-block:: python

        import anthropic
        from langchain_community.llms import Anthropic

        model = Anthropic(model="<model_name>", anthropic_api_key="my-api-key")

        # Simplest invocation, automatically wrapped with HUMAN_PROMPT
        # and AI_PROMPT.
        response = model.invoke("What are the biggest risks facing humanity?")

        # Or if you want to use the chat mode, build a few-shot-prompt, or
        # put words in the Assistant's mouth, use HUMAN_PROMPT and AI_PROMPT:
        raw_prompt = "What are the biggest risks facing humanity?"
        prompt = f"{anthropic.HUMAN_PROMPT} {prompt}{anthropic.AI_PROMPT}"
        response = model.invoke(prompt)
T)populate_by_namearbitrary_types_allowedr8   r9   c                 2    [         R                  " S5        U$ )z,Raise warning that this class is deprecated.zpThis Anthropic LLM is deprecated. Please use `from langchain_community.chat_models import ChatAnthropic` instead)warningswarn)r<   r8   s     r>   raise_warningAnthropic.raise_warning   s     		

 rA   c                     g)zReturn type of llm.zanthropic-llmr`   rV   s    r>   	_llm_typeAnthropic._llm_type   s     rA   promptc                 4   U R                   (       a  U R                  (       d  [        S5      eUR                  U R                   5      (       a  U$ [        R
                  " SU R                   U5      u  p#US:X  a  U$ U R                    SU U R                   S3$ )Nr[   z
^\n*Human:    z Sure, here you go:
)r1   r2   r\   
startswithresubn)rQ   r}   corrected_promptn_subss       r>   _wrap_promptAnthropic._wrap_prompt   s      KLLT..//M $&77=$:K:KV#T Q;## ##$AfXdnn-==RSSrA   NrY   run_managerkwargsc                 J   U R                   (       a.  SnU R                  " SXUS.UD6 H  nXVR                  -  nM     U$ U R                  U5      n0 U R                  EUEnU R
                  R                  R                  " SU R                  U5      US.UD6nUR                  $ )a  Call out to Anthropic's completion endpoint.

Args:
    prompt: The prompt to pass into the model.
    stop: Optional list of stop words to use when generating.

Returns:
    The string generated by the model.

Example:
    .. code-block:: python

        prompt = "What are the biggest risks facing humanity?"
        prompt = f"\n\nHuman: {prompt}\n\nAssistant:"
        response = model.invoke(prompt)

 r}   rY   r   r}   stop_sequencesr`   )
r+   _streamtextr^   rS   r   completionscreater   
completion	rQ   r}   rY   r   r   r   chunkparamsresponses	            r>   _callAnthropic._call   s    0 >>J kEK jj(
 ''-3D((3F3;;**11 
$$V,
 

 """rA   c                 @    U R                  UR                  5       5      $ r;   )r   	to_string)rQ   r}   s     r>   convert_promptAnthropic.convert_prompt  s      !1!1!344rA   c                 t  #    U R                   (       a0  SnU R                  " SXUS.UD6  Sh  vN nXVR                  -  nM  U R                  U5      n0 U R                  EUEnU R
                  R                  R                  " SU R                  U5      US.UD6I Sh  vN nUR                  $  N
 U$  N7f)z;Call out to Anthropic's completion endpoint asynchronously.r   r   Nr   r`   )
r+   _astreamr   r^   rS   r    r   r   r   r   r   s	            r>   _acallAnthropic._acall  s      >>J#}}  k EK  )e jj(
 ''-3D((3F3**66== 
$$V,
 
 

 """)   

s2   )B8B3B1B3A-B8 B6!B81B33B8c              +   >  #    U R                  U5      n0 U R                  EUEnU R                  R                  R                  " SU R                  U5      USS.UD6 H;  n[        UR                  S9nU(       a  UR                  UR                  US9  Uv   M=     g7f)a  Call Anthropic completion_stream and return the resulting generator.

Args:
    prompt: The prompt to pass into the model.
    stop: Optional list of stop words to use when generating.
Returns:
    A generator representing the stream of tokens from Anthropic.
Example:
    .. code-block:: python

        prompt = "Write a poem about a stream."
        prompt = f"\n\nHuman: {prompt}\n\nAssistant:"
        generator = anthropic.stream(prompt)
        for token in generator:
            yield token
Tr}   r   streamr   r   Nr`   )
r^   rS   r   r   r   r   r   r   on_llm_new_tokenr   rQ   r}   rY   r   r   r   tokenr   s           r>   r   Anthropic._stream  s     . ''-3D((3F3[[,,33 
$$V,T$
RX
E $)9)9:E,,UZZu,EK
s   BBc                v  #    U R                  U5      n0 U R                  EUEnU R                  R                  R                  " SU R                  U5      USS.UD6I Sh  vN   Sh  vN n[        UR                  S9nU(       a"  UR                  UR                  US9I Sh  vN   U7v   ML   NP NI N
 g7f)a  Call Anthropic completion_stream and return the resulting generator.

Args:
    prompt: The prompt to pass into the model.
    stop: Optional list of stop words to use when generating.
Returns:
    A generator representing the stream of tokens from Anthropic.
Example:
    .. code-block:: python
        prompt = "Write a poem about a stream."
        prompt = f"\n\nHuman: {prompt}\n\nAssistant:"
        generator = anthropic.stream(prompt)
        for token in generator:
            yield token
Tr   Nr   r   r`   )
r^   rS   r    r   r   r   r   r   r   r   r   s           r>   r   Anthropic._astream@  s     , ''-3D((3F3!%!2!2!>!>!E!E "
$$V,"
 	"
 
 
 		% $)9)9:E!225::U2KKKK
 		 L
sH   AB9 B1!B9%B7)B3*B7-8B9%B5&B93B75B97B9r   c                 \    U R                   (       d  [        S5      eU R                  U5      $ )zCalculate number of tokens.r[   )r3   r\   )rQ   r   s     r>   get_num_tokensAnthropic.get_num_tokensd  s(      KLL  &&rA   r`   )NN)ra   rb   rc   rd   __doc__r   model_configr   r   rx   rl   rf   r{   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   rg   r   rm   r`   rA   r>   rJ   rJ      s   2  $L
 4 D   3  T3 T3 T$ %):>	'#'# tCy!'# 67	'#
 '# 
'#R5[ 5S 5 %)?C	## tCy!# ;<	#
 # 
#: %):>	   tCy!  67	 
   
/	" J %)?C	"" tCy!" ;<	"
 " 
	'"H'3 '3 'rA   rJ   )'r   rv   typingr   r   r   r   r   r   r	   r
   langchain_core._api.deprecationr   langchain_core.callbacksr   r   langchain_core.language_modelsr   #langchain_core.language_models.llmsr   langchain_core.outputsr   langchain_core.prompt_valuesr   langchain_core.utilsr   r   r   r   langchain_core.utils.utilsr   r   pydanticr   r   r   r   r   rJ   r`   rA   r>   <module>r      s    	 	 	 	 7 = 3 2 4  R B Br( rj 
9
M'% M'
M'rA   