Package: autobot-ml
autobotAn 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.See more examples onTwitter, or read theblog post.N.B. Autobot is a prototype and isn't recommended for use on large codebases. See:"Limitations".Getting startedAutobot is available asautobot-mlon PyPI:pipinstallautobot-mlAutobot depends on theOpenAI APIand, in particular, expects your OpenAI
organization ID and API key to be exposed as theOPENAI_ORGANIZATIONandOPENAI_API_KEYenvironment variables, respectively.Autobot can also read from a.envfile: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"):autobotrunuseless_object_inheritance/path/to/file.pyExample usageTL;DR: Autobot is a command-line tool. To generate patches, useautobot run; to review the
generated patches, useautobot 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_equalsconvert_to_dataclassnumpy_builtin_aliasesprint_statementsorted_attributesstandard_library_genericsunnecessary_f_stringsuse_generatoruseless_object_inheritanceFor example: to remove any usages of NumPy's deprecatednp.intand associated aliases, we'd first
runautobot run numpy_builtin_aliases /path/to/file.py, followed byautobot review.Theschematicargument toautobot runcan either reference a directory withinschematics(likenumpy_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 theschematicsdirectory, but it's intended to be used with user-provided schematics.A schematic is a directory containing two 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.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 toclass Foo. To automatically remove
those useless object inheritances from our codebase, we'd create auseless_object_inheritancedirectory, and add the following two files:# before.py"""...with object inheritance."""classFoo(Bar,object):def__init__(self,x:int)->None:self.x=x# after.py"""...without object inheritance."""classFoo(Bar):def__init__(self,x:int)->None:self.x=xWe'd then runautobot run ./useless_object_inheritance /path/to/file/or/directoryto generate
patches, followed byautobot reviewto apply or reject the suggested changes.LimitationsRunning Autobot consumes OpenAI credits and thus could cost you money. Be careful!By default, Autobot uses OpenAI'stext-davinci-002model, thoughautobot runaccepts a--modelparameter, allowing you to select an alternative OpenAI model. Note, though, that
OpenAI's Codex models are currently in a private beta, socode-davinci-002and friends may
error for you.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 1toautobot runto disable multi-threading. Running Autobot over large codebases is not recommended (yet).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.Autobot isn't smart enough to handle nested functions (or nested classes), so nested functions
will likely be processed and appear twice.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.)RoadmapMulti-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 leveragetree-sitter.Supporting large codebases.What would it take to run Autobot over hundreds of thousands of
lines of code?LicenseMIT