Last active
March 20, 2023 18:03
-
-
Save tcodes0/7c1c6630ff5a360f3d6d50b68fd3a261 to your computer and use it in GitHub Desktop.
chat-gpt-curl
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
# add to shell init file | |
# invoke with `chatgpt` | |
# write prompt to prompt.txt on current dir | |
# install jq | |
chatgpt() { | |
if ! [ -f ./prompt.txt ]; then | |
echo "\"prompt.txt\" file not found on $PWD" | |
return 1 | |
fi | |
if [ -z "$OPENAI_API_KEY" ]; then | |
echo "\$OPENAI_API_KEY not found in env" | |
return 1 | |
fi | |
PROMPT=$(<"$PWD/prompt.txt") | |
curl https://api.openai.com/v1/chat/completions \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer $OPENAI_API_KEY" \ | |
-d "{ | |
\"model\": \"gpt-3.5-turbo\", | |
\"messages\": [{\"role\": \"user\", \"content\": \"$PROMPT\"}] | |
}" | | |
jq '.choices[0].message.content' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment