Skip to content

Instantly share code, notes, and snippets.

@amn41
Created March 2, 2023 08:40
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amn41/0b82b7e78778f3d0c7aa99869bd218f2 to your computer and use it in GitHub Desktop.
Save amn41/0b82b7e78778f3d0c7aa99869bd218f2 to your computer and use it in GitHub Desktop.
chatGPT on the command line - add to your bashrc/zshrc
function gpt() {
local url="https://api.openai.com/v1/chat/completions"
local model="gpt-3.5-turbo"
local body="{\"model\":\"$model\", \"messages\":[{\"role\": \"user\", \"content\": \"$1\"}]}"
local headers="Content-Type: application/json"
local auth="Authorization: Bearer ${OPENAI_API_KEY}"
curl -s -H "$headers" -H "$auth" -d "$body" "$url" \
| jq -r '.choices[0].message.content'
}
@dingusagar
Copy link

nice,
just confirming, this wouldn't handle multi-turn conversations, right? if we want that we need to store the history in the "messages" attribute of the body. correct?

@amn41
Copy link
Author

amn41 commented Mar 2, 2023

yes, that's right.

@hecker
Copy link

hecker commented Mar 4, 2023

I got the following error:

❯ gpt "hello"
gpt:8: command not found: jq

But really nice idea btw. 👍

@dingusagar
Copy link

@hecker, it looks like jq is not installed on your system. its a tool to parse json. you can install jq like this.
brew install jq # if you are on mac
sudo apt install jq -y # if you are on ubuntu.

@himalayasharma
Copy link

himalayasharma commented Mar 13, 2023

I have exported my Open AI API token as an environment variable (OPENAI_API_KEY). Then I added the function to my .zshrc. After running ~/.zshrc and running gpt "hello" subsequently I am always getting a NULL return value. How to fix this?

@JuanBarros2
Copy link

I have the same problem @himalayasharma but when I debug the return I realize that I got the message:
"You exceeded your current quota, please check your plan and billing details.". Check if you have quota too. 😄

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