Skip to content

Instantly share code, notes, and snippets.

@qpwo
Created January 3, 2024 19:32
Show Gist options
  • Save qpwo/80fdcf7222bda1a923bf7c730d075583 to your computer and use it in GitHub Desktop.
Save qpwo/80fdcf7222bda1a923bf7c730d075583 to your computer and use it in GitHub Desktop.
chatgpt cli
#!/usr/bin/bash
# copy into /usr/bin or whatever and chmod+x
# example:
# $ gpt 'untar and ensure subdir (but no extraneous nesting)'
# ```bash mkdir subdir && tar -xvf archive.tar -C subdir --strip-components=1 ```
# https://platform.openai.com/api-keys
OPENAI_API_KEY='...'
# join all args with space:
question="$*"
# escape double quotes:
question="${question//\"/\\\"}"
resp=$(curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-s \
-d '{
"model": "gpt-4-1106-preview",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant. You answer extremely briefly, often with just a line of code."
},
{
"role": "user",
"content": "'"$question"'"
}
]
}')
answer=$(echo $resp | jq -r '.choices[0].message.content')
echo $answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment