From e85e50bb76c7083540f220724dac2dff164c6e46 Mon Sep 17 00:00:00 2001 From: Ivan Ogasawara Date: Tue, 30 May 2023 21:14:36 -0400 Subject: [PATCH] feat: Add extra criteria input used by ChatGPT (#4) Add extra criteria input that will be used by ChatGPT in order to tune the initial instructions for the review. --- .github/workflows/test_action.yaml | 5 +++- README.md | 47 +++++++++++++++++++++++------- action.yml | 10 +++++++ entrypoint.sh | 5 ++-- main.py | 30 +++++++++++++++++-- 5 files changed, 81 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test_action.yaml b/.github/workflows/test_action.yaml index 6303cd2..d163e5d 100644 --- a/.github/workflows/test_action.yaml +++ b/.github/workflows/test_action.yaml @@ -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-actions-chatgpt-pr-reviewer@1.0.0 + # uses: osl-incubator/github-actions-chatgpt-pr-reviewer@1.0.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 }} diff --git a/README.md b/README.md index 27fb1c4..d52ed8c 100644 --- a/README.md +++ b/README.md @@ -9,16 +9,43 @@ Include the following into your github actions step ```yaml - name: GitHub Actions ChatGPT PR Reviewer - uses: osl-incubator/github-actions-chatgpt-pr-reviewer@1.0.2 + uses: osl-incubator/github-actions-chatgpt-pr-reviewer@1.0.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 diff --git a/action.yml b/action.yml index dcf35ca..652891c 100644 --- a/action.yml +++ b/action.yml @@ -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 @@ -26,6 +30,11 @@ inputs: description: 'Github PR ID' required: true default: '' + +branding: + icon: check-circle + color: green + runs: using: 'docker' image: 'Dockerfile' @@ -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 }} diff --git a/entrypoint.sh b/entrypoint.sh index 51ba330..aeb1d1e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -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 diff --git a/main.py b/main.py index 0ba9def..5081992 100644 --- a/main.py +++ b/main.py @@ -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 @@ -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: @@ -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()