A common approach to getting code reviews with ChatGPT is by posting a prompt along with a code snippet. While effective, this process can be time-consuming and repetitive. If you want to streamline, customize, or fully automate your code review process, meet gptscript.
Gptscript is a powerful automation tool designed to build tools of any complexity on top of GPT models.
At the core of gptscript is a script defining a series of steps to execute, along with available tools such as Git operations, file reading, web scraping, and more.
In this guide, we’ll demonstrate how to perform a Python code review using a simple script (python-code-review.gpt
).
tools: sys.read
You are an expert Python developer, your task is to review a set of pull requests.
You are given a list of filenames and their partial contents, but note that you might not have the full context of the code.
Only review lines of code which have been changed (added or removed) in the pull request. The code looks similar to the output of a git diff command. Lines which have been removed are prefixed with a minus (-) and lines which have been added are prefixed with a plus (+). Other lines are added to provide context but should be ignored in the review.
Begin your review by evaluating the changed code using a risk score similar to a LOGAF score but measured from 1 to 5, where 1 is the lowest risk to the code base if the code is merged and 5 is the highest risk which would likely break something or be unsafe.
In your feedback, focus on highlighting potential bugs, improving readability if it is a problem, making code cleaner, and maximising the performance of the programming language. Flag any API keys or secrets present in the code in plain text immediately as highest risk. Rate the changes based on SOLID principles if applicable.
Do not comment on breaking functions down into smaller, more manageable functions unless it is a huge problem. Also be aware that there will be libraries and techniques used which you are not familiar with, so do not comment on those unless you are confident that there is a problem.
Use markdown formatting for the feedback details. Also do not include the filename or risk level in the feedback details.
Ensure the feedback details are brief, concise, accurate. If there are multiple similar issues, only comment on the most critical.
Include brief example code snippets in the feedback details for your suggested changes when you're confident your suggestions are improvements. Use the same programming language as the file under review.
If there are multiple improvements you suggest in the feedback details, use an ordered list to indicate the priority of the changes.
Format the response in a valid Markdown format as a list of feedbacks, where the value is an object containing the filename ("fileName"), risk score ("riskScore") and the feedback ("details"). The schema of the Markdown feedback object must be:
## File: filename
Risk: riskScore
Details: details
The content for review is provided as input file.
Testing time
You will need gptscript installed.
The prompt from above as python-code-review.gpt
file.
File to review. I am using the following Python code (code.py
):
colors = {
"apple": "red",
"banana": "yellow",
"cherry": "red",
"mango": "red",
"lemon": "yellow",
"plum": "purple"
}
common = {}
for k, v in colors.items():
if v in common:
common[v] += 1
else:
common[v] = 1
common = sorted(common.items(), key=lambda x:x[1], reverse=True)
print(common[0][0])
Run gptscript with promptfile and code as first two inputs:
gptscript python-code-review.gpt code.py
Output:
## File: code.py
Risk: 2
Details:
1. The sorting of the `common` dictionary could be optimized by using the `collections.Counter` class, which is specifically designed for counting hashable objects. This would make the code more readable and efficient.
```python
from collections import Counter
common = Counter(colors.values())
most_common_color = common.most_common(1)[0][0]
print(most_common_color)
```
2. Consider using more descriptive variable names to improve readability, such as `color_counts` instead of `common`.
The results is in Markdown syntax which is easy to read for a human.
But, if you want to add automation I would prefer to change output toJSON format or a format of your choice which suits you tools.
Let’s refactor the promt to request JSON output:
Format the response in a valid JSON format as a list of feedbacks, where the value is an object containing the filename ("fileName"), risk score ("riskScore") and the feedback ("details").
The schema of the JSON feedback object must be:
{
"fileName": {
"type": "string"
},
"riskScore": {
"type": "number"
},
"details": {
"type": "string"
}
}
The content for review is provided as input file.
Re-run the script and you will get something like this:
[
{
"fileName": "code.py",
"riskScore": 2,
"details": "1. Consider using a `defaultdict` from the `collections` module to simplify the counting logic. This will make the code cleaner and more efficient.\n\nExample:\n```python\nfrom collections import defaultdict\n\ncommon = defaultdict(int)\nfor v in colors.values():\n common[v] += 1\n```\n\n2. The sorting and accessing the first element can be improved for readability by using `max` with a key function.\n\nExample:\n```python\nmost_common_color = max(common.items(), key=lambda x: x[1])[0]\nprint(most_common_color)\n```"
}
]
Remember, LLMs output are not determined, you can get different result for the same request.
Now let’s review what actually happens when you run gptscript with the prompt.
What It Does in a Nutshell
- Extracts code for review: Captures content from the input file(code.py).
- Sets context for the LLM: Instructs the LLM to act as an expert Python developer tasked with providing a detailed and sophisticated code review.
- Defines Structured Output: Returns results in two fields:
Risk: A score indicating the potential risk associated with the changes.
Details: A comprehensive explanation of the changes and their implications.
Conclusion
Gptscript is a powerful tool to kickstart your journey into automation using OpenAPI models. By defining custom scripts, you can streamline complex workflows, such as automated code reviews, with minimal effort.
This example just scratches the surface—there’s much more you can achieve with gptscript. Explore additional examples from the gptscript project to discover more possibilities and enhance your automation capabilities.
Happy automating!