Merge pull request #13 from fsboehme/main

more robust parsing of JSON (+ indentation)
pull/23/head^2
biobootloader 2023-04-14 15:44:12 -07:00 zatwierdzone przez GitHub
commit 742aaaf9d1
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
5 zmienionych plików z 82 dodań i 27 usunięć

Wyświetl plik

@ -1 +1,2 @@
OPENAI_API_KEY=your_api_key
#DEFAULT_MODEL=gpt-3.5-turbo

2
.gitignore vendored
Wyświetl plik

@ -1,3 +1,5 @@
venv
.venv
.env
env/
.vscode/

Wyświetl plik

@ -15,7 +15,7 @@ For a quick demonstration see my [demo video on twitter](https://twitter.com/bio
pip install -r requirements.txt
cp .env.sample .env
Add your openAI api key to `.env`
Add your openAI api key to `.env`
_warning!_ By default wolverine uses GPT-4 and may make many repeated calls to the api.
@ -29,6 +29,10 @@ You can also run with other models, but be warned they may not adhere to the edi
python wolverine.py --model=gpt-3.5-turbo buggy_script.py "subtract" 20 3
If you want to use GPT-3.5 by default instead of GPT-4 uncomment the default model line in `.env`:
DEFAULT_MODEL=gpt-3.5-turbo
## Future Plans
This is just a quick prototype I threw together in a few hours. There are many possible extensions and contributions are welcome:

Wyświetl plik

@ -4,10 +4,13 @@ Because you are part of an automated system, the format you respond in is very s
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:
Be ABSOLUTELY SURE to include the CORRECT INDENTATION when making replacements.
example response:
[
{"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": "Replace", "line": 18, "content": " x += 1"},
{"operation": "Delete", "line": 20, "content": ""}
]

Wyświetl plik

@ -14,6 +14,12 @@ from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
DEFAULT_MODEL = os.environ.get("DEFAULT_MODEL", "gpt-4")
with open("prompt.txt") as f:
SYSTEM_PROMPT = f.read()
def run_script(script_name, script_args):
script_args = [str(arg) for arg in script_args]
@ -26,7 +32,49 @@ def run_script(script_name, script_args):
return result.decode("utf-8"), 0
def send_error_to_gpt(file_path, args, error_message, model):
def json_validated_response(model, messages):
"""
This function is needed because the API can return a non-json response.
This will run recursively until a valid json response is returned.
todo: might want to stop after a certain number of retries
"""
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0.5,
)
messages.append(response.choices[0].message)
content = response.choices[0].message.content
# see if json can be parsed
try:
json_start_index = content.index(
"["
) # find the starting position of the JSON data
json_data = content[
json_start_index:
] # extract the JSON data from the response string
json_response = json.loads(json_data)
except (json.decoder.JSONDecodeError, ValueError) as e:
cprint(f"{e}. Re-running the query.", "red")
# debug
cprint(f"\nGPT RESPONSE:\n\n{content}\n\n", "yellow")
# append a user message that says the json is invalid
messages.append(
{
"role": "user",
"content": "Your response could not be parsed by json.loads. Please restate your last message as pure JSON.",
}
)
# rerun the api call
return json_validated_response(model, messages)
except Exception as e:
cprint(f"Unknown error: {e}", "red")
cprint(f"\nGPT RESPONSE:\n\n{content}\n\n", "yellow")
raise e
return json_response
def send_error_to_gpt(file_path, args, error_message, model=DEFAULT_MODEL):
with open(file_path, "r") as f:
file_lines = f.readlines()
@ -35,12 +83,7 @@ def send_error_to_gpt(file_path, args, error_message, model):
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"
@ -52,26 +95,27 @@ def send_error_to_gpt(file_path, args, error_message, model):
)
# print(prompt)
response = openai.ChatCompletion.create(
model=model,
messages=[
{
"role": "user",
"content": prompt,
}
],
temperature=1.0,
)
messages = [
{
"role": "system",
"content": SYSTEM_PROMPT,
},
{
"role": "user",
"content": prompt,
},
]
return response.choices[0].message.content.strip()
return json_validated_response(model, messages)
def apply_changes(file_path, changes_json):
def apply_changes(file_path, changes: list):
"""
Pass changes as loaded json (list of dicts)
"""
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 = [
@ -114,7 +158,7 @@ def apply_changes(file_path, changes_json):
print(line, end="")
def main(script_name, *script_args, revert=False, model="gpt-4"):
def main(script_name, *script_args, revert=False, model=DEFAULT_MODEL):
if revert:
backup_file = script_name + ".bak"
if os.path.exists(backup_file):
@ -140,11 +184,12 @@ def main(script_name, *script_args, revert=False, model="gpt-4"):
print("Output:", output)
json_response = send_error_to_gpt(
file_path=script_name,
args=script_args,
error_message=output,
model=model,
file_path=script_name,
args=script_args,
error_message=output,
model=model,
)
apply_changes(script_name, json_response)
cprint("Changes applied. Rerunning...", "blue")