
    7|h                     (   d dl 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	ed
efdZ dddee   deeeeef         ded
eeef   fdZ! eddd       G d dee             Z"y)    )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                     t        | t              st        | t              ryt        | t              ryt        | t              ryt        d|        )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    e/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/chat_models/cohere.pyget_roler$      sG     ';':g|+L	GY	'	G]	+,WI677    N)
connectorsmessagesr&   kwargsc          	         d|vrdnVt        |d         D cg c]=  \  }}|j                  |j                  j                  d      xs dt	        |       d? c}}}|j                  dd       ||nd}||dnd}| d   j                  | dd D cg c]  }t        |      |j                  d c}|||d	|}	|	j                         D 
ci c]  \  }
}|	|
| c}}
S c c}}w c c}w c c}}
w )
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+=$>?

 3 ++ll&&t,?$s1vh
  JJ!4(%.%6zD
 ':+At 
 B<''AI#2
<=Xa[QYY7
 &.	 	C !YY[:TQAMAqD::9
&
 ;s   AC#C)
C.C.z0.0.30z1.0zlangchain_cohere.ChatCohere)sinceremovalalternative_importc                      e Zd ZdZ edd      Zedefd       Zede	ee
f   fd       Zede	ee
f   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e
de	ee
f   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y)
ChatCohereaA  `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                      y)zReturn type of chat model.zcohere-chat selfs    r#   	_llm_typezChatCohere._llm_type~   s     r%   c                     d| j                   iS )z2Get the default parameters for calling Cohere API.temperature)rP   rL   s    r#   _default_paramszChatCohere._default_params   s     4++
 	
r%   c                 :    i d| j                   i| j                  S )zGet the identifying parameters.model)rS   rQ   rL   s    r#   _identifying_paramszChatCohere._identifying_params   s$     A7DJJ'@4+?+?@@r%   Nr'   stoprun_managerr(   c              +     K   t        |fi | j                  |}t        | j                  d      r | j                  j                  di |}n  | j                  j
                  di |ddi}|D ]M  }|j                  dk(  s|j                  }t        t        |            }	|r|j                  ||	       |	 O y w	Nchat_streamstreamTztext-generationr9   r"   )chunkrK   )rB   rQ   hasattrclientrY   chat
event_typetextr   r   on_llm_new_token
rM   r'   rU   rV   r(   requestrZ   datadeltar\   s
             r#   _streamzChatCohere._stream   s      *(Ud6J6JUfU4;;.,T[[,,7w7F%T[[%%===F 	D"33		+N54QR00e0D	s   B C>Cc                  K   t        |fi | j                  |}t        | j                  d      r% | j                  j                  di | d {   }n( | j                  j
                  di |ddi d {   }|2 3 d {   }|j                  dk(  s|j                  }t        t        |            }	|r|j                  ||	       d {    |	 ^7 7 f7 ]7 6 y wrX   )rB   rQ   r]   async_clientrY   r_   r`   ra   r   r   rb   rc   s
             r#   _astreamzChatCohere._astream   s      *(Ud6J6JUfU4$$m484,,88C7CCF14,,11IGIDIIF  	 	$"33		+N54QR%66uE6JJJ DI	
 K !s`   AC(C(C(9C :C( C&C"C&C(:C(C$C( C("C&$C(&C(responsec                 v    |j                   |j                  |j                  |j                  |j                  dS )z1Get the generation info from cohere API response.r1   	citationssearch_resultssearch_queriestoken_countrm   )rM   rk   s     r#   _get_generation_infozChatCohere._get_generation_info   s;     "++!++&55&55#//
 	
r%   c                 ^   | j                   r! | j                  |f||d|}t        |      S t        |fi | j                  |} | j
                  j                  di |}t        |j                        }d }	t        |d      r| j                  |      }	t        t        ||	      g      S N)rU   rV   r[   r1   )r   generation_info)generationsrK   )	streamingrg   r   rB   rQ   r^   r_   r   ra   r]   rr   r   r   
rM   r'   rU   rV   r(   stream_iterrd   rk   r   ru   s
             r#   	_generatezChatCohere._generate   s     >>&$,,#@FK (44)(Ud6J6JUfU#4;;##.g.HMM28[)"77AOwP
 	
r%   c                 z  K   | j                   r) | j                  |f||d|}t        |       d {   S t        |fi | j                  |} | j
                  j                  di |}t        |j                        }d }	t        |d      r| j                  |      }	t        t        ||	      g      S 7 wrt   )rw   rj   r   rB   rQ   r^   r_   r   ra   r]   rr   r   r   rx   s
             r#   
_ageneratezChatCohere._agenerate   s      >>'$--#@FK /{;;;)(Ud6J6JUfU#4;;##.g.HMM28[)"77AOwP
 	
 <s   1B;B9BB;ra   c                 `    t        | j                  j                  |      j                        S )zCalculate number of tokens.)ra   )lenr^   tokenizetokens)rM   ra   s     r#   get_num_tokenszChatCohere.get_num_tokens   s%    4;;''T'299::r%   )NN)__name__
__module____qualname____doc__r   model_configpropertyr7   rN   r   r   rQ   rT   r   r   r   r   r   r   rg   r
   r   rj   rr   r   rz   r|   intr   rK   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   rK   r%   r#   <module>r      s    E E 6 
  S R  68k 8c 82 26/;;/; d38n-./; 	/;
 
#s(^/;d 
E6SU;
 U;U;r%   