Skip to main content

A natural language debugger.

Project description

roboduck logo

Documentation PyPI version Build Status Open in Colab

rubber duck debugging: a method of debugging code by articulating a problem in spoken or written natural language. The name is a reference to a story in the book The Pragmatic Programmer in which a programmer would carry around a rubber duck and debug their code by forcing themselves to explain it, line-by-line, to the duck. [1]

robo duck debugging: a bit like rubber duck debugging, but the duck talks back.

About

Many AI-powered dev tools help you write boilerplate more quickly, but the hardest and most time-consuming part of programming is often the last mile. Roboduck's goal is to help you understand and fix those bugs. It essentially embeds an LLM (large language model) in the Python interpreter, providing drop-in natural language replacements for Python's standard approaches to:

  • debugging
  • error handling
  • logging

Quickstart

Install

pip install roboduck

API Key Setup

You need an openai API key to begin using roboduck. Once you have an account (sign up here), you can visit https://platform.openai.com/account/api-keys to retrieve your key. Your simplest option is then to call roboduck.set_openai_api_key(api_key, update_config=True) which essentially does the following:

mkdir ~/.roboduck
echo "openai_api_key: your_api_key" > ~/.roboduck/config.yaml

Manually setting an OPENAI_API_KEY environment variable also works.

Roboduck does not store your API key or collect any usage data.

Debugger

We provide a natural language equivalent of python's built-in breakpoint function. Once you're in an interactive session, you can use the standard pdb commands to navigate your code (cmd+f "debugger commands" here. TLDR: type n to execute the next line, a variable name to view its current value, or q to quit the debugging session). However, you can also type a question like "Why do we get an index error when j changes from 3 to 4?" or "Why does nums have three 9s in it when the input list only had one?". Concretely, any time you type something including a question mark, an LLM will try to answer. This is not just performing static analysis - the LLM can access information about the current state of your program.

from roboduck import duck

def bubble_sort(nums):
    for i in range(len(nums)):
        for j in range(len(nums)):
            if nums[j] > nums[j + 1]:
                nums[j + 1] = nums[j]
                nums[j] = nums[j + 1]
                duck()   # <--------------------------- instead of breakpoint()
    return nums

nums = [3, 1, 9, 2, 1]
bubble_sort(nums)

Errors

Roboduck is also good at explaining error messages. Importing the errors module automatically enables optional error explanations. errors.disable() reverts to python's regular behavior on errors. errors.enable() can be used to re-enable error explanations or to change settings. For example, setting auto=True automatically explains all errors rather than asking the user if they want an explanation (y/n) when an error occurs (this is probably excessive for most use cases, but you're free to do it).

from roboduck import errors

data = {'x': 0}
y = data.x

errors.disable()
y = data.x

errors.enable(auto=True)
y = data.x

Jupyter Magic

Jupyter provides a %debug magic that can be used after an error occurs to enter a postmortem debugging session. Roboduck's %duck magic works similarly, but with all of our debugging module's conversational capabilities:

# cell 1
from roboduck import magic

nums = [1, 2, 3]
nums.add(4)
# cell 2
%duck

Logging

Roboduck also provides a logger that can write to stdout and/or a file. Whenever you log an Exception object, an LLM will try to diagnose and suggest a fix for the problem. (Unlike the debug module, the logger does not type responses live because we assume logs will typically be viewed after the fact.)

from roboduck import logging

logger = logging.getLogger(path='/tmp/log.txt')
data = {'x': 0}
try:
    x = data.x
except Exception as e:
    logger.error(e)

CLI

You can also run a python script with error explanations enabled:

duck my_script.py

Run duck --help for more info.

Contributing

To create a virtual environment and install relevant packages:

make dev_env

To run unit tests:

make test

To rebuild the docs locally:

make docs

Start of auto-generated file data.
Last updated: 2023-04-23 20:52:09

File Summary Line Count Last Modified Size
__init__.py _ 5 2023-04-16 21:46:51 142.00 b
debug.py A conversational debugger and drop-in replacement for pdb. Python's default
interactive debugging session is already a crude conversation with your
program or interpreter, in a sense - this just lets your program communicate to
you more effectively.

Quickstart
----------
# Our replacement for python's `breakpoint`.
from roboduck.debug import duck

# Broken version of bubble sort. Notice the duck() call on the second to last
# line.
def bubble_sort(nums):
for i in range(len(nums)):
for j in range(len(nums)):
if nums[j] > nums[j + 1]:
nums[j + 1], nums[j] = nums[j], nums[j + 1]
duck()
return nums
424 2023-04-16 21:52:28 18.17 kb
errors.py Errors that explain themselves! Or more precisely, that are explained to you
by a gpt-esque model. Simply importing this module will change python's default
behavior when it encounters an error.

Quickstart
----------
# After this import, error explanations are automatically enabled.
from roboduck import errors

# Go back to python's regular behavior on errors.
errors.disable()

# You can use `enable` to change settings or manually re-enable gpt
# explanations. By default, we ask the user if they want an explanation after
# each error (y/n). Setting auto=True skips this step and always explains
# errors (not recommended in most cases, but it's an option).
errors.enable(auto=True)
225 2023-04-12 21:48:03 9.27 kb
logging.py Logger that attempts to diagnose and propose a solution for any errors it
is asked to log. Unlike our debugger and errors modules, explanations are
not streamed because the intended use case is not focused on live development.

Quickstart
----------
from roboduck import logging

logger = logging.getLogger()
118 2023-04-14 21:55:16 4.75 kb
magic.py GPT-powered rough equivalent of the `%debug` Jupyter magic. After an error
occurs, just run %duck in the next cell to get an explanation. This is very
similar to using the errors module, but is less intrusive - you only call it
when you want an explanation, rather than having to type y/n after each error.
We also provide `paste` mode, which attempts to paste a solution into a new
code cell below, and `interactive` mode, which throws you into a conversational
debugging session (technically closer to the original `%debug` magic
functionality.

Quickstart
----------
# cell 1
from roboduck import magic

# cell 2
nums = [1, 2, 3]
nums.add(4)

# cell 3
%duck
134 2023-04-13 22:05:19 5.46 kb
shell.py This module allows our roboduck `%duck` magic to work in ipython. Ipython
uses a TerminalInteractiveShell class which makes its debugger_cls attribute
read only. We provide a drop-in replacement that allows our magic class to
set that attribute when necessary. Note that you'd need to start an ipython
session with the command:

```
ipython --TerminalIPythonApp.interactive_shell_class=roboduck.shell.RoboDuckTerminalInteractiveShell
```

for this to work. You'll still need to run `from roboduck import magic` inside
your session to make it avaialble.

Alternatively, you can make it available automatically for all ipython
sessions by adding the following lines to your ipython config (usually found at
~/.ipython/profile_default/ipython_config.py):

```
cfg = get_config()
cfg.TerminalIPythonApp.interactive_shell_class = roboduck.shell.RoboDuckTerminalInteractiveShell
cfg.InteractiveShellApp.exec_lines = ["from roboduck import magic"]
```
34 2023-03-20 21:20:45 1.31 kb
utils.py Utility functions used by other roboduck modules. 599 2023-04-21 23:15:17 21.12 kb

End of auto-generated file data. Do not add anything below this.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

roboduck-0.3.5.tar.gz (45.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page