Skip to content

Instantly share code, notes, and snippets.

@raaar
Last active January 26, 2023 21:53
Show Gist options
  • Save raaar/6b165e9798ce91665170e6b108e5b6b3 to your computer and use it in GitHub Desktop.
Save raaar/6b165e9798ce91665170e6b108e5b6b3 to your computer and use it in GitHub Desktop.
Ask a question from the terminal: sh ./chat.sh "what's the time?"
#!/bin/bash
# Set the API key
source ~/env.sh
endpoint="https://api.openai.com/v1/completions"
max_tokens=${2:-50}
prompt=$1
temperature=0.5
model="text-davinci-003"
response=$(curl https://api.openai.com/v1/completions \
-H "Content-Type: application/json" \
-H "charset=UTF-8" \
-H "Authorization: Bearer $api_key" \
-d "{\"model\": \"$model\", \"prompt\": \"$prompt\", \"temperature\": $temperature, \"max_tokens\": $max_tokens}")
printf '%s' "$response" | jq -r '.choices[0].text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment