
    dh                     (   S SK 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JrJr  S SKJrJrJrJrJrJr  S SKJrJrJr  S SKJr  S SKJr  S	\S
\4S jr SS.S\\   S\\\\\4         S\S
\\\4   4S jjr!\" SSSS9 " S S\\5      5       r"g)    )AnyAsyncIteratorDictIteratorListOptional)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseChatModelagenerate_from_streamgenerate_from_stream)	AIMessageAIMessageChunkBaseMessageChatMessageHumanMessageSystemMessage)ChatGenerationChatGenerationChunk
ChatResult)
ConfigDict)
BaseCoheremessagereturnc                     [        U [        5      (       d  [        U [        5      (       a  g[        U [        5      (       a  g[        U [        5      (       a  g[        SU  35      e)zGet the role of the message.

Args:
    message: The message.

Returns:
    The role of the message.

Raises:
    ValueError: If the message is of an unknown type.
UserChatbotSystemzGot unknown type )
isinstancer   r   r   r   
ValueErrorr   s    ^/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/chat_models/cohere.pyget_roler$      sS     ';'':g|+L+L	GY	'	'	G]	+	+,WI677    N)
connectorsmessagesr&   kwargsc          	         SU;  a  SO\[        US   5       VVs/ sHB  u  p4UR                  UR                  R                  S5      =(       d    S[	        U5       3S.PMD     snnnUR                  SS5        Uc  UOSnUc  Ub  SOSnU S   R                  U SS  Vs/ sH  n[        U5      UR                  S.PM     snUUUS	.UEn	U	R                  5        V
Vs0 sH  u  pUc  M
  X_M     snn
$ s  snnf s  snf s  snn
f )
zGet the request for the Cohere chat API.

Args:
    messages: The messages.
    connectors: The connectors.
    **kwargs: The keyword arguments.

Returns:
    The request for the Cohere chat API.
source_documentsNidzdoc-)snippetr+   AUTO)roler   )r   chat_history	documentsr&   prompt_truncation)		enumeratepage_contentmetadatagetstrpopcontentr$   items)r'   r&   r(   idocr1   maybe_connectorsr2   xreqkvs               r#   get_cohere_chat_requestrB   1   s/   $ V+ 	 $F+=$>?

 @ ++ll&&t,?$s1vh @
  JJ!4(%.%6zD
 ':+At 
 B<''AI#2
ANAXa[QYY7
 &.	 	C !YY[:[TQADAD[::9
&
 ;s   AC2!C8C=(C=z0.0.30z1.0zlangchain_cohere.ChatCohere)sinceremovalalternative_importc                      \ rS rSrSr\" SSS9r\S\4S j5       r	\S\
\\4   4S j5       r\S\
\\4   4S j5       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   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\4S jrSrg	)
ChatCoherec   a  `Cohere` chat large language models.

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

Example:
    .. code-block:: python

        from langchain_community.chat_models import ChatCohere
        from langchain_core.messages import HumanMessage

        chat = ChatCohere(max_tokens=256, temperature=0.75)

        messages = [HumanMessage(content="knock knock")]
        chat.invoke(messages)
T)populate_by_namearbitrary_types_allowedr   c                     g)zReturn type of chat model.zcohere-chat selfs    r#   	_llm_typeChatCohere._llm_type~   s     r%   c                     SU R                   0$ )z2Get the default parameters for calling Cohere API.temperature)rR   rM   s    r#   _default_paramsChatCohere._default_params   s     4++
 	
r%   c                 :    0 SU R                   0EU R                  E$ )zGet the identifying parameters.model)rV   rS   rM   s    r#   _identifying_paramsChatCohere._identifying_params   s$     A7DJJ'@4+?+?@@r%   Nr'   stoprun_managerr(   c              +     #    [        U40 U R                  DUD6n[        U R                  S5      (       a  U R                  R                  " S0 UD6nO U R                  R
                  " S0 UDSS0D6nU HK  nUR                  S:X  d  M  UR                  n[        [        US9S9n	U(       a  UR                  XS9  U	v   MM     g 7f	Nchat_streamstreamTztext-generationr9   r"   )chunkrL   )rB   rS   hasattrclientr]   chat
event_typetextr   r   on_llm_new_token
rN   r'   rY   rZ   r(   requestr^   datadeltar`   s
             r#   _streamChatCohere._stream   s      *(Ud6J6JUfU4;;..[[,,7w7F[[%%===FD"33		+N54QR000D s   BC
;Cc                  #    [        U40 U R                  DUD6n[        U R                  S5      (       a%  U R                  R                  " S0 UD6I S h  vN nO(U R                  R
                  " S0 UDSS0D6I S h  vN nU  S h  vN nUR                  S:X  d  M  UR                  n[        [        US9S9n	U(       a  UR                  XS9I S h  vN   U	7v   M\   N Nd N[ N
 g 7fr\   )rB   rS   ra   async_clientr]   rc   rd   re   r   r   rf   rg   s
             r#   _astreamChatCohere._astream   s      *(Ud6J6JUfU4$$m44,,88C7CCF,,11IGIDIIF  	$"33		+N54QR%66u6JJJ DI	
 K !s`   AC+C!(C+>C#?C+C)	C%
C)C+ 5C+C'C+#C+%C)'C+)C+responsec                 v    UR                   UR                  UR                  UR                  UR                  S.$ )z1Get the generation info from cohere API response.r1   	citationssearch_resultssearch_queriestoken_countrs   )rN   rq   s     r#   _get_generation_infoChatCohere._get_generation_info   s;     "++!++&55&55#//
 	
r%   c                 \   U R                   (       a   U R                  " U4X#S.UD6n[        U5      $ [        U40 U R                  DUD6nU R
                  R                  " S0 UD6n[        UR                  S9nS n	[        US5      (       a  U R                  U5      n	[        [        XS9/S9$ N)rY   rZ   r_   r1   )r   generation_info)generationsrL   )	streamingrk   r   rB   rS   rb   rc   r   re   ra   rx   r   r   
rN   r'   rY   rZ   r(   stream_iterrh   rq   r   r|   s
             r#   	_generateChatCohere._generate   s     >>,,#@FK (44)(Ud6J6JUfU;;##.g.HMM28[))"77AOwP
 	
r%   c                 x  #    U R                   (       a(  U R                  " U4X#S.UD6n[        U5      I S h  vN $ [        U40 U R                  DUD6nU R
                  R                  " S0 UD6n[        UR                  S9nS n	[        US5      (       a  U R                  U5      n	[        [        XS9/S9$  N7fr{   )r~   ro   r   rB   rS   rb   rc   r   re   ra   rx   r   r   r   s
             r#   
_agenerateChatCohere._agenerate   s      >>--#@FK /{;;;)(Ud6J6JUfU;;##.g.HMM28[))"77AOwP
 	
 <s   5B:B8BB:re   c                 Z    [        U R                  R                  US9R                  5      $ )zCalculate number of tokens.)re   )lenrb   tokenizetokens)rN   re   s     r#   get_num_tokensChatCohere.get_num_tokens   s%    4;;''T'299::r%   rL   )NN)__name__
__module____qualname____firstlineno____doc__r   model_configpropertyr7   rO   r   r   rS   rW   r   r   r   r   r   r   rk   r
   r   ro   rx   r   r   r   intr   __static_attributes__rL   r%   r#   rG   rG   c   s   $  $L
 3   
c3h 
 
 AT#s(^ A A %):>	{# tCy! 67	
  
%	&2 %)?C	{# tCy! ;<	
  
*	+,
S 
T#s(^ 
 %):>	
{#
 tCy!
 67	

 
 

: %)?C	
{#
 tCy!
 ;<	

 
 

4;3 ;3 ;r%   rG   )#typingr   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   pydanticr   langchain_community.llms.coherer   r7   r$   rB   rG   rL   r%   r#   <module>r      s    E E 6 
  S R  68k 8c 82 26/;;/; d38n-./; 	/;
 
#s(^/;d 
E6SU;
 U;U;r%   