Merge pull request #463 from muellerberndt/shellcommands

Add capability to execute shell commands
This commit is contained in:
Richard Beales
2023-04-13 07:16:52 +01:00
committed by GitHub
5 changed files with 39 additions and 8 deletions

View File

@@ -9,6 +9,7 @@ FAST_LLM_MODEL=gpt-3.5-turbo
GOOGLE_API_KEY= GOOGLE_API_KEY=
CUSTOM_SEARCH_ENGINE_ID= CUSTOM_SEARCH_ENGINE_ID=
USE_AZURE=False USE_AZURE=False
EXECUTE_LOCAL_COMMANDS=False
OPENAI_AZURE_API_BASE=your-base-url-for-azure OPENAI_AZURE_API_BASE=your-base-url-for-azure
OPENAI_AZURE_API_VERSION=api-version-for-azure OPENAI_AZURE_API_VERSION=api-version-for-azure
OPENAI_AZURE_DEPLOYMENT_ID=deployment-id-for-azure OPENAI_AZURE_DEPLOYMENT_ID=deployment-id-for-azure

View File

@@ -7,7 +7,7 @@ import speak
from config import Config from config import Config
import ai_functions as ai import ai_functions as ai
from file_operations import read_file, write_to_file, append_to_file, delete_file, search_files from file_operations import read_file, write_to_file, append_to_file, delete_file, search_files
from execute_code import execute_python_file from execute_code import execute_python_file, execute_shell
from json_parser import fix_and_parse_json from json_parser import fix_and_parse_json
from image_gen import generate_image from image_gen import generate_image
from duckduckgo_search import ddg from duckduckgo_search import ddg
@@ -103,6 +103,11 @@ def execute_command(command_name, arguments):
return ai.write_tests(arguments["code"], arguments.get("focus")) return ai.write_tests(arguments["code"], arguments.get("focus"))
elif command_name == "execute_python_file": # Add this command elif command_name == "execute_python_file": # Add this command
return execute_python_file(arguments["file"]) return execute_python_file(arguments["file"])
elif command_name == "execute_shell":
if cfg.execute_local_commands:
return execute_shell(arguments["command_line"])
else:
return "You are not allowed to run local shell commands. To execute shell commands, EXECUTE_LOCAL_COMMANDS must be set to 'True' in your config. Do not attempt to bypass the restriction."
elif command_name == "generate_image": elif command_name == "generate_image":
return generate_image(arguments["prompt"]) return generate_image(arguments["prompt"])
elif command_name == "do_nothing": elif command_name == "do_nothing":

View File

@@ -45,6 +45,8 @@ class Config(metaclass=Singleton):
self.openai_api_key = os.getenv("OPENAI_API_KEY") self.openai_api_key = os.getenv("OPENAI_API_KEY")
self.use_azure = False self.use_azure = False
self.use_azure = os.getenv("USE_AZURE") == 'True' self.use_azure = os.getenv("USE_AZURE") == 'True'
self.execute_local_commands = os.getenv('EXECUTE_LOCAL_COMMANDS', 'False') == 'True'
if self.use_azure: if self.use_azure:
self.openai_api_base = os.getenv("OPENAI_AZURE_API_BASE") self.openai_api_base = os.getenv("OPENAI_AZURE_API_BASE")
self.openai_api_version = os.getenv("OPENAI_AZURE_API_VERSION") self.openai_api_version = os.getenv("OPENAI_AZURE_API_VERSION")

View File

@@ -22,9 +22,10 @@ COMMANDS:
16. Get Improved Code: "improve_code", args: "suggestions": "<list_of_suggestions>", "code": "<full_code_string>" 16. Get Improved Code: "improve_code", args: "suggestions": "<list_of_suggestions>", "code": "<full_code_string>"
17. Write Tests: "write_tests", args: "code": "<full_code_string>", "focus": "<list_of_focus_areas>" 17. Write Tests: "write_tests", args: "code": "<full_code_string>", "focus": "<list_of_focus_areas>"
18. Execute Python File: "execute_python_file", args: "file": "<file>" 18. Execute Python File: "execute_python_file", args: "file": "<file>"
19. Task Complete (Shutdown): "task_complete", args: "reason": "<reason>" 19. Execute Shell Command, non-interactive commands only: "execute_shell", args: "command_line": "<command_line>".
20. Generate Image: "generate_image", args: "prompt": "<prompt>" 20. Task Complete (Shutdown): "task_complete", args: "reason": "<reason>"
21. Do Nothing: "do_nothing", args: "" 21. Generate Image: "generate_image", args: "prompt": "<prompt>"
22. Do Nothing: "do_nothing", args: ""
RESOURCES: RESOURCES:

View File

@@ -1,17 +1,20 @@
import docker import docker
import os import os
import subprocess
WORKSPACE_FOLDER = "auto_gpt_workspace"
def execute_python_file(file): def execute_python_file(file):
"""Execute a Python file in a Docker container and return the output""" """Execute a Python file in a Docker container and return the output"""
workspace_folder = "auto_gpt_workspace"
print (f"Executing file '{file}' in workspace '{workspace_folder}'") print (f"Executing file '{file}' in workspace '{WORKSPACE_FOLDER}'")
if not file.endswith(".py"): if not file.endswith(".py"):
return "Error: Invalid file type. Only .py files are allowed." return "Error: Invalid file type. Only .py files are allowed."
file_path = os.path.join(workspace_folder, file) file_path = os.path.join(WORKSPACE_FOLDER, file)
if not os.path.isfile(file_path): if not os.path.isfile(file_path):
return f"Error: File '{file}' does not exist." return f"Error: File '{file}' does not exist."
@@ -26,7 +29,7 @@ def execute_python_file(file):
'python:3.10', 'python:3.10',
f'python {file}', f'python {file}',
volumes={ volumes={
os.path.abspath(workspace_folder): { os.path.abspath(WORKSPACE_FOLDER): {
'bind': '/workspace', 'bind': '/workspace',
'mode': 'ro'}}, 'mode': 'ro'}},
working_dir='/workspace', working_dir='/workspace',
@@ -46,3 +49,22 @@ def execute_python_file(file):
except Exception as e: except Exception as e:
return f"Error: {str(e)}" return f"Error: {str(e)}"
def execute_shell(command_line):
current_dir = os.getcwd()
if not WORKSPACE_FOLDER in current_dir: # Change dir into workspace if necessary
work_dir = os.path.join(os.getcwd(), WORKSPACE_FOLDER)
os.chdir(work_dir)
print (f"Executing command '{command_line}' in working directory '{os.getcwd()}'")
result = subprocess.run(command_line, capture_output=True, shell=True)
output = f"STDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}"
# Change back to whatever the prior working dir was
os.chdir(current_dir)
return output