
    Ng              	       <   d dl mZmZmZmZmZmZmZ d dlm	Z	 d dl
mZ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lmZmZmZ d dlmZ d dlmZ d d	l m!Z! d
ede"de"de"fdZ#ddddee         de"de"de"fdZ$ e	ddd           G d dee!                      Z%dS )    )AnyAsyncIteratorDictIteratorListOptionalcast)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseChatModelagenerate_from_streamgenerate_from_stream)	AIMessageAIMessageChunkBaseMessageChatMessageHumanMessageSystemMessage)ChatGenerationChatGenerationChunk
ChatResult)PromptValue)
ConfigDict)_AnthropicCommonmessagehuman_prompt	ai_promptreturnc                 l   t          t          | j                  }t          | t                    r d| j                                         d| }ndt          | t                    r| d| }nGt          | t                    r| d| }n*t          | t                    r|}nt          d|            |S )Nz

z:  zGot unknown type )r	   strcontent
isinstancer   role
capitalizer   r   r   
ValueError)r   r   r   r#   message_texts        e/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/chat_models/anthropic.py_convert_one_message_to_textr*      s    
 3((G';'' 	8Dgl5577DD7DD	G\	*	* 8&2222	GY	'	' 8#//g//	G]	+	+ 86W66777    z

Human:z

Assistant:)r   r   messagesc                   |                                  } t          | d         t                    s#|                     t          d                     d                    fd| D                       }|                                S )a  Format a list of messages into a full prompt for the Anthropic model
    Args:
        messages (List[BaseMessage]): List of BaseMessage to combine.
        human_prompt (str, optional): Human prompt tag. Defaults to "

Human:".
        ai_prompt (str, optional): AI prompt tag. Defaults to "

Assistant:".
    Returns:
        str: Combined string with necessary human_prompt and ai_prompt tags.
     r#   c              3   :   K   | ]}t          |          V  d S N)r*   ).0r   r   r   s     r)   	<genexpr>z7convert_messages_to_prompt_anthropic.<locals>.<genexpr>B   sC         	%WlIFF     r+   )copyr$   r   appendjoinrstrip)r,   r   r   texts    `` r)   $convert_messages_to_prompt_anthropicr:   /   s     }}HhrlI.. /	"---...77         D ;;==r+   z0.0.28z1.0z!langchain_anthropic.ChatAnthropic)sinceremovalalternative_importc                   T   e Zd ZdZ edd          Zedeeef         fd            Z	edefd            Z
edefd            Zedee         fd            Zd	ee         defd
ZdedefdZ	 	 dd	ee         deee                  dee         dedee         f
dZ	 	 dd	ee         deee                  dee         dedee         f
dZ	 	 dd	ee         deee                  dee         dedef
dZ	 	 dd	ee         deee                  dee         dedef
dZdede fdZ!dS )ChatAnthropica  `Anthropic` chat 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.chat_models import ChatAnthropic
            model = ChatAnthropic(model="<model_name>", anthropic_api_key="my-api-key")
    T)populate_by_namearbitrary_types_allowedr   c                 
    ddiS )Nanthropic_api_keyANTHROPIC_API_KEY selfs    r)   
lc_secretszChatAnthropic.lc_secretsd   s    #%899r+   c                     dS )zReturn type of chat model.zanthropic-chatrE   rF   s    r)   	_llm_typezChatAnthropic._llm_typeh   s
      r+   c                     dS )z9Return whether this model can be serialized by Langchain.TrE   clss    r)   is_lc_serializablez ChatAnthropic.is_lc_serializablem   s	     tr+   c                 
    g dS )z*Get the namespace of the langchain object.)	langchainchat_models	anthropicrE   rL   s    r)   get_lc_namespacezChatAnthropic.get_lc_namespacer   s     9888r+   r,   c                 f    i }| j         r
| j         |d<   | j        r
| j        |d<   t          dd|i|S )a  Format a list of messages into a full prompt for the Anthropic model
        Args:
            messages (List[BaseMessage]): List of BaseMessage to combine.
        Returns:
            str: Combined string with necessary HUMAN_PROMPT and AI_PROMPT tags.
        r   r   r,   rE   )HUMAN_PROMPT	AI_PROMPTr:   )rG   r,   prompt_paramss      r)   _convert_messages_to_promptz)ChatAnthropic._convert_messages_to_promptw   sP      	>,0,=M.)> 	8)-M+&3WWXWWWWr+   promptc                 P    |                      |                                          S r2   )rX   to_messages)rG   rY   s     r)   convert_promptzChatAnthropic.convert_prompt   s"    //0B0B0D0DEEEr+   Nstoprun_managerkwargsc              +      K   |                      |          }d|i| j        |}|r||d<    | j        j        j        di |ddi}|D ]D}|j        }	t          t          |	                    }
|r|                    |	|
           |
V  Ed S 	NrY   stop_sequencesstreamTr0   r   )chunkrE   )	rX   _default_paramsclientcompletionscreate
completionr   r   on_llm_new_tokenrG   r,   r]   r^   r_   rY   paramsstream_respdatadeltare   s              r)   _streamzChatAnthropic._stream   s       11(;;"*F!Ud6J!Uf!U 	,'+F#$4dk-4KKvKKdKKK 	 	DOE'u0M0M0MNNNE A,,U%,@@@KKKK	 	r+   c                F  K   |                      |          }d|i| j        |}|r||d<    | j        j        j        di |ddi d {V }|2 3 d {V }|j        }	t          t          |	                    }
|r|                    |	|
           d {V  |
W V  Q6 d S ra   )	rX   rf   async_clientrh   ri   rj   r   r   rk   rl   s              r)   _astreamzChatAnthropic._astream   s      11(;;"*F!Ud6J!Uf!U 	,'+F#$@D-9@WW6WWRVWWWWWWWWW% 	 	 	 	 	 	 	$OE'u0M0M0MNNNE G!2252FFFFFFFFFKKKKK &++s   B c                 <   | j         r  | j        |f||d|}t          |          S |                     |          }d|i| j        |}|r||d<    | j        j        j        di |}|j        }	t          |	          }
t          t          |
          g          S N)r]   r^   rY   rb   r0   rd   )generationsrE   )	streamingrq   r   rX   rf   rg   rh   ri   rj   r   r   r   rG   r,   r]   r^   r_   stream_iterrY   rm   responserj   r   s              r)   	_generatezChatAnthropic._generate   s     > 	5&$,# @F K (44411
 
 f"
""
 "

  	,'+F#$14;*1;;F;;(
J///~g'F'F'F&GHHHHr+   c                 X  K   | j         r& | j        |f||d|}t          |           d {V S |                     |          }d|i| j        |}|r||d<    | j        j        j        di | d {V }|j        }	t          |	          }
t          t          |
          g          S rv   )rx   rt   r   rX   rf   rs   rh   ri   rj   r   r   r   ry   s              r)   
_ageneratezChatAnthropic._agenerate   s      > 	<'$-# @F K /{;;;;;;;;;11
 
 f"
""
 "

  	,'+F#$=*6=GGGGGGGGGG(
J///~g'F'F'F&GHHHHr+   r9   c                 X    | j         st          d          |                      |          S )zCalculate number of tokens.z-Please ensure the anthropic package is loaded)count_tokens	NameError)rG   r9   s     r)   get_num_tokenszChatAnthropic.get_num_tokens   s1      	MKLLL  &&&r+   )NN)"__name__
__module____qualname____doc__r   model_configpropertyr   r"   rH   rJ   classmethodboolrN   r   rS   r   rX   r   r\   r   r   r   r   r   rq   r   r   rt   r   r|   r~   intr   rE   r+   r)   r?   r?   K   s         : $  L
 :DcN : : : X:  3       X  4    [ 9c 9 9 9 [9XD4E X# X X X XF[ FS F F F F %):>	 {# tCy! 67	
  
%	&   . %)?C	 {# tCy! ;<	
  
*	+   . %):>	I I{#I tCy!I 67	I
 I 
I I I I< %)?C	I I{#I tCy!I ;<	I
 I 
I I I I6'3 '3 ' ' ' ' ' 'r+   r?   N)&typingr   r   r   r   r   r   r	   langchain_core._api.deprecationr
   langchain_core.callbacksr   r   *langchain_core.language_models.chat_modelsr   r   r   langchain_core.messagesr   r   r   r   r   r   langchain_core.outputsr   r   r   langchain_core.prompt_valuesr   pydanticr   "langchain_community.llms.anthropicr   r"   r*   r:   r?   rE   r+   r)   <module>r      sA   K K K K K K K K K K K K K K K K K K 6 6 6 6 6 6                
                S R R R R R R R R R 4 4 4 4 4 4       ? ? ? ? ? ?  		   , %%	  ;  	
 	   8 
:  
Z' Z' Z' Z' Z'M#3 Z' Z' 
Z' Z' Z'r+   