Skip to content

Instantly share code, notes, and snippets.

@dctremblay
Last active May 28, 2024 14:37
Show Gist options
  • Save dctremblay/cdaf8e419acd43e0bce72741f1d9a309 to your computer and use it in GitHub Desktop.
Save dctremblay/cdaf8e419acd43e0bce72741f1d9a309 to your computer and use it in GitHub Desktop.
quickgpt.sh

In ~/.profile :

alias ask="$HOME/.quickgpt"

alias syn="ask 20 synonyms and their nuances of:"
alias def="ask definition and usage example of:"
alias term="ask 5 most accurate terms and their nuances that describe:"
#!/bin/bash
# Title: QuickGPT
# Version: 0.1
# Date: 2024-05-21
# License: MIT
# Author: dctremblay@dasio.ca
[ $OPENAI_API_KEY ] || { echo "OPENAI_API_KEY env var must be set" >&2; exit 1; }
gptModel="gpt-4o" #gpt-3.5-turbo
role="you answer the most concise and short way possible"
query() {
curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "$(cat <<-EOF
{
"model": "$gptModel",
"messages": $1
}
EOF
)"
}
function esc() { sed 's/"/\\"/g' <<< "$1"; }
prompt="$@"
conv="[{\"role\":\"system\", \"content\": \"$(esc "$role")\"},{\"role\":\"user\", \"content\": \"$(esc "$prompt")\"}]"
jsonAnswer=$(query "${conv}" | jq .choices[0].message)
jq -j .content <<< "$jsonAnswer"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment