Skip to content
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
19 changes: 13 additions & 6 deletions chatgpt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ 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 need to set your OPENAI_KEY and OPENAI_BASE_URL(optional) to use this script"
echo "You can set it temporarily by running this on your terminal: export OPENAI_KEY=YOUR_KEY_HERE"
exit 1
fi

if [[ -z "$OPENAI_BASE_URL" ]]; then
OPENAI_BASE_URL="https://api.openai.com"
echo "Use official api url."
else
echo "Use \"${OPENAI_BASE_URL}\" as api url."
fi

usage() {
cat <<EOF
A simple, lightweight shell script to use OpenAI's Language Models and DALL-E from the terminal without installing Python or Node.js. Open Source and written in 100% Shell (Bash)
Expand Down Expand Up @@ -81,7 +88,7 @@ handle_error() {
# request to openAI API models endpoint. Returns a list of models
# takes no input parameters
list_models() {
models_response=$(curl https://api.openai.com/v1/models \
models_response=$(curl ${OPENAI_BASE_URL}/v1/models \
-sS \
-H "Authorization: Bearer $OPENAI_KEY")
handle_error "$models_response"
Expand All @@ -94,7 +101,7 @@ list_models() {
request_to_completions() {
local prompt="$1"

curl https://api.openai.com/v1/completions \
curl ${OPENAI_BASE_URL}/v1/completions \
-sS \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $OPENAI_KEY" \
Expand All @@ -110,7 +117,7 @@ request_to_completions() {
# $1 should be the prompt
request_to_image() {
local prompt="$1"
image_response=$(curl https://api.openai.com/v1/images/generations \
image_response=$(curl ${OPENAI_BASE_URL}/v1/images/generations \
-sS \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $OPENAI_KEY" \
Expand All @@ -127,7 +134,7 @@ request_to_chat() {
local message="$1"
escaped_system_prompt=$(escape "$SYSTEM_PROMPT")

curl https://api.openai.com/v1/chat/completions \
curl ${OPENAI_BASE_URL}/v1/chat/completions \
-sS \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $OPENAI_KEY" \
Expand Down Expand Up @@ -367,7 +374,7 @@ while $running; do
elif [[ "$prompt" == "models" ]]; then
list_models
elif [[ "$prompt" =~ ^model: ]]; then
models_response=$(curl https://api.openai.com/v1/models \
models_response=$(curl ${OPENAI_BASE_URL}/v1/models \
-sS \
-H "Authorization: Bearer $OPENAI_KEY")
handle_error "$models_response"
Expand Down