Skip to content

Use OPENAI_API_KEY #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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