Cosmetic update

- sorted imports
- del 'r' from open since it is the default
- add the default encoding
- upd pep8
pull/27/head
twsomt 2023-04-16 13:08:49 +03:00
rodzic 2f5a026ff9
commit fc2f5a2d5a
1 zmienionych plików z 22 dodań i 18 usunięć

Wyświetl plik

@ -1,14 +1,14 @@
import difflib import difflib
import fire
import json import json
import os import os
import shutil import shutil
import subprocess import subprocess
import sys import sys
import openai
from termcolor import cprint
from dotenv import load_dotenv
import fire
import openai
from dotenv import load_dotenv
from termcolor import cprint
# Set up the OpenAI API # Set up the OpenAI API
load_dotenv() load_dotenv()
@ -17,16 +17,16 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
DEFAULT_MODEL = os.environ.get("DEFAULT_MODEL", "gpt-4") DEFAULT_MODEL = os.environ.get("DEFAULT_MODEL", "gpt-4")
with open("prompt.txt") as f: with open("prompt.txt", encoding="utf-8") as file:
SYSTEM_PROMPT = f.read() SYSTEM_PROMPT = file.read()
def run_script(script_name, script_args): def run_script(script_name, script_args):
script_args = [str(arg) for arg in script_args]
""" """
If script_name.endswith(".py") then run with python If script_name.endswith(".py") then run with python
else run with node else run with node
""" """
script_args = [str(arg) for arg in script_args]
subprocess_args = ( subprocess_args = (
[sys.executable, script_name, *script_args] [sys.executable, script_name, *script_args]
if script_name.endswith(".py") if script_name.endswith(".py")
@ -34,9 +34,12 @@ def run_script(script_name, script_args):
) )
try: try:
result = subprocess.check_output(subprocess_args, stderr=subprocess.STDOUT) result = subprocess.check_output(
except subprocess.CalledProcessError as e: subprocess_args,
return e.output.decode("utf-8"), e.returncode stderr=subprocess.STDOUT
)
except subprocess.CalledProcessError as error:
return error.output.decode("utf-8"), error.returncode
return result.decode("utf-8"), 0 return result.decode("utf-8"), 0
@ -62,28 +65,29 @@ def json_validated_response(model, messages):
json_start_index: json_start_index:
] # extract the JSON data from the response string ] # extract the JSON data from the response string
json_response = json.loads(json_data) json_response = json.loads(json_data)
except (json.decoder.JSONDecodeError, ValueError) as e: except (json.decoder.JSONDecodeError, ValueError) as error:
cprint(f"{e}. Re-running the query.", "red") cprint(f"{error}. Re-running the query.", "red")
# debug # debug
cprint(f"\nGPT RESPONSE:\n\n{content}\n\n", "yellow") cprint(f"\nGPT RESPONSE:\n\n{content}\n\n", "yellow")
# append a user message that says the json is invalid # append a user message that says the json is invalid
messages.append( messages.append(
{ {
"role": "user", "role": "user",
"content": "Your response could not be parsed by json.loads. Please restate your last message as pure JSON.", "content": """Your response could not be parsed by json.loads.
Please restate your last message as pure JSON.""",
} }
) )
# rerun the api call # rerun the api call
return json_validated_response(model, messages) return json_validated_response(model, messages)
except Exception as e: except Exception as error:
cprint(f"Unknown error: {e}", "red") cprint(f"Unknown error: {error}", "red")
cprint(f"\nGPT RESPONSE:\n\n{content}\n\n", "yellow") cprint(f"\nGPT RESPONSE:\n\n{content}\n\n", "yellow")
raise e raise error
return json_response return json_response
def send_error_to_gpt(file_path, args, error_message, model=DEFAULT_MODEL): def send_error_to_gpt(file_path, args, error_message, model=DEFAULT_MODEL):
with open(file_path, "r") as f: with open(file_path) as f:
file_lines = f.readlines() file_lines = f.readlines()
file_with_lines = [] file_with_lines = []
@ -121,7 +125,7 @@ def apply_changes(file_path, changes: list, confirm=False):
""" """
Pass changes as loaded json (list of dicts) Pass changes as loaded json (list of dicts)
""" """
with open(file_path, "r") as f: with open(file_path) as f:
original_file_lines = f.readlines() original_file_lines = f.readlines()
# Filter out explanation elements # Filter out explanation elements