From c94723c9effc966bf7515e00a7ce1a07dfab2b46 Mon Sep 17 00:00:00 2001 From: "Lennart J. Kurzweg (Nx2)" Date: Wed, 28 Aug 2024 21:33:37 +0200 Subject: [PATCH] repl2 --- libs/tools.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/libs/tools.py b/libs/tools.py index d948649..cdd4cd0 100644 --- a/libs/tools.py +++ b/libs/tools.py @@ -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."