Skip to content

python

June 06 2023

City on fire (hot!)

Videos

Over 200 AQI in NYC (yikes)

Thoughts

I've been using my shortcuts to keep track of a food journal, always cool to overload an automation cornerstone (capture workflow) into a brand new highly useful adaptation


I've been writing a lot, my draft folder is bulging with 3+ essays waiting to be published, wow!

Code

Use GPT-4 to automatically diagnose errors whenever they are thrown, this is already proving hella useful (so much so I might convert it into a library so it can be used across projects), check it out here.

(full code below (...mostly))

error_wrap.py

import openai
import os
import dotenv
import json
import sys
import inspect

# read these for more info:
# https://chat.openai.com/share/9341df1e-65c0-4fd8-87dc-80e0dc9fa5bc
# https://chat.openai.com/share/193482c6-e7b3-4022-b42b-cd2530efb839

dotenv.load_dotenv()

openai.api_key = os.getenv("OPENAI_API_KEY")

async def run_chat_prompt(prompt, model="gpt-4"):
    completion = openai.ChatCompletion.create(
        model=model,
        messages=[
            {"role": "user", "content": prompt}
        ]
    )

    return (completion.choices[0].message.content)

def wrap_error(e: Exception, description: str = None):
    exc_type, exc_value, exc_traceback = sys.exc_info()
    filename = exc_traceback.tb_frame.f_code.co_filename
    line_number = exc_traceback.tb_lineno
    func_name = exc_traceback.tb_frame.f_code.co_name
    module = inspect.getmodule(exc_traceback.tb_frame)
    function_obj = getattr(module, func_name)

    # Get and print the source code of the function
    func_code = inspect.getsource(function_obj)

    error_message = f"This error message occurred because of '{str(e)}' at line {line_number} in file {filename}, function {func_name}. The exception type is {exc_type}. The function is: \n\n```python\n{func_code}\n```\n\n How do I fix this?"
    if description:
        error_message += f'Other details: {description}'

    return error_message

main.py

import error_wrap

async def main():
    try:
       raise Exception("test")
    except Exception as e:
        potential_fix = await error_wrap.run_chat_prompt(error_wrap.wrap_error(e))
        # an optional description can be added with something along the lines of: error_wrap.wrap_error(e, "here's some specific info passed in by me, a human")
        return {"error": str(e), "potential_fix": potential_fix}

Audio

I was on a podcast about GPT-4 and Go! Check it out here.

Freeform

bramadams.dev is a reader-supported published Zettelkasten. Both free and paid subscriptions are available. If you want to support my work, the best way is by taking out a paid subscription.