Skip to content

Commit

Permalink
Parameterize the model prompt (#24)
Browse files Browse the repository at this point in the history
Attempt to log new vars

Test

Test commit

Potential syntax fix
  • Loading branch information
johnlk authored Jun 25, 2023
1 parent debee1f commit 5ce955d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
1 change: 1 addition & 0 deletions .github/workflows/robin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPEN_AI_API_KEY: ${{ secrets.OPEN_AI_API_KEY }}
root_language: "bash"
files_to_ignore: |
"README.md"
"assets/*"
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ Named after Batman's assistant, Robin AI is an open source Github project that a
|---------------------|----------|---------------------------|-------------------------------------------------------------------------------------------------------------------|
| `GITHUB_TOKEN` | Yes | Automatically supplied | A Github access token with the `repo` and `pull_request` scopes. |
| `OPEN_AI_API_KEY` | Yes | N/A | An API key from Open AI's developer portal. |
| `gpt_model_name` | No | `gpt-3.5-turbo` | The name of the GPT model to use for text generation. |
| `github_api_url` | No | `https://api.github.com` | The URL for the Github API endpoint. (Only relevant to enterprise customers.) |
| `gpt_model_name` | No | `gpt-3.5-turbo` | The name of the GPT model to use for text generation. |
| `github_api_url` | No | `https://api.github.com` | The URL for the Github API endpoint. (Only relevant to enterprise customers.) |
| `root_language` | No | `to infer` | The primary coding language of the project. |
| `coding_principles` | No | `readability, maintainability, single responsibility principle, DRY principle` | The main coding principles to uphold. |
| `ignored_principles`| No | `code comments, heredoc comments, unused imports` | The coding principles to ignore. |
| `files_to_ignore` | No | ` ` (empty string) | A whitespace delimited list of files to ignore. |
|

## Installation

Expand Down
15 changes: 15 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ inputs:
description: 'URL to the API of your Github Server, only necessary for Github Enterprise customers'
required: false
default: 'https://api.github.com'
root_language:
description: 'The primary language of the repository'
required: false
default: 'to infer'
coding_principles:
description: 'The main points of feedback and code principles to maintain'
required: false
default: 'readability, maintainability, single responsibility principle, DRY principle'
ignored_principles:
description: 'Feedback to ignore or omit'
required: false
default: 'code comments, heredoc comments, unused imports'
files_to_ignore:
description: 'Whitespace separated list of files to ignore'
required: false
Expand All @@ -27,6 +39,9 @@ runs:
- --open_ai_api_key=${{ inputs.OPEN_AI_API_KEY }}
- --gpt_model_name=${{ inputs.gpt_model_name }}
- --github_api_url=${{ inputs.github_api_url }}
- --root_language=${{ inputs.root_language }}
- --coding_principles=${{ inputs.coding_principles }}
- --ignored_principles=${{ inputs.ignored_principles }}
- --files_to_ignore=${{ inputs.files_to_ignore }}
branding:
icon: 'tag'
Expand Down
41 changes: 20 additions & 21 deletions src/gpt.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
#!/usr/bin/env bash

INITIAL_PROMPT=$(cat <<EOF
I'm going to assign you a role. Your role is being a pull request code reviewer on our engineering team. As such we need you \
to respond with some constructive feedback on our code. Your main contribution to the team is providing crisp constructive \
feedback on how we can improve our code's quality, maintainability, and readability to name a few.\n\
The code will come as a git diff. If a file is deleted, do not give feedback on it.\n\
You will first give feedback in the form of a score between 0-100. The number will estimate how likely the code change will \
be accepted. Assume the CTO is really stingy and only accepts high level production code, so they reject most initial code \
changes. Do not give a justification for your 0-100 score.\n\
Second, you will respond with a short list of improvements. Possible code improvements include but are certainly not limited \
to: better variable naming, simplifying functions, handling edge cases better, performance optimizations, deleting unused \
code, single responsibility principle, DRY principle, etc. You are not to give feedback on commenting with heredocs. Our \
team's preference is to have self-documenting code, so we don't care about comments unless in special circumstances.\n\
Finally, your last piece of feedback should include a code block. Important: If you assigned an score of >= 90, you should \
not produce a code block, just repond "N/A". For scores < 90, the code block can either be a complete re-write of the code \
being scrutinized or a subset of it, but you should illustrate your feedback with a code example. Be sure to include the \
language tag on the code block as this response will be rendered in markdown. Do not explain the code block!\n\
The code block should be the last part of all your responses.
EOF
)

gpt::prompt_model() {
local -r git_diff="$1"
local -r initial_prompt=$(cat <<EOF
root_language: $2
coding_principles: $3
ignored_principles: $4
Pretend you're the pull request reviewer on our software engineering team. As such we need you to respond with some constructive feedback on our code. \
Your main contribution to the team is providing crisp constructive feedback on how we can improve our code's quality. The code is primarily written in \
"root_language".\n\
The code will come as a git-diff. If a file is deleted, do not give feedback on it.\n\
You will first give feedback in the form of a score between 0-100. The number will estimate how likely the code change will be accepted. Assume the \
team only accepts high level production code, so they reject most initial code changes. Do not give a justification for your 0-100 score.\n\
Second, you will respond with a short list of improvements. Possible code improvements include but are certainly not limited to: "coding_principles". \
You are not to give feedback on "ignored_principles".\n\
Finally, you will respond with a code block. Important: If you assigned an score of >= 90, you should not produce a code block; instead respond "N/A". \
For scores < 90, the code block can either be a complete re-write of the code being scrutinized or a subset of it, but you should illustrate your \
feedback with a code example. Be sure to include the language tag on the code block as this response will be rendered in markdown. Do not explain the \
code block!\n\
The code block should be the last thing you output. Do not write any messaging after the code block.
EOF
)

local -r body=$(curl -sSL \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPEN_AI_API_KEY" \
-d "$(jq -n --arg model "$GPT_MODEL" --arg prompt "$INITIAL_PROMPT" --arg git_diff "$git_diff" '{model: $model, messages: [{role: "user", content: $prompt}, {role: "user", content: $git_diff}]}')" \
-d "$(jq -n --arg model "$GPT_MODEL" --arg prompt "$initial_prompt" --arg git_diff "$git_diff" '{model: $model, messages: [{role: "user", content: $prompt}, {role: "user", content: $git_diff}]}')" \
"https://api.openai.com/v1/chat/completions")

local -r error=$(echo "$body" | jq -r '.error')
Expand Down
4 changes: 2 additions & 2 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ source "$HOME_DIR/src/gpt.sh"
##? Auto-reviews a Pull Request
##?
##? Usage:
##? main.sh --github_token=<token> --open_ai_api_key=<token> --gpt_model_name=<name> --github_api_url=<url> --files_to_ignore=<files>
##? main.sh --github_token=<token> --open_ai_api_key=<token> --gpt_model_name=<name> --github_api_url=<url> --root_language=<text> --coding_principles=<text> --ignored_principles=<text> --files_to_ignore=<files>
main() {
eval "$(/root/bin/docpars -h "$(grep "^##?" "$HOME_DIR/src/main.sh" | cut -c 5-)" : "$@")"

Expand All @@ -26,7 +26,7 @@ main() {
exit
fi

local -r gpt_response=$(gpt::prompt_model "$commit_diff")
local -r gpt_response=$(gpt::prompt_model "$commit_diff" "$root_language" "$coding_principles" "$ignored_principles" )

if [ -z "$gpt_response" ]; then
echoerr "GPT's response was NULL. Double check your API key and billing details."
Expand Down

0 comments on commit 5ce955d

Please sign in to comment.