Skip to content

Instantly share code, notes, and snippets.

@eliask
Last active August 8, 2024 22:58
Show Gist options
  • Save eliask/f30f993da8d265602d9375586d2647f1 to your computer and use it in GitHub Desktop.
Save eliask/f30f993da8d265602d9375586d2647f1 to your computer and use it in GitHub Desktop.
Groq simple CLI
#! /usr/bin/env bash
# Usage: groq.sh "Convert foo to bar" < file.foo > file.bar
set -Eeuo pipefail
content="$@
$(cat)"
escaped_content=$(jq -Rsa . <<< "$content")
model="llama-3.1-8b-instant"
json=$(jq -n --arg content "$escaped_content" --arg model "$model" \
'{messages: [{role: "user", content: $content}], model: $model}')
headers_file=$(mktemp)
trap "rm -f $headers_file" EXIT
# max 5 retries
for _ in {1..5}; do
response=$(
curl -s -D "$headers_file" -X POST "https://api.groq.com/openai/v1/chat/completions" \
-H "Authorization: Bearer $GROQ_API_KEY" \
-H "Content-Type: application/json" \
-d "$json"
)
# Check if Retry-After header is set
if grep -q "Retry-After" "$headers_file"; then
retry_after=$(grep "Retry-After" "$headers_file" | awk '{print $2}')
echo "Retry-After header is set. Retrying after $retry_after seconds." >&2
sleep "$retry_after"
else
# break if the response json has "id" key
has_id=$(echo "$response" | jq -e 'has("id")')
has_error=$(echo "$response" | jq -e 'has("error")')
if [ "$has_id" = true ] && [ "$has_error" = false ]; then
# encountered some invalid escape sequences in the past
escaped_response=$(echo "$response" | jq -Rsa .)
echo "$escaped_response" | jq -r . | jq -r .choices[].message.content
fi
fi
done
echo "Failed to get a valid response after 5 retries." >&2
echo $response >&2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment