Skip to content

Instantly share code, notes, and snippets.

@eSlider
Created December 8, 2022 03:42
Show Gist options
  • Save eSlider/b5b509dfcb59dca25e36517b0512de65 to your computer and use it in GitHub Desktop.
Save eSlider/b5b509dfcb59dca25e36517b0512de65 to your computer and use it in GitHub Desktop.
OpenAI Chat CLI
#!/bin/bash
## Install
# apt-get install -y jq
## Create an key here: https://beta.openai.com/account/api-keys
export OPENAI_API_KEY='sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
## AI Model
model=text-davinci-003
## Call API and save results
response=$(curl -s https://api.openai.com/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "{\"model\": \"$model\", \"prompt\": \"$1\", \"temperature\": 0.5, \"max_tokens\": 1024}")
## Save prompt into history
echo "$1" | jq . >> ~/.openai-gpt-3-history
echo ";\n\n\n\n" >> ~/.openai-gpt-3-history
## Save result into history
echo "$response" | jq . >> ~/.openai-gpt-3-history
echo ";\n\n\n\n" >> ~/.openai-gpt-3-history
# Show only first result
echo "$response" | jq -r .choices[0].text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment