repl2
This commit is contained in:
@@ -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."
|
||||
|
||||
|
||||
Reference in New Issue
Block a user