Created
March 5, 2023 19:08
-
-
Save cohan/7d2abfa56b2dfdaae98bf70e84c62218 to your computer and use it in GitHub Desktop.
Talk to GPT from the command line
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 | |
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