o
    +iv                     @  s   d 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
 ddlmZ ddlmZmZ ddlmZmZ G d	d
 d
eZdddZdddZdS )z(Output parsers for Anthropic tool calls.    )annotations)Anycast)	AIMessageToolCall)	tool_call)BaseGenerationOutputParser)ChatGeneration
Generation)	BaseModel
ConfigDictc                   @  s`   e Zd ZU dZdZded< 	 dZded< 	 dZded< 	 ed	d
Z	dddddZ
dddZdS )ToolsOutputParserzOutput parser for tool calls.Fboolfirst_tool_only	args_onlyNzlist[type[BaseModel]] | Nonepydantic_schemasforbid)extra)partialresultlist[Generation]r   returnr   c                  s   |r	t |d tsjrdS g S td|d j}dd t|D }t |jtr:dd t|jD   fdd|D }j	rGfd	d|D }nj
rRd
d |D }n	 jr^|r\|d S dS t|S )a  Parse a list of candidate model Generations into a specific format.

        Args:
            result: A list of `Generation` to be parsed. The Generations are assumed
                to be different candidate outputs for a single model input.
            partial: (Not used) Whether the result is a partial result. If `True`, the
                parser may return a partial result, which may not be complete or valid.

        Returns:
            Structured output.

        r   Nr   c                 S  s   g | ]}t |qS  )dict.0tcr   r   _/var/www/html/psymed-ai/venv/lib/python3.10/site-packages/langchain_anthropic/output_parsers.py
<listcomp>,   s    z2ToolsOutputParser.parse_result.<locals>.<listcomp>c                 S  s0   i | ]\}}t |tr|d  dkr|d |qS )typetool_useid)
isinstancer   )r   iblockr   r   r   
<dictcomp>1   s    z2ToolsOutputParser.parse_result.<locals>.<dictcomp>c                   s$   g | ]}i |d  |d  iqS )indexr!   r   r   )id_to_indexr   r   r   6   s   $ c                   s   g | ]}  |qS r   )_pydantic_parser   )selfr   r   r   8   s    c                 S  s   g | ]}|d  qS )argsr   r   r   r   r   r   :       )r"   r	   r   r   message _extract_tool_calls_from_messagecontentlist	enumerater   r   )r)   r   r   r,   
tool_callsr   )r'   r)   r   parse_result   s&   zToolsOutputParser.parse_resultr   r   r   c                 C  s.   dd | j pg D |d  }|di |d S )Nc                 S  s   i | ]}|j |qS r   )__name__)r   schemar   r   r   r%   C   r+   z5ToolsOutputParser._pydantic_parse.<locals>.<dictcomp>namer*   r   )r   )r)   r   cls_r   r   r   r(   B   s   z!ToolsOutputParser._pydantic_parse)r   r   r   r   r   r   )r   r   r   r   )r3   
__module____qualname____doc__r   __annotations__r   r   r   model_configr2   r(   r   r   r   r   r      s   
 &r   r,   r   r   list[ToolCall]c                 C  s   | j r| j S t| jS )1Extract tool calls from a list of content blocks.)r1   extract_tool_callsr.   )r,   r   r   r   r-   I   s   
r-   r.   str | list[str | dict]c                 C  sZ   t | tr+g }| D ]}t |trq	|d dkrq	|t|d |d |d d q	|S g S )r=   r   r    r5   inputr!   )r5   r*   r!   )r"   r/   strappendr   )r.   r1   r$   r   r   r   r>   P   s   

r>   N)r,   r   r   r<   )r.   r?   r   r<   )r9   
__future__r   typingr   r   langchain_core.messagesr   r   langchain_core.messages.toolr   langchain_core.output_parsersr   langchain_core.outputsr	   r
   pydanticr   r   r   r-   r>   r   r   r   r   <module>   s    
;