-
-
Save dave1010/64332d1ede5d7e11e45d93570d973bde to your computer and use it in GitHub Desktop.
GPT-Vision Command line script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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