Package: backseat-driver
Backseat DriverRequests 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.Installationpipinstallbackseat-driverUsageUsers need tocreate an OpenAI accountand API key.
Backseat Driver needs the API key to be able to request code review from a
language model.
Set theOPENAI_API_KEYenvironment 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 modelis $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.LocalRun Backseat Driver on the command line with the following.backseat-drivermy_script.pyProvide multiple scripts for simultaneous code review of all given files.backseat-driverscript1.pyscript2.pyscript3.pyWildcard operators also work as expected.backseat-driver*.pySet thefail_underflag 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_underB*.pyGitHub ActionsTODO# TODO not sure what source-directory should be in GitHub Actionsbackseat-driver:openai_api_key:${{ secrets.OPENAI_API_KEY }}fail_under:Bsource_directory:/filter_files_by_suffix:".py"Helpfoo@bar:~$backseat-driver-husage: 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 outputYou can try Backseat Driver on the input program below to see how it works.
Copy the code intotest.py."""A file for testing prompt creation."""deffib(n):"""Returns the nth fibonacci number."""ifn<=2:return1returnfib(n-1)+fib(n-2)deffact(n):"""Returns n factorial."""ifn<=1:return1returnn*fact(n-1)defhailstone(n):"""Returns the hailstone sequence starting with positive integer n."""ifn<=0:raiseValueError(f"Cannot compute hailstone of negative number{n}")ifn==1:return[n]ifn%2==0:return[n]+hailstone(n//2)return[n]+hailstone(3*n+1)Run Backseat Driver with the following.backseat-drivertest.pyWhat Backseat Driver says:Grade: AThis 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 thefib()andfact()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 thehailstone()function. However, these are minor suggestions and are not necessary for the code to function correctly.