Skip to content

Instantly share code, notes, and snippets.

@SCJLabs
Last active March 14, 2024 14:55
Show Gist options
  • Save SCJLabs/fb4e8564222a9ed1d1970caa32936615 to your computer and use it in GitHub Desktop.
Save SCJLabs/fb4e8564222a9ed1d1970caa32936615 to your computer and use it in GitHub Desktop.
Use ChatGPT API from the terminal (MacOS, ZSH)
# ChatGPT
# Ask ChatGPT questions from your terminal. Results are shown in terminal and markdown is formatted with `glow`.
#
# Requirements:
# 1. In terminal, run: `brew install jq glow`
# 2. Get your free API key here from: https://platform.openai.com/account/api-keys
# 3. Paste in your API key below.
# 3. Paste everything in this gist into your `.zshrc` file. Rename aliases to your liking.
# 4. Remember to source your `.zshrc` file using `source ~/.zshrc` or restart terminal for changes to take effect.
#
# How to Use:
# * In terminal, run: `c 'some question to chatgpt'` to ask a question.
# * In terminal, run: `cc` to view the last answer.
# * In terminal, run: `ccc` to view the last answer and have it read out loud.
CHAT_GPT_API_KEY='paste_in_your_api_key_here'
alias c='f() {
curl --silent https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $CHAT_GPT_API_KEY" \
-H "Content-Type: application/json" \
-d "{ \"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"$@\"}]}" \
| jq -r ".choices[0].message.content" \
| tee ~/Documents/chat.md \
| glow
};f'
alias cc='glow ~/Documents/chat.md'
alias ccc='glow ~/Documents/chat.md && cat ~/Documents/chat.md | say --rate="200" --voice='Samantha''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment