a
    ”˜ëj¦%  ã                   @  sú   d Z ddlmZ ddlmZ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 ddlmZmZ ddlmZ erŒdd	lmZ dd
lmZ G dd„ deƒZddddœdd„ZG dd„ deƒZdddœdd„Zdddœdd„Zddœdddd œd!d"„Zd#S )$zBase message.é    )Úannotations)ÚTYPE_CHECKINGÚAnyÚOptionalÚUnionÚcast)Ú
ConfigDictÚField)ÚSerializable)Úget_bolded_text)Úmerge_dictsÚmerge_lists)Úis_interactive_env)ÚSequence©ÚChatPromptTemplatec                      sì   e Zd ZU dZded< eed�Zded< eed�Zded< ded	< d
Z	ded< ed
dd�Z
ded< edd�Zddddœ‡ fdd„Zeddœdd„ƒZeddœdd„ƒZddœdd„Zdd d!œd"d#„Zd*ddd%œd&d'„Zddœd(d)„Z‡  ZS )+ÚBaseMessagez]Base abstract message class.

    Messages are the inputs and outputs of ``ChatModel``s.
    ú"Union[str, list[Union[str, dict]]]Úcontent)Údefault_factoryÚdictÚadditional_kwargsÚresponse_metadataÚstrÚtypeNzOptional[str]ÚnameT)ÚdefaultZcoerce_numbers_to_strÚidZallow)Úextrar   ÚNone)r   ÚkwargsÚreturnc                   s   t ƒ jf d|i|¤Ž dS )zlInitialize ``BaseMessage``.

        Args:
            content: The string contents of the message.
        r   N)ÚsuperÚ__init__)Úselfr   r    ©Ú	__class__© ú[/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/messages/base.pyr#   E   s    
zBaseMessage.__init__Úbool)r!   c                 C  s   dS )zL``BaseMessage`` is serializable.

        Returns:
            True
        Tr'   ©Úclsr'   r'   r(   Úis_lc_serializableQ   s    zBaseMessage.is_lc_serializablez	list[str]c                 C  s   g d¢S )zyGet the namespace of the langchain object.

        Returns:
            ``["langchain", "schema", "messages"]``
        )Z	langchainZschemaÚmessagesr'   r*   r'   r'   r(   Úget_lc_namespaceZ   s    zBaseMessage.get_lc_namespacec                 C  s6   t | jtƒr| jS dd„ | jD ƒ}d dd„ |D ƒ¡S )zqGet the text ``content`` of the message.

        Returns:
            The text content of the message.

        c                 S  s8   g | ]0}t |tƒs0| d ¡dkrt | d¡tƒr|‘qS )r   Útext)Ú
isinstancer   Úget©Ú.0Úblockr'   r'   r(   Ú
<listcomp>n   s   
ýz$BaseMessage.text.<locals>.<listcomp>Ú c                 s  s$   | ]}t |tƒr|n|d  V  qdS )r/   N)r0   r   r2   r'   r'   r(   Ú	<genexpr>t   s   z#BaseMessage.text.<locals>.<genexpr>)r0   r   r   Újoin)r$   Úblocksr'   r'   r(   r/   c   s    þ
ÿzBaseMessage.textr   ©Úotherr!   c                 C  s    ddl m} || gd�}|| S )zÔConcatenate this message with another message.

        Args:
            other: Another message to concatenate with this one.

        Returns:
            A ChatPromptTemplate containing both messages.
        r   r   ©r-   )Úlangchain_core.prompts.chatr   )r$   r;   r   Úpromptr'   r'   r(   Ú__add__x   s    
zBaseMessage.__add__F)Úhtmlr!   c                 C  s@   t | j ¡ d |d�}| jdur0|d| j› �7 }|› d| j› �S )a  Get a pretty representation of the message.

        Args:
            html: Whether to format the message as HTML. If True, the message will be
                formatted with HTML tags. Default is False.

        Returns:
            A pretty representation of the message.

        z Message©ÚboldNz
Name: z

)Úget_msg_title_reprr   Útitler   r   )r$   r@   rD   r'   r'   r(   Úpretty_repr‡   s    
zBaseMessage.pretty_reprc                 C  s   t | jtƒ d�ƒ dS )z-Print a pretty representation of the message.)r@   N)ÚprintrE   r   )r$   r'   r'   r(   Úpretty_print›   s    zBaseMessage.pretty_print)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r	   r   r   r   r   r   r   Zmodel_configr#   Úclassmethodr,   r.   r/   r?   rE   rG   Ú__classcell__r'   r'   r%   r(   r      s(   

ÿ þr   r   )Úfirst_contentÚcontentsr!   c                 G  sŠ   | }|D ]|}t |tƒr6t |tƒr*||7 }q„|g|¢}qt |tƒrRttd|ƒ|ƒ}q|rvt |d tƒrv|d  |7  < q|r| |¡ q|S )zéMerge multiple message contents.

    Args:
        first_content: The first ``content``. Can be a string or a list.
        contents: The other ``content``s. Can be a string or a list.

    Returns:
        The merged content.

    Úlistéÿÿÿÿ)r0   r   rQ   r   r   Úappend)rO   rP   Úmergedr   r'   r'   r(   Úmerge_content    s    



rU   c                   @  s    e Zd ZdZdd dœdd„ZdS )ÚBaseMessageChunkzCMessage chunk, which can be concatenated with other Message chunks.r   r:   c                 C  sê   t |tƒr@| j| j| jt| j|jƒt| j|jƒt| j	|j	ƒd�S t |t
ƒrÄtdd„ |D ƒƒrÄt| jgdd„ |D ƒ¢R Ž }t| jgdd„ |D ƒ¢R Ž }t| j	gdd„ |D ƒ¢R Ž }| j| j|||d�S d| jj› d	|jj› d
�}t|ƒ‚dS )a¡  Message chunks support concatenation with other message chunks.

        This functionality is useful to combine message chunks yielded from
        a streaming model into a complete message.

        Args:
            other: Another message chunk to concatenate with this one.

        Returns:
            A new message chunk that is the concatenation of this message chunk
            and the other message chunk.

        Raises:
            TypeError: If the other object is not a message chunk.

        For example,

        ``AIMessageChunk(content="Hello") + AIMessageChunk(content=" World")``

        will give ``AIMessageChunk(content="Hello World")``

        )r   r   r   r   r   c                 s  s   | ]}t |tƒV  qd S ©N)r0   rV   ©r3   Úor'   r'   r(   r7   ð   s   z+BaseMessageChunk.__add__.<locals>.<genexpr>c                 s  s   | ]}|j V  qd S rW   )r   rX   r'   r'   r(   r7   ó   ó    c                 s  s   | ]}|j V  qd S rW   )r   rX   r'   r'   r(   r7   õ   rZ   c                 s  s   | ]}|j V  qd S rW   )r   rX   r'   r'   r(   r7   ø   rZ   )r   r   r   r   z$unsupported operand type(s) for +: "z" and "ú"N)r0   rV   r&   r   r   rU   r   r   r   r   rQ   ÚallrH   Ú	TypeError)r$   r;   r   r   r   Úmsgr'   r'   r(   r?   Ê   sN    
ÿÿùÿÿÿÿÿüÿþÿzBaseMessageChunk.__add__N)rH   rI   rJ   rK   r?   r'   r'   r'   r(   rV   Ç   s   rV   r   )Úmessager!   c                 C  s   | j |  ¡ dœS )zõConvert a Message to a dictionary.

    Args:
        message: Message to convert.

    Returns:
        Message as a dict. The dict will have a ``type`` key with the message type
        and a ``data`` key with the message data as a dict.

    )r   Údata)r   Z
model_dump)r_   r'   r'   r(   Úmessage_to_dict  s    ra   zSequence[BaseMessage]z
list[dict])r-   r!   c                 C  s   dd„ | D ƒS )zÄConvert a sequence of Messages to a list of dictionaries.

    Args:
        messages: Sequence of messages (as ``BaseMessage``s) to convert.

    Returns:
        List of messages as dicts.

    c                 S  s   g | ]}t |ƒ‘qS r'   )ra   )r3   Úmr'   r'   r(   r5      rZ   z$messages_to_dict.<locals>.<listcomp>r'   r<   r'   r'   r(   Úmessages_to_dict  s    
rc   FrA   r   r)   )rD   rB   r!   c                C  sX   d|  d }dt |ƒ d }d| }t |ƒd r8|d n|}|rHt|ƒ}|› |› |› �S )z¿Get a title representation for a message.

    Args:
        title: The title.
        bold: Whether to bold the title. Default is False.

    Returns:
        The title representation.

    ú éP   é   ú=)Úlenr   )rD   rB   ÚpaddedZsep_lenÚsepZ
second_sepr'   r'   r(   rC   #  s    rC   N)rK   Ú
__future__r   Útypingr   r   r   r   r   Zpydanticr   r	   Z langchain_core.load.serializabler
   Zlangchain_core.utilsr   Zlangchain_core.utils._merger   r   Z$langchain_core.utils.interactive_envr   Úcollections.abcr   r=   r   r   rU   rV   ra   rc   rC   r'   r'   r'   r(   Ú<module>   s"    'A