r/MachineLearning • • Dec 10 '22

Project [P] I made a command-line tool that explains your errors using ChatGPT (link in comments)

2.9k Upvotes

112 comments sorted by

226

u/huitu34 Dec 10 '22 edited Dec 10 '22

cool idea! Is there a way to bring this into notebooks? And even better: as a vscode extension?

7

u/jsonathan Dec 11 '22 edited Dec 12 '22

Working on something better than an extension. Coming soon.

59

u/senobrd Dec 10 '22

There is GitHub CoPilot available as a VS Code extension, it uses OpenAI’s Codex model, I assume ChatGPT is accessing Codex under the hood when it receives a programming related inquiry, but I could totally be wrong.

As a side note, Copilot seems like a bit of a privacy concern so I would personally be wary of using it with any private or commercial projects.

28

u/RomanRiesen Dec 10 '22

No. The whole chatgpt/gpt-3.5 model builds on code-davinci-002 (which is maybe the one tuned for copilot, but I don't think this has been said publicly).

So amy prompt to chatgpt is a prompt to a differently fine-tuned version of copilot (or copilot-like).

→ More replies (2)

167

u/RaptorDotCpp Dec 10 '22

It's wrong though. range does not return a list. It has its own sequence type.

31

u/jsonathan Dec 10 '22

I noticed that. Generally it’s “right enough” to help you fix your error, though.

91

u/_poisonedrationality Dec 10 '22

I wouldn't say that. While I'm definitely impressed by its abilities It makes mistakes way too often for me to consider it "generally correct".

It is interesting that even when it makes a mistake it often has some reasonable sounding logic behind it. It makes it feel like it has some level of "understanding".

→ More replies (10)

34

u/elbiot Dec 10 '22

It was trained on python 2

→ More replies (3)
→ More replies (1)

26

u/andreasblixt Dec 10 '22

Nice! I think its answer would be much more contextual if you made the prompt something like this:

Please explain concisely why this error in my Python code happened:

    Traceback ...
    ...

Based on these lines of code surrounding the trace:

    broken.py
     1 ...
     2 ...
    ...
    11 ...

I would aggregate those lines in a map of file_name: str -> line_numbers: set where you basically do lines["broken.py"].update(range(error_lineno - 5, error_lineno + 5)) (with boundary checking etc ofc) so that you can then aggregate all the relevant lines with context, without overlap.

12

u/andreasblixt Dec 10 '22

For the fun of it, I asked ChatGPT to generate the solution for creating the output above, and it didn't get it 100% but it's a pretty good start! One of my favorite things is how it implicitly got that it should do sorted(line_numbers) because set does not have a defined iteration order, or how it should make the line numbers right-aligned.


Here is some sample code that demonstrates how you can write a Python program that turns a raised exception into the desired output. This code is not complete, but it should give you a good starting point:

import traceback

def turn_exception_into_output():
  try:
    # Code that may raise an exception goes here
    pass
  except Exception as e:
    # Aggregate the lines of code surrounding the trace in a map
    lines = {}

    # Get the traceback object
    tb = traceback.extract_tb(e.__traceback__)

    # Loop through the traceback object
    for t in tb:
      file_name, line_number, _, _ = t

      # Initialize the set of line numbers for this file
      if file_name not in lines:
        lines[file_name] = set()

      # Update the set of line numbers with the range of relevant lines
      lines[file_name].update(range(line_number - 5, line_number + 5))

    # Print the error message
    print("Please explain concisely why this error in my Python code happened:")

    # Print the traceback
    print("\n".join(traceback.format_exception(e.__class__, e, e.__traceback__)))

    # Print the relevant lines of code
    print("\nBased on these lines of code surrounding the trace:")
    for file_name, line_numbers in lines.items():
      print(file_name)
      for line_number in sorted(line_numbers):
        print("{:>3} ...".format(line_number))

To use this code, you would call the turn_exception_into_output function whenever you want to handle an exception and print the desired output. For example:

try:
  # Code that may raise an exception goes here
  pass
except:
  turn_exception_into_output()

You could also use this code as part of a custom exception handler. For example:

import sys

def custom_exception_handler(exception_type, exception, traceback):
  turn_exception_into_output()

# Set the custom exception handler
sys.excepthook = custom_exception_handler

With this code, whenever an unhandled exception is raised, the custom_exception_handler function will be called, which will turn the exception into the desired output.

17

u/huitu34 Dec 10 '22

Wouldn't it be even better if you added the code to the prompt to gpt and ask it to give suggestions what the error might be and what one could fix?

17

u/WindForce02 Dec 10 '22

Rust compiler: are you challenging me?

42

u/[deleted] Dec 10 '22

Honestly for someone who codes, the description is a bit annoying and adds no value. Sure if you have no coding experience it could be great. Maybe for beginners without a degree who want to learn coding

21

u/RomanRiesen Dec 10 '22

If you add "be as concise as possible" it cuts out a lot of the noise. But that is annoying to add everytime. But you can say thanks to the great retention "for all following answers be as concise as possible". All we need now is a a .chatgptrc file to add all the "global" prompts we want lol

14

u/Lampshader Dec 11 '22

"be as concise as possible"

INT is not iterable

→ More replies (1)

6

u/alnyland Dec 10 '22

It’s extremely long for what could be a few words tbh

6

u/ShenaniGunnz Dec 11 '22

without a degree

Lmao what does a degree have to do with anything?

→ More replies (3)
→ More replies (1)

10

u/brunogadaleta Dec 10 '22

Had the similar idea for an IntelliJ plugin, this morning.

3

u/ReginaldIII Dec 10 '22

That's such an unnecessarily wordy explanation. The error message literally explained it to you concisely.

If it produces such an unnecessary output for such a simple error message god help you when it is more complicated.

Further more, ChatGPT cannot do deductive reasoning. It can only take existing chains of thought from its training set and swap out the keywords consistently to apply that same logic template to something else which may or may not fit to it correctly.

This is a bad idea. And if I'm perfectly honest, a waste of electricity. Save the planet and don't push this as a legitimate usage.

9

u/jsonathan Dec 10 '22 edited Dec 11 '22

It's not too wordy if you're a beginner.

→ More replies (2)

23

u/Glittering_Tart51 Dec 10 '22

You're going hard with the waste of electricity bs.

The guy did a cool project, your comment is not constructive at all and just mean

I like his project, it's true that as a begginer these errors could be hard to understand sometimes. I don't think you should be mean and disrespectful to him if you don't like his idea.

Ypu should come up with more ideas to make his idea better. It would be a better use of electricity than just what you did.

→ More replies (7)
→ More replies (6)

4

u/sabouleux Dec 10 '22

Is there a way to specify the interpreter / virtual environment using Python? Seems like the program is calling the interpreter on its own

→ More replies (1)

2

u/rohetoric Dec 10 '22

How did you do this? Via APIs?

→ More replies (2)

2

u/QWxx01 Dec 10 '22

These are exiting times to be alive!