a
    dg}                      @   s   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	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 d d	lmZ d d
lmZmZmZ d dlmZ dZG dd deZeedddZeddddG dd deZ dS )    N)defaultdict)
HTMLParser)AnyDefaultDictDictListOptionalcast)ChatGeneration
ChatResult)ChatAnthropic)
deprecated)CallbackManagerForLLMRun)BaseChatModel)	AIMessageBaseMessageSystemMessage)model_validatora  In addition to responding, you can use tools. You have access to the following tools.

{tools}

In order to use a tool, you can use <tool></tool> to specify the name, and the <tool_input></tool_input> tags to specify the parameters. Each parameter should be passed in as <$param_name>$value</$param_name>, Where $param_name is the name of the specific parameter, and $value is the value for that parameter.

You will then get back a response in the form <observation></observation>
For example, if you have a tool called 'search' that accepts a single parameter 'query' that could run a google search, in order to search for the weather in SF you would respond:

<tool>search</tool><tool_input><query>weather in SF</query></tool_input>
<observation>64 degrees</observation>c                       sX   e Zd ZdZdd fddZeeddddZedd	d
dZeddddZ	  Z
S )	TagParserzParser for the tool tags.Nreturnc                    s4   t    tt| _| jg| _d| _d| _d| _dS )a  A heavy-handed solution, but it's fast for prototyping.

        Might be re-implemented later to restrict scope to the limited grammar, and
        more efficiency.

        Uses an HTML parser to parse a limited grammar that allows
        for syntax of the form:

            INPUT -> JUNK? VALUE*
            JUNK -> JUNK_CHARACTER+
            JUNK_CHARACTER -> whitespace | ,
            VALUE -> <IDENTIFIER>DATA</IDENTIFIER> | OBJECT
            OBJECT -> <IDENTIFIER>VALUE+</IDENTIFIER>
            IDENTIFIER -> [a-Z][a-Z0-9_]*
            DATA -> .*

        Interprets the data to allow repetition of tags and recursion
        to support representation of complex types.

        ^ Just another approximately wrong grammar specification.
        Tr   N)	super__init__r   list
parse_datastacksuccessdepthdataself	__class__ }/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_experimental/llms/anthropic_functions.pyr   .   s    


zTagParser.__init__)tagattrsr   c                 C   s(   |  j d7  _ | jtt d| _dS )z#Hook when a new tag is encountered.   N)r   r   appendr   r   r   )r    r%   r&   r#   r#   r$   handle_starttagL   s    zTagParser.handle_starttag)r%   r   c                 C   sT   |  j d8  _ t| jd}| jdu}|r2| jn|}| jd | | d| _dS )zHook when a tag is closed.r'   N)r   dictr   popr   r(   )r    r%   Ztop_of_stackZis_leafvaluer#   r#   r$   handle_endtagR   s    
zTagParser.handle_endtag)r   r   c                 C   s.   |  }| jdkr |dvr d| _|r*|| _dS )zHook when handling data.r   ), FN)stripr   r   r   )r    r   Zstripped_datar#   r#   r$   handle_datab   s
    zTagParser.handle_data)__name__
__module____qualname____doc__r   strr   r)   r.   r2   __classcell__r#   r#   r!   r$   r   +   s
   r   )
tool_inputr   c                 C   sx   t | trdd |  D S t | trpt | d trNt| dkrH| d S tqtt | d trjdd | D S tntd S )Nc                 S   s   i | ]\}}|t |qS r#   _destrip).0kvr#   r#   r$   
<dictcomp>o       z_destrip.<locals>.<dictcomp>r   r'   c                 S   s   g | ]}t |qS r#   r:   )r<   r>   r#   r#   r$   
<listcomp>w   r@   z_destrip.<locals>.<listcomp>)
isinstancer+   itemsr   r7   len
ValueError)r9   r#   r#   r$   r;   m   s    

r;   z0.0.54z1.0z3langchain_anthropic.experimental.ChatAnthropicTools)ZsinceZremovalZalternative_importc                   @   s   e Zd ZU dZeed< eddeee	dddZ
eedd	d
Zdee eee  ee e	edddZeedddZdS )AnthropicFunctionsz4Chat model for interacting with Anthropic functions.llmbefore)mode)valuesr   c                 C   s    | dptf i ||d< |S )NrG   )getr   )clsrJ   r#   r#   r$   validate_environment   s    z'AnthropicFunctions.validate_environmentr   c                 C   s   | j S )zFor backwards compatibility.)rG   r   r#   r#   r$   model   s    zAnthropicFunctions.modelN)messagesstoprun_managerkwargsr   c                 K   s  d}d}d|v rd|v r(|d }|d= nd}|dkrbt jtj|d ddd	}t|d
}|g| }t|trd}|d }	|td|	 dd
 |d= |d u rdg}q|d nd|v rt	d| j
j|f||d|}
tt|
j}|rbt }d|v r.|| d  |jd d }tt|}n|}d}d|	|di}td|d}tt|dgdS d|v rt }|| d  |dd  }|jd d }d|jd d tt|di}t||d}tt|dgdS tt|
j |
_tt|
dgdS d S )NFr0   Z	functionsfunction_callautonone   )indent)Ztools)contentTnamez<tool>z</tool>z</tool_input>z5if `function_call` provided, `functions` must also be)rP   	callbacksz<tool_input>r9   r   )rY   	arguments)rX   Zadditional_kwargs)message)ZgenerationsZtool)promptformatjsondumpsr   rB   r+   r(   r   rE   rN   Zinvoker	   r7   rX   r   feedr1   r   r;   r   r
   split)r    rO   rP   rQ   rR   ZforcedrS   rX   systemZfunction_call_nameresponse
completionZ
tag_parserZv1r[   r\   msgr#   r#   r$   	_generate   sx    




zAnthropicFunctions._generatec                 C   s   dS )NZanthropic_functionsr#   r   r#   r#   r$   	_llm_type   s    zAnthropicFunctions._llm_type)NN)r3   r4   r5   r6   r   __annotations__r   classmethodr   r   rM   propertyrN   r   r   r   r7   r   r   rg   rh   r#   r#   r#   r$   rF   ~   s$   
  
OrF   )!r_   collectionsr   html.parserr   typingr   r   r   r   r   r	   Zlangchain.schemar
   r   Z)langchain_community.chat_models.anthropicr   Zlangchain_core._api.deprecationr   Z langchain_core.callbacks.managerr   Zlangchain_core.language_modelsr   Zlangchain_core.messagesr   r   r   Zpydanticr   r]   r   r;   rF   r#   r#   r#   r$   <module>   s&    B