pull/8/head
BioBootloader 2023-03-18 15:16:47 -07:00
commit 4ff6bfae22
7 zmienionych plików z 270 dodań i 0 usunięć

2
.gitignore vendored 100644
Wyświetl plik

@ -0,0 +1,2 @@
venv
openai_key.txt

21
LICENSE 100644
Wyświetl plik

@ -0,0 +1,21 @@
MIT License
Copyright (c) [2023] [BioBootloader]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

32
README.md 100644
Wyświetl plik

@ -0,0 +1,32 @@
# Wolverine
## About
Give your python scripts regenerative healing abilities!
Run your scripts with Wolverine and when they crash, GPT-4 edits them and explains what went wrong. Even if you have many bugs it will repeatedly rerun until it's fixed.
For a quick demonstration see my [demo video on twitter](https://twitter.com/bio_bootloader/status/1636880208304431104).
## Setup
python3 -m venv venv
pip install -r requirements.txt
source venv/bin/activate
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.
## Example Usage
python wolverine.py buggy_script.py "subtract" 20 3
## Future Plans
This is just a quick prototype I threw together in a few hours. There are many possible extensions and contributions are welcome:
- add flags to customize usage, such as using GPT3.5-turbo instead or 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
- 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

30
buggy_script.py 100644
Wyświetl plik

@ -0,0 +1,30 @@
import sys
import fire
def add_numbers(a, b):
return a + b
def multiply_numbers(a, b):
return a * b
def divide_numbers(a, b):
return a / b
def calculate(operation, num1, num2):
if operation == "add":
result = add_numbers(num1, num2)
elif operation == "subtract":
result = subtract_numbers(num1, num2)
elif operation == "multiply":
result = multiply_numbers(num1, num2)
elif operation == "divide":
result = divide_numbers(num1, num2)
else:
print("Invalid operation")
return res
if __name__ == "__main__":
fire.Fire(calculate)

13
prompt.txt 100644
Wyświetl plik

@ -0,0 +1,13 @@
You are part of an elite automated software fixing team. You will be given a script followed by the arguments it was provided and the stacktrace of the error it produced. Your job is to figure out what went wrong and suggest changes to the code.
Because you are part of an automated system, the format you respond in is very strict. You must provide changes in JSON format, using one of 3 actions: 'Replace', 'Delete', or 'InsertAfter'. 'Delete' will remove that line from the code. 'Replace' will replace the existing line with the content you provide. 'InsertAfter' will insert the new lines you provide after the code already at the specified line number. For multi-line insertions or replacements, provide the content as a single string with '\n' as the newline character. The first line in each file is given line number 1. Edits will be applied in reverse line order so that line numbers won't be impacted by other edits.
In addition to the changes, please also provide short explanations of the what went wrong. A single explanation is required, but if you think it's helpful, feel free to provide more explanations for groups of more complicated changes. Be careful to use proper indentation and spacing in your changes. An example response could be:
[
{"explanation": "this is just an example, this would usually be a brief explanation of what went wrong"},
{"operation": "InsertAfter", "line": 10, "content": "x = 1\ny = 2\nz = x * y"},
{"operation": "Delete", "line": 15, "content": ""},
{"operation": "Replace", "line": 18, "content": "x += 1"},
{"operation": "Delete", "line": 20, "content": ""}
]

17
requirements.txt 100644
Wyświetl plik

@ -0,0 +1,17 @@
aiohttp==3.8.4
aiosignal==1.3.1
async-timeout==4.0.2
attrs==22.2.0
certifi==2022.12.7
charset-normalizer==3.1.0
fire==0.5.0
frozenlist==1.3.3
idna==3.4
multidict==6.0.4
openai==0.27.2
requests==2.28.2
six==1.16.0
termcolor==2.2.0
tqdm==4.65.0
urllib3==1.26.15
yarl==1.8.2

155
wolverine.py 100644
Wyświetl plik

@ -0,0 +1,155 @@
import difflib
import json
import os
import shutil
import subprocess
import sys
import openai
from termcolor import cprint
# Set up the OpenAI API
with open("openai_key.txt") as f:
openai.api_key = f.read().strip()
def run_script(script_name, *args):
try:
result = subprocess.check_output(
[sys.executable, script_name, *args], stderr=subprocess.STDOUT
)
except subprocess.CalledProcessError as e:
return e.output.decode("utf-8"), e.returncode
return result.decode("utf-8"), 0
def send_error_to_gpt4(file_path, args, error_message):
with open(file_path, "r") as f:
file_lines = f.readlines()
file_with_lines = []
for i, line in enumerate(file_lines):
file_with_lines.append(str(i + 1) + ": " + line)
file_with_lines = "".join(file_with_lines)
with open("prompt.txt") as f:
initial_prompt_text = f.read()
prompt = (
initial_prompt_text +
"\n\n"
"Here is the script that needs fixing:\n\n"
f"{file_with_lines}\n\n"
"Here are the arguments it was provided:\n\n"
f"{args}\n\n"
"Here is the error message:\n\n"
f"{error_message}\n"
"Please provide your suggested changes, and remember to stick to the "
"exact format as described above."
)
# print(prompt)
response = openai.ChatCompletion.create(
# model="gpt-3.5-turbo",
model="gpt-4",
messages=[
{
"role": "user",
"content": prompt,
}
],
temperature=1.0,
)
return response.choices[0].message.content.strip()
def apply_changes(file_path, changes_json):
with open(file_path, "r") as f:
original_file_lines = f.readlines()
changes = json.loads(changes_json)
# Filter out explanation elements
operation_changes = [change for change in changes if "operation" in change]
explanations = [
change["explanation"] for change in changes if "explanation" in change
]
# Sort the changes in reverse line order
operation_changes.sort(key=lambda x: x["line"], reverse=True)
file_lines = original_file_lines.copy()
for change in operation_changes:
operation = change["operation"]
line = change["line"]
content = change["content"]
if operation == "Replace":
file_lines[line - 1] = content + "\n"
elif operation == "Delete":
del file_lines[line - 1]
elif operation == "InsertAfter":
file_lines.insert(line, content + "\n")
with open(file_path, "w") as f:
f.writelines(file_lines)
# Print explanations
cprint("Explanations:", "blue")
for explanation in explanations:
cprint(f"- {explanation}", "blue")
# Show the diff
print("\nChanges:")
diff = difflib.unified_diff(original_file_lines, file_lines, lineterm="")
for line in diff:
if line.startswith("+"):
cprint(line, "green", end="")
elif line.startswith("-"):
cprint(line, "red", end="")
else:
print(line, end="")
def main():
if len(sys.argv) < 3:
print("Usage: wolverine.py <script_name> <arg1> <arg2> ... [--revert]")
sys.exit(1)
script_name = sys.argv[1]
args = sys.argv[2:]
# Revert changes if requested
if "--revert" in args:
backup_file = script_name + ".bak"
if os.path.exists(backup_file):
shutil.copy(backup_file, script_name)
print(f"Reverted changes to {script_name}")
sys.exit(0)
else:
print(f"No backup file found for {script_name}")
sys.exit(1)
# Make a backup of the original script
shutil.copy(script_name, script_name + ".bak")
while True:
output, returncode = run_script(script_name, *args)
if returncode == 0:
cprint("Script ran successfully.", "blue")
print("Output:", output)
break
else:
cprint("Script crashed. Trying to fix...", "blue")
print("Output:", output)
json_response = send_error_to_gpt4(script_name, args, output)
apply_changes(script_name, json_response)
cprint("Changes applied. Rerunning...", "blue")
if __name__ == "__main__":
main()