
    Ng                     .   d dl 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 d dlmZmZ d dlmZmZ d dlmZmZ d dlmZmZmZmZ d d	lmZ d
ee         deeeeeeeeeeef         f                  f         f                  fdZ G d de          ZdS )    )AnyDictListOptionalUnion)ClientSession)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseChatModel)	AIMessageBaseMessage)ChatGeneration
ChatResultconvert_to_secret_strget_from_dict_or_env)
ConfigDictField	SecretStrmodel_validator)Requestsmessagesreturnc                     g }| D ]S}|j         dk    r|                    d|j        d           +|j         dk    r|                    d|j        d           T|S )Nhumanuser)rolecontentsystem)typeappendr   )r   formatted_messagesmessages      c/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/chat_models/dappier.py_format_dappier_messagesr%      s{      V V<7""%%v'/&R&RSSSS\X%%%%xGO&T&TUUU    c                      e Zd ZU dZdZeed<   dZeed<    edd          Z	e
e         ed	<    ed
          Z ed          ededefd                        Zedefd            Zedefd            Z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dee         de
ee                  de
e         dedef
dZdS )ChatDappierAIa  `Dappier` chat large language models.

    `Dappier` is a platform enabling access to diverse, real-time data models.
    Enhance your AI applications with Dappier's pre-trained, LLM-ready data models
    and ensure accurate, current responses with reduced inaccuracies.

    To use one of our Dappier AI Data Models, you will need an API key.
    Please visit Dappier Platform (https://platform.dappier.com/) to log in
    and create an API key in your profile.

    Example:
        .. code-block:: python

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

            # Initialize `ChatDappierAI` with the desired configuration
            chat = ChatDappierAI(
                dappier_endpoint="https://api.dappier.com/app/datamodel/dm_01hpsxyfm2fwdt2zet9cg6fdxt",
                dappier_api_key="<YOUR_KEY>")

            # Create a list of messages to interact with the model
            messages = [HumanMessage(content="hello")]

            # Invoke the model with the provided messages
            chat.invoke(messages)


    you can find more details here : https://docs.dappier.com/introductionz1https://api.dappier.com/app/datamodelconversationdappier_endpointdm_01hpsxyfm2fwdt2zet9cg6fdxtdappier_modelNzDappier API Token)descriptiondappier_api_keyforbid)extrabefore)modevaluesr   c                 H    t          t          |dd                    |d<   |S )z,Validate that api key exists in environment.r-   DAPPIER_API_KEYr   )clsr2   s     r$   validate_environmentz"ChatDappierAI.validate_environmentM   s1     %: ):<MNN%
 %
 ! r&   c                      ddl m}  d|  S )Nr   __version__z
langchain/)langchain_communityr9   r8   s    r$   get_user_agentzChatDappierAI.get_user_agentV   s"    333333)K)))r&   c                     dS )zReturn type of chat model.zdappier-realtimesearch-chat selfs    r$   	_llm_typezChatDappierAI._llm_type\   s
     -,r&   c                 F    | j         r| j                                         S dS )N )r-   get_secret_valuer>   s    r$   _api_keyzChatDappierAI._api_keya   s'     	;'88:::rr&   r   stoprun_managerkwargsc                    | j          }d| j         |                                 d}t          |          }| j        |d}t          |          }	|	                    ||          }
|
                                 |
                                }|d         }t          t          t          |          	          g
          S )NBearer Authorizationz
User-Agentr   modelconversation)headers)urldatar#   r   r#   generations)r)   rD   r;   r%   r+   r   postraise_for_statusjsonr   r   r   )r?   r   rE   rF   rG   rQ   rP   
user_querypayloadrequestresponserR   message_responses                r$   	_generatezChatDappierAI._generateg   s     &(6t}66--//
 
 .x@@@
'&#
 #

 7+++<<Cg<66!!###}}	?'	BR0S0S0STTTU
 
 
 	
r&   c           	      n  K   | j          }d| j         |                                 d}t          |          }| j        |d}t                      4 d {V }	|	                    |||          4 d {V }
|
                                 |
                                 d {V }|d         }t          t          t          |                    g	          cd d d           d {V  cd d d           d {V  S # 1 d {V swxY w Y   	 d d d           d {V  d S # 1 d {V swxY w Y   d S )
NrI   rJ   rL   rM   )rY   rP   r#   rS   rT   rU   )r)   rD   r;   r%   r+   r   rW   rX   rY   r   r   r   )r?   r   rE   rF   rG   rQ   rP   rZ   r[   sessionr]   rR   r^   s                r$   
_ageneratezChatDappierAI._agenerate   s      &(6t}66--//
 
 .x@@@
'&#
 #

 !?? 
	 
	 
	 
	 
	 
	 
	g||Cgw|GG 	 	 	 	 	 	 	8))+++%]]__,,,,,,#'	? !&yAQ/R/R/RSSS!  	 	 	 	 	 	 	 	 	 	 	 	 	
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
		 	 	 	 	 	 	 	 	 	 	 	 	 	 	
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	s7   D$7A#C?D$?
D			D$D		D$$
D.1D.)NN)__name__
__module____qualname____doc__r)   str__annotations__r+   r   r-   r   r   r   model_configr   classmethodr   r   r6   staticmethodr;   propertyr@   rD   r   r   r
   r   r_   r	   rb   r=   r&   r$   r(   r(   $   s
        N N< PcOOO8M3888+05CV+W+W+WOXi(WWW:  L _(###$ 3    [ $# *C * * * \*
 -3 - - - X- #    X %):>	
 
{#
 tCy!
 67	

 
 

 
 
 
B %)?C	 {# tCy! ;<	
  
     r&   r(   N) typingr   r   r   r   r   aiohttpr   langchain_core.callbacksr	   r
   *langchain_core.language_models.chat_modelsr   langchain_core.messagesr   r   langchain_core.outputsr   r   langchain_core.utilsr   r   pydanticr   r   r   r   &langchain_community.utilities.requestsr   rg   r%   r(   r=   r&   r$   <module>rv      s   3 3 3 3 3 3 3 3 3 3 3 3 3 3 ! ! ! ! ! !                    > = = = = = = = L L L L L L L L B B B B B B B B B B B B ; ; ; ; ; ;;	$sE#tE#tCH~*=$>??@@
AB   } } } } }M } } } } }r&   