
    Ng                         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 dedefdZd	ed
edee         fdZd	edefdZ G d de          ZdZ G d de          Z G d d          ZdS )    N)Path)DictListType)Tool)	BaseModelField	md_stringreturnc                 v    t          j        dd| t           j                  }t          j        dd|          }|S )z"Strip markdown code from a string.z^`{1,3}.*?\n )flagsz`{1,3}$)resubDOTALL)r
   stripped_strings     a/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/tools/bearly/tool.pystrip_markdown_coder      s5    f_b)29MMMOfZ_==O    pathnc                     	 t          | d          5 }d t          j        ||          D             cddd           S # 1 swxY w Y   dS # t          $ r g cY S w xY w)z Get the first n lines of a file.rc                 ,    g | ]}t          |          S  )str).0lines     r   
<listcomp>zhead_file.<locals>.<listcomp>   s    AAA$CIIAAAr   N)open	itertoolsislice	Exception)r   r   fs      r   	head_filer%      s    $__ 	BAA)*:1a*@*@AAA	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B   			s1   A >A AA AA AAc                     t          | d          5 }t          j        |                                                                          cddd           S # 1 swxY w Y   dS )zConvert a file to base64.rbN)r    base64	b64encodereaddecode)r   r$   s     r   file_to_base64r,      s    	dD		 3Q))00223 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3s   8AAAc                   <    e Zd ZU dZ eddg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.)examplesdescriptionpython_codeN)__name__
__module____qualname____doc__r	   r1   r   __annotations__r   r   r   r.   r.   #   sQ         22u()3	  K     r   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                   2    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_pathr0   target_pathN)r2   r3   r4   r5   r   r6   r   r   r   r8   r8   @   s<         22r   r8   c                      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<   defd	Zed
efd            Zed
efd            Zd
ee         fdZded
efdZded
efdZdededed
dfdZddZd
efdZdS )BearlyInterpreterToolz9Tool for evaluating python code in a sandbox environment.api_key%https://exec.bearly.ai/v1/interpreterendpointbearly_interpreternameargs_schemafilesc                     || _         d S N)r=   )selfr=   s     r   __init__zBearlyInterpreterTool.__init__Q   s    r   r   c           
         t          | j                  dk    rdS dg}| j                                        D ]>\  }}t          |j        d          }|                    d| d| d|j         d           ?d	                    |          S )
Nr   r   z<The following files available in the evaluation environment:   z	- path: `z` 
 first four lines: z 
 description: ``
)lenrC   itemsr%   r9   appendr0   join)rF   linesr:   	file_infopeek_contents        r   file_descriptionz&BearlyInterpreterTool.file_descriptionT   s    tz??a2OP&*j&6&6&8&8 	 	"K$Y%:A>>LLL>K > > > >%.%:> > >    yyr   c                 J    t           dz   | j        z                                   S )Nz

)base_descriptionrS   striprF   s    r   r0   z!BearlyInterpreterTool.descriptiona   s!     6)D,AAHHJJJr   c                     g }| j                                         D ]/\  }}|                    |t          |j                  d           0|S )N)pathnamecontentsBasesixtyfour)rC   rM   rN   r,   r9   )rF   rC   r:   rQ   s       r   make_input_filesz&BearlyInterpreterTool.make_input_filese   sf    &*j&6&6&8&8 	 	"KLL +-;I<Q-R-R     r   r1   c           
         t          |          }t          j        dt          j        ||                                 ddd          d| j        i                                          }|d         r,t          j        |d                   	                                nd|d	         r,t          j        |d	                   	                                nd|d
         |d         dS )Nr>   zoutput/T)fileContents
inputFiles	outputDiroutputAsLinksAuthorization)dataheadersstdoutBasesixtyfourr   stderrBasesixtyfour	fileLinksexitCode)stdoutstderrrf   rg   )
r   requestspostjsondumpsr[   r=   r(   	b64decoder+   )rF   r1   scriptresps       r   _runzBearlyInterpreterTool._runp   s    $[11}3$*"&"7"7"9"9!*%)	   %dl3
 
 
 $&& 	 -. &;!<==DDFFF -. &;!<==DDFFFk*Z(
 
 	
r   queryc                 $   K   t          d          )zUse the tool asynchronously.z$custom_search does not support async)NotImplementedError)rF   rr   s     r   _arunzBearlyInterpreterTool._arun   s      !"HIIIr   r9   r:   r0   Nc                     || j         v rt          d          t          |                                          st          d          t	          |||          | j         |<   d S )Nztarget_path already existszsource_path does not exist)r:   r9   r0   )rC   
ValueErrorr   existsr8   )rF   r9   r:   r0   s       r   add_filezBearlyInterpreterTool.add_file   sq    $*$$9:::K  '')) 	;9:::"*#+#
 #
 #

;r   c                     i | _         d S rE   )rC   rW   s    r   clear_filesz!BearlyInterpreterTool.clear_files   s    


r   c                 Z    t          j        | j        | j        | j        | j                  S )N)funcrA   r0   rB   )r   from_functionrq   rA   r0   rB   rW   s    r   as_toolzBearlyInterpreterTool.as_tool   s2    !((	
 
 
 	
r   )r   N)r2   r3   r4   r5   r   r6   r?   rA   r.   rB   r   r   rC   r   r8   rG   propertyrS   r0   r   dictr[   rq   ru   ry   r{   r   r   r   r   r   r<   r<   H   s        CCLLL;Hc;;;$D#$$$#AKiAAA!#E4X###     
 # 
  
  
  X
  KS K K K XK	$t* 	 	 	 	
 
 
 
 
 
:J J J J J J
C 
c 
 
PT 
 
 
 
   

 
 
 
 
 
 
r   r<   )r(   r!   rl   r   pathlibr   typingr   r   r   rj   langchain_core.toolsr   pydanticr   r	   r   r   intr%   r,   r.   rU   r8   r<   r   r   r   <module>r      s         				       # # # # # # # # # #  % % % % % % % % % % % % % %3 3    C C DI    3 3 3 3 3 3    Y   A     y   ]
 ]
 ]
 ]
 ]
 ]
 ]
 ]
 ]
 ]
r   