Created
September 18, 2023 14:20
-
-
Save chr5tphr/74b9ae8c86fa65fd82e5a74fa7d9b8fe to your computer and use it in GitHub Desktop.
Use ChatGPT to rephrase scientific sentences on the command line. May be used as a template for other ChatGPT-based CLI tools.
This file contains 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 characters
#!/usr/bin/env bash | |
die(){ | |
echo "$@" | |
exit 1 | |
} | |
APIKEY="$(gpg -qd ~/.config/apikey/openai.gpg)" | |
[ "$APIKEY" ] || die "Failed to fetch api key." | |
export SYSMSG="You are an assistant that rephrases sentences for camera-ready scientific publications. Do not resolve latex code. Provide three suggestions separated by empty newlines." | |
export USERMSG="${1}" | |
[ "$USERMSG" ] || export USERMSG="$(cat)" | |
[ "$USERMSG" ] || die "Prompt is emtpy!" | |
make-payload(){ | |
jq -n '{ | |
"model": "gpt-3.5-turbo", | |
"messages": [ | |
{ | |
"role": "system", | |
"content": env.SYSMSG | |
}, | |
{ | |
"role": "user", | |
"content": env.USERMSG | |
} | |
], | |
"n": 1 | |
}' || die "Failed making payload!" | |
} | |
curl -sS https://api.openai.com/v1/chat/completions \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer ${APIKEY}" \ | |
-d "$(make-payload)" | jq -r '.choices | [.[] | .message.content] | join("\n\n")' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment