This commit is contained in:
Lennart J. Kurzweg (Nx2)
2024-08-28 21:33:37 +02:00
parent 895565c3ea
commit c94723c9ef

View File

@@ -3,6 +3,7 @@ from datetime import datetime, timedelta
from re import search from re import search
from dataclasses import dataclass from dataclasses import dataclass
from typing import Union from typing import Union
import logging
from langchain_core.tools import Tool from langchain_core.tools import Tool
from langchain_experimental.utilities import PythonREPL from langchain_experimental.utilities import PythonREPL
@@ -165,9 +166,11 @@ def save_python_repl(command: str):
"""Simulates the normal python repl but with certain patterns blocked for savety reasons""" """Simulates the normal python repl but with certain patterns blocked for savety reasons"""
python_repl = PythonREPL() python_repl = PythonREPL()
blocked_patterns = [ blocked_patterns = [
"^os\\.", "^ *os\\.",
"^subprocess\\.", "import os",
"^with open\\(", "^ *subprocess\\.",
"^ *(with)? ?open\\(",
"^ *shutil\\.",
] ]
valid = True valid = True
for pattern in blocked_patterns: for pattern in blocked_patterns:
@@ -176,7 +179,12 @@ def save_python_repl(command: str):
break break
if valid: if valid:
return python_repl.run(command) ret = python_repl.run(command)
if ret == "":
ret = python_repl.run(f'print(({command}), end="")')
return ret
else: else:
return f"Command not executed, becaise the blocked pattern `{pattern}` was found in the command." return f"Command not executed, becaise the blocked pattern `{pattern}` was found in the command."