From a608d8dbdc1d61b7a34e4a3643f759072e7611b6 Mon Sep 17 00:00:00 2001 From: Torantulino Date: Sat, 1 Apr 2023 04:08:30 +0100 Subject: [PATCH] Adds full file I/O --- scripts/commands.py | 16 +++++------- scripts/data/prompt.txt | 7 +++-- scripts/file_operations.py | 52 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 scripts/file_operations.py diff --git a/scripts/commands.py b/scripts/commands.py index 98018ac1..bac8b759 100644 --- a/scripts/commands.py +++ b/scripts/commands.py @@ -5,6 +5,7 @@ import datetime import agent_manager as agents import speak from config import Config +from file_operations import read_file, write_to_file, append_to_file, delete_file cfg = Config() @@ -55,8 +56,14 @@ def execute_command(command_name, arguments): return get_text_summary(arguments["url"]) elif command_name == "get_hyperlinks": return get_hyperlinks(arguments["url"]) + elif command_name == "read_file": + return read_file(arguments["file"]) elif command_name == "write_to_file": return write_to_file(arguments["file"], arguments["text"]) + elif command_name == "append_to_file": + return append_to_file(arguments["file"], arguments["text"]) + elif command_name == "delete_file": + return delete_file(arguments["file"]) elif command_name == "browse_website": return browse_website(arguments["url"]) elif command_name == "task_complete": @@ -130,15 +137,6 @@ def overwrite_memory(key, string): print("Invalid key, cannot overwrite memory.") return None -def write_to_file(filename, text): - try: - f = open(filename, "w") - f.write(text) - f.close() - except Exception as e: - return "Error: " + str(e) - return "File written to successfully." - def shutdown(): print("Shutting down...") quit() diff --git a/scripts/data/prompt.txt b/scripts/data/prompt.txt index dc51babf..1c7280db 100644 --- a/scripts/data/prompt.txt +++ b/scripts/data/prompt.txt @@ -16,7 +16,10 @@ COMMANDS: 9. List GPT Agents: "list_agents", args: "" 10. Delete GPT Agent: "delete_agent", args: "key": "" 11. Write to file: "write_to_file", args: "file": "", "text": "" -12. Task Complete (Shutdown): "task_complete", args: "reason": "" +12. Read file: "read_file", args: "file": "" +13. Append to file: "append_to_file", args: "file": "", "text": "" +14. Delete file: "delete_file", args: "file": "" +15. Task Complete (Shutdown): "task_complete", args: "reason": "" RESOURCES: @@ -50,4 +53,4 @@ RESPONSE FORMAT: "criticism": "constructive self-criticism" "speak": "thoughts summary to say to user" } -} +} \ No newline at end of file diff --git a/scripts/file_operations.py b/scripts/file_operations.py new file mode 100644 index 00000000..998c5635 --- /dev/null +++ b/scripts/file_operations.py @@ -0,0 +1,52 @@ +import os +import os.path + +# Set a dedicated folder for file I/O +working_directory = "auto_gpt_workspace" + +if not os.path.exists(working_directory): + os.makedirs(working_directory) + +def safe_join(base, *paths): + new_path = os.path.join(base, *paths) + norm_new_path = os.path.normpath(new_path) + + if os.path.commonprefix([base, norm_new_path]) != base: + raise ValueError("Attempted to access outside of working directory.") + + return norm_new_path + +def read_file(filename): + try: + filepath = safe_join(working_directory, filename) + with open(filepath, "r") as f: + content = f.read() + return content + except Exception as e: + return "Error: " + str(e) + +def write_to_file(filename, text): + try: + filepath = safe_join(working_directory, filename) + with open(filepath, "w") as f: + f.write(text) + return "File written to successfully." + except Exception as e: + return "Error: " + str(e) + +def append_to_file(filename, text): + try: + filepath = safe_join(working_directory, filename) + with open(filepath, "a") as f: + f.write(text) + return "Text appended successfully." + except Exception as e: + return "Error: " + str(e) + +def delete_file(filename): + try: + filepath = safe_join(working_directory, filename) + os.remove(filepath) + return "File deleted successfully." + except Exception as e: + return "Error: " + str(e) \ No newline at end of file