a
    dg                     @   s   d Z ddlZddlZ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 ddlmZ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 edddZeedddZG dd deZG dd deZG dd deZdS )z)A tool for running python code in a REPL.    N)redirect_stdout)StringIO)AnyDictOptionalType)AsyncCallbackManagerForToolRunCallbackManagerForToolRun)run_in_executor)BaseTool)	BaseModelFieldmodel_validator)
PythonREPL)returnc                   C   s   t t d dS )N)Z_globalsZ_locals)r   globals r   r   v/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_experimental/tools/python/tool.py_get_default_python_repl   s    r   )queryr   c                 C   s    t dd| } t dd| } | S )zSanitize input to the python REPL.

    Remove whitespace, backtick & python (if llm mistakes python console as terminal)

    Args:
        query: The query to sanitize

    Returns:
        str: The sanitized query
    z^(\s|`)*(?i:python)?\s* z(\s|`)*$)resub)r   r   r   r   sanitize_input   s    r   c                   @   sx   e Zd ZU dZdZeed< dZeed< ee	dZ
eed< dZeed	< deee edddZdeee edddZd
S )PythonREPLTool'Tool for running python code in a REPL.ZPython_REPLnamezA Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.descriptiondefault_factorypython_replTr   Nr   run_managerr   c                 C   s   | j rt |}| j|S )Use the tool.)r   r    runselfr   r"   r   r   r   _run9   s    zPythonREPLTool._runc                    s"   | j rt |}td| j|I dH S zUse the tool asynchronously.N)r   r
   r$   r%   r   r   r   _arunC   s    zPythonREPLTool._arun)N)N)__name__
__module____qualname____doc__r   str__annotations__r   r   r   r    r   r   boolr   r	   r   r'   r   r)   r   r   r   r   r   ,   s$   

  r   c                   @   s$   e Zd ZU dZeddZeed< dS )PythonInputszPython inputs.zcode snippet to run)r   r   N)r*   r+   r,   r-   r   r   r.   r/   r   r   r   r   r1   O   s   
r1   c                   @   s   e Zd ZU dZdZeed< dZeed< ee	dZ
ee ed< ee	dZee ed< d	Zeed
< eZee ed< eddeeedddZdeee edddZdeee edddZdS )PythonAstREPLToolr   Zpython_repl_astr   zA Python shell. Use this to execute python commands. Input should be a valid python command. When using this tool, sometimes output is abbreviated - make sure it does not look abbreviated before using it in your answer.r   r   r   localsTr   args_schemabefore)mode)valuesr   c                 C   s   t jdk rtdt j |S )zValidate valid python version.)   	   zuThis tool relies on Python 3.9 or higher (as it uses new functionality in the `ast` module, you have Python version: )sysversion_info
ValueErrorversion)clsr7   r   r   r   validate_python_versiond   s    
z)PythonAstREPLTool.validate_python_versionNr!   c           
      C   s  z@| j rt |}t|}tj|jdd g d}tt|| j| j tj|jdd g d}t|}t	 }ztt
|X t|| j| j}|du r| W  d   W W S |W  d   W W S W d   n1 s0    Y  W nX ty>   t
|  t|| j| j W d   n1 s&0    Y  |  Y W S 0 W n< ty~ }	 z"dt|	jt|	W  Y d}	~	S d}	~	0 0 dS )r#   N)type_ignoresz{}: {})r   astparseModulebodyexecunparser   r3   r   r   evalgetvalue	Exceptionformattyper*   r.   )
r&   r   r"   treemoduleZ
module_endZmodule_end_strZ	io_bufferreter   r   r   r'   p   s*    


8
0zPythonAstREPLTool._runc                    s   t d| j|I dH S r(   )r
   r'   r%   r   r   r   r)      s    zPythonAstREPLTool._arun)N)N)r*   r+   r,   r-   r   r.   r/   r   r   dictr   r   r   r3   r   r0   r1   r4   r   r   r   classmethodr   r?   r	   r'   r   r)   r   r   r   r   r2   U   s.   

   r2   ) r-   rB   r   r:   
contextlibr   ior   typingr   r   r   r   Z langchain_core.callbacks.managerr   r	   Zlangchain_core.runnables.configr
   Zlangchain_core.toolsr   Zpydanticr   r   r   Z'langchain_experimental.utilities.pythonr   r   r.   r   r   r1   r2   r   r   r   r   <module>   s    #