@@ -124,22 +124,31 @@ request_to_image() {
124
124
# request to OpenAPI API chat completion endpoint function
125
125
# $1 should be the message(s) formatted with role and content
126
126
request_to_chat () {
127
- local message=" $1 "
128
127
escaped_system_prompt=$( escape " $SYSTEM_PROMPT " )
128
+ if [ " $CONTEXT " = true ]; then
129
+ local message=$( [[ -s /tmp/chatgpt-last-session.json ]] && jq " . += [$1 ]" /tmp/chatgpt-last-session.json || " $1 " )
130
+ else
131
+ local message=' [
132
+ {"role": "system", "content": "' " $escaped_system_prompt " ' "},
133
+ ' " $1 " '
134
+ ]'
135
+ fi
136
+ local json=' {
137
+ "model": "' " $MODEL " ' ",
138
+ "max_tokens": ' $MAX_TOKENS ' ,
139
+ "temperature": ' $TEMPERATURE ' ,
140
+ "messages": ' $message '
141
+ }'
129
142
130
- curl https://api.openai.com/v1/chat/completions \
143
+ local response= $( curl https://api.openai.com/v1/chat/completions \
131
144
-sS \
132
145
-H ' Content-Type: application/json' \
133
146
-H " Authorization: Bearer $OPENAI_KEY " \
134
- -d ' {
135
- "model": "' " $MODEL " ' ",
136
- "messages": [
137
- {"role": "system", "content": "' " $escaped_system_prompt " ' "},
138
- ' " $message " '
139
- ],
140
- "max_tokens": ' $MAX_TOKENS ' ,
141
- "temperature": ' $TEMPERATURE '
142
- }'
147
+ -d " $json " )
148
+ local response_message=" $( echo $response | jq ' .choices[0].message' ) "
149
+ jq " .[0].messages += [.[1].choices[0].message] | .[0].messages" -s <( echo $json ) <( echo $response ) > /tmp/chatgpt-last-session.json
150
+
151
+ echo $response
143
152
}
144
153
145
154
# build chat context before each request for /completions (all models except
@@ -218,14 +227,12 @@ while [[ "$#" -gt 0 ]]; do
218
227
-i | --init-prompt)
219
228
CHAT_INIT_PROMPT=" $2 "
220
229
SYSTEM_PROMPT=" $2 "
221
- CONTEXT=true
222
230
shift
223
231
shift
224
232
;;
225
233
--init-prompt-from-file)
226
234
CHAT_INIT_PROMPT=$( cat " $2 " )
227
235
SYSTEM_PROMPT=$( cat " $2 " )
228
- CONTEXT=true
229
236
shift
230
237
shift
231
238
;;
@@ -386,7 +393,7 @@ while $running; do
386
393
387
394
if [[ " $prompt " =~ ^command: ]]; then
388
395
echo -e " $OVERWRITE_PROCESSING_LINE "
389
- echo -e " ${CHATGPT_CYAN_LABEL} ${response_data} " | fold -s -w $COLUMNS
396
+ echo -e " ${CHATGPT_CYAN_LABEL} ${response_data} " | fold -s -w ${ COLUMNS:- 80}
390
397
dangerous_commands=(" rm" " >" " mv" " mkfs" " :(){:|:&};" " dd" " chmod" " wget" " curl" )
391
398
392
399
for dangerous_command in " ${dangerous_commands[@]} " ; do
@@ -421,7 +428,7 @@ while $running; do
421
428
echo -e " ${CHATGPT_CYAN_LABEL} "
422
429
echo " ${response_data} " | glow -
423
430
else
424
- echo -e " ${CHATGPT_CYAN_LABEL}${response_data} " | fold -s -w " $COLUMNS "
431
+ echo -e " ${CHATGPT_CYAN_LABEL}${response_data} " | fold -s -w " ${ COLUMNS:- 80} "
425
432
fi
426
433
add_assistant_response_to_chat_message " $( escape " $response_data " ) "
427
434
@@ -447,7 +454,7 @@ while $running; do
447
454
else
448
455
# else remove empty lines and print
449
456
formatted_text=$( echo " ${response_data} " | sed ' 1,2d; s/^A://g' )
450
- echo -e " ${CHATGPT_CYAN_LABEL}${formatted_text} " | fold -s -w $COLUMNS
457
+ echo -e " ${CHATGPT_CYAN_LABEL}${formatted_text} " | fold -s -w ${ COLUMNS:- 80}
451
458
fi
452
459
453
460
if [ " $CONTEXT " = true ]; then
0 commit comments