Skip to main content

autobot

PyPI version

An automated code refactoring tool powered by GPT-3. Like GitHub Copilot, for your existing codebase.

Autobot takes an example change as input and generates patches for you to review by scanning your codebase for similar code blocks and "applying" that change to the existing source code.

Sorting class attributes

See more examples on Twitter, or read the blog post.

N.B. Autobot is a prototype and isn't recommended for use on large codebases. See: "Limitations".

Getting started

Autobot is available as autobot-ml on PyPI:

pip install autobot-ml

Autobot depends on the OpenAI API and, in particular, expects your OpenAI organization ID and API key to be exposed as the OPENAI_ORGANIZATION and OPENAI_API_KEY environment variables, respectively.

Autobot can also read from a .env file:

OPENAI_ORGANIZATION=${YOUR_OPENAI_ORGANIZATION}
OPENAI_API_KEY=${YOUR_OPENAI_API_KEY}

From there, you can run any of Autobot's built-in refactors (called "schematics"):

autobot run useless_object_inheritance /path/to/file.py

Example usage

TL;DR: Autobot is a command-line tool. To generate patches, use autobot run; to review the generated patches, use autobot review.

Autobot is designed around a two-step workflow.

In the first step (autobot run {schematic} {files_to_analyze}), we point Autobot to (1) the "schematic" that defines our desired change and (2) the files to which the change should be applied.

In the second step (autobot review), we review the patches that Autobot generated and, for each suggested change, either apply it to the codebase or reject the patch entirely.

Autobot ships with several schematics that you can use out-of-the-box:

  • assert_equals
  • convert_to_dataclass
  • numpy_builtin_aliases
  • print_statement
  • sorted_attributes
  • standard_library_generics
  • unnecessary_f_strings
  • use_generator
  • useless_object_inheritance

For example: to remove any usages of NumPy's deprecated np.int and associated aliases, we'd first run autobot run numpy_builtin_aliases /path/to/file.py, followed by autobot review.

The schematic argument to autobot run can either reference a directory within schematics (like numpy_builtin_aliases, above) or a path to a user-defined schematic directory on-disk.

Implementing a new refactor ("schematic")

Every refactor facilitated by Autobot requires a "schematic". Autobot ships with a few schematics in the schematics directory, but it's intended to be used with user-provided schematics.

A schematic is a directory containing two files:

  1. before.py: A code snippet demonstrating the "before" state of the refactor.
  2. after.py: A code snippet demonstrating the "after" state of the refactor.

Each file is expected to consist of a brief top-level docstring describing the "before" or "after" state, followed by a single function or class.

For example: in Python 3, class Foo(object) is equivalent to class Foo. To automatically remove those useless object inheritances from our codebase, we'd create a useless_object_inheritance directory, and add the following two files:

# before.py
"""...with object inheritance."""
class Foo(Bar, object):
    def __init__(self, x: int) -> None:
        self.x = x
# after.py
"""...without object inheritance."""
class Foo(Bar):
    def __init__(self, x: int) -> None:
        self.x = x

We'd then run autobot run ./useless_object_inheritance /path/to/file/or/directory to generate patches, followed by autobot review to apply or reject the suggested changes.

Limitations

  1. Running Autobot consumes OpenAI credits and thus could cost you money. Be careful!
  2. By default, Autobot uses OpenAI's text-davinci-002 model, though autobot run accepts a --model parameter, allowing you to select an alternative OpenAI model. Note, though, that OpenAI's Codex models are currently in a private beta, so code-davinci-002 and friends may error for you.
  3. To speed up execution, Autobot calls out to the OpenAI API in parallel. If you haven't upgraded to a paid account, you may hit rate-limit errors. You can pass --nthreads 1 to autobot run to disable multi-threading. Running Autobot over large codebases is not recommended (yet).
  4. Depending on the transform type, Autobot will attempt to generate a patch for every function or every class. Any function or class that's "too long" for GPT-3's maximum prompt size will be skipped.
  5. Autobot isn't smart enough to handle nested functions (or nested classes), so nested functions will likely be processed and appear twice.
  6. Autobot only supports Python code for now. (Autobot relies on parsing the AST to extract relevant code snippets, so additional languages require extending AST support.)

Roadmap

  1. Multi-language support. Autobot only supports Python code for now. Extending to multi-language support, at least with the current algorithm, will require supporting additional AST parsers. The most likely outcome here will either be to leverage tree-sitter.
  2. Supporting large codebases. What would it take to run Autobot over hundreds of thousands of lines of code?

License

MIT

Release files for autobot-ml 0.0.16

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for autobot-ml 0.0.16
File Size Uploaded
autobot-ml-0.0.16.tar.gz 17.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for autobot-ml 0.0.16
File Interpreter ABI Platform
autobot_ml-0.0.16-py3-none-any.whl Python 3 none any Details

Total release size: 40.3 kB

Release files / autobot-ml-0.0.16.tar.gz

Download URL autobot-ml-0.0.16.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
95c87ab3822d5f2459f27bf51c3a0210fc523d5324600f686153940acb787b18
BLAKE2b-256 checksum
How to use checksums
34903c1be71bc87fff08142fe847f60cebeb33856064a7bb1b2ce158ccacbff7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Release files / autobot_ml-0.0.16-py3-none-any.whl

Download URL autobot_ml-0.0.16-py3-none-any.whl
Size 23.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fdf163fac23ef1be073b45bf33014f4a5d194a83595b8e47b36b9143be2e9e76
BLAKE2b-256 checksum
How to use checksums
e7c594ea096e493f446285fe93261ff4019bed8b94ecf4a1ef6d00fbf0f59c5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.0.16 This release

2 release files

0.0.15

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page