o
    +i                     @  sf   d Z ddlmZ ddlZddlmZ dZdZdZdddZ	dddZ
d ddZd!ddZd"ddZdS )#z<Experimental tool-calling support for Anthropic chat models.    )annotationsN)Anyak  In this environment you have access to a set of tools you can use to answer the user's question.

You may call them like this:
<function_calls>
<invoke>
<tool_name>$TOOL_NAME</tool_name>
<parameters>
<$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME>
...
</parameters>
</invoke>
</function_calls>

Here are the tools available:
<tools>
{formatted_tools}
</tools>z<tool_description>
<tool_name>{tool_name}</tool_name>
<description>{tool_description}</description>
<parameters>
{formatted_parameters}
</parameters>
</tool_description>z<parameter>
<name>{parameter_name}</name>
<type>{parameter_type}</type>
<description>{parameter_description}</description>
</parameter>	parameterdict[str, Any]returnstrc                 C  sN   d| v r| d S d| v rt d| d iS d| v r"t d| d iS t | S )NtypeanyOfallOf)jsondumps)r    r   ]/var/www/html/psymed-ai/venv/lib/python3.10/site-packages/langchain_anthropic/experimental.py	_get_type+   s   
r   tools
list[dict]c                 C  s.   dd | D }d dd |D }tj|dS )z=Generate a system message that describes the available tools.c              	   S  s<   g | ]}|d  |d d dd |d d  D dqS )namedescription
c                 S  s*   g | ]\}}t j|t||d dqS )r   )parameter_nameparameter_typeparameter_description)TOOL_PARAMETER_FORMATformatr   get).0r   r   r   r   r   
<listcomp><   s    z1get_system_message.<locals>.<listcomp>.<listcomp>
parameters
properties	tool_nametool_descriptionformatted_parameters)joinitemsr   toolr   r   r   r   7   s    z&get_system_message.<locals>.<listcomp>r   c                 S  s(   g | ]}t j|d  |d |d dqS )r    r!   r"   r   )TOOL_FORMATr   r%   r   r   r   r   I   s    )formatted_tools)r#   SYSTEM_PROMPT_FORMATr   )r   
tools_datatools_formattedr   r   r   get_system_message5   s   
r,   tr   str | dict[str, Any]c                 C  sx   t | dkr| jp
dS i }| D ]*}|j|vrt|||j< qt||j ts/||j g||j< ||j t| q|S )Nr    )lentexttag_xml_to_dict
isinstancelistappend)r-   dchildr   r   r   r3   U   s   

r3   invokec                   s  |  dj t|  d} fdd|D }t|dkryt|tsy|d }| D ]O\}}||d d v rxd|d d | v rx|d d | d dkrUt|tsU|g||< |d d | d d	krxt|trxt|	 d
krxt
t| ||< q) t|dddS )Nr    r   c                   s   g | ]
}|d   kr|qS r   r   r%   r:   r   r   r   l   s    z)_xml_to_function_call.<locals>.<listcomp>r   r   r   arrayobject   )r   	argumentsfunction)r?   r   )findr1   r3   r0   r4   r   r$   r5   dictkeysnextitervaluesr   r   )r9   r   r>   filtered_toolsr&   keyvaluer   r:   r   _xml_to_function_callg   s6   
rI   elemlist[dict[str, Any]]c                   s   |  d} fdd|D S )zJConvert an XML element and its children into a dictionary of dictionaries.r9   c                   s   g | ]}t | qS r   )rI   )r   r9   r   r   r   r      s    z&_xml_to_tool_calls.<locals>.<listcomp>)findall)rJ   r   invokesr   rL   r   _xml_to_tool_calls   s   
rO   )r   r   r   r   )r   r   r   r   )r-   r   r   r.   )r9   r   r   r   r   r   )rJ   r   r   r   r   rK   )__doc__
__future__r   r   typingr   r)   r'   r   r   r,   r3   rI   rO   r   r   r   r   <module>   s    



 
!