Last active
December 31, 2023 10:54
-
-
Save llimllib/b4e7428e434d7161b02d498421f651b9 to your computer and use it in GitHub Desktop.
a bash function to return gpt results. Now improved and made into a full repo: https://github.com/llimllib/gpt-bash-cli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Installation (OS X): | |
# | |
# - save this file as "gpt" somewhere in your path | |
# - get an openai api key at: https://platform.openai.com/account/api-keys | |
# - save the openai api key to your keychain with: | |
# security add-generic-password -s 'openai' -a '<account name>' -w '<api key>' | |
# - If you don't already have `jq`, install it with `brew install jq` or whatever | |
# | |
# If you prefer, you can put your API key directly in here or modify the | |
# `OPENAIKEY` line to use your preferred secrets manager | |
OPENAIKEY=$(security find-generic-password -w -s 'openai') | |
MODEL="gpt-3.5-turbo" | |
TEMPERATURE="0.7" | |
VERBOSE= | |
function usage { | |
cat <<EOF | |
gpt [-vh] [-m <model>] <description> | |
chat with openai's /chat/completions endpoint | |
FLAGS: | |
-v, --verbose: print the URL of the image and the filename when done | |
-h, --help: print this help and exit | |
-m, --model: set the model you want to use. Defaults to $MODEL | |
-t, --temperature: set the temperature. Defaults to $TEMPERATURE | |
EXAMPLE USAGE: | |
gpt write a bash script that uses curl to access the openai API | |
EOF | |
exit 1 | |
} | |
function gptrequest { | |
cat <<EOF | |
{ | |
"model": "$MODEL", | |
"messages": [{ | |
"role": "user", | |
"content": "$@" | |
}], | |
"temperature": $TEMPERATURE | |
} | |
EOF | |
} | |
function gpt { | |
if res=$(curl https://api.openai.com/v1/chat/completions -s \ | |
-H 'Content-Type: application/json' \ | |
-H "Authorization: Bearer $OPENAIKEY" \ | |
-d "$(gptrequest "$@")"); then | |
# if there's an "error" key in the return value, we didn't succeed. | |
# Print and quit | |
if echo "$res" | jq -e 'has("error")' &> /dev/null ; then | |
echo "error: $res" | |
exit 1 | |
fi | |
# print the raw response from openai if verbose is set | |
if [[ -n $VERBOSE ]]; then | |
echo "response from openai: $res" | |
fi | |
echo "$res" | jq -r '.choices[0].message.content' | |
fi | |
} | |
case $1 in | |
help | -h | --help) | |
usage | |
;; | |
-m | --model) | |
shift | |
MODEL=$1 | |
shift | |
;; | |
-t | --temperature) | |
shift | |
TEMPERATURE=$1 | |
shift | |
;; | |
-v | --verbose) | |
VERBOSE=true | |
shift | |
;; | |
-vv | --very-verbose) | |
set -x | |
shift | |
;; | |
esac | |
if [[ -z $OPENAIKEY ]]; then | |
tput setaf 1 | |
printf "ERROR: unable to find an openAI API key.\n\n" | |
tput sgr0 | |
printf "You can set one by editing this script directly or, if you're on a mac, using:\n\n security add-generic-password -s 'openai' -a '<account name>' -w '<api key>'\n\n" | |
exit 1 | |
fi | |
gpt "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gpt please write me a bash function that uses curl to make requests to the openai API | |
Here is a sample bash function that uses curl to make requests to the OpenAI API: | |
``` | |
function openai_request() { | |
API_KEY="your_api_key_here" # Replace with your API key | |
MODEL_NAME="davinci" # Replace with the name of the OpenAI model you want to use | |
URL="https://api.openai.com/v1/${1}" # The API endpoint you want to access, passed as the first argument to the function | |
shift # Remove the first argument from the list of arguments passed to the function | |
DATA="$@" # The remaining arguments are the data you want to send in the request | |
curl -s -X POST \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer ${API_KEY}" \ | |
-d '{"model": "'${MODEL_NAME}'", "prompt": "'${DATA}'"}' \ | |
"${URL}" | |
} | |
``` | |
You can use this function to make requests to any OpenAI API endpoint by passing the endpoint path as the first argument, and any required data as additional arguments. For example, to request the completions API: | |
``` | |
openai_request "completions" "The quick brown fox jumps over the lazy dog." | |
``` | |
This will send a request to the OpenAI API to generate a completion for the given prompt. The response will be returned by the function as a JSON string, which you can parse and manipulate as needed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment