@@ -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 "
127
+ if [ " $CONTEXT " = true ]; then
128
+ local message=$( [[ -s /tmp/chatgpt-last-session.json ]] && jq " . += [$1 ]" /tmp/chatgpt-last-session.json || " $1 " )
129
+ else
130
+ local message=' [
131
+ {"role": "system", "content": "' " $escaped_system_prompt " ' "},
132
+ ' " $1 " '
133
+ ]'
134
+ fi
128
135
escaped_system_prompt=$( escape " $SYSTEM_PROMPT " )
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
@@ -386,7 +395,7 @@ while $running; do
386
395
387
396
if [[ " $prompt " =~ ^command: ]]; then
388
397
echo -e " $OVERWRITE_PROCESSING_LINE "
389
- echo -e " ${CHATGPT_CYAN_LABEL} ${response_data} " | fold -s -w $COLUMNS
398
+ echo -e " ${CHATGPT_CYAN_LABEL} ${response_data} " | fold -s -w ${ COLUMNS:- 80}
390
399
dangerous_commands=(" rm" " >" " mv" " mkfs" " :(){:|:&};" " dd" " chmod" " wget" " curl" )
391
400
392
401
for dangerous_command in " ${dangerous_commands[@]} " ; do
@@ -421,7 +430,7 @@ while $running; do
421
430
echo -e " ${CHATGPT_CYAN_LABEL} "
422
431
echo " ${response_data} " | glow -
423
432
else
424
- echo -e " ${CHATGPT_CYAN_LABEL}${response_data} " | fold -s -w " $COLUMNS "
433
+ echo -e " ${CHATGPT_CYAN_LABEL}${response_data} " | fold -s -w " ${ COLUMNS:- 80} "
425
434
fi
426
435
add_assistant_response_to_chat_message " $( escape " $response_data " ) "
427
436
@@ -447,7 +456,7 @@ while $running; do
447
456
else
448
457
# else remove empty lines and print
449
458
formatted_text=$( echo " ${response_data} " | sed ' 1,2d; s/^A://g' )
450
- echo -e " ${CHATGPT_CYAN_LABEL}${formatted_text} " | fold -s -w $COLUMNS
459
+ echo -e " ${CHATGPT_CYAN_LABEL}${formatted_text} " | fold -s -w ${ COLUMNS:- 80}
451
460
fi
452
461
453
462
if [ " $CONTEXT " = true ]; then
0 commit comments