Skip to content

Instantly share code, notes, and snippets.

@cohan
Created March 5, 2023 19:08
Show Gist options
  • Save cohan/7d2abfa56b2dfdaae98bf70e84c62218 to your computer and use it in GitHub Desktop.
Save cohan/7d2abfa56b2dfdaae98bf70e84c62218 to your computer and use it in GitHub Desktop.
Talk to GPT from the command line
#!/bin/bash
api_key='xyzzy'
input="$@"
output=`curl --silent --location --request POST 'https://api.openai.com/v1/chat/completions' \
--header "Authorization: Bearer ${api_key}" \
--header 'Content-Type: application/json' \
--data-raw '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "'"${input}"'"}]
}'`
error=`echo ${output} | jq -r .error.message`
if [[ "${error}" != "null" ]];
then
echo "Error: ${error}"
else
echo "${output}" | jq -r '.choices[]'.message.content
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment