|
|
|
@ -1,4 +1,15 @@ |
|
|
|
|
#!/bin/bash |
|
|
|
|
|
|
|
|
|
# Error handling function |
|
|
|
|
# $1 should be the response body |
|
|
|
|
handleError() { |
|
|
|
|
if echo "$1" | jq -e '.error' >/dev/null; then |
|
|
|
|
echo -e "Your request to Open AI API failed: \033[0;31m$(echo $1 | jq -r '.error.type')\033[0m" |
|
|
|
|
echo $1 | jq -r '.error.message' |
|
|
|
|
exit 1 |
|
|
|
|
fi |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
echo -e "Welcome to chatgpt. You can quit with '\033[36mexit\033[0m'." |
|
|
|
|
running=true |
|
|
|
|
|
|
|
|
@ -15,7 +26,7 @@ while $running; do |
|
|
|
|
if [ "$prompt" == "exit" ]; then |
|
|
|
|
running=false |
|
|
|
|
elif [[ "$prompt" =~ ^image: ]]; then |
|
|
|
|
image_url=$(curl https://api.openai.com/v1/images/generations \ |
|
|
|
|
image_response=$(curl https://api.openai.com/v1/images/generations \ |
|
|
|
|
-sS \ |
|
|
|
|
-H 'Content-Type: application/json' \ |
|
|
|
|
-H "Authorization: Bearer $OPENAI_KEY" \ |
|
|
|
@ -23,8 +34,11 @@ while $running; do |
|
|
|
|
"prompt": "'"${prompt#*image:}"'", |
|
|
|
|
"n": 1, |
|
|
|
|
"size": "512x512" |
|
|
|
|
}' | jq -r '.data[0].url') |
|
|
|
|
}') |
|
|
|
|
handleError "$image_response" |
|
|
|
|
image_url=$(echo $image_response | jq -r '.data[0].url') |
|
|
|
|
echo -e "\n\033[36mchatgpt \033[0mYour image was created. \n\nLink: ${image_url}\n" |
|
|
|
|
|
|
|
|
|
if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then |
|
|
|
|
curl -sS $image_url -o temp_image.png |
|
|
|
|
imgcat temp_image.png |
|
|
|
@ -37,7 +51,7 @@ while $running; do |
|
|
|
|
fi |
|
|
|
|
fi |
|
|
|
|
elif [[ "$prompt" == "history" ]]; then |
|
|
|
|
echo -e "\n$(cat ~/.chatgpt_history)" |
|
|
|
|
echo -e "\n$(cat ~/.chatgpt_history)" |
|
|
|
|
else |
|
|
|
|
# escape quotation marks |
|
|
|
|
escaped_prompt=$(echo "$prompt" | sed 's/"/\\"/g') |
|
|
|
@ -51,11 +65,13 @@ while $running; do |
|
|
|
|
"prompt": "'"${escaped_prompt}"'", |
|
|
|
|
"max_tokens": 1000, |
|
|
|
|
"temperature": 0.7 |
|
|
|
|
}' | jq -r '.choices[].text' | sed '1,2d') |
|
|
|
|
}') |
|
|
|
|
|
|
|
|
|
echo -e "\n\033[36mchatgpt \033[0m${response}" |
|
|
|
|
handleError "$response" |
|
|
|
|
response_data=$(echo $response | jq -r '.choices[].text' | sed '1,2d') |
|
|
|
|
echo -e "\n\033[36mchatgpt \033[0m${response_data}" |
|
|
|
|
|
|
|
|
|
timestamp=$(date +"%d/%m/%Y %H:%M") |
|
|
|
|
echo -e "$timestamp $prompt \n$response \n" >> ~/.chatgpt_history |
|
|
|
|
echo -e "$timestamp $prompt \n$response_data \n" >>~/.chatgpt_history |
|
|
|
|
fi |
|
|
|
|
done |
|
|
|
|
done |
|
|
|
|