Skip to content

Instantly share code, notes, and snippets.

@rcmdnk
Last active March 27, 2025 04:35
Show Gist options
  • Save rcmdnk/0c1132b8ac95382c66d988932bd32bc3 to your computer and use it in GitHub Desktop.
Save rcmdnk/0c1132b8ac95382c66d988932bd32bc3 to your computer and use it in GitHub Desktop.
Simple Chat API call script
#!/usr/bin/env bash
_MODEL="gpt-4o"
_ENDPOINT=""
_ENDPOINT_OPENAI="https://api.openai.com/v1/chat/completions"
_ENDPOINT_DEEPSEEK="https://api.deepseek.com/v1/chat/completions"
_ENDPOINT_GEMINI="https://generativelanguage.googleapis.com/v1beta/openai/chat/completions"
_ENDPOINT_OLLAMA="http://localhost:11434/v1/chat/completions"
_KEY_NAME=""
_KEY_NAME_OPENAI="OPENAI_API_KEY"
_KEY_NAME_DEEPSEEK="DEEPSEEK_API_KEY"
_KEY_NAME_GEMINI="GEMINI_API_KEY"
HELP="Usage: ask.sh [-m MODEL] [-e ENDPOINT] [-k KEY_NAME] [-d] [MESSAGE...]"
conf=${XDG_CONFIG_HOME:-$HOME/.config}/chat/config
verbose=0
function encode {
local str="$*"
str=${str//\\/\\\\}
str=${str//\"/\\\"}
str=${str//$'\t'/\\t}
str=${str//$'\n'/\\n}
str=${str//$'\r'/}
echo "$str"
}
function extract {
local key="$1"
shift
local json="$*"
# shellcheck disable=SC2059
printf "$(echo "$json"|sed -n 's/.*"'"$key"'":[[:space:]]*"\([^"\\]*\(\\.[^"\\]*\)*\)".*/\1/p')"
}
if [ -f "$conf" ]; then
# shellcheck disable=SC1090
source "$conf"
fi
while getopts m:e:k:vh opt; do
case $opt in
m)
MODEL=$OPTARG
;;
e)
ENDPOINT=$OPTARG
;;
k)
KEY_NAME=$OPTARG
;;
v)
verbose=1
;;
h)
echo "$HELP"
exit 0
;;
\?)
echo "Invalid option: $OPTARG" 1>&2
echo ""
echo "$HELP" 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
MODEL="${MODEL:-$_MODEL}"
ENDPOINT="${ENDPOINT:-$_ENDPOINT}"
KEY_NAME="${KEY_NAME:-$_KEY_NAME}"
if [[ "$MODEL" =~ ^gpt* ]] || [[ "$MODEL" =~ ^chatgpt* ]] || [[ "$MODEL" =~ ^o1-* ]]; then
ENDPOINT="${ENDPOINT:-$_ENDPOINT_OPENAI}"
KEY_NAME="${KEY_NAME:-$_KEY_NAME_OPENAI}"
elif [[ "$MODEL" =~ ^deepseek* ]]; then
ENDPOINT="${ENDPOINT:-$_ENDPOINT_DEEPSEEK}"
KEY_NAME="${KEY_NAME:-$_KEY_NAME_DEEPSEEK}"
elif [[ "$MODEL" =~ ^gemini* ]]; then
ENDPOINT="${ENDPOINT:-$_ENDPOINT_GEMINI}"
KEY_NAME="${KEY_NAME:-$_KEY_NAME_GEMINI}"
else
ENDPOINT="${ENDPOINT:-$_ENDPOINT_OLLAMA}"
fi
if [ -z "$ENDPOINT" ]; then
echo "ENDPOINT is needed to be set for $MODEL model."
exit 1
fi
key=""
if [ -z "$KEY_NAME" ]; then
if [ "$ENDPOINT" = "$_ENDPOINT_OPENAI" ] || [ "$ENDPOINT" = "$_ENDPOINT_DEEPSEEK" ] || [ "$ENDPOINT" = "$_ENDPOINT_GEMINI" ]; then
echo "KEY_NAME is needed to be set for $MODEL model."
exit 1
fi
else
key="${!KEY_NAME}"
if [ -z "$key" ]; then
echo "$KEY_NAME is not set. Please set $KEY_NAME environment variable."
exit 1
fi
fi
if [ -p /dev/stdin ] || [ -f /dev/stdin ]; then
content=$(cat /dev/stdin)
content="${content}
$*"
else
content="$*"
fi
content=$(encode "$content")
input_json='{"model": "'$MODEL'", "messages": [{"role": "user", "content": "'"$content"'"}]}'
if [ "$verbose" = 1 ]; then
echo "Request:"
echo "$input_json"
echo ""
fi
json=$(curl -s "$ENDPOINT" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $key" \
-d "$input_json")
if [ "$verbose" = 1 ]; then
echo "Response:"
echo "$json"
echo ""
fi
output=$(extract content "$json")
if [ -z "$output" ];then
echo "Failed to get a response from the API (ENDPOINT=$ENDPOINT, MODEL=$MODEL)." 1>&2
echo "" 1>&2
extract message "$json" >&2
exit 1
fi
echo "$output"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment