Skip to content

Instantly share code, notes, and snippets.

@nicksherron
Last active January 31, 2024 07:18
Show Gist options
  • Save nicksherron/5ab08d4d65d3dc29bf9c27ee5cea98e8 to your computer and use it in GitHub Desktop.
Save nicksherron/5ab08d4d65d3dc29bf9c27ee5cea98e8 to your computer and use it in GitHub Desktop.
Openai completion api bash function
#!/usr/bin/env bash
# Description: A function to use openai's gpt-4-turbo-preview chat completions
# to generate responses to input. It uses redis to cache responses for 1 hour
# and has a flag to skip cached results.
# Usage: ai [-s|--skip-cache] [input]
function ai {
## check if we should skip cache
skip_cache=false
while true; do
case $1 in
-s|--skip-cache) skip_cache=true; shift ;;
*) break ;;
-h|--help) echo "Usage: ai [-s|--skip-cache] [input]"; return ;;
esac
done
# get input from stdin if no args
if [[ -z $1 ]]; then
input=$(cat)
else
input=$@
fi
# make cache key
cache_key=$(echo "$input" | sha256sum | awk '{print "ai:" $1}')
# check cache
if [[ $skip_cache = false ]] && [[ $(redis-cli get $cache_key) ]]; then
echo $(redis-cli get $cache_key)
return
fi
escaped_input=$(echo "$input" | sed 's/"/\\"/g' | sed "s/'/\\'/g" | sed 's/\n/\\n/g')
body=$( echo $(cat <<EOF
{
"model": "gpt-4-turbo-preview",
"messages": [
{
"role": "user",
"content": "$escaped_input"
}
],
"temperature": 0,
"max_tokens": 4095,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
}
EOF
) | jq -c .)
out=$(curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "$body" | jq '.choices[0].message.content')
redis-cli setex $cache_key 3600 "$out"
echo $out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment