File size: 1,213 Bytes
8a682b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Python interpreter tool implementation.
"""

import sys
from io import StringIO
from contextlib import redirect_stdout, redirect_stderr
from typing import Optional
from langchain_core.tools import tool
from pydantic import BaseModel, Field

class PythonInterpreterInput(BaseModel):
    """Input schema for Python interpreter tool."""
    code: str = Field(description="Python code to execute")

@tool
def python_interpreter(code: str) -> str:
    """
    Execute Python code and return the output.
    
    Args:
        code (str): Python code to execute
        
    Returns:
        str: Execution output or error message
    """
    try:
        # Capture stdout and stderr
        stdout = StringIO()
        stderr = StringIO()
        
        with redirect_stdout(stdout), redirect_stderr(stderr):
            # Execute code
            exec(code, {'__builtins__': __builtins__})
            
        # Get output
        output = stdout.getvalue()
        error = stderr.getvalue()
        
        if error:
            return f"Error: {error}"
        return output or "Code executed successfully"
                
    except Exception as e:
        return f"Error executing Python code: {str(e)}"