An automated code refactoring tool powered by GPT-3.
Project description
autobot
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.
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}
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 it.
For example: to remove any usages of NumPy's deprecated np.int
and associated aliases, we'd first
run autobot run ./schematics/numpy_builtin_aliases ./path/to/main.py
, followed by
autobot review
.
Implementing a novel refactor
Every refactor facilitated by Autobot requires a "schematic". Autobot ships with a few example
schematics in the schematics
directory, but it's intended to be used with user-provided
schematics.
A schematic is a directory containing three files:
before.py
: A code snippet demonstrating the "before" state of the refactor.after.py
: A code snippet demonstrating the "after" state of the refactor.autobot.json
: A JSON object containing a plaintext description of the before (before_description
) and after (after_description
) states, along with thetransform_type
("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 above files.
# before.py
class Foo(Bar, object):
def __init__(self, x: int) -> None:
self.x = x
# after.py
class Foo(Bar):
def __init__(self, x: int) -> None:
self.x = x
// autobot.json
{
"before_description": "with object inheritance",
"after_description": "without object inheritance",
"transform_type": "Class"
}
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
- 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
toautobot run
to disable multi-threading. - Depending on the transform type, Autobot will either generate a patch for every function or every class. Any function or class that's "too long" will be GPT-3's maximum prompt size,
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for autobot_ml-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b253c119d9e3a4f6d5cc90529c034293d0fdc48f66a781ad0b266df99ed2c5e |
|
MD5 | 723d0dacc5c6671650f505ad1c0c989a |
|
BLAKE2b-256 | 38ed3e6597abc0b9745ae855501f15d0c7c1a79d74d97d68a6515325c32a8205 |