a
    bg                     @   s   d Z ddlmZ ddlmZmZmZmZmZm	Z	 ddl
Z
ddlmZ ddlmZ ddlmZmZmZmZ ddlmZmZ dd	lmZ dd
lmZ ee ddddZdee e	ee  edddZG dd deZdS )z<ChatModel wrapper which returns user input as the response..    )StringIO)AnyCallableDictListMappingOptionalN)CallbackManagerForLLMRun)BaseChatModel)BaseMessageHumanMessage_message_from_dictmessages_to_dict)ChatGeneration
ChatResult)Field)enforce_stop_tokens)messagesreturnc              	   C   sJ   t | }|D ]8}tj|ddddd d}tddd t| tdd qd S )	NFTi'  )Zdefault_flow_style	sort_keysZallow_unicodewidthZ
line_break
z ======= start of message =======z

z======= end of message =======)r   yamldumpprint)r   Zdict_messagesmessageyaml_string r   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/chat_models/human.py_display_messages   s    r   )r   stopr   c                    s   g }t     sq<|r0t fdd|D r0q<|  qd|}zTttt|}|du rnt	ddW S |rt
|jtrt|j||_ntd|W S  tjy   tdY n ty   td	Y n0 dS )
z3Collects and returns user input as a single string.c                 3   s   | ]}| v V  qd S Nr   ).0seqliner   r   	<genexpr>0       z&_collect_yaml_input.<locals>.<genexpr>r   N )contentz'Cannot use when output is not a string.zInvalid YAML string entered.zInvalid message entered.)inputstripanyappendjoinr   r   Z	safe_loadr   r   
isinstancer)   strr   
ValueErrorZ	YAMLError)r   r    linesr   r   r   r$   r   _collect_yaml_input'   s*    
r3   c                   @   s   e Zd ZU dZedd dZeed< edd dZeed< dZ	e
ed	< i Zee
ef ed
< i Zee
ef ed< eee
ef dddZee
dddZdee eee
  ee eedddZdS )HumanInputChatModelz3ChatModel which returns user input as the response.c                   C   s   t S r!   )r3   r   r   r   r   <lambda>I   r'   zHumanInputChatModel.<lambda>)default_factory
input_funcc                   C   s   t S r!   )r   r   r   r   r   r5   J   r'   message_funcr   	separatorinput_kwargsmessage_kwargs)r   c                 C   s   | j j| jjdS )N)r7   r8   )r7   __name__r8   selfr   r   r   _identifying_paramsO   s    z'HumanInputChatModel._identifying_paramsc                 C   s   dS )zReturns the type of LLM.zhuman-input-chat-modelr   r=   r   r   r   	_llm_typeV   s    zHumanInputChatModel._llm_typeN)r   r    run_managerkwargsr   c                 K   s>   | j |fi | j | j|fd|i| j}tt|dgdS )a  
        Displays the messages to the user and returns their input as a response.

        Args:
            messages (List[BaseMessage]): The messages to be displayed to the user.
            stop (Optional[List[str]]): A list of stop strings.
            run_manager (Optional[CallbackManagerForLLMRun]): Currently not used.

        Returns:
            ChatResult: The user's input as a response.
        r    )r   )Zgenerations)r8   r;   r7   r:   r   r   )r>   r   r    rA   rB   Z
user_inputr   r   r   	_generate[   s    zHumanInputChatModel._generate)NN)r<   
__module____qualname____doc__r   r7   r   __annotations__r8   r9   r0   r:   r   r   r;   propertyr   r?   r@   r   r   r   r	   r   rC   r   r   r   r   r4   F   s&   
  
r4   )N)rF   ior   typingr   r   r   r   r   r   r   Zlangchain_core.callbacksr	   Z*langchain_core.language_models.chat_modelsr
   Zlangchain_core.messagesr   r   r   r   Zlangchain_core.outputsr   r   Zpydanticr   Zlangchain_community.llms.utilsr   r   r0   r3   r4   r   r   r   r   <module>   s      