Porównaj commity

...

3 Commity

Autor SHA1 Wiadomość Data
biobootloader 20c3051d5c
Merge pull request #27 from twsomt/main
Cosmetic update
2023-04-25 15:11:23 -07:00
BioBootloader 43d1d17d96 Merge branch 'main' into twsomt/main 2023-04-25 15:07:16 -07:00
twsomt fc2f5a2d5a Cosmetic update
- sorted imports
- del 'r' from open since it is the default
- add the default encoding
- upd pep8
2023-04-16 13:08:49 +03:00
1 zmienionych plików z 19 dodań i 16 usunięć

Wyświetl plik

@ -4,10 +4,10 @@ import os
import shutil
import subprocess
import sys
import openai
from termcolor import cprint
from dotenv import load_dotenv
import openai
from dotenv import load_dotenv
from termcolor import cprint
# Set up the OpenAI API
load_dotenv()
@ -16,16 +16,16 @@ 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()
with open("prompt.txt", encoding="utf-8") as file:
SYSTEM_PROMPT = file.read()
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
else run with node
"""
script_args = [str(arg) for arg in script_args]
subprocess_args = (
[sys.executable, script_name, *script_args]
if script_name.endswith(".py")
@ -33,9 +33,12 @@ def run_script(script_name, script_args):
)
try:
result = subprocess.check_output(subprocess_args, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
return e.output.decode("utf-8"), e.returncode
result = subprocess.check_output(
subprocess_args,
stderr=subprocess.STDOUT
)
except subprocess.CalledProcessError as error:
return error.output.decode("utf-8"), error.returncode
return result.decode("utf-8"), 0
@ -61,8 +64,8 @@ def json_validated_response(model, messages):
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")
except (json.decoder.JSONDecodeError, ValueError) as error:
cprint(f"{error}. 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
@ -77,15 +80,15 @@ def json_validated_response(model, messages):
)
# rerun the api call
return json_validated_response(model, messages)
except Exception as e:
cprint(f"Unknown error: {e}", "red")
except Exception as error:
cprint(f"Unknown error: {error}", "red")
cprint(f"\nGPT RESPONSE:\n\n{content}\n\n", "yellow")
raise e
raise error
return json_response
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_with_lines = []
@ -123,7 +126,7 @@ def apply_changes(file_path, changes: list, confirm=False):
"""
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()
# Filter out explanation elements