-
Notifications
You must be signed in to change notification settings - Fork 0
/
igpt.sh
executable file
·448 lines (345 loc) · 8.75 KB
/
igpt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env bash
#
# Copyright (c) 2023 jhonesto
# Licensed under the MIT License (2023). See LICENSE for details or
# See <https://opensource.org/licenses/MIT> for details.
#
# DEFAULT VALUES
TOKEN=${TOKEN:-""}
MODEL=${MODEL:-"gpt-3.5-turbo-instruct"}
MAX_TOKENS=${MAX_TOKENS:-"1000"}
TEMPERATURE=${TEMPERATURE:-"1.0"}
#COLORS
RED="\033[31m"
YELLOW="\033[33m"
GREEN="\033[36m"
COLOR_OFF="\033[0m"
INPUT_COLOR=${INPUT_COLOR:-$GREEN}
OUTPUT_COLOR=${OUTPUT_COLOR:-$YELLOW}
INFO_COLOR=${INFO_COLOR:-$YELLOW}
ERROR_COLOR=${ERROR_COLOR:-$RED}
FORECOLOR=${FORECOLOR:-$COLOR_OFF}
#MESSAGES
OPT_INTERACT="Interact to ChatGPT"
OPT_INSERT_TOKEN="Insert Token"
OPT_CHANGE_MODEL="Change Model"
OPT_CHANGE_TEMPERATURE="Change Temperature"
OPT_CHANGE_MAX_TOKENS="Change Max Tokens"
OPT_QUIT="Quit"
MAIN_MENU_QUESTION="What would you like to do?"
MODEL_MENU_QUESTION="Which model do you prefer to use?"
ENTER_YOUR_API_TOKEN="Please enter your api token: "
YOUR_API_TOKEN_IS="Your api token is "
YOUR_MODEL_IS="Your new model is"
YOUR_CURRENT_MODEL_IS="Your current model is"
INSERT_TEMPERATURE="Please enter a temperature: "
YOUR_TEMPERATURE_IS="Your new temperature is:"
YOUR_CURRENT_TEMPERATURE_IS="Your current temperature is:"
TEMPERATURE_MUST_BE_BETWEEN_X_AND_Y="Temperature must be between 0 and 2."
INSERT_MAX_TOKENS="Please enter your max tokens:"
YOUR_MAXIMUM_TOKENS="Your max. tokens:"
MAX_TOKENS_INVALID="Max token must be an integer!"
MAX_TOKENS_ZERO="Max token must be greater than zero!"
MAXIMUM_TOKENS_FOR_THIS_MODEL="Max tokens for"
MAXIMUM_TOKENS_FOR_THIS_MODEL_INVALID="Invalid number. Max tokens for"
YOUR_TOKEN_IS_EMPTY_OR_NULL="Your Token is empty or null."
ROLLING_BACK="Rolling back"
YOUR_MAX_TOKENS_IS_EMPTY_OR_NULL="Your Max Tokens is empty or null."
YOUR_TEMPERATURE_IS_EMPTY_OR_NULL="Your Temperature is empty or null."
EXITING_APPLICATION="Exiting the application."
INVALID_OPTION="Invalid option. Please try again."
INVALID_MODEL="Invalid option. Return to main? (y|n)"
CHOOSE_VALID_MODEL_INFO="Choose a valid model."
RETURN_TO_MAIN_MENU="Returning to the main menu."
MODEL_HEADER="Model"
MAX_TOKENS_HEADER="Max Tokens"
MENU_INFO="[type exit to quit, ? to menu]"
INSERT_YOUR_PROMPT="Insert your prompt:"
COMMAND_EXIT="exit"
COMMAND_MENU="?"
LABEL_ERROR="ERROR"
ERROR_000="Verify your Internet connection."
declare -i TRUE=0
declare -i FALSE=1
declare -i EXIT_SUCESS=0
declare -i EXIT_ERROR=1
declare -i ZERO=0
EMPTY=""
OPT1=$OPT_INTERACT
OPT2=$OPT_INSERT_TOKEN
OPT3=$OPT_CHANGE_MODEL
OPT4=$OPT_CHANGE_TEMPERATURE
OPT5=$OPT_CHANGE_MAX_TOKENS
OPT6=$OPT_QUIT
options=("$OPT1" "$OPT2" "$OPT3" "$OPT4" "$OPT5" "$OPT6" )
MOD1="babbage-002"
MOD2="davinci-002"
MOD3="gpt-3.5-turbo-instruct"
MOD4="text-davinci-003"
models=( "$MOD1" "$MOD2" "$MOD3" "$MOD4" )
declare -A arr_models=(
["$MOD1"]=16384
["$MOD2"]=16384
["$MOD3"]=4096
["$MOD4"]=4096
)
PS3_MAIN_MENU="
$MAIN_MENU_QUESTION [1-${#options[@]}] "
PS3_MODEL_MENU="
$MODEL_MENU_QUESTION [1-${#arr_models[@]}] "
#FUNCTIONS
print_input () {
printf "$INPUT_COLOR\n$1$FORECOLOR"
}
print_output () {
echo -e "$OUTPUT_COLOR\n$1$FORECOLOR"
}
print_error () {
echo -e "$ERROR_COLOR\n$1\n$FORECOLOR"
}
print_info () {
printf "$INFO_COLOR\n$1\n$FORECOLOR"
}
print_model () {
printf "$INFO_COLOR%-25s %s\n$FORECOLOR" "$1" "$2"
}
is_empty_or_null () {
if [ -z "$1" ] || [ -z "${1// }" ]; then
return $TRUE
fi
return $FALSE
}
is_not_empty_or_null () {
if ! is_empty_or_null $1; then
return $TRUE
fi
return $FALSE
}
is_valid_number () {
if [[ $1 =~ ^[0-9]+$ ]]; then
return $TRUE
fi
return $FALSE
}
exit_app () {
print_info "$EXITING_APPLICATION\n"
exit $1
}
insert_token () {
local OLD_TOKEN=${TOKEN:-$EMPTY}
print_input "$ENTER_YOUR_API_TOKEN"
read TOKEN
if is_not_empty_or_null $TOKEN; then
print_info "$YOUR_API_TOKEN_IS $TOKEN"
elif is_empty_or_null $TOKEN && is_not_empty_or_null $OLD_TOKEN; then
print_info "$YOUR_TOKEN_IS_EMPTY_OR_NULL"
print_info "$ROLLING_BACK to $OLD_TOKEN"
TOKEN=$OLD_TOKEN
fi
}
change_model () {
MODEL="$1"
print_info "$YOUR_MODEL_IS $MODEL"
if [[ ${arr_models[$MODEL]} -lt $MAX_TOKENS ]]; then
MAX_TOKENS=${arr_models[$key]}
print_info "$YOUR_MAXIMUM_TOKENS $MAX_TOKENS"
fi
}
change_temperature () {
local NEW_TEMP
print_info "$YOUR_CURRENT_TEMPERATURE_IS $TEMPERATURE"
print_input "$INSERT_TEMPERATURE"
read NEW_TEMP
if [[ $NEW_TEMP =~ ^([0-2]|[0-1]\.[0-9])$ ]]; then
TEMPERATURE=$( printf "%.1f" $NEW_TEMP)
print_info "$YOUR_TEMPERATURE_IS $TEMPERATURE"
else
print_info "$TEMPERATURE_MUST_BE_BETWEEN_X_AND_Y"
fi
}
insert_max_tokens () {
print_info "$MAXIMUM_TOKENS_FOR_THIS_MODEL $MODEL: ${arr_models[$MODEL]}"
print_info "$YOUR_MAXIMUM_TOKENS $MAX_TOKENS"
local NEW_MAX_TOKENS
print_input "$INSERT_MAX_TOKENS "
read NEW_MAX_TOKENS
if is_valid_number $NEW_MAX_TOKENS; then
if [[ $(echo "$NEW_MAX_TOKENS" | sed s/^0*// ) -gt $ZERO ]]; then
if [[ ${arr_models[$MODEL]} -ge $NEW_MAX_TOKENS ]]; then
MAX_TOKENS=$(echo "$NEW_MAX_TOKENS" | sed s/^0*// )
print_info "$YOUR_MAXIMUM_TOKENS $MAX_TOKENS"
else
print_info "$MAXIMUM_TOKENS_FOR_THIS_MODEL_INVALID $MODEL: ${arr_models[$MODEL]}"
fi
else
print_info "$MAX_TOKENS_ZERO"
fi
else
print_info "$MAX_TOKENS_INVALID"
fi
}
check_api_token () {
while is_empty_or_null $TOKEN ; do
print_info "$YOUR_TOKEN_IS_EMPTY_OR_NULL"
insert_token
done
}
check_max_tokens () {
while is_empty_or_null $MAX_TOKENS; do
print_info "$YOUR_MAX_TOKENS_IS_EMPTY_OR_NULL"
insert_max_tokens
done
}
check_temperature () {
while is_empty_or_null $TEMPERATURE; do
print_info "$YOUR_TEMPERATURE_IS_EMPTY_OR_NULL"
change_temperature
done
}
check_parameters () {
check_api_token
check_max_tokens
check_temperature
}
show_models_menu () {
select opt in "${models[@]}"
do
case $opt in
"$MOD1")
change_model "$MOD1"
break
;;
"$MOD2")
change_model "$MOD2"
break
;;
"$MOD3")
change_model "$MOD3"
break
;;
"$MOD4")
change_model "$MOD4"
break
;;
*)
print_info "$INVALID_MODEL"
read -re choice
case $choice in
[Yy]*)
break
;;
[Nn]*)
print_info "$CHOOSE_VALID_MODEL_INFO"
;;
*)
break
;;
esac
;;
esac
done
}
show_models () {
print_model "$MODEL_HEADER" "$MAX_TOKENS_HEADER"
for key in "${!arr_models[@]}"; do
print_model "$key" "${arr_models[$key]}"
done
}
show_current_model () {
print_info "$YOUR_CURRENT_MODEL_IS $MODEL"
print_info
}
model_maintenance () {
show_models
show_current_model
set_ps3 "$PS3_MODEL_MENU"
show_models_menu
print_info "$RETURN_TO_MAIN_MENU"
set_ps3 "$PS3_MAIN_MENU"
}
show_menu () {
select opt in "${options[@]}"
do
case $opt in
"$OPT1")
break
;;
"$OPT2")
insert_token
;;
"$OPT3")
model_maintenance
;;
"$OPT4")
change_temperature
;;
"$OPT5")
insert_max_tokens
;;
"$OPT6")
exit_app $EXIT_SUCESS
;;
*)
print_info "$INVALID_OPTION"
;;
esac
done
}
show_figlet () {
echo "
+-+-+-+-+-+-+-+-+-+-+-+
_ __ ___ _____
| | / /\`_ | |_) | |
|_| \_\_/ |_| |_|
+-+-+ +-+-+-+ +-+-+-+-+
|b y| |j h o n e s t o|
+-+-+ +-+-+-+ +-+-+-+-+
"
}
set_ps3 () {
PS3=$1
}
# STARTING THE APPLICATION
set_ps3 "$PS3_MAIN_MENU"
show_figlet
check_parameters
print_info "$MENU_INFO"
while true
do
print_input "$INSERT_YOUR_PROMPT\n\n"
read -re message
if [ "$message" == "$COMMAND_EXIT" ]; then
exit_app $EXIT_SUCESS
elif [ "$message" == "$COMMAND_MENU" ]; then
print_info
show_menu
elif is_not_empty_or_null $message; then
check_parameters
prompt=$(echo "$message" | sed 's/\"/\\u0022/g; s/\\/\\\\/g')
OUTPUT=$(curl -sw "%{http_code}" https://api.openai.com/v1/completions \
-H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" \
-d "{\"model\": \"$MODEL\", \"prompt\": \"${prompt}\", \"temperature\": $TEMPERATURE, \"max_tokens\": $MAX_TOKENS}" \
)
HTTP_STATUS=${OUTPUT: -3}
OUTPUT=${OUTPUT::-3}
TEXT=$EMPTY
ERROR=$EMPTY
if [ "$HTTP_STATUS" = "200" ]; then
TEXT=$( echo "$OUTPUT" | python -c "import sys, json; print(json.load(sys.stdin)['choices'][0]['text'].strip())" 2>/dev/null)
print_output "$TEXT"
elif [ "$HTTP_STATUS" = "000" ]; then
OUTPUT="$ERROR_000"
print_error "$LABEL_ERROR: $HTTP_STATUS - $OUTPUT"
else
ERROR=$( echo "$OUTPUT" | python -c "import sys, json; print(json.load(sys.stdin)['error']['message'])" 2>/dev/null )
if is_not_empty_or_null $ERROR; then
print_info "$ERROR"
else
print_error "$LABEL_ERROR: $HTTP_STATUS - $OUTPUT"
exit_app $EXIT_ERROR
fi
fi
else
print_info "\n$MENU_INFO"
fi
done