Last active
March 25, 2024 02:20
-
-
Save PurpleBooth/974e84f2801e42e81b3f07d80b321afd to your computer and use it in GitHub Desktop.
Revisions
-
PurpleBooth revised this gist
Mar 5, 2023 . 1 changed file with 74 additions and 48 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,52 +1,79 @@ #!/usr/bin/env python import json import logging import subprocess import sys import textwrap from typing import Literal, Sequence, TypedDict, Union from urllib.error import HTTPError from urllib.request import Request, urlopen api_key = "replaceme" class Message(TypedDict): role: Union[Literal["system"], Literal["assistant"], Literal["user"]] content: str def get_subject(summary: str, diff: str) -> tuple[str, list[Message]]: messages: list[Message] = [ { "role": "system", "content": "You are a tool that explains code changes", }, {"role": "user", "content": "Can you explain change?"}, { "role": "assistant", "content": "Sure, do you have any information other than the diff?", }, {"role": "user", "content": summary}, {"role": "assistant", "content": "Can you provide me the diff?"}, {"role": "user", "content": diff}, { "role": "assistant", "content": "Here is a terse, 50-character summary of the changes in the imperative tense", }, ] result = do_request(messages) return result, messages + [{"role": "assistant", "content": result}] def get_body(previous: Sequence[Message]) -> str: messages: list[Message] = list(previous) + [ { "role": "user", "content": "Can you give me a longer description of the changes?", }, { "role": "assistant", "content": "Sure, what follows is a longer description of the changes in the imperative tense", }, ] return do_request(messages) def do_request(messages: Sequence[Message]) -> str: request = { "model": "gpt-3.5-turbo", "messages": messages, } body = json.dumps(request) httprequest = Request( "https://api.openai.com/v1/chat/completions", method="POST", headers={ "Content-Type": "application/json", "Authorization": f"Bearer {api_key}", }, ) try: with urlopen(httprequest, data=body.encode("utf-8")) as response: response_body = response.read().decode() return json.loads(response_body)["choices"][0]["message"]["content"] except HTTPError as e: logging.exception(e.read().decode()) raise e from e hint = "No" @@ -59,27 +86,26 @@ def ask_openai(summary: str, diff: str) -> str: ) diff_output.check_returncode() subject, messages = get_subject(hint, diff_output.stdout.decode()) subject = subject.rstrip(".") body = get_body(messages).split("\n\n") wrapped_subject = [subject] if len(subject) > 72: body = [subject] + body subject = textwrap.shorten(subject, width=50) wrapped_subject = [subject] wrapped_paragraphs = [ textwrap.fill( s, width=72, ) for s in body ] wrapped_text = "\n\n".join( wrapped_subject + wrapped_paragraphs, ) print(wrapped_text) -
PurpleBooth revised this gist
Mar 3, 2023 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -60,6 +60,12 @@ def ask_openai(summary: str, diff: str) -> str: diff_output.check_returncode() suggestion = ask_openai(hint, diff_output.stdout.decode()).split("\n\n") subject = suggestion[0] if len(subject) > 50: suggestion = [textwrap.shorten(subject, width=50), subject] + suggestion[1:] wrapped_text = "\n\n".join( [ suggestion[0], -
PurpleBooth revised this gist
Mar 2, 2023 . 1 changed file with 13 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -61,7 +61,19 @@ def ask_openai(summary: str, diff: str) -> str: suggestion = ask_openai(hint, diff_output.stdout.decode()).split("\n\n") wrapped_text = "\n\n".join( [ suggestion[0], *[ textwrap.fill( s, width=72, drop_whitespace=False, replace_whitespace=False, expand_tabs=False, ) for s in suggestion[1:] ], ] ) print(wrapped_text) -
PurpleBooth revised this gist
Mar 2, 2023 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import json import os import subprocess import textwrap import sys from pprint import pprint from urllib.request import Request, urlopen @@ -58,4 +59,9 @@ def ask_openai(summary: str, diff: str) -> str: ) diff_output.check_returncode() suggestion = ask_openai(hint, diff_output.stdout.decode()).split("\n\n") wrapped_text = "\n\n".join( [suggestion[0], *[textwrap.fill(s, width=72) for s in suggestion[1:]]] ) print(wrapped_text) -
PurpleBooth revised this gist
Mar 2, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -51,7 +51,7 @@ def ask_openai(summary: str, diff: str) -> str: hint = "No" if len(sys.argv) > 1: hint = " ".join(sys.argv[1:]) diff_output = subprocess.run( ["git", "diff", "--patch", "--cached"], capture_output=True -
PurpleBooth revised this gist
Mar 2, 2023 . 1 changed file with 16 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,24 +18,29 @@ def ask_openai(summary: str, diff: str) -> str: "messages": [ { "role": "system", "content": "Write a well formatted commit message, without trailers, do not write a conventional commit", }, { "role": "assistant", "content": "Sure, do you have any information other than the diff?", }, {"role": "user", "content": summary}, {"role": "assistant", "content": "Can you provide me the diff?"}, {"role": "user", "content": diff}, { "role": "assistant", "content": "Thanks, the next message has the text you would put in your editor", }, ], } ) httprequest = Request( url, method="POST", headers={ "Content-Type": "application/json", "Authorization": f"Bearer {api_key}", }, ) with urlopen(httprequest, data=body.encode("utf-8")) as response: @@ -48,7 +53,9 @@ def ask_openai(summary: str, diff: str) -> str: if len(sys.argv) > 1: hint = " ".join(sys.argv()) diff_output = subprocess.run( ["git", "diff", "--patch", "--cached"], capture_output=True ) diff_output.check_returncode() print(ask_openai(hint, diff_output.stdout.decode())) -
PurpleBooth revised this gist
Mar 2, 2023 . 2 changed files with 54 additions and 80 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ #!/usr/bin/env python import json import os import subprocess import sys from pprint import pprint from urllib.request import Request, urlopen api_key = "replaceme" def ask_openai(summary: str, diff: str) -> str: url = "https://api.openai.com/v1/chat/completions" body = json.dumps( { "model": "gpt-3.5-turbo", "messages": [ { "role": "system", "content": "Write a well formatted commit message, without trailers, do not write a conventional commit" }, { "role": "assistant", "content": "Sure, do you have any information other than the diff?" }, {"role": "user", "content": summary}, {"role": "assistant", "content": "Can you provide me the diff?"}, {"role": "user", "content": diff}, {"role": "assistant", "content": "Thanks, the next message has the text you would put in your editor"}, ] } ) httprequest = Request( url, method="POST", headers={ "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" } ) with urlopen(httprequest, data=body.encode("utf-8")) as response: body = response.read().decode() return json.loads(body)["choices"][0]["message"]["content"] hint = "No" if len(sys.argv) > 1: hint = " ".join(sys.argv()) diff_output = subprocess.run(["git", "diff", "--patch", "--cached"], capture_output=True) diff_output.check_returncode() print(ask_openai(hint, diff_output.stdout.decode())) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,80 +0,0 @@ -
PurpleBooth revised this gist
Mar 2, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,7 @@ ask_openai() { request="$(jq --null-input \ --argjson max_tokens "$max_tokens" \ --arg "prompt" "$prompt" \ '{"model": "gpt-3.5-turbo", "prompt": $prompt, "temperature": 0, "max_tokens": $max_tokens }')" response="$(curl https://api.openai.com/v1/completions \ --silent \ -
PurpleBooth revised this gist
Feb 20, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -67,7 +67,7 @@ prompt_for_body=$( $subject Now describe the change in detail in a longer commit message END ) -
PurpleBooth revised this gist
Feb 19, 2023 . 1 changed file with 2 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -57,23 +57,20 @@ prompt=$( END ) subject="$(ask_openai "$prompt")" prompt_for_body=$( cat <<-END $prompt $subject Now describe the change in detail END ) body="$(ask_openai "$prompt_for_body")" echo "$subject" -
PurpleBooth revised this gist
Feb 18, 2023 . 1 changed file with 51 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,9 +3,33 @@ set -euo pipefail api_key="replaceme" truncate_at=4092 ask_openai() { local prompt="$1" max_tokens=2048 request="$(jq --null-input \ --argjson max_tokens "$max_tokens" \ --arg "prompt" "$prompt" \ '{"model": "text-davinci-003", "prompt": $prompt, "temperature": 0, "max_tokens": $max_tokens }')" response="$(curl https://api.openai.com/v1/completions \ --silent \ --header "Content-Type: application/json" \ --header "Authorization: Bearer $api_key" \ --data "$request" \ --output -)" text="$(echo "$response" | jq --raw-output .choices[0].text)" if [ -z "$text" ] || [ "null" == "$text" ]; then echo "" else python -c "import sys; print( sys.argv[1].strip() )" "$text" fi } hint="No" if [ $# -ne 0 ]; then @@ -29,22 +53,31 @@ prompt=$( $(git diff -p --cached | head --bytes="$truncate_at") Please write a short, 50 characters or less, commit message for these changes? END ) subject="$(ask_openai "$prompt" | head --bytes=50)" prompt_for_body=$( cat <<-END $prompt $subject Now describe the change in detail END ) body="$(ask_openai "$prompt_for_body")" echo "$subject" if [ -n "$body" ] && [ "$subject" != "$body" ]; then echo echo "$body" | fold -s -w 72 fi -
PurpleBooth revised this gist
Feb 17, 2023 . 1 changed file with 2 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,23 +17,7 @@ prompt=$( Please write me a commit message Sure, do you have any information other than the diff? $hint @@ -45,7 +29,7 @@ prompt=$( $(git diff -p --cached | head --bytes="$truncate_at") Here is the commit message: END ) -
PurpleBooth revised this gist
Feb 15, 2023 . 1 changed file with 11 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,13 +15,12 @@ fi prompt=$( cat <<-END Please write me a commit message Sure, what does a good commit message look like? Capitalized, short (50 chars or less) summary More detailed explanatory text, if necessary. Wrap it to about 72 characters or so. In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank @@ -32,23 +31,22 @@ prompt=$( Write your commit message in the imperative: "Fix bug" and not "Fixed bug" or "Fixes bug." This convention matches up with commit messages generated by commands like git merge and git revert. Do you have any additional information that should included? $hint Can you provide me the diff? $(git diff -p --cached | head --bytes="$truncate_at") A good commit message would be: END ) request="$(jq --null-input \ -
PurpleBooth revised this gist
Feb 15, 2023 . 1 changed file with 17 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,17 +6,19 @@ api_key="replaceme" max_tokens=2048 truncate_at=4092 hint="No" if [ $# -ne 0 ]; then hint="$@" fi prompt=$( cat <<-END Please write me a commit message Sure, what does a good commit message look like? Capitalized, short (50 chars or less) summary @@ -30,13 +32,21 @@ prompt=$( Write your commit message in the imperative: "Fix bug" and not "Fixed bug" or "Fixes bug." This convention matches up with commit messages generated by commands like git merge and git revert. Do you have any additional information that should included? $hint Can you provide me the diff? $(git diff -p --cached | head --bytes="$truncate_at") A good commit message would be END ) -
PurpleBooth revised this gist
Feb 13, 2023 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -55,5 +55,4 @@ response="$(curl https://api.openai.com/v1/completions \ echo "$response" | jq --raw-output .choices[0].text | python -c "import sys; print( sys.stdin.read().strip())" -
PurpleBooth revised this gist
Feb 12, 2023 . 1 changed file with 7 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ set -euo pipefail api_key="replaceme" max_tokens=2048 truncate_at=4092 @@ -14,9 +14,9 @@ fi prompt=$( cat <<-END Summarize these changes for a commit message Use the syle in this example below Capitalized, short (50 chars or less) summary @@ -31,19 +31,10 @@ prompt=$( or "Fixes bug." This convention matches up with commit messages generated by commands like git merge and git revert. $hint Below is a diff of the changes. Do not include it verbaitem $(git diff -p --cached | head --bytes="$truncate_at") END @@ -64,4 +55,5 @@ response="$(curl https://api.openai.com/v1/completions \ echo "$response" | jq --raw-output .choices[0].text | sed -e's/[[:space:]]*$//' | sed -e's/^[[:space:]]*//' -
PurpleBooth revised this gist
Feb 12, 2023 . 1 changed file with 32 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,23 +5,47 @@ set -euo pipefail api_key="sk-hahaha" max_tokens=2048 truncate_at=4092 hint="" if [ $# -ne 0 ]; then hint="It is very important to include this information: $@" fi prompt=$( cat <<-END Summarize this diff for a commit message The following is a good commit message Capitalized, short (50 chars or less) summary More detailed explanatory text, if necessary. Wrap it to about 72 characters or so. In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank line separating the summary from the body is critical (unless you omit the body entirely); tools like rebase can get confused if you run the two together. Write your commit message in the imperative: "Fix bug" and not "Fixed bug" or "Fixes bug." This convention matches up with commit messages generated by commands like git merge and git revert. Further paragraphs come after blank lines. - Bullet points are okay, too - Typically a hyphen or asterisk is used for the bullet, followed by a single space, with blank lines in between, but conventions vary here - Use a hanging indent $@ Below is a diff of changes, lines beginning with "-" are deletions, and lines beginning with "+" are additions $(git diff -p --cached | head --bytes="$truncate_at") END ) @@ -31,11 +55,13 @@ request="$(jq --null-input \ --arg "prompt" "$prompt" \ '{"model": "text-davinci-003", "prompt": $prompt, "temperature": 0, "max_tokens": $max_tokens }')" response="$(curl https://api.openai.com/v1/completions \ --silent \ --header "Content-Type: application/json" \ --header "Authorization: Bearer $api_key" \ --data "$request" \ --output -)" echo "$response" | jq --raw-output .choices[0].text | xargs -
PurpleBooth created this gist
Feb 12, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ #!/usr/bin/env bash set -euo pipefail api_key="sk-hahaha" max_tokens=2048 hint="" if [ $# -ne 0 ]; then hint="Hint about the intention of this commit: $@" fi prompt=$( cat <<-END Summarize this diff for a commit message Make the first line a summary of 50 characters or less, then a blank line, remaining text should be wrapped at 72 characters and include additional details $@ A diff of the change: $(git diff -p --cached) END ) request="$(jq --null-input \ --argjson max_tokens "$max_tokens" \ --arg "prompt" "$prompt" \ '{"model": "text-davinci-003", "prompt": $prompt, "temperature": 0, "max_tokens": $max_tokens }')" curl https://api.openai.com/v1/completions \ --silent \ --header "Content-Type: application/json" \ --header "Authorization: Bearer $api_key" \ --data "$request" \ --output - | jq --raw-output .choices[0].text | xargs