Skip to content

Instantly share code, notes, and snippets.

@qapquiz
Created April 1, 2023 12:06
Show Gist options
  • Save qapquiz/c7c80a4f267c1d04247c398d466285a6 to your computer and use it in GitHub Desktop.
Save qapquiz/c7c80a4f267c1d04247c398d466285a6 to your computer and use it in GitHub Desktop.
ask.sh (ask ChatGPT in command line)
#!/bin/bash
function ask_gpt() {
PROMPT=$(gum input --width 80 --placeholder "prompt")
if [[ -z "$PROMPT" ]]; then
exit 0
fi
gum style --foreground 212 "> $PROMPT"
RESULT=$(gum spin -s line --title "Asking ChatGPT..." --show-output -- curl -sS https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPEN_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "'"$PROMPT"'"}]
}' | jq ".choices[0].message.content"
)
printf "$RESULT" | glow
}
while true; do
ask_gpt
done
@qapquiz
Copy link
Author

qapquiz commented Apr 1, 2023

Demo

ask

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment