@@ -63,24 +63,24 @@ handle_error() {
63
63
# request to OpenAI API completions endpoint function
64
64
# $1 should be the request prompt
65
65
request_to_completions () {
66
- request_prompt =" $1 "
66
+ local prompt =" $1 "
67
67
68
- response= $( curl https://api.openai.com/v1/completions \
68
+ curl https://api.openai.com/v1/completions \
69
69
-sS \
70
70
-H ' Content-Type: application/json' \
71
71
-H " Authorization: Bearer $OPENAI_KEY " \
72
72
-d ' {
73
73
"model": "' " $MODEL " ' ",
74
- "prompt": "' " ${request_prompt} " ' ",
74
+ "prompt": "' " $prompt " ' ",
75
75
"max_tokens": ' $MAX_TOKENS ' ,
76
76
"temperature": ' $TEMPERATURE '
77
- }' )
77
+ }'
78
78
}
79
79
80
80
# request to OpenAI API image generations endpoint function
81
81
# $1 should be the prompt
82
82
request_to_image () {
83
- prompt=" $1 "
83
+ local prompt=" $1 "
84
84
image_response=$( curl https://api.openai.com/v1/images/generations \
85
85
-sS \
86
86
-H ' Content-Type: application/json' \
@@ -95,8 +95,8 @@ request_to_image() {
95
95
# request to OpenAPI API chat completion endpoint function
96
96
# $1 should be the message(s) formatted with role and content
97
97
request_to_chat () {
98
- message=" $1 "
99
- response= $( curl https://api.openai.com/v1/chat/completions \
98
+ local message=" $1 "
99
+ curl https://api.openai.com/v1/chat/completions \
100
100
-sS \
101
101
-H ' Content-Type: application/json' \
102
102
-H " Authorization: Bearer $OPENAI_KEY " \
@@ -108,20 +108,19 @@ request_to_chat() {
108
108
],
109
109
"max_tokens": ' $MAX_TOKENS ' ,
110
110
"temperature": ' $TEMPERATURE '
111
- }' )
111
+ }'
112
112
}
113
113
114
114
# build chat context before each request for /completions (all models except
115
115
# gpt turbo and gpt 4)
116
- # $1 should be the chat context
117
- # $2 should be the escaped prompt
116
+ # $1 should be the escaped request prompt,
117
+ # it extends $chat_context
118
118
build_chat_context () {
119
- chat_context=" $1 "
120
- escaped_prompt=" $2 "
119
+ local escaped_request_prompt=" $1 "
121
120
if [ -z " $chat_context " ]; then
122
- chat_context=" $CHAT_INIT_PROMPT \nQ: $escaped_prompt "
121
+ chat_context=" $CHAT_INIT_PROMPT \nQ: $escaped_request_prompt "
123
122
else
124
- chat_context=" $chat_context \nQ: $escaped_prompt "
123
+ chat_context=" $chat_context \nQ: $escaped_request_prompt "
125
124
fi
126
125
request_prompt=" ${chat_context// $' \n ' / \\ n} "
127
126
}
@@ -130,13 +129,12 @@ build_chat_context() {
130
129
# gpt turbo and gpt 4)
131
130
# builds chat context from response,
132
131
# keeps chat context length under max token limit
133
- # $1 should be the chat context
134
- # $2 should be the response data (only the text)
132
+ # * $1 should be the escaped response data
133
+ # * it extends $chat_context
135
134
maintain_chat_context () {
136
- chat_context=" $1 "
137
- response_data=" $2 "
135
+ local escaped_response_data=" $1 "
138
136
# add response to chat context as answer
139
- chat_context=" $chat_context ${chat_context: +\n } \nA: ${response_data // $' \n ' / \\ n} "
137
+ chat_context=" $chat_context ${chat_context: +\n } \nA: $escaped_response_data "
140
138
# check prompt length, 1 word =~ 1.3 tokens
141
139
# reserving 100 tokens for next user prompt
142
140
while (( $(echo "$chat_context " | wc - c) * 1 , 3 > (MAX_TOKENS - 100 )) ); do
@@ -149,36 +147,29 @@ maintain_chat_context() {
149
147
150
148
# build user chat message function for /chat/completions (gpt models)
151
149
# builds chat message before request,
152
- # $1 should be the chat message
153
- # $2 should be the escaped prompt
150
+ # $1 should be the escaped request prompt,
151
+ # it extends $chat_message
154
152
build_user_chat_message () {
155
- chat_message=" $1 "
156
- escaped_prompt=" $2 "
153
+ local escaped_request_prompt=" $1 "
157
154
if [ -z " $chat_message " ]; then
158
- chat_message=" {\" role\" : \" user\" , \" content\" : \" $escaped_prompt \" }"
155
+ chat_message=" {\" role\" : \" user\" , \" content\" : \" $escaped_request_prompt \" }"
159
156
else
160
- chat_message=" $chat_message , {\" role\" : \" user\" , \" content\" : \" $escaped_prompt \" }"
157
+ chat_message=" $chat_message , {\" role\" : \" user\" , \" content\" : \" $escaped_request_prompt \" }"
161
158
fi
162
-
163
- request_prompt=" $chat_message "
164
159
}
165
160
166
161
# adds the assistant response to the message in (chatml) format
167
162
# for /chat/completions (gpt models)
168
163
# keeps messages length under max token limit
169
- # $1 should be the chat message
170
- # $2 should be the response data (only the text)
164
+ # * $1 should be the escaped response data
165
+ # * it extends and potentially shrinks $chat_message
171
166
add_assistant_response_to_chat_message () {
172
- chat_message=" $1 "
173
- local local_response_data=" $2 "
174
-
175
- # replace new line characters from response with space
176
- local_response_data=$( echo " $local_response_data " | tr ' \n' ' ' )
167
+ local escaped_response_data=" $1 "
177
168
# add response to chat context as answer
178
- chat_message=" $chat_message , {\" role\" : \" assistant\" , \" content\" : \" $local_response_data \" }"
169
+ chat_message=" $chat_message , {\" role\" : \" assistant\" , \" content\" : \" $escaped_response_data \" }"
179
170
180
171
# transform to json array to parse with jq
181
- chat_message_json=" [ $chat_message ]"
172
+ local chat_message_json=" [ $chat_message ]"
182
173
# check prompt length, 1 word =~ 1.3 tokens
183
174
# reserving 100 tokens for next user prompt
184
175
while (( $(echo "$chat_message " | wc - c) * 1 , 3 > (MAX_TOKENS - 100 )) ); do
@@ -334,15 +325,12 @@ while $running; do
334
325
echo -e " $OVERWRITE_PROCESSING_LINE "
335
326
echo -e " ${CHATGPT_CYAN_LABEL} Complete details for model: ${prompt#* model: } \n ${model_data} "
336
327
elif [[ " $prompt " =~ ^command: ]]; then
337
- # escape quotation marks
328
+ # escape quotation marks, new lines, backslashes...
338
329
escaped_prompt=$( echo " $prompt " | sed ' s/"/\\"/g' )
339
- # escape new lines
340
- if [[ " $prompt " =~ ^command: ]]; then
341
- escaped_prompt=${prompt# command: }
342
- request_prompt=$COMMAND_GENERATION_PROMPT ${escaped_prompt// $' \n ' / ' ' }
343
- fi
344
- build_user_chat_message " $chat_message " " $request_prompt "
345
- request_to_chat " $request_prompt "
330
+ escaped_prompt=${escaped_prompt# command: }
331
+ request_prompt=$COMMAND_GENERATION_PROMPT$escaped_prompt
332
+ build_user_chat_message " $request_prompt "
333
+ response=$( request_to_chat " $chat_message " )
346
334
handle_error " $response "
347
335
response_data=$( echo $response | jq -r ' .choices[].message.content' )
348
336
@@ -363,8 +351,7 @@ while $running; do
363
351
eval $response_data
364
352
fi
365
353
fi
366
- escaped_response_data=$( echo " $response_data " | sed ' s/"/\\"/g' )
367
- add_assistant_response_to_chat_message " $chat_message " " $escaped_response_data "
354
+ add_assistant_response_to_chat_message " $( echo " $response_data " | tr ' \n' ' ' ) "
368
355
369
356
timestamp=$( date +" %d/%m/%Y %H:%M" )
370
357
echo -e " $timestamp $prompt \n$response_data \n" >> ~/.chatgpt_history
@@ -375,8 +362,8 @@ while $running; do
375
362
# escape new lines
376
363
request_prompt=${escaped_prompt// $' \n ' / ' ' }
377
364
378
- build_user_chat_message " $chat_message " " $ request_prompt"
379
- request_to_chat " $request_prompt "
365
+ build_user_chat_message " $request_prompt "
366
+ response= $( request_to_chat " $chat_message " )
380
367
handle_error " $response "
381
368
response_data=$( echo " $response " | jq -r ' .choices[].message.content' )
382
369
@@ -401,10 +388,10 @@ while $running; do
401
388
request_prompt=${escaped_prompt// $' \n ' / ' ' }
402
389
403
390
if [ " $CONTEXT " = true ]; then
404
- build_chat_context " $chat_context " " $escaped_prompt "
391
+ build_chat_context " $request_prompt "
405
392
fi
406
393
407
- request_to_completions " $request_prompt "
394
+ response= $( request_to_completions " $request_prompt " )
408
395
handle_error " $response "
409
396
response_data=$( echo " $response " | jq -r ' .choices[].text' )
410
397
@@ -420,8 +407,7 @@ while $running; do
420
407
fi
421
408
422
409
if [ " $CONTEXT " = true ]; then
423
- escaped_response_data=$( echo " $response_data " | sed ' s/"/\\"/g' )
424
- maintain_chat_context " $chat_context " " $escaped_response_data "
410
+ maintain_chat_context " $escaped_response_data "
425
411
fi
426
412
427
413
timestamp=$( date +" %d/%m/%Y %H:%M" )
0 commit comments