Skip to main content

Backseat Driver

Requests a code review from a large language model.

Software developers want reviews so that they can improve their code. But developers are busy. Peers may be unavailable or lack the context to provide comments. Backseat Driver prompts a large language model to give code a letter grade for readability, expressiveness, and organization. It also instructs the model to explain its reasoning. Developers gain the benefits of code review without needing another engineer.

Installation

pip install backseat-driver

Usage

Users need to create an OpenAI account and API key. Backseat Driver needs the API key to be able to request code review from a language model. Set the OPENAI_API_KEY environment variable to your API key.

Note that OpenAI will charge your account for Backseat Driver's requests. Each Backseat Driver invocation will make a request of at most 4096 tokens on a language model. ChatGPT's current pricing model is $0.002 per 1K tokens. At this price, users can expect each call to Backseat Driver to cost $0.002 * 4.096 = $0.008192, or just under 1 cent.

Local

Run Backseat Driver on the command line with the following.

backseat-driver my_script.py

Provide multiple scripts for simultaneous code review of all given files.

backseat-driver script1.py script2.py script3.py

Wildcard operators also work as expected.

backseat-driver *.py

Set the fail_under flag to cause Backseat Driver to exit with an error if the model gives the code a lower grade than what you have specified.

backseat-driver --fail_under B *.py

GitHub Actions

TODO

# TODO not sure what source-directory should be in GitHub Actions
backseat-driver:
  openai_api_key: ${{ secrets.OPENAI_API_KEY }}
  fail_under: B
  source_directory: /
  filter_files_by_suffix: ".py"

Help

foo@bar:~$ backseat-driver -h
usage: backseat-driver [-h] [--fail_under {A,B,C,D}] filenames [filenames ...]

Requests a code review from a large language model (LLM). The model will grade the code based on readability, expressiveness, and organization. The output will include a letter grade in ['A', 'B', 'C', 'D', 'F'], as well as the model's reasoning.

positional arguments:
  filenames             The files to pass to the LLM for code review.

options:
  -h, --help            show this help message and exit
  --fail_under {A,B,C,D}
                        If specified, exit with non-zero status if the LLM's grade falls below the given value. This value is not inclusive: if this value is "B" and the LLM gives a final grade of "B," then the program will exit with a zero status. If not specified, then the program will exit with a zero status no matter the LLM's grade.

Example output

You can try Backseat Driver on the input program below to see how it works. Copy the code into test.py.

"""A file for testing prompt creation."""


def fib(n):
    """Returns the nth fibonacci number."""
    if n <= 2:
        return 1
    return fib(n - 1) + fib(n - 2)


def fact(n):
    """Returns n factorial."""
    if n <= 1:
        return 1
    return n * fact(n - 1)


def hailstone(n):
    """Returns the hailstone sequence starting with positive integer n."""
    if n <= 0:
        raise ValueError(f"Cannot compute hailstone of negative number {n}")
    if n == 1:
        return [n]
    if n % 2 == 0:
        return [n] + hailstone(n // 2)
    return [n] + hailstone(3 * n + 1)

Run Backseat Driver with the following.

backseat-driver test.py

What Backseat Driver says:

Grade: A

This code is well-written, easy to read, and well-organized. The function names are clear and descriptive, and the docstrings provide useful information about the functions' purpose and behavior. The code also follows the recommended Python style guidelines (PEP 8), including appropriate indentation, whitespace, and naming conventions. Overall, there are no major issues with the code, and it is highly readable and maintainable.

One possible improvement could be to add some error handling to the fib() and fact() functions, for cases where the input is not a positive integer. Another potential improvement could be to add some more comments to explain the logic behind the hailstone() function. However, these are minor suggestions and are not necessary for the code to function correctly.

Release files for backseat-driver 0.0.2

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

Source distribution (sdist)

Source distribution for backseat-driver 0.0.2
File Size Uploaded
backseat-driver-0.0.2.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for backseat-driver 0.0.2
File Interpreter ABI Platform
backseat_driver-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 14.6 kB

Release files / backseat-driver-0.0.2.tar.gz

Download URL backseat-driver-0.0.2.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0ab478ec3e27cd834a12347a006fb56b84009ac5dbfc12492655da913abd92a5
BLAKE2b-256 checksum
How to use checksums
fb3a6b0a443b968972ce9239078faff3882df73bf3e28d2874c1608f4bb16a17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / backseat_driver-0.0.2-py3-none-any.whl

Download URL backseat_driver-0.0.2-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
954e757f09ca04ca9c1ccc8d53c29e942e44092941f5c6b8074dd31be2be2b86
BLAKE2b-256 checksum
How to use checksums
449b2219a1f244a4c4dcf22a38522753906f648c80ea82d0d58e0dfc611fb767
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

0.0.2 This release

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