Last active
June 21, 2024 00:50
-
-
Save stefanherdy/65315e544ccfb7114c97da83edae7018 to your computer and use it in GitHub Desktop.
How to upgrade your exception handling by using decorators to automatically request debug suggestions from ChatGPT. Code to the tutorial: https://medium.com/@stefan.herdy/upgrade-your-python-exception-handling-with-decorators-e30cc6a4eefa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openai | |
import yaml | |
import sys | |
import numpy as np | |
import openai | |
import yaml | |
import traceback | |
import inspect | |
def message_compose(err_type, err_message, err_code, err_description): | |
message = f"""Hey ChatGPT! I have the following {err_type}: {err_message}. | |
The last lines of my code are: {err_code} . | |
The description of the failing function is: {err_description} | |
How can I improve my code to fix this error? Be as precise as possible!""" | |
return message | |
def chat_with_chatgpt(api_path, message): | |
with open(api_path, 'r') as file: | |
config = yaml.safe_load(file) | |
openai.api_key = config['api_key'] | |
messages = [ {"role": "system", "content": | |
"Hey ChatGPT!"} ] | |
if message: | |
messages.append( | |
{"role": "user", "content": message}, | |
) | |
chat = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", messages=messages | |
) | |
reply = chat.choices[0].message.content | |
print(f"ChatGPT: {reply}") | |
messages.append({"role": "assistant", "content": reply}) | |
return reply | |
def capture_stderr(err_description = '', api_path = './config.yaml'): | |
def wrapper(func): | |
def wrapped_args(*args): | |
# Create a StringIO object to capture stderr | |
error_message = '' | |
try: | |
result = func(*args) | |
return result | |
# If there is an error message, you can handle it here | |
except Exception as e: | |
# Access more detailed information about the exception | |
err_type = type(e).__name__ | |
err_message = e.args | |
print(f'Error Type: {err_type}') | |
print(f'Error Message: {e}') | |
print("Traceback:") | |
traceback.print_tb(e.__traceback__) | |
tracebackmessage = e.__traceback__ | |
# Retrieve the last n lines of code from the traceback | |
stack = traceback.extract_tb(sys.exc_info()[2]) | |
#for filename, line_number, function, line in stack[-1]: | |
line_number = stack[-1].lineno | |
function = stack[-1].name | |
source_code = inspect.getsourcelines(inspect.getmodule(inspect.currentframe()))[0] | |
err_code = source_code[line_number-5: line_number] | |
message = message_compose(err_type, err_message, err_code, err_description) | |
reply = chat_with_chatgpt(api_path, message) | |
print(reply) | |
return wrapped_args | |
return wrapper | |
description = 'This function initializes an array and returns the value at desired index.' | |
index = 7 | |
@capture_stderr(description) | |
def get_value(index): | |
simplearray = np.zeros_like(range(7)) | |
value = simplearray[index] | |
return value | |
value = get_value(index) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment