Skip to content

Instantly share code, notes, and snippets.

@dave1010
Created November 22, 2023 16:59
Show Gist options
  • Save dave1010/64332d1ede5d7e11e45d93570d973bde to your computer and use it in GitHub Desktop.
Save dave1010/64332d1ede5d7e11e45d93570d973bde to your computer and use it in GitHub Desktop.
GPT-Vision Command line script
#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 '<prompt>' '<image_url>'"
exit 1
fi
PROMPT=$1
IMAGE_URL=$2
# Construct the JSON payload
read -r -d '' PAYLOAD <<EOM
{
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "$PROMPT"
},
{
"type": "image_url",
"image_url": {
"url": "$IMAGE_URL",
"detail": "high"
}
}
]
}
],
"max_tokens": 2000
}
EOM
# Temporary file to store response
TMP_FILE=$(mktemp)
# Execute the curl command and store the response
curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "$PAYLOAD" > "$TMP_FILE"
# Check for errors in the response
if jq -e '.error' "$TMP_FILE" > /dev/null; then
cat "$TMP_FILE"
rm "$TMP_FILE"
exit 1
fi
# Extract and echo the assistant's response
jq -r '.choices[0].message.content' "$TMP_FILE"
# Clean up
rm "$TMP_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment