Skip to content

Instantly share code, notes, and snippets.

@codeinthehole
Last active December 8, 2022 12:12
Show Gist options
  • Save codeinthehole/85c86268b76f4338d7d40188e84378a6 to your computer and use it in GitHub Desktop.
Save codeinthehole/85c86268b76f4338d7d40188e84378a6 to your computer and use it in GitHub Desktop.
Use OpenAI's GPT3 model to generate pull request descriptions.
#!/usr/bin/env bash
#
# Print a summary of the current pull request's commits.
#
# Requires an OPENAI_API_KEY env var to authenticate requests - see:
# https://beta.openai.com/docs/api-reference/authentication
# Commit selection variables.
TARGET_BRANCH=master
# OpenAI tuning variables (can be overridden by passing as env vars).
TEMPERATURE=${TEMPERATURE:-1}
MAX_TOKENS=${MAX_TOKENS:-512}
MODEL=${MODEL:-"text-davinci-002"}
INTRO=${INTRO:-"Write a summary of the commits in this pull request"}
# This command uses:
# - JQ to build the JSON payload (to handle escaping)
# - httpie to post it to the OpenAI REST API to generate the completion
# - JQ (again) to extract the completion text out of the response
# - sed to strip blank lines
jq -n \
--arg model "$MODEL" \
--argjson temp $TEMPERATURE \
--argjson tokens $MAX_TOKENS \
--arg prompt "$(
echo "$INTRO";
git log origin/$TARGET_BRANCH.. --format='%B';
)" '{model: $model, temperature: $temp, max_tokens: $tokens, prompt: $prompt}' \
| http https://api.openai.com/v1/completions Authorization:"Bearer $OPENAI_API_KEY" \
| jq -r '.choices[0].text' | sed '/./,$!d'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment