I recently discovered that ChatGPT’s web chat now uses the faster (but dumber) Turbo model, so I’ve been looking at ways to use the OpenAI API in my daily processes. I pretty much live with a command prompt open, so why not connect it to ChatGPT?
Ends up, it’s insanely easy. Just add this to your .bash_profile and make sure to swap in your OpenAI API token:
ask() {
# Run the curl command
RESULT=$(curl -sS --location --insecure --request POST 'https://api.openai.com/v1/chat/completions' \
--header 'Authorization: Bearer YOUR_API_TOKEN_GOES_HERE' \
--header 'Content-Type: application/json' \
--data-raw "{
\"model\": \"gpt-4-1106-preview\",
\"max_tokens\": 2000,
\"messages\": [
{\"role\": \"system\", \"content\": \"Please keep your answer extremely brief if possible.\"},
{\"role\": \"user\", \"content\": \"$*\"}
]
}" | sed -En 's/.*"content": "(.*)"/\1/p' | sed 's/\\n/\n/g' | fold -s)
# Print the result in blue
echo -e "\033[34m$RESULT\033[0m"
echo ""
}Now it’s this easy to ask ChatGPT:
> ask what is 1 + 1
<mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-pale-cyan-blue-color">1 + 1 equals 2.</mark>
