a
    bg                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	 d dl
Z
d dlmZ d dlmZmZ eedddZeeee d	d
dZeedddZG dd deZdZG dd deZG dd dZdS )    N)Path)DictListType)Tool)	BaseModelField)	md_stringreturnc                 C   s&   t jdd| t jd}t dd|}|S )z"Strip markdown code from a string.z^`{1,3}.*?\n )flagsz`{1,3}$)resubDOTALL)r	   Zstripped_string r   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/tools/bearly/tool.pystrip_markdown_code   s    r   )pathnr
   c                 C   sb   zFt | d(}dd t||D W  d   W S 1 s:0    Y  W n ty\   g  Y S 0 dS )z Get the first n lines of a file.rc                 S   s   g | ]}t |qS r   )str).0liner   r   r   
<listcomp>       zhead_file.<locals>.<listcomp>N)open	itertoolsislice	Exception)r   r   fr   r   r   	head_file   s
    :r    )r   r
   c                 C   s@   t | d"}t|  W  d   S 1 s20    Y  dS )zConvert a file to base64.rbN)r   base64	b64encodereaddecode)r   r   r   r   r   file_to_base64   s    r&   c                   @   s*   e Zd ZU dZeddgddZeed< dS )BearlyInterpreterToolArgumentsz(Arguments for the BearlyInterpreterTool..zprint('Hello World')zmThe pure python script to be evaluated. The contents will be in main.py. It should not be in markdown format.)Zexamplesdescriptionpython_codeN)__name__
__module____qualname____doc__r   r)   r   __annotations__r   r   r   r   r'   #   s   
r'   a  Evaluates python code in a sandbox environment. The environment resets on every execution. You must send the whole script every time and print your outputs. Script should be pure python code that can be evaluated. It should be in python format NOT markdown. The code should NOT be wrapped in backticks. All python packages including requests, matplotlib, scipy, numpy, pandas, etc are available. If you have any files outputted write them to "output/" relative to the execution path. Output can only be read from the directory, stdout, and stdin. Do not use things like plot.show() as it will not work instead write them out `output/` and a link to the file will be returned. print() any output and results so you can capture the output.c                   @   s*   e Zd ZU dZeed< eed< eed< dS )FileInfoz(Information about a file to be uploaded.source_pathr(   target_pathN)r*   r+   r,   r-   r   r.   r   r   r   r   r/   @   s   
r/   c                   @   s   e Zd ZU dZeed< dZeed< dZeed< eZ	e
e ed< i Zeeef ed< ed	d
dZeedddZeedddZee dddZeedddZeedddZeeeddddZddddZeddd ZdS )!BearlyInterpreterToolz9Tool for evaluating python code in a sandbox environment.api_key%https://exec.bearly.ai/v1/interpreterendpointZbearly_interpreternameargs_schemafilesr3   c                 C   s
   || _ d S Nr9   )selfr3   r   r   r   __init__Q   s    zBearlyInterpreterTool.__init__)r
   c              
   C   sb   t | jdkrdS dg}| j D ]4\}}t|jd}|d| d| d|j d q"d	|S )
Nr   r   z<The following files available in the evaluation environment:   z	- path: `z` 
 first four lines: z 
 description: ``
)lenr8   itemsr    r0   appendr(   join)r;   linesr1   	file_infoZpeek_contentr   r   r   file_descriptionT   s    z&BearlyInterpreterTool.file_descriptionc                 C   s   t d | j  S )Nz

)base_descriptionrF   stripr;   r   r   r   r(   a   s    z!BearlyInterpreterTool.descriptionc                 C   s2   g }| j  D ]\}}||t|jd q|S )N)pathnameZcontentsBasesixtyfour)r8   rA   rB   r&   r0   )r;   r8   r1   rE   r   r   r   make_input_filese   s    z&BearlyInterpreterTool.make_input_files)r)   r
   c              	   C   s   t |}tjdt||  dddd| jid }|d rPt|d 	 nd|d	 rlt|d	 	 nd|d
 |d dS )Nr4   zoutput/T)ZfileContentsZ
inputFilesZ	outputDirZoutputAsLinksAuthorization)dataheadersZstdoutBasesixtyfourr   ZstderrBasesixtyfour	fileLinksexitCode)stdoutstderrrO   rP   )
r   requestspostjsondumpsrK   r3   r"   	b64decoder%   )r;   r)   scriptrespr   r   r   _runp   s*    
zBearlyInterpreterTool._run)queryr
   c                    s   t ddS )zUse the tool asynchronously.z$custom_search does not support asyncN)NotImplementedError)r;   r[   r   r   r   _arun   s    zBearlyInterpreterTool._arunN)r0   r1   r(   r
   c                 C   s>   || j v rtdt| s&tdt|||d| j |< d S )Nztarget_path already existszsource_path does not exist)r1   r0   r(   )r8   
ValueErrorr   existsr/   )r;   r0   r1   r(   r   r   r   add_file   s    
zBearlyInterpreterTool.add_filec                 C   s
   i | _ d S r:   )r8   rI   r   r   r   clear_files   s    z!BearlyInterpreterTool.clear_filesc                 C   s   t j| j| j| j| jdS )N)funcr6   r(   r7   )r   from_functionrZ   r6   r(   r7   rI   r   r   r   as_tool   s    zBearlyInterpreterTool.as_tool)r*   r+   r,   r-   r   r.   r5   r6   r'   r7   r   r   r8   r   r/   r<   propertyrF   r(   r   dictrK   rZ   r]   r`   ra   r   rd   r   r   r   r   r2   H   s"   
	r2   )r"   r   rU   r   pathlibr   typingr   r   r   rS   Zlangchain_core.toolsr   Zpydanticr   r   r   r   intr    r&   r'   rG   r/   r2   r   r   r   r   <module>   s   	