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 dataclasses import dataclass
from typing import Union
import logging
from langchain_core.tools import Tool
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"""
python_repl = PythonREPL()
blocked_patterns = [
"^os\\.",
"^subprocess\\.",
"^with open\\(",
"^ *os\\.",
"import os",
"^ *subprocess\\.",
"^ *(with)? ?open\\(",
"^ *shutil\\.",
]
valid = True
for pattern in blocked_patterns:
@@ -176,7 +179,12 @@ def save_python_repl(command: str):
break
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:
return f"Command not executed, becaise the blocked pattern `{pattern}` was found in the command."