Skip to content

Instantly share code, notes, and snippets.

@senko
Created March 29, 2023 17:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save senko/68026864d502473eacd56b2e16e40108 to your computer and use it in GitHub Desktop.
Save senko/68026864d502473eacd56b2e16e40108 to your computer and use it in GitHub Desktop.
Small bash script to use ChatGPT from command line
#!/bin/bash
if test -z "$1"; then
echo "Usage: $0 <prompt>"
exit 1
fi
if test -z "$OPENAI_API_KEY"; then
echo "OpenAI key is missing - \$OPENAI_API_KEY must be set"
exit 1
fi
prompt="$*"
curl https://api.openai.com/v1/chat/completions -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"$prompt\"}], \"temperature\": 0.7 }" \
| jq -r '.choices[0].message.content'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment