Skip to content

Instantly share code, notes, and snippets.

@tcodes0
Last active March 20, 2023 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcodes0/7c1c6630ff5a360f3d6d50b68fd3a261 to your computer and use it in GitHub Desktop.
Save tcodes0/7c1c6630ff5a360f3d6d50b68fd3a261 to your computer and use it in GitHub Desktop.
chat-gpt-curl
# 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