Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions chatgpt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ CHATGPT_CYAN_LABEL="\033[36mchatgpt \033[0m"
PROCESSING_LABEL="\n\033[90mProcessing... \033[0m\033[0K\r"
OVERWRITE_PROCESSING_LINE=" \033[0K\r"

if [[ -z "$OPENAI_KEY" ]]; then
echo "You need to set your OPENAI_KEY to use this script"
echo "You can set it temporarily by running this on your terminal: export OPENAI_KEY=YOUR_KEY_HERE"
if [[ -z "$OPENAI_API_KEY" ]]; then
if [[ -n "$OPENAI_KEY" ]]; then
OPENAI_API_KEY="$OPENAI_KEY"
echo "OPENAI_KEY has been deprecated. Change it to OPENAI_API_KEY."
fi
echo "You need to set your OPENAI_API_KEY to use this script"
echo "You can set it temporarily by running this on your terminal: export OPENAI_API_KEY=YOUR_KEY_HERE"
exit 1
fi

Expand Down Expand Up @@ -83,7 +87,7 @@ handle_error() {
list_models() {
models_response=$(curl https://api.openai.com/v1/models \
-sS \
-H "Authorization: Bearer $OPENAI_KEY")
-H "Authorization: Bearer $OPENAI_API_KEY")
handle_error "$models_response"
models_data=$(echo $models_response | jq -r -C '.data[] | {id, owned_by, created}')
echo -e "$OVERWRITE_PROCESSING_LINE"
Expand All @@ -97,7 +101,7 @@ request_to_completions() {
curl https://api.openai.com/v1/completions \
-sS \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $OPENAI_KEY" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "'"$MODEL"'",
"prompt": "'"$prompt"'",
Expand All @@ -113,7 +117,7 @@ request_to_image() {
image_response=$(curl https://api.openai.com/v1/images/generations \
-sS \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $OPENAI_KEY" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"prompt": "'"${prompt#*image:}"'",
"n": 1,
Expand All @@ -130,7 +134,7 @@ request_to_chat() {
curl https://api.openai.com/v1/chat/completions \
-sS \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $OPENAI_KEY" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "'"$MODEL"'",
"messages": [
Expand Down Expand Up @@ -369,7 +373,7 @@ while $running; do
elif [[ "$prompt" =~ ^model: ]]; then
models_response=$(curl https://api.openai.com/v1/models \
-sS \
-H "Authorization: Bearer $OPENAI_KEY")
-H "Authorization: Bearer $OPENAI_API_KEY")
handle_error "$models_response"
model_data=$(echo $models_response | jq -r -C '.data[] | select(.id=="'"${prompt#*model:}"'")')
echo -e "$OVERWRITE_PROCESSING_LINE"
Expand Down