@@ -124,22 +124,31 @@ request_to_image() {
124124# request to OpenAPI API chat completion endpoint function
125125# $1 should be the message(s) formatted with role and content
126126request_to_chat () {
127- local message=" $1 "
128127 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+ }'
129142
130- curl https://api.openai.com/v1/chat/completions \
143+ local response= $( curl https://api.openai.com/v1/chat/completions \
131144 -sS \
132145 -H ' Content-Type: application/json' \
133146 -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
143152}
144153
145154# build chat context before each request for /completions (all models except
@@ -218,14 +227,12 @@ while [[ "$#" -gt 0 ]]; do
218227 -i | --init-prompt)
219228 CHAT_INIT_PROMPT=" $2 "
220229 SYSTEM_PROMPT=" $2 "
221- CONTEXT=true
222230 shift
223231 shift
224232 ;;
225233 --init-prompt-from-file)
226234 CHAT_INIT_PROMPT=$( cat " $2 " )
227235 SYSTEM_PROMPT=$( cat " $2 " )
228- CONTEXT=true
229236 shift
230237 shift
231238 ;;
@@ -386,7 +393,7 @@ while $running; do
386393
387394 if [[ " $prompt " =~ ^command: ]]; then
388395 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}
390397 dangerous_commands=(" rm" " >" " mv" " mkfs" " :(){:|:&};" " dd" " chmod" " wget" " curl" )
391398
392399 for dangerous_command in " ${dangerous_commands[@]} " ; do
@@ -421,7 +428,7 @@ while $running; do
421428 echo -e " ${CHATGPT_CYAN_LABEL} "
422429 echo " ${response_data} " | glow -
423430 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} "
425432 fi
426433 add_assistant_response_to_chat_message " $( escape " $response_data " ) "
427434
@@ -447,7 +454,7 @@ while $running; do
447454 else
448455 # else remove empty lines and print
449456 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}
451458 fi
452459
453460 if [ " $CONTEXT " = true ]; then
0 commit comments