Skip to content

Commit 78b0a28

Browse files
Zeming Linebetica
Zeming Lin
authored andcommitted
Add continuation support for chatgpt
1 parent 926587a commit 78b0a28

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

chatgpt.sh

+23-14
Original file line numberDiff line numberDiff line change
@@ -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
126126
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
128135
escaped_system_prompt=$(escape "$SYSTEM_PROMPT")
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
@@ -386,7 +395,7 @@ while $running; do
386395

387396
if [[ "$prompt" =~ ^command: ]]; then
388397
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}
390399
dangerous_commands=("rm" ">" "mv" "mkfs" ":(){:|:&};" "dd" "chmod" "wget" "curl")
391400

392401
for dangerous_command in "${dangerous_commands[@]}"; do
@@ -421,7 +430,7 @@ while $running; do
421430
echo -e "${CHATGPT_CYAN_LABEL}"
422431
echo "${response_data}" | glow -
423432
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}"
425434
fi
426435
add_assistant_response_to_chat_message "$(escape "$response_data")"
427436

@@ -447,7 +456,7 @@ while $running; do
447456
else
448457
# else remove empty lines and print
449458
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}
451460
fi
452461

453462
if [ "$CONTEXT" = true ]; then

0 commit comments

Comments
 (0)