Skip to content

Instantly share code, notes, and snippets.

@dctremblay
Created June 2, 2024 08:02
Show Gist options
  • Save dctremblay/2b9585f586c44ac7d25f3586406b036f to your computer and use it in GitHub Desktop.
Save dctremblay/2b9585f586c44ac7d25f3586406b036f to your computer and use it in GitHub Desktop.
summarizes text from stdin using openai chat completion api
#!/bin/bash
# Title: summarize.sh
# Version: 0.1
# Date: 2024-06-02
# License: MIT
[ $OPENAI_API_KEY ] || { echo "OPENAI_API_KEY must be set" >&2; exit 1; }
: "${SYSMSG:="You summarize text to make it as short as possible"}"
: "${MODEL:="gpt-3.5-turbo"}"
query() {
curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "$(jq -n --arg model "$MODEL" --argjson messages "$(cat)" '{$model,$messages}')"
}
parse() { jq -r '.choices[0].message.content'; }
toObj() { jq -n --arg role "$1" --arg content "$(cat)" '{$role,$content}'; }
toArray() { jq -s '.'; }
(
toObj system <<< "$SYSMSG"
toObj user
) | toArray | query | parse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment