Skip to content

Instantly share code, notes, and snippets.

@chr5tphr
Created September 18, 2023 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chr5tphr/74b9ae8c86fa65fd82e5a74fa7d9b8fe to your computer and use it in GitHub Desktop.
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.
#!/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