a
    ”˜ëj; ã                   @  sô  U d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
 ddlmZ ddlmZmZmZ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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) ddl*m+Z+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3m4Z4 ddl5m6Z6m7Z7m8Z8 e�r`ddl9m:Z: ddl;m<Z< ddl=m>Z> zddl?m@Z@ dZAW n eB�yŒ   dZAY n0 e CeD¡ZEdddœdd„ZFeeee"edd�f ee.edd�f ee(ed d�f ee3ed!d�f ee+ed"d�f ee7ed#d�f ee#ed$d�f ee/ed%d�f ee)ed&d�f ee4ed'd�f ee,ed(d�f ee8ed)d�f f eeeFƒd*�f ZGd›d-dddd.œd/d0„ZHd1d2d3œd4d5„ZId6d7d8œd9d:„ZJd2d2d;œd<d=„ZKee%eLeM eNeMeMf eMeOeMef f ZPdœddd>d>d?d>dd2d@œdAdB„ZQdCd2d3œdDdE„ZRdFd7d8œdGdH„ZSdIdIdJœdKdL„ZTeTddddddddMœdFdNdNdOdOdNdNdPd7dQœ	dRdS„ƒZUeTdTdUœdFdd7dVœdWdX„ƒZVeTdYddddddZœdFd[d\d]d^d_d_d^d`d7daœ
dbdc„ƒZWddddeœdfdgd^dhdiœdjdk„ZXdddlœd-d[dmdndod_d7dpœdqdr„ZYdddddsœd-d[dmdnd^d^d_d_d7dtœ	dudv„ZZe.e/e"e#e3e4e7e8e+e,e(e)iZ[dwe\dx< dydz„ e[ ]¡ D ƒZ^d2d{d3œd|d}„Z_d{d2d;œd~d„Z`dd€d�œd‚dƒ„Zad2d„d^d…œd†d‡„Zbdˆdd‰œdŠd‹„Zcd2dd3œdŒd�„ZddŽd�d�œd‘d’„Zed“d”dd•œd–d—d—d^d[d˜œd™dš„ZfdS )�a9  Module contains utility functions for working with messages.

Some examples of what you can do with these functions include:

* Convert messages to strings (serialization)
* Convert messages from dicts to Message objects (deserialization)
* Filter messages from a list of messages based on name, type or id etc.

é    )ÚannotationsN)ÚIterableÚSequence)Úpartial)	ÚTYPE_CHECKINGÚ	AnnotatedÚAnyÚCallableÚLiteralÚOptionalÚUnionÚcastÚoverload)ÚDiscriminatorÚFieldÚTag)Ú	ErrorCodeÚcreate_message)Úconvert_to_openai_data_blockÚis_data_content_block)Ú	AIMessageÚAIMessageChunk)ÚBaseMessageÚBaseMessageChunk)ÚChatMessageÚChatMessageChunk)ÚFunctionMessageÚFunctionMessageChunk)ÚHumanMessageÚHumanMessageChunk)ÚRemoveMessage)ÚSystemMessageÚSystemMessageChunk)ÚToolCallÚToolMessageÚToolMessageChunk)ÚBaseLanguageModel©ÚPromptValue)ÚRunnable)ÚTextSplitterTFr   Ústr)ÚvÚreturnc                 C  sF   t | tƒrd| v r| d S t| dƒr*| jS dt| ƒ› d�}t|ƒ‚dS )zCGet the type associated with the object for serialization purposes.ÚtypezfExpected either a dictionary with a 'type' key or an object with a 'type' attribute. Instead got type Ú.N)Ú
isinstanceÚdictÚhasattrr.   Ú	TypeError)r,   Úmsg© r5   ú\/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/messages/utils.pyÚ	_get_type<   s    
ÿÿr7   Úai)ÚtagÚhumanÚchatÚsystemÚfunctionÚtoolr   r   r   r"   r   r%   )ZdiscriminatorÚHumanÚAIúSequence[BaseMessage])ÚmessagesÚhuman_prefixÚ	ai_prefixr-   c                 C  sÌ   g }| D ]¸}t |tƒr|}ndt |tƒr,|}nTt |tƒr<d}nDt |tƒrLd}n4t |tƒr\d}n$t |tƒrn|j}nd|› �}t|ƒ‚|› d| 	¡ › �}t |tƒr¶d|j
v r¶||j
d › 7 }| |¡ qd |¡S )a  Convert a sequence of messages to strings and concatenate them into one string.

    Args:
        messages: Messages to be converted to strings.
        human_prefix: The prefix to prepend to contents of ``HumanMessage``s.
            Default is ``'Human'``.
        ai_prefix: The prefix to prepend to contents of ``AIMessage``. Default is
            ``'AI'``.

    Returns:
        A single string concatenation of all input messages.

    Raises:
        ValueError: If an unsupported message type is encountered.

    Example:
        .. code-block:: python

            from langchain_core import AIMessage, HumanMessage

            messages = [
                HumanMessage(content="Hi, how are you?"),
                AIMessage(content="Good, how are you?"),
            ]
            get_buffer_string(messages)
            # -> "Human: Hi, how are you?\nAI: Good, how are you?"

    ÚSystemZFunctionZToolzGot unsupported message type: z: Zfunction_callÚ
)r0   r   r   r!   r   r$   r   ÚroleÚ
ValueErrorÚtextÚadditional_kwargsÚappendÚjoin)rB   rC   rD   Zstring_messagesÚmrG   r4   Úmessager5   r5   r6   Úget_buffer_string\   s*    






rO   r1   r   )rN   r-   c                 C  sx  | d }|dkr"t f i | d ¤ŽS |dkr<tf i | d ¤ŽS |dkrVtf i | d ¤ŽS |dkrptf i | d ¤ŽS |dkrŠtf i | d ¤ŽS |dkr¤tf i | d ¤ŽS |d	kr¾tf i | d ¤ŽS |d
krØtf i | d ¤ŽS |dkròtf i | d ¤ŽS |dk�rt	f i | d ¤ŽS |dk�r*t
f i | d ¤ŽS |dk�rFtf i | d ¤ŽS |dk�rbtf i | d ¤ŽS d|› �}t|ƒ‚d S )Nr.   r:   Údatar8   r<   r;   r=   r>   Úremover   r   r   r%   r"   r   zGot unexpected message type: )r   r   r!   r   r   r$   r    r   r   r   r%   r"   r   rH   )rN   Útype_r4   r5   r5   r6   Ú_message_from_dict”   s:    




rS   zSequence[dict]úlist[BaseMessage]©rB   r-   c                 C  s   dd„ | D ƒS )zÇConvert a sequence of messages from dicts to ``Message`` objects.

    Args:
        messages: Sequence of messages (as dicts) to convert.

    Returns:
        list of messages (BaseMessages).

    c                 S  s   g | ]}t |ƒ‘qS r5   )rS   ©Ú.0rM   r5   r5   r6   Ú
<listcomp>¾   ó    z&messages_from_dict.<locals>.<listcomp>r5   ©rB   r5   r5   r6   Úmessages_from_dict´   s    
r[   )Úchunkr-   c                   sR   t | tƒs| S dg‰ t | tƒr(ˆ  d¡ | jjd f i ‡ fdd„| j ¡ D ƒ¤ŽS )z�Convert a message chunk to a ``Message``.

    Args:
        chunk: Message chunk to convert.

    Returns:
        Message.
    r.   Útool_call_chunksé   c                   s   i | ]\}}|ˆ vr||“qS r5   r5   ©rW   Úkr,   ©Zignore_keysr5   r6   Ú
<dictcomp>Ñ   rY   z,message_chunk_to_message.<locals>.<dictcomp>)r0   r   r   rK   Ú	__class__Ú__mro__Ú__dict__Úitems)r\   r5   ra   r6   Úmessage_chunk_to_messageÁ   s    	


ÿrg   zOptional[str]zOptional[list[dict[str, Any]]])Úmessage_typeÚcontentÚnameÚtool_call_idÚ
tool_callsÚidrJ   r-   c                 K  sf  i }|dur||d< |dur$||d< |rZ|  dd¡ }r@||d< ||d< | |  di ¡¡ |durj||d< |duräg |d< |D ]d}	d|	v rÔ|	d d	 }
t|
tƒr®tj|
d
d�}
|d  |	d d |
|	d ddœ¡ q~|d  |	¡ q~| dv �r&| di ¡  dd
¡ }�r||d< tf d|i|¤Ž}�n<| dv �rf| di ¡  dd
¡ }�rR||d< t	f d|i|¤Ž}nü| dv �r®| dk�rš| d¡�pˆi |d< d|d d< t
f d|i|¤Ž}n´| dk�rÌtf d|i|¤Ž}n–| dk�r&| di ¡  dd¡}| di ¡  dd¡}|du�r||d< tf ||dœ|¤Ž}n<| dk�r@tf i |¤Ž}n"d| › d�}t|tjd�}t|ƒ‚|S )a#  Create a message from a ``Message`` type and content string.

    Args:
        message_type: (str) the type of the message (e.g., ``'human'``, ``'ai'``, etc.).
        content: (str) the content string.
        name: (str) the name of the message. Default is None.
        tool_call_id: (str) the tool call id. Default is None.
        tool_calls: (list[dict[str, Any]]) the tool calls. Default is None.
        id: (str) the id of the message. Default is None.
        additional_kwargs: (dict[str, Any]) additional keyword arguments.

    Returns:
        a message of the appropriate type.

    Raises:
        ValueError: if the message type is not one of ``'human'``, ``'user'``, ``'ai'``,
            ``'assistant'``, ``'function'``, ``'tool'``, ``'system'``, or
            ``'developer'``.
    Nrj   rk   Úresponse_metadatarJ   rm   rl   r=   Ú	argumentsF)ÚstrictÚ	tool_call)rj   Úargsrm   r.   >   Úuserr:   Úexampleri   >   r8   Ú	assistant>   Ú	developerr<   rv   Ú__openai_role__r>   ÚartifactÚstatus)ri   rx   rQ   zUnexpected message type: 'z_'. Use one of 'human', 'user', 'ai', 'assistant', 'function', 'tool', 'system', or 'developer'.©rN   Z
error_code)ÚpopÚupdater0   r+   ÚjsonÚloadsrK   Úgetr   r   r!   r   r$   r    r   r   ÚMESSAGE_COERCION_FAILURErH   )rh   ri   rj   rk   rl   rm   rJ   Úkwargsrn   rq   rr   rt   rN   rx   ry   r4   r5   r5   r6   Ú!_create_message_from_message_typeÚ   sn    

üÿ	








ÿr‚   ÚMessageLikeRepresentationc           	   
   C  s&  t | tƒr| }�nt | tƒr(td| ƒ}nút | tƒrRt| ƒdkrR| \}}t||ƒ}nÐt | tƒrþ|  ¡ }z>z| d¡}W n t	y�   | d¡}Y n0 | d¡pžd}W nF t	yè } z.d| › �}t
|tjd�}t|ƒ|‚W Y d	}~n
d	}~0 0 t||fi |¤Ž}n$d
t| ƒ› �}t
|tjd�}t|ƒ‚|S )aÅ  Instantiate a ``Message`` from a variety of message formats.

    The message format can be one of the following:

    - ``BaseMessagePromptTemplate``
    - ``BaseMessage``
    - 2-tuple of (role string, template); e.g., (``'human'``, ``'{user_input}'``)
    - dict: a message dict with role and content keys
    - string: shorthand for (``'human'``, template); e.g., ``'{user_input}'``

    Args:
        message: a representation of a message in one of the supported formats.

    Returns:
        an instance of a message or a message template.

    Raises:
        NotImplementedError: if the message type is not supported.
        ValueError: if the message dict does not contain the required keys.

    r:   é   rG   r.   ri   Ú z9Message dict must contain 'role' and 'content' keys, got rz   NzUnsupported message type: )r0   r   r+   r‚   r   Úlenr1   Úcopyr{   ÚKeyErrorr   r   r€   rH   r.   ÚNotImplementedError)	rN   Zmessage_Zmessage_type_strÚtemplateZ
msg_kwargsÚmsg_typeZmsg_contentÚer4   r5   r5   r6   Ú_convert_to_message5  s<    



ÿ ÿÿr�   z7Union[Iterable[MessageLikeRepresentation], PromptValue]c                 C  s,   ddl m} t| |ƒr|  ¡ S dd„ | D ƒS )z°Convert a sequence of messages to a list of messages.

    Args:
        messages: Sequence of messages to convert.

    Returns:
        list of messages (BaseMessages).

    r   r'   c                 S  s   g | ]}t |ƒ‘qS r5   )r�   rV   r5   r5   r6   rX   ~  rY   z'convert_to_messages.<locals>.<listcomp>)Úlangchain_core.prompt_valuesr(   r0   Zto_messages)rB   r(   r5   r5   r6   Úconvert_to_messagesm  s    
r�   r	   )Úfuncr-   c                   sR   t dddddœdd„ƒ}t ddddœd	d„ƒ}dd
dddœ‡ fdd„}ˆ j|_|S )NÚNoner   z@Runnable[Sequence[MessageLikeRepresentation], list[BaseMessage]])rB   r�   r-   c                 [  s   d S ©Nr5   ©rB   r�   r5   r5   r6   Úwrapped‚  s    z"_runnable_support.<locals>.wrappedz#Sequence[MessageLikeRepresentation]rT   c                 [  s   d S r’   r5   r“   r5   r5   r6   r”   ‡  s    z0Union[Sequence[MessageLikeRepresentation], None]zZUnion[list[BaseMessage], Runnable[Sequence[MessageLikeRepresentation], list[BaseMessage]]]c                   s>   ddl m} | d ur$ˆ | fi |¤ŽS |tˆ fi |¤Žˆ jd�S )Nr   )ÚRunnableLambda)rj   )Úlangchain_core.runnables.baser•   r   Ú__name__)rB   r�   r•   ©r�   r5   r6   r”   Œ  s    )N)N)r   Ú__doc__)r�   r”   r5   r˜   r6   Ú_runnable_support�  s     ÿ ÿrš   )Úinclude_namesÚexclude_namesÚinclude_typesÚexclude_typesÚinclude_idsÚexclude_idsÚexclude_tool_callszOptional[Sequence[str]]z1Optional[Sequence[Union[str, type[BaseMessage]]]]zOptional[Sequence[str] | bool])	rB   r›   rœ   r�   rž   rŸ   r    r¡   r-   c                  s@  t | ƒ} g }| D �](}	|r$|	j|v s|r2t|	|ƒs|rB|	j|v rBqˆ du rft|	tƒrZ|	jst|	tƒrfqtˆ tt	t
fƒrðt|	tƒrÚ|	jrÚ‡ fdd„|	jD ƒ}
|
s q|	j}t|	jtƒrÆ‡ fdd„|	jD ƒ}|	j|
|dœd�}	nt|	tƒrð|	jˆ v rðq|sþ|sþ|�r0|�r|	j|v �s0|�r"t|	|ƒ�s0|r|	j|v r| |	¡ q|S )aÆ  Filter messages based on ``name``, ``type`` or ``id``.

    Args:
        messages: Sequence Message-like objects to filter.
        include_names: Message names to include. Default is None.
        exclude_names: Messages names to exclude. Default is None.
        include_types: Message types to include. Can be specified as string names
            (e.g. ``'system'``, ``'human'``, ``'ai'``, ...) or as ``BaseMessage``
            classes (e.g. ``SystemMessage``, ``HumanMessage``, ``AIMessage``, ...).
            Default is None.
        exclude_types: Message types to exclude. Can be specified as string names
            (e.g. ``'system'``, ``'human'``, ``'ai'``, ...) or as ``BaseMessage``
            classes (e.g. ``SystemMessage``, ``HumanMessage``, ``AIMessage``, ...).
            Default is None.
        include_ids: Message IDs to include. Default is None.
        exclude_ids: Message IDs to exclude. Default is None.
        exclude_tool_calls: Tool call IDs to exclude. Default is None.
            Can be one of the following:
            - ``True``: all ``AIMessage``s with tool calls and all
              ``ToolMessage``s will be excluded.
            - a sequence of tool call IDs to exclude:
              - ``ToolMessage``s with the corresponding tool call ID will be
                excluded.
              - The ``tool_calls`` in the AIMessage will be updated to exclude
                matching tool calls. If all ``tool_calls`` are filtered from an
                AIMessage, the whole message is excluded.

    Returns:
        A list of Messages that meets at least one of the ``incl_*`` conditions and none
        of the ``excl_*`` conditions. If not ``incl_*`` conditions are specified then
        anything that is not explicitly excluded will be included.

    Raises:
        ValueError if two incompatible arguments are provided.

    Example:
        .. code-block:: python

            from langchain_core.messages import (
                filter_messages,
                AIMessage,
                HumanMessage,
                SystemMessage,
            )

            messages = [
                SystemMessage("you're a good assistant."),
                HumanMessage("what's your name", id="foo", name="example_user"),
                AIMessage("steve-o", id="bar", name="example_assistant"),
                HumanMessage(
                    "what's your favorite color",
                    id="baz",
                ),
                AIMessage(
                    "silicon blue",
                    id="blah",
                ),
            ]

            filter_messages(
                messages,
                incl_names=("example_user", "example_assistant"),
                incl_types=("system",),
                excl_ids=("bar",),
            )

        .. code-block:: python

            [
                SystemMessage("you're a good assistant."),
                HumanMessage("what's your name", id="foo", name="example_user"),
            ]

    Tc                   s   g | ]}|d  ˆ vr|‘qS )rm   r5   ©rW   rq   ©r¡   r5   r6   rX     s   þz#filter_messages.<locals>.<listcomp>c                   s6   g | ].}t |tƒr.| d ¡dks.| d¡ˆ vr|‘qS )r.   Útool_userm   )r0   r1   r   )rW   Zcontent_blockr£   r5   r6   rX     s
   
û)rl   ri   )r|   )r�   rj   Ú_is_message_typerm   r0   r   rl   r$   ÚlistÚtupleÚsetri   Ú
model_copyrk   rK   )rB   r›   rœ   r�   rž   rŸ   r    r¡   Úfilteredr4   rl   ri   r5   r£   r6   Úfilter_messagesž  s€    V
ÿÿþþýýÿÿþ
þ
þ
ÿÿÿÿÿÿþþýýüür«   rF   )Úchunk_separator)rB   r¬   r-   c                C  sÌ   | sg S t | ƒ} g }| D ]®}|r(| ¡ nd}|s<| |¡ qt|tƒsRt||jƒsb| ||g¡ qt|ƒ}t|ƒ}|jr‚|j 	¡  t|j
tƒr´t|j
tƒr´|j
r´|j
r´| j
|7  _
| t|| ƒ¡ q|S )a5  Merge consecutive Messages of the same type.

    .. note::
        ToolMessages are not merged, as each has a distinct tool call id that can't be
        merged.

    Args:
        messages: Sequence Message-like objects to merge.
        chunk_separator: Specify the string to be inserted between message chunks.
        Default is ``'\n'``.

    Returns:
        list of BaseMessages with consecutive runs of message types merged into single
        messages. By default, if two messages being merged both have string contents,
        the merged content is a concatenation of the two strings with a new-line
        separator.
        The separator inserted between message chunks can be controlled by specifying
        any string with ``chunk_separator``. If at least one of the messages has a list
        of content blocks, the merged content is a list of content blocks.

    Example:

        .. code-block:: python

            from langchain_core.messages import (
                merge_message_runs,
                AIMessage,
                HumanMessage,
                SystemMessage,
                ToolCall,
            )

            messages = [
                SystemMessage("you're a good assistant."),
                HumanMessage(
                    "what's your favorite color",
                    id="foo",
                ),
                HumanMessage(
                    "wait your favorite food",
                    id="bar",
                ),
                AIMessage(
                    "my favorite colo",
                    tool_calls=[
                        ToolCall(
                            name="blah_tool", args={"x": 2}, id="123", type="tool_call"
                        )
                    ],
                    id="baz",
                ),
                AIMessage(
                    [{"type": "text", "text": "my favorite dish is lasagna"}],
                    tool_calls=[
                        ToolCall(
                            name="blah_tool",
                            args={"x": -10},
                            id="456",
                            type="tool_call",
                        )
                    ],
                    id="blur",
                ),
            ]

            merge_message_runs(messages)

        .. code-block:: python

            [
                SystemMessage("you're a good assistant."),
                HumanMessage(
                    "what's your favorite color\\n"
                    "wait your favorite food", id="foo",
                ),
                AIMessage(
                    [
                        "my favorite colo",
                        {"type": "text", "text": "my favorite dish is lasagna"}
                    ],
                    tool_calls=[
                        ToolCall({
                            "name": "blah_tool",
                            "args": {"x": 2},
                            "id": "123",
                            "type": "tool_call"
                        }),
                        ToolCall({
                            "name": "blah_tool",
                            "args": {"x": -10},
                            "id": "456",
                            "type": "tool_call"
                        })
                    ]
                    id="baz"
                ),
            ]

    N)r�   r{   rK   r0   r$   rc   ÚextendÚ_msg_to_chunkrn   Úclearri   r+   Ú_chunk_to_msg)rB   r¬   Úmergedr4   ÚlastÚ
last_chunkZ
curr_chunkr5   r5   r6   Úmerge_message_runs/  s2    i

ÿ
þýür´   r²   )ÚstrategyÚallow_partialÚend_onÚstart_onÚinclude_systemÚtext_splitterÚintzZUnion[Callable[[list[BaseMessage]], int], Callable[[BaseMessage], int], BaseLanguageModel]zLiteral['first', 'last']ÚboolzPOptional[Union[str, type[BaseMessage], Sequence[Union[str, type[BaseMessage]]]]]z9Optional[Union[Callable[[str], list[str]], TextSplitter]])
rB   Ú
max_tokensÚtoken_counterrµ   r¶   r·   r¸   r¹   rº   r-   c             
     s,  |r|dkrd}	t |	ƒ‚|r0|dkr0d}	t |	ƒ‚t| ƒ} tˆ dƒrJˆ j}
nZtˆ ƒrŒttt ˆ ¡j	 
¡ ƒƒjtu r†dddœ‡ fdd	„}
q¤ˆ }
nd
tˆ ƒ› d�}	t |	ƒ‚trºt|tƒrº|j}n|rÊtd|ƒ}nt}|dkròt| ||
||rèdnd|d�S |dk�rt| ||
|||||d�S d|›d�}	t |	ƒ‚dS )a¡.  Trim messages to be below a token count.

    ``trim_messages`` can be used to reduce the size of a chat history to a specified
    token count or specified message count.

    In either case, if passing the trimmed chat history back into a chat model
    directly, the resulting chat history should usually satisfy the following
    properties:

    1. The resulting chat history should be valid. Most chat models expect that chat
       history starts with either (1) a ``HumanMessage`` or (2) a ``SystemMessage``
       followed by a ``HumanMessage``. To achieve this, set ``start_on='human'``.
       In addition, generally a ``ToolMessage`` can only appear after an ``AIMessage``
       that involved a tool call.
       Please see the following link for more information about messages:
       https://python.langchain.com/docs/concepts/#messages
    2. It includes recent messages and drops old messages in the chat history.
       To achieve this set the ``strategy='last'``.
    3. Usually, the new chat history should include the ``SystemMessage`` if it
       was present in the original chat history since the ``SystemMessage`` includes
       special instructions to the chat model. The ``SystemMessage`` is almost always
       the first message in the history if present. To achieve this set the
       ``include_system=True``.

    .. note::
        The examples below show how to configure ``trim_messages`` to achieve a behavior
        consistent with the above properties.

    Args:
        messages: Sequence of Message-like objects to trim.
        max_tokens: Max token count of trimmed messages.
        token_counter: Function or llm for counting tokens in a ``BaseMessage`` or a
            list of ``BaseMessage``. If a ``BaseLanguageModel`` is passed in then
            ``BaseLanguageModel.get_num_tokens_from_messages()`` will be used.
            Set to ``len`` to count the number of **messages** in the chat history.

            .. note::
                Use ``count_tokens_approximately`` to get fast, approximate token
                counts.
                This is recommended for using ``trim_messages`` on the hot path, where
                exact token counting is not necessary.

        strategy: Strategy for trimming.
            - ``'first'``: Keep the first ``<= n_count`` tokens of the messages.
            - ``'last'``: Keep the last ``<= n_count`` tokens of the messages.
            Default is ``'last'``.
        allow_partial: Whether to split a message if only part of the message can be
            included. If ``strategy='last'`` then the last partial contents of a message
            are included. If ``strategy='first'`` then the first partial contents of a
            message are included.
            Default is False.
        end_on: The message type to end on. If specified then every message after the
            last occurrence of this type is ignored. If ``strategy='last'`` then this
            is done before we attempt to get the last ``max_tokens``. If
            ``strategy='first'`` then this is done after we get the first
            ``max_tokens``. Can be specified as string names (e.g. ``'system'``,
            ``'human'``, ``'ai'``, ...) or as ``BaseMessage`` classes (e.g.
            ``SystemMessage``, ``HumanMessage``, ``AIMessage``, ...). Can be a single
            type or a list of types.
            Default is None.
        start_on: The message type to start on. Should only be specified if
            ``strategy='last'``. If specified then every message before
            the first occurrence of this type is ignored. This is done after we trim
            the initial messages to the last ``max_tokens``. Does not
            apply to a ``SystemMessage`` at index 0 if ``include_system=True``. Can be
            specified as string names (e.g. ``'system'``, ``'human'``, ``'ai'``, ...) or
            as ``BaseMessage`` classes (e.g. ``SystemMessage``, ``HumanMessage``,
            ``AIMessage``, ...). Can be a single type or a list of types.
            Default is None.
        include_system: Whether to keep the SystemMessage if there is one at index 0.
            Should only be specified if ``strategy="last"``.
            Default is False.
        text_splitter: Function or ``langchain_text_splitters.TextSplitter`` for
            splitting the string contents of a message. Only used if
            ``allow_partial=True``. If ``strategy='last'`` then the last split tokens
            from a partial message will be included. if ``strategy='first'`` then the
            first split tokens from a partial message will be included. Token splitter
            assumes that separators are kept, so that split contents can be directly
            concatenated to recreate the original text. Defaults to splitting on
            newlines.

    Returns:
        list of trimmed ``BaseMessage``.

    Raises:
        ValueError: if two incompatible arguments are specified or an unrecognized
            ``strategy`` is specified.

    Example:
        Trim chat history based on token count, keeping the ``SystemMessage`` if
        present, and ensuring that the chat history starts with a ``HumanMessage`` (
        or a ``SystemMessage`` followed by a ``HumanMessage``).

        .. code-block:: python

            from langchain_core.messages import (
                AIMessage,
                HumanMessage,
                BaseMessage,
                SystemMessage,
                trim_messages,
            )

            messages = [
                SystemMessage(
                    "you're a good assistant, you always respond with a joke."
                ),
                HumanMessage("i wonder why it's called langchain"),
                AIMessage(
                    'Well, I guess they thought "WordRope" and "SentenceString" just '
                    "didn't have the same ring to it!"
                ),
                HumanMessage("and who is harrison chasing anyways"),
                AIMessage(
                    "Hmmm let me think.\n\nWhy, he's probably chasing after the last "
                    "cup of coffee in the office!"
                ),
                HumanMessage("what do you call a speechless parrot"),
            ]


            trim_messages(
                messages,
                max_tokens=45,
                strategy="last",
                token_counter=ChatOpenAI(model="gpt-4o"),
                # Most chat models expect that chat history starts with either:
                # (1) a HumanMessage or
                # (2) a SystemMessage followed by a HumanMessage
                start_on="human",
                # Usually, we want to keep the SystemMessage
                # if it's present in the original history.
                # The SystemMessage has special instructions for the model.
                include_system=True,
                allow_partial=False,
            )

        .. code-block:: python

            [
                SystemMessage(
                    content="you're a good assistant, you always respond with a joke."
                ),
                HumanMessage(content="what do you call a speechless parrot"),
            ]

        Trim chat history based on the message count, keeping the ``SystemMessage`` if
        present, and ensuring that the chat history starts with a ``HumanMessage`` (
        or a ``SystemMessage`` followed by a ``HumanMessage``).

            trim_messages(
                messages,
                # When `len` is passed in as the token counter function,
                # max_tokens will count the number of messages in the chat history.
                max_tokens=4,
                strategy="last",
                # Passing in `len` as a token counter function will
                # count the number of messages in the chat history.
                token_counter=len,
                # Most chat models expect that chat history starts with either:
                # (1) a HumanMessage or
                # (2) a SystemMessage followed by a HumanMessage
                start_on="human",
                # Usually, we want to keep the SystemMessage
                # if it's present in the original history.
                # The SystemMessage has special instructions for the model.
                include_system=True,
                allow_partial=False,
            )

        .. code-block:: python

            [
                SystemMessage(
                    content="you're a good assistant, you always respond with a joke."
                ),
                HumanMessage(content="and who is harrison chasing anyways"),
                AIMessage(
                    content="Hmmm let me think.\n\nWhy, he's probably chasing after "
                    "the last cup of coffee in the office!"
                ),
                HumanMessage(content="what do you call a speechless parrot"),
            ]


        Trim chat history using a custom token counter function that counts the
        number of tokens in each message.

        .. code-block:: python

            messages = [
                SystemMessage("This is a 4 token text. The full message is 10 tokens."),
                HumanMessage(
                    "This is a 4 token text. The full message is 10 tokens.", id="first"
                ),
                AIMessage(
                    [
                        {"type": "text", "text": "This is the FIRST 4 token block."},
                        {"type": "text", "text": "This is the SECOND 4 token block."},
                    ],
                    id="second",
                ),
                HumanMessage(
                    "This is a 4 token text. The full message is 10 tokens.", id="third"
                ),
                AIMessage(
                    "This is a 4 token text. The full message is 10 tokens.",
                    id="fourth",
                ),
            ]


            def dummy_token_counter(messages: list[BaseMessage]) -> int:
                # treat each message like it adds 3 default tokens at the beginning
                # of the message and at the end of the message. 3 + 4 + 3 = 10 tokens
                # per message.

                default_content_len = 4
                default_msg_prefix_len = 3
                default_msg_suffix_len = 3

                count = 0
                for msg in messages:
                    if isinstance(msg.content, str):
                        count += (
                            default_msg_prefix_len
                            + default_content_len
                            + default_msg_suffix_len
                        )
                    if isinstance(msg.content, list):
                        count += (
                            default_msg_prefix_len
                            + len(msg.content) * default_content_len
                            + default_msg_suffix_len
                        )
                return count

        First 30 tokens, allowing partial messages:
            .. code-block:: python

                trim_messages(
                    messages,
                    max_tokens=30,
                    token_counter=dummy_token_counter,
                    strategy="first",
                    allow_partial=True,
                )

            .. code-block:: python

                [
                    SystemMessage(
                        "This is a 4 token text. The full message is 10 tokens."
                    ),
                    HumanMessage(
                        "This is a 4 token text. The full message is 10 tokens.",
                        id="first",
                    ),
                    AIMessage(
                        [{"type": "text", "text": "This is the FIRST 4 token block."}],
                        id="second",
                    ),
                ]

    Úfirstz5start_on parameter is only valid with strategy='last'z;include_system parameter is only valid with strategy='last'Úget_num_tokens_from_messagesrA   r»   rU   c                   s   t ‡ fdd„| D ƒƒS )Nc                 3  s   | ]}ˆ |ƒV  qd S r’   r5   )rW   r4   ©r¾   r5   r6   Ú	<genexpr>ä  rY   z<trim_messages.<locals>.list_token_counter.<locals>.<genexpr>)ÚsumrZ   rÁ   r5   r6   Úlist_token_counterã  s    z)trim_messages.<locals>.list_token_counterz'token_counter' expected to be a model that implements 'get_num_tokens_from_messages()' or a function. Received object of type r/   r	   N©r½   r¾   rº   Úpartial_strategyr·   r²   )r½   r¾   r¶   r¹   r¸   r·   rº   zUnrecognized strategy=z.. Supported strategies are 'last' and 'first'.)rH   r�   r2   rÀ   ÚcallableÚnextÚiterÚinspectÚ	signatureÚ
parametersÚvaluesÚ
annotationr   r.   Ú_HAS_LANGCHAIN_TEXT_SPLITTERSr0   r*   Z
split_textr   Ú_default_text_splitterÚ_first_max_tokensÚ_last_max_tokens)rB   r½   r¾   rµ   r¶   r·   r¸   r¹   rº   r4   rÄ   Ztext_splitter_fnr5   rÁ   r6   Útrim_messages´  sd      !
ÿÿþÿ
ú
ø
rÓ   Ústring)Útext_formatÚ
include_idzEUnion[MessageLikeRepresentation, Sequence[MessageLikeRepresentation]]zLiteral['string', 'block']zUnion[dict, list[dict]])rB   rÕ   rÖ   r-   c                  sb  |dvrd|›d�}t |ƒ‚g }t| tttfƒ }r:| g} t| ƒ} t| ƒD �]\}}dt|ƒi}g }	|jrt|j|d< t|t	ƒr’|j
r’t|j
ƒ|d< |j d¡r¬|jd |d< t|tƒrÀ|j|d< |rÔ|jrÔ|j|d	< |jsî|d
krædng }
�n$t|jtƒ�r |d
k�r|j}
nd|jdœg}
�nò|d
k�rZtdd„ |jD ƒƒ�rZd dd„ |jD ƒ¡}
�n¸g }
t|jƒD �]t\}‰ tˆ tƒ�r’|
 dˆ dœ¡ �qhˆ  d¡dk�rú‡ fdd„d[D ƒ }�rÞd|› d|› d|› dˆ › �}t |ƒ‚|
 ˆ d ˆ d dœ¡ �qhˆ  d¡dk�r^‡ fdd„d\D ƒ }�rFd|› d|› d|› dˆ › �}t |ƒ‚|
 dˆ d dœ¡ �qhtˆ ƒ�r¼tˆ ƒ}| d¡dk�r®d|v �r®d|d v�r®t d¡ d |d d< |
 |¡ �qhˆ  d¡d!k�sÖd!ˆ v �rúˆ  d"¡ ‰�rV‡fd#d„d$D ƒ }�r"d|› d|› d%|› dˆ › �}t |ƒ‚|
 dd&d'ˆd( › d)ˆd › d*ˆd+ › �idœ¡ n ˆ  d!¡ ‰�rØ‡fd,d„d]D ƒ }�r¢d|› d|› d.|› dˆ › �}t |ƒ‚tˆd" d/ ƒ}|
 dd&d0ˆd- › d1|› �idœ¡ nd|› d|› d2ˆ › �}t |ƒ‚�qhˆ  d¡dk�rrtˆ  d¡tƒ�rrtˆ  di ¡ d3¡tƒ�rrˆ  di ¡ d¡d4u �rdt d¡ d ˆ d d< |
 ˆ ¡ �qhˆ  d¡d5k�rÖtˆ  d5¡tƒ�rÖtˆ  d5i ¡ d+¡tƒ�rÖtˆ  d5i ¡ d-¡tƒ�rÖ|
 ˆ ¡ �qhˆ  d¡d6k�rˆ‡ fd7d„d8D ƒ }�r"d|› d|› d9|› dˆ › �}t |ƒ‚t‡ fd:d„td;|ƒj
D ƒƒ�sÜ| dg ¡|d< |d  d<ˆ d	 ˆ d tjˆ d= d>d?�d@œdAœ¡ �qhˆ  d¡dBk�r‡ fdCd„d^D ƒ }�rÔd|› d|› dF|› dˆ › �}t |ƒ‚tˆ dD ˆ dE ˆ  dG¡�ròdHndIdJ�}|	 t|g|dK�¡ �qhˆ  d¡dLk�s,dLˆ v �rrdLˆ v�rTd|› d|› dMˆ › �}t |ƒ‚|
 dt ˆ dL ¡dœ¡ �qhˆ  d¡dNk�sŒdNˆ v �r dNˆ v�s¤dˆ dN v�rÎd|› d|› dO|› d|› dPˆ › �
}t |ƒ‚ˆ dN d }t|tƒ�rî|d }|
 d|dœ¡ nÜˆ  d¡dQk�r¢‡ fdRd„d_D ƒ }�rLd|› d|› dT|› dˆ › �}t |ƒ‚d!ˆ dS v�rpdUˆ dS › �}t |ƒ‚tˆ d+ ƒ}|
 dd&d'ˆ dS › d1|› �idœ¡ n:ˆ  d¡dVv �r¾|
 ˆ ¡ nd|› d|› dWˆ › �}t |ƒ‚�qh|d
k�rtdXd„ |
D ƒƒ�sd dYd„ |
D ƒ¡}
|
|dD< |j�r>|dD �s>|	�r>| |	¡ qJ| |g|	¢¡ qJ|�r^|dZ S |S )`a’  Convert LangChain messages into OpenAI message dicts.

    Args:
        messages: Message-like object or iterable of objects whose contents are
            in OpenAI, Anthropic, Bedrock Converse, or VertexAI formats.
        text_format: How to format string or text block contents:
                - ``'string'``:
                    If a message has a string content, this is left as a string. If
                    a message has content blocks that are all of type ``'text'``, these
                    are joined with a newline to make a single string. If a message has
                    content blocks and at least one isn't of type ``'text'``, then
                    all blocks are left as dicts.
                - ``'block'``:
                    If a message has a string content, this is turned into a list
                    with a single content block of type ``'text'``. If a message has
                    content blocks these are left as is.
        include_id: Whether to include message ids in the openai messages, if they
                    are present in the source messages.

    Raises:
        ValueError: if an unrecognized ``text_format`` is specified, or if a message
            content block is missing expected keys.

    Returns:
        The return type depends on the input type:

        - dict:
          If a single message-like object is passed in, a single OpenAI message
          dict is returned.
        - list[dict]:
          If a sequence of message-like objects are passed in, a list of OpenAI
          message dicts is returned.

    Example:

        .. code-block:: python

            from langchain_core.messages import (
                convert_to_openai_messages,
                AIMessage,
                SystemMessage,
                ToolMessage,
            )

            messages = [
                SystemMessage([{"type": "text", "text": "foo"}]),
                {
                    "role": "user",
                    "content": [
                        {"type": "text", "text": "whats in this"},
                        {
                            "type": "image_url",
                            "image_url": {"url": "data:image/png;base64,'/9j/4AAQSk'"},
                        },
                    ],
                },
                AIMessage(
                    "",
                    tool_calls=[
                        {
                            "name": "analyze",
                            "args": {"baz": "buz"},
                            "id": "1",
                            "type": "tool_call",
                        }
                    ],
                ),
                ToolMessage("foobar", tool_call_id="1", name="bar"),
                {"role": "assistant", "content": "thats nice"},
            ]
            oai_messages = convert_to_openai_messages(messages)
            # -> [
            #   {'role': 'system', 'content': 'foo'},
            #   {'role': 'user', 'content': [{'type': 'text', 'text': 'whats in this'}, {'type': 'image_url', 'image_url': {'url': "data:image/png;base64,'/9j/4AAQSk'"}}]},
            #   {'role': 'assistant', 'tool_calls': [{'type': 'function', 'id': '1','function': {'name': 'analyze', 'arguments': '{"baz": "buz"}'}}], 'content': ''},
            #   {'role': 'tool', 'name': 'bar', 'content': 'foobar'},
            #   {'role': 'assistant', 'content': 'thats nice'}
            # ]

    .. versionadded:: 0.3.11

    >   rÔ   ÚblockzUnrecognized text_format=z&, expected one of 'string' or 'block'.rG   rj   rl   Zrefusalrk   rm   rÔ   r…   rI   )r.   rI   c                 s  s&   | ]}t |tƒp| d ¡dkV  qdS ©r.   rI   N)r0   r+   r   ©rW   r×   r5   r5   r6   rÂ   ‰  s   ÿz-convert_to_openai_messages.<locals>.<genexpr>rF   c                 s  s$   | ]}t |tƒr|n|d  V  qdS ©rI   N©r0   r+   rÙ   r5   r5   r6   rÂ   �  s   ÿr.   c                   s   g | ]}|ˆ vr|‘qS r5   r5   ©rW   r`   ©r×   r5   r6   rX   ˜  rY   z.convert_to_openai_messages.<locals>.<listcomp>z'Unrecognized content block at messages[z
].content[z4] has 'type': 'text' but is missing expected key(s) z. Full content block:

Ú	image_urlc                   s   g | ]}|ˆ vr|‘qS r5   r5   rÜ   rÝ   r5   r6   rX   ¢  rY   z9] has 'type': 'image_url' but is missing expected key(s) )r.   rÞ   ÚfileÚfilenamezGenerating a fallback filename.ZLC_AUTOGENERATEDÚimageÚsourcec                   s   g | ]}|ˆ vr|‘qS r5   r5   rÜ   )râ   r5   r6   rX   ¿  s   )Ú
media_typer.   rP   z>] has 'type': 'image' but 'source' is missing expected key(s) Úurlzdata:rã   ú;ú,rP   c                   s   g | ]}|ˆ vr|‘qS r5   r5   rÜ   )rá   r5   r6   rX   Ö  s   Úformatz:] has key 'image', but 'image' is missing expected key(s) Úbyteszdata:image/z;base64,zX] has 'type': 'image' but does not have a 'source' or 'image' key. Full content block:

Z	file_dataNZinput_audior¤   c                   s   g | ]}|ˆ vr|‘qS r5   r5   rÜ   rÝ   r5   r6   rX     s   )rm   rj   Úinputz9] has 'type': 'tool_use', but is missing expected key(s) c                 3  s   | ]}|d  ˆ d  kV  qdS )rm   Nr5   r¢   rÝ   r5   r6   rÂ     s   ÿr   r=   ré   F©Úensure_ascii©rj   ro   ©r.   rm   r=   Ztool_resultc                   s   g | ]}|ˆ vr|‘qS r5   r5   rÜ   rÝ   r5   r6   rX   "  s   ri   Útool_use_idz<] has 'type': 'tool_result', but is missing expected key(s) Zis_errorÚerrorÚsuccess)rk   ry   )rÕ   r}   zJ] has 'type': 'json' but does not have a 'json' key. Full content block:

Zguard_contentz;] has 'type': 'guard_content' but does not have a messages[z5]['guard_content']['text'] key. Full content block:

Zmediac                   s   g | ]}|ˆ vr|‘qS r5   r5   rÜ   rÝ   r5   r6   rX   Y  rY   Ú	mime_typez/] has 'type': 'media' but does not have key(s) zaOpenAI messages can only support text and image data. Received content block with media of type: )ZthinkingZ	reasoningz_] does not match OpenAI, Anthropic, Bedrock Converse, or VertexAI format. Full content block:

c                 s  s   | ]}|d  dkV  qdS rØ   r5   rÙ   r5   r5   r6   rÂ   {  s   c                 s  s   | ]}|d  V  qdS rÚ   r5   rÙ   r5   r5   r6   rÂ   ~  rY   r   )rI   )rÞ   )râ   rç   )ri   rî   )rñ   rP   ) rH   r0   r   r1   r+   r�   Ú	enumerateÚ_get_message_openai_rolerj   r   rl   Ú_convert_to_openai_tool_callsrJ   r   r$   rk   rm   ri   ÚallrL   rK   r   r   ÚloggerÚinfoÚ_bytes_to_b64_strÚanyr   r}   Údumpsr­   Úconvert_to_openai_messages)rB   rÕ   rÖ   ÚerrZoai_messagesZ	is_singleÚirN   Zoai_msgZtool_messagesri   ÚjÚmissingZformatted_blockZ	b64_imager4   Ztool_messagerI   r5   )r×   rá   râ   r6   rû     sR   X





þ

þÿÿýýÿÿÿýýÿþÿ
ÿþ
ý

ÿÿÿýýÿÿÿþþÿ
ÿÿÿýýÿþþÿÿÿýÿÿþý
ÿþýü
ÿÿÿýýÿ
þ
ÿþýÿ
ÿÿÿýýÿýÿÿ
ÿÿýÿþÿÿ
þÿÿýýüÿÿÿþýÿþÿÿþÿÿÿýÿÿ
rû   )rÆ   r·   z"Callable[[list[BaseMessage]], int]zCallable[[str], list[str]]z"Optional[Literal['first', 'last']])rB   r½   r¾   rº   rÆ   r·   r-   c                C  s¶  t | ƒ} | s| S || ƒ|krR|rNtt| ƒƒD ] }t| d |ƒsH|  ¡  q, qNq,| S dt| ƒ }}t| ƒ ¡ }	t|	ƒD ]F}||kr„ q¼|| d d }
|| d |
… ƒ|kr²|
}|
}qt|
d }qt|}|�rf|t| ƒk �rfd}d}t| | jt ƒ�r¬| | jdd�}d}t|jƒ}|dk�r&t t	|jƒƒ|_td|ƒD ]Z}|jd d… |_|g | d |… ¢|‘ƒ|k�r0g | d |… ¢|‘} |d7 }d} �qŒ�q0|�r¬|dk�r¬t t	|jƒƒ|_|�sf|�sÌ| | jdd�}d}d }t|jt
ƒ�ræ|j}nft|jt ƒ�rL|j�rL|jD ]H}t|t
ƒ�r|} �qLt|tƒ�r| d	¡d
k�r| d
¡} �qL�q|�rf|�sd|jdd�}||ƒ}|| d |… ƒ}|dk�r’t t	|ƒƒ}dt|ƒ }}t|ƒ ¡ }	t|	ƒD ]\}||k�rÈ �q|| d d }
d |d |
… ¡|_|||gƒ |k�r|
}n|
d }�q´|dk�rf|d |… }|dk�r>t t	|ƒƒ}d |¡|_g | d |… ¢|‘} |d7 }|�rªt|ƒD ]4}|dk�r t| |d  |ƒ�s |d8 }n �qª�qt| d |… S )Néÿÿÿÿr   r^   r„   FT)Údeepr²   r.   rI   r…   )r¦   Úranger†   r¥   r{   Ú
bit_lengthr0   ri   r©   Úreversedr+   r1   r   rL   )rB   r½   r¾   rº   rÆ   r·   Ú_ÚleftÚrightZmax_iterationsZmidÚidxZincluded_partialÚcopiedZexcludedZ	num_blockrI   r×   Zsplit_textsZbase_message_countZcontent_splitsr5   r5   r6   rÑ   Š  s¨    













rÑ   )r¶   r¹   r¸   r·   )	rB   r½   r¾   rº   r¶   r¹   r¸   r·   r-   c                C  sê   t | ƒ} t| ƒdkrg S |rJtt| ƒƒD ] }t| d |ƒsD|  ¡  q( qJq(d }	|r€t| ƒdkr€t| d tƒr€| d }	| dd … } | d d d… }
|}|	r®||	gƒ}td|| ƒ}t|
||||rÀdnd |d�}|d d d… }|	ræ|	g|¢}|S )Nr   r   r^   r²   rÅ   )	r¦   r†   r  r¥   r{   r0   r!   ÚmaxrÑ   )rB   r½   r¾   rº   r¶   r¹   r¸   r·   r  Zsystem_messageZreversed_messagesZremaining_tokensZsystem_tokensZreversed_resultÚresultr5   r5   r6   rÒ      s:    


ú

rÒ   z/dict[type[BaseMessage], type[BaseMessageChunk]]Ú_MSG_CHUNK_MAPc                 C  s   i | ]\}}||“qS r5   r5   r_   r5   r5   r6   rb   C  rY   rb   r   c                 C  s”   | j tv r(t| j  f i | jdhd�¤ŽS t ¡ D ].\}}t| |ƒr0|f i | jdhd�¤Ž  S q0d| j › dtt ¡ ƒ› �}t|tj	d�}t
|ƒ‚d S )Nr.   ©ÚexcludezUnrecognized message class ú. Supported classes are rz   )rc   r  Ú
model_dumprf   r0   r¦   Úkeysr   r   r€   rH   )rN   Úmsg_clsÚ	chunk_clsr4   r5   r5   r6   r®   F  s    



ÿÿr®   c                 C  s˜   | j tv r*t| j  f i | jddhd�¤ŽS t ¡ D ]0\}}t| |ƒr2|f i | jddhd�¤Ž  S q2d| j › dtt ¡ ƒ› �}t|tj	d�}t
|ƒ‚d S )Nr.   r]   r  z!Unrecognized message chunk class r  rz   )rc   Ú_CHUNK_MSG_MAPr  rf   r0   r¦   r  r   r   r€   rH   )r\   r  r  r4   r5   r5   r6   r°   V  s    
ÿ
 

ÿÿr°   z	list[str])rI   r-   c                 C  s,   |   d¡}dd„ |d d… D ƒ|dd …  S )NrF   c                 S  s   g | ]}|d  ‘qS )rF   r5   )rW   Úsr5   r5   r6   rX   i  rY   z*_default_text_splitter.<locals>.<listcomp>r   )Úsplit)rI   Úsplitsr5   r5   r6   rÐ   g  s    
rÐ   zFUnion[str, type[BaseMessage], Sequence[Union[str, type[BaseMessage]]]])rN   rR   r-   c                 C  sL   t |ttfƒr|gn|}dd„ |D ƒ}tdd„ |D ƒƒ}| j|v pJt | |ƒS )Nc                 S  s   g | ]}t |tƒr|‘qS r5   rÛ   ©rW   Útr5   r5   r6   rX   q  rY   z$_is_message_type.<locals>.<listcomp>c                 s  s   | ]}t |tƒr|V  qd S r’   )r0   r.   r  r5   r5   r6   rÂ   r  rY   z#_is_message_type.<locals>.<genexpr>)r0   r+   r.   r§   )rN   rR   ÚtypesZ	types_strZtypes_typesr5   r5   r6   r¥   l  s    r¥   rè   )Úbytes_r-   c                 C  s   t  | ¡ d¡S )Nzutf-8)Úbase64Ú	b64encodeÚdecode)r  r5   r5   r6   rø   w  s    rø   c                 C  sz   t | tƒrdS t | tƒrdS t | tƒr*dS t | tƒrB| j dd¡S t | tƒrPdS t | tƒr`| j	S d| j
› d�}t|ƒ‚d S )	Nru   rs   r>   rw   r<   r=   zUnknown BaseMessage type r/   )r0   r   r   r$   r!   rJ   r   r   r   rG   rc   rH   )rN   r4   r5   r5   r6   ró   {  s    





ró   zlist[ToolCall]z
list[dict])rl   r-   c                 C  s   dd„ | D ƒS )Nc              	   S  s4   g | ],}d |d |d t j|d dd�dœdœ‘qS )r=   rm   rj   rr   Frê   rì   rí   )r}   rú   r¢   r5   r5   r6   rX   �  s   	ùþýz1_convert_to_openai_tool_calls.<locals>.<listcomp>r5   )rl   r5   r5   r6   rô   Œ  s    	÷rô   g      @g      @)Úchars_per_tokenÚextra_tokens_per_messageÚ
count_namez#Iterable[MessageLikeRepresentation]Úfloat)rB   r  r   r!  r-   c          
      C  sâ   d}t | ƒD ]Ê}d}t|jtƒr0|t|jƒ7 }nt|jƒ}|t|ƒ7 }t|tƒrxt|jtƒsx|jrxt|jƒ}|t|ƒ7 }t|t	ƒr�|t|j
ƒ7 }t|ƒ}	|t|	ƒ7 }|jr¼|r¼|t|jƒ7 }|t || ¡7 }||7 }qt |¡S )aè  Approximate the total number of tokens in messages.

    The token count includes stringified message content, role, and (optionally) name.
    - For AI messages, the token count also includes stringified tool calls.
    - For tool messages, the token count also includes the tool call ID.

    Args:
        messages: List of messages to count tokens for.
        chars_per_token: Number of characters per token to use for the approximation.
            Default is 4 (one token corresponds to ~4 chars for common English text).
            You can also specify float values for more fine-grained control.
            `See more here. <https://platform.openai.com/tokenizer>`__
        extra_tokens_per_message: Number of extra tokens to add per message.
            Default is 3 (special tokens, including beginning/end of message).
            You can also specify float values for more fine-grained control.
            `See more here. <https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb>`__
        count_name: Whether to include message names in the count.
            Enabled by default.

    Returns:
        Approximate number of tokens in the messages.

    .. note::
        This is a simple approximation that may not match the exact token count used by
        specific models. For accurate counts, use model-specific tokenizers.

    Warning:
        This function does not currently support counting image tokens.

    .. versionadded:: 0.3.46

    g        r   )r�   r0   ri   r+   r†   Úreprr   r¦   rl   r$   rk   ró   rj   ÚmathÚceil)
rB   r  r   r!  Ztoken_countrN   Zmessage_charsri   Ztool_calls_contentrG   r5   r5   r6   Úcount_tokens_approximatelyš  s0    '
ÿ
ýü



r&  )r?   r@   )NNNN)gr™   Ú
__future__r   r  rÊ   r}   Úloggingr$  Úcollections.abcr   r   Ú	functoolsr   Útypingr   r   r   r	   r
   r   r   r   r   Zpydanticr   r   r   Zlangchain_core.exceptionsr   r   Zlangchain_core.messagesr   r   Zlangchain_core.messages.air   r   Zlangchain_core.messages.baser   r   Zlangchain_core.messages.chatr   r   Z langchain_core.messages.functionr   r   Zlangchain_core.messages.humanr   r   Z langchain_core.messages.modifierr    Zlangchain_core.messages.systemr!   r"   Zlangchain_core.messages.toolr#   r$   r%   Zlangchain_core.language_modelsr&   rŽ   r(   r–   r)   Zlangchain_text_splittersr*   rÏ   ÚImportErrorÚ	getLoggerr—   rö   r7   Z
AnyMessagerO   rS   r[   rg   r¦   r+   r§   r1   rƒ   r‚   r�   r�   rš   r«   r´   rÓ   rû   rÑ   rÒ   r  Ú__annotations__rf   r  r®   r°   rÐ   r¥   rø   ró   rô   r&  r5   r5   r5   r6   Ú<module>   sö   
,

õÿòÿ ÿ8  ÿ    ú[8÷$ ý 
î&  _ü   ÷|ó"<úû