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