Skip to content

Instantly share code, notes, and snippets.

@richinseattle
Created November 10, 2023 00:17
Show Gist options
  • Save richinseattle/e04728522c523986983e11c23f9cca70 to your computer and use it in GitHub Desktop.
Save richinseattle/e04728522c523986983e11c23f9cca70 to your computer and use it in GitHub Desktop.
bash OpenAI GPT client loop
#!/bin/bash
# bashGPT - rjohnson@fuzzing.io
[ "${OPENAI_API_KEY}" == "" ] && echo "Please set OPENAI_API_KEY env variable" && exit
[ "$(which jq)" == "" ] && echo "Please install the jq program" && exit
[ "$(which curl)" == "" ] && echo "Please install the curl program" && exit
PROMPT="$1"
while true
do
echo -n ">>>> "
[ "$PROMPT" != "" ] && echo $PROMPT
[ "$PROMPT" == "" ] && read PROMPT
REPLY="$(curl -s 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')"
PROMPT=""
echo -e ${REPLY:3:-1}
done
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment