From 0995bd428b4f75f1d615c7ab15b41b7e318da494 Mon Sep 17 00:00:00 2001 From: Itezaz-ul-Hassan Date: Fri, 14 Apr 2023 15:35:54 +0500 Subject: [PATCH 1/2] Add Support for Args Flags and .env --- .env.sample | 1 + .gitignore | 13 ++++++++++++- args.py | 16 ++++++++++++++++ requirements.txt | 1 + wolverine.py | 25 +++++++++++++++++++++---- 5 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 .env.sample create mode 100644 args.py diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..113aa6d --- /dev/null +++ b/.env.sample @@ -0,0 +1 @@ +OPENAI_API_KEY = diff --git a/.gitignore b/.gitignore index 673fe09..3f234bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,13 @@ +# Virtual environments venv -openai_key.txt +.env + +# Byte-compiled / optimized / DLL files +__pycache__/ + +# IDE files +.idea/ +.vscode/ + +# Miscellaneous +.DS_Store diff --git a/args.py b/args.py new file mode 100644 index 0000000..955c95a --- /dev/null +++ b/args.py @@ -0,0 +1,16 @@ +import argparse + +parser = argparse.ArgumentParser( + description='Give your python scripts regenerative healing abilities!' +) +parser.add_argument('-y', '--yes', help='Run Every Change made by GPT', + required=False, action='store_true' +) +parser.add_argument('-f', '--file', help='Path to buggy file', required=True) +parser.add_argument('-m', '--model', help='Model Name', required=False, + default='gpt-4' +) +parser.add_argument('-r', '--revert', help='Revert changes from backup file', + required=False, default=False +) +parser.add_argument('args', nargs='+', help='Arguments to pass to script') diff --git a/requirements.txt b/requirements.txt index 9d61d58..3923778 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,6 +13,7 @@ multidict==6.0.4 openai==0.27.2 pycodestyle==2.10.0 pyflakes==3.0.1 +python-dotenv==1.0.0 requests==2.28.2 six==1.16.0 termcolor==2.2.0 diff --git a/wolverine.py b/wolverine.py index 42f7ff8..05228e2 100644 --- a/wolverine.py +++ b/wolverine.py @@ -5,13 +5,15 @@ import shutil import subprocess import sys +from dotenv import load_dotenv +from args import parser import openai from termcolor import cprint # Set up the OpenAI API -with open("openai_key.txt") as f: - openai.api_key = f.read().strip() +load_dotenv() +openai.api_key = os.getenv("OPENAI_API_KEY") def run_script(script_name, script_args): @@ -114,7 +116,13 @@ def apply_changes(file_path, changes_json): print(line, end="") -def main(script_name, *script_args, revert=False, model="gpt-4"): +def main(): + args = parser.parse_args() + script_name = args.file + script_args = args.args + revert = args.revert + model = args.model + run_until_success = args.yes if revert: backup_file = script_name + ".bak" if os.path.exists(backup_file): @@ -127,9 +135,18 @@ def main(script_name, *script_args, revert=False, model="gpt-4"): # Make a backup of the original script shutil.copy(script_name, script_name + ".bak") + run_first_time = False while True: + if run_first_time and not run_until_success: + cprint("Do you want to run the script again? [y/n]", "blue") + user_input = input() + while user_input.lower() != "y" and user_input.lower() != "n": + cprint("Incorrect entry. Please try again.", "red") + if user_input.lower() == "n": + break output, returncode = run_script(script_name, script_args) + run_first_time = True if returncode == 0: cprint("Script ran successfully.", "blue") @@ -150,4 +167,4 @@ def main(script_name, *script_args, revert=False, model="gpt-4"): if __name__ == "__main__": - fire.Fire(main) + main() From 56df9a71d22e87612d790be11003b03b84151fb2 Mon Sep 17 00:00:00 2001 From: Itezaz-ul-Hassan Date: Fri, 14 Apr 2023 15:42:29 +0500 Subject: [PATCH 2/2] Add flags and their usage --- README.md | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 55813f7..e6f4efb 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,9 @@ For a quick demonstration see my [demo video on twitter](https://twitter.com/bio python3 -m venv venv source venv/bin/activate pip install -r requirements.txt + cp .env.sample .env -Add your openAI api key to `openai_key.txt` - _warning!_ by default this uses GPT-4 and may make many repeated calls to the api. +Add your openAI api key to `.env` ## Example Usage @@ -24,7 +25,19 @@ To run with gpt-4 (the default, tested option): You can also run with other models, but be warned they may not adhere to the edit format as well: - python wolverine.py --model=gpt-3.5-turbo buggy_script.py "subtract" 20 3 + python wolverine.py --model=gpt-3.5-turbo -f buggy_script.py "subtract" 20 3 + + +## Flags and their usage + +- To run with specific model, pass the `--model` or `-m` flag with model name +- To pass the buggy script name, pass the `-f` or `--flag` flag with script name +- To run the updated changes to the script till success, pass the `-y` or `--yes` flag +- To revert the script to its original state, pass the `-r` or `--revert` flag + +## Sample full command + + python wolverine.py --model=gpt-3.5-turbo -f buggy_script.py -y "subtract" 20 3 ## Future Plans @@ -32,7 +45,7 @@ This is just a quick prototype I threw together in a few hours. There are many p - add flags to customize usage, such as asking for user confirmation before running changed code - further iterations on the edit format that GPT responds in. Currently it struggles a bit with indentation, but I'm sure that can be improved -- a suite of example buggy files that we can test prompts on to ensure reliablity and measure improvement +- a suite of example buggy files that we can test prompts on to ensure reliability and measure improvement - multiple files / codebases: send GPT everything that appears in the stacktrace - graceful handling of large files - should we just send GPT relevant classes / functions? - extension to languages other than python