Skip to content

Commit

Permalink
feat: Add extra criteria input used by ChatGPT (#4)
Browse files Browse the repository at this point in the history
Add extra criteria input that will be used by ChatGPT in order to tune the initial instructions for the review.
  • Loading branch information
xmnlab authored May 31, 2023
1 parent a055417 commit e85e50b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/test_action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ jobs:
id: chatgpt_pr_reviewer
uses: ./ # Uses an action in the root directory
# or use a released Github Action
# uses: osl-incubator/github-action/github-[email protected].0
# uses: osl-incubator/[email protected].3
with:
openai_api_key: ${{ secrets.OPENAI_API_KEY }}
openai_model: 'gpt-3.5-turbo'
openai_temperature: 0.5
openai_max_tokens: 2048
openai_extra_criteria: | # optional, use `;` for separate the criteria items
- prefer readable variable name instead of short names like `k` and `v`, when apply;
- verify `SOLID` principles design pattern violations, when apply;
github_token: ${{ secrets.GITHUB_TOKEN }}
github_pr_id: ${{ github.event.number }}

Expand Down
47 changes: 37 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,43 @@ Include the following into your github actions step

```yaml
- name: GitHub Actions ChatGPT PR Reviewer
uses: osl-incubator/[email protected].2
uses: osl-incubator/[email protected].3
with:
openai_api_key: ${{ secrets.OPENAI_API_KEY }}
openai_model: 'gpt-3.5-turbo'
openai_temperature: 0.5
openai_max_tokens: 2048
github_token: ${{ secrets.GITHUB_TOKEN }}
github_pr_id: ${{ github.event.number }}
openai_api_key: ${{ secrets.OPENAI_API_KEY }} # required
openai_model: 'gpt-3.5-turbo' # optional
openai_temperature: 0.5 # optional
openai_max_tokens: 2048 # optional
openai_extra_criteria: | # optional, use `;` for separate the criteria items
- prefer readable variable name instead of short names like `k` and `v`, when apply;
- verify `SOLID` principles design pattern violations, when apply;
github_token: ${{ secrets.GITHUB_TOKEN }} # required
github_pr_id: ${{ github.event.number }} # required
```
Note: this GitHub actions is based on the following tutorial:
https://shipyard.build/blog/your-first-python-github-action/
and very inspired on https://github.com/cirolini/chatgpt-github-actions
The initial criteria use for the ChatGPT would look like this:
```
You are a GitHub PR reviewer bot, so you will receive a text that
contains the diff from the PR with all the proposal changes and you
need to take a time to analyze and check if the diff looks good, or
if you see any way to improve the PR, you will return any suggestion
in order to improve the code or fix issues, using the following
criteria for recommendation:
- best practice that would improve the changes
- code style formatting
- recommendation specific for that programming language
- performance improvement
- improvements from the software engineering perspective
- docstrings, when it applies
- prefer explicit than implicit, for example, in python, avoid
importing using `*`, because we don't know what is being imported
```
Note: This initial criteria is presented here just to give an idea about
what is used, for the latest version used, check the main.py file.
## References
This GitHub actions:
- is based on the following tutorial: https://shipyard.build/blog/your-first-python-github-action/
- and very inspired on https://github.com/cirolini/chatgpt-github-actions
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ inputs:
description: 'The maximum number of tokens to generate in the completion.'
required: false
default: '2048'
openai_extra_criteria:
description: 'Add extra criteria for the PR reviewer. Split your criteria with `;`'
required: false
default: ''
github_token:
description: 'Github API Key'
required: true
Expand All @@ -26,6 +30,11 @@ inputs:
description: 'Github PR ID'
required: true
default: ''

branding:
icon: check-circle
color: green

runs:
using: 'docker'
image: 'Dockerfile'
Expand All @@ -34,5 +43,6 @@ runs:
- ${{ inputs.openai_model }}
- ${{ inputs.openai_temperature }}
- ${{ inputs.openai_max_tokens }}
- ${{ inputs.openai_extra_criteria }}
- ${{ inputs.github_token }}
- ${{ inputs.github_pr_id }}
5 changes: 3 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export OPENAI_API_KEY="$1"
export OPENAI_MODEL="$2"
export OPENAI_TEMPERATURE="$3"
export OPENAI_MAX_TOKENS="$4"
export GITHUB_TOKEN="$5"
export GITHUB_PR_ID="$6"
export OPENAI_EXTRA_CRITERIA="$5"
export GITHUB_TOKEN="$6"
export GITHUB_PR_ID="$7"

python /main.py
30 changes: 27 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ def _config_openai(self):
openai_model_default = "gpt-3.5-turbo"
openai_temperature_default = 0.5
openai_max_tokens_default = 2048
openai_extra_criteria_default = ""

openai_api_key = os.environ.get("OPENAI_API_KEY")
os.environ["OPENAI_API_KEY"] = openai_api_key
self.openai_model = os.environ.get("OPENAI_MODEL", openai_model_default)
self.openai_temperature = os.environ.get("OPENAI_TEMPERATURE", openai_temperature_default)
self.openai_max_tokens = os.environ.get("OPENAI_MAX_TOKENS", openai_max_tokens_default)
self.openai_extra_criteria = os.environ.get("OPENAI_EXTRA_CRITERIA", openai_extra_criteria_default)

openai.api_key = openai_api_key

self.chatgpt_initial_instruction = """
self.chatgpt_initial_instruction = f"""
You are a GitHub PR reviewer bot, so you will receive a text that
contains the diff from the PR with all the proposal changes and you
need to take a time to analyze and check if the diff looks good, or
Expand All @@ -53,11 +55,27 @@ def _config_openai(self):
- performance improvement
- improvements from the software engineering perspective
- docstrings, when it applies
- prefer explicit than implicit, for example, in python, avoid
importing using `*`, because we don't know what is being imported
{self._prepare_extra_criteria(self.openai_extra_criteria)}
Please, return your response in markdown format.
If the changes presented by the diff looks good, just say: "LGTM!"
""".strip()

def _prepare_extra_criteria(self, extra_criteria: str):
criteria = []
for item in extra_criteria.split(';'):
_item = item.strip()
prefix = ''
suffix = ''
if not _item.startswith('-'):
prefix = '- '
if not _item.endswith('\n'):
suffix = '\n'
criteria.append(f"{prefix}{_item}{suffix}")
return ''.join(criteria)

def get_pr_content(self):
response = requests.request("GET", self.gh_pr_url, headers=self.gh_headers)
if response.status_code != 200:
Expand Down Expand Up @@ -136,11 +154,17 @@ def pr_review(self, pr_diff: dict):

return results


def comment_review(self, review: list):
repo = self.gh_api.get_repo(self.gh_repo_name)
pull_request = repo.get_pull(int(self.gh_pr_id))
pull_request.create_issue_comment('\n'.join(review))

comment = (
'# OSL ChatGPT Reviewer\n\n'
'*NOTE: This is generated by an AI program, '
'so maybe some comments here would not make sense.*\n\n'
)
comment += '\n'.join(review)
pull_request.create_issue_comment(comment)

def run(self):
pr_diff = self.get_diff()
Expand Down

0 comments on commit e85e50b

Please sign in to comment.