diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2023-03-25 15:16:50 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2023-03-25 15:16:50 +0800 |
commit | bb8ecd1ab9bbfad1cca941f0195cab3a25f986bc (patch) | |
tree | 57dfb5446f8551c1ea7d4e9ba3a132d551504a1f | |
parent | 5ae55cd1e29473e3e39811ed07d6e259fe15488c (diff) |
Update
-rwxr-xr-x | bin/gpt/gpt | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/bin/gpt/gpt b/bin/gpt/gpt index d125204..04b3f3c 100755 --- a/bin/gpt/gpt +++ b/bin/gpt/gpt | |||
@@ -10,7 +10,7 @@ | |||
10 | stacks=( curl jq sed ) | 10 | stacks=( curl jq sed ) |
11 | 11 | ||
12 | # User can dynamically change these options for API call | 12 | # User can dynamically change these options for API call |
13 | configurable_options=( model behavior temperature max_tokens stream ) | 13 | configurable_options=( model behavior temperature max_tokens stream context ) |
14 | 14 | ||
15 | # If script is interupt by SIGINT, simply print leave message | 15 | # If script is interupt by SIGINT, simply print leave message |
16 | trap _print_leave_message INT | 16 | trap _print_leave_message INT |
@@ -60,12 +60,17 @@ Options: | |||
60 | 60 | ||
61 | -S|--stream Use stream mode. If this is set, token usage would not be shown | 61 | -S|--stream Use stream mode. If this is set, token usage would not be shown |
62 | 62 | ||
63 | -c|--context Number of messages in session. If it is set, API calls only | ||
64 | contains limited previous chats. If it is 1, then GPT only get | ||
65 | your latest prompt input. (By default, API calls use the | ||
66 | whole previous chats, which is not friendly to token usage) | ||
67 | |||
63 | * The other arguments would be treated as message content. | 68 | * The other arguments would be treated as message content. |
64 | If no message is specified, user should input content by hands. | 69 | If no message is specified, user should input content by hands. |
65 | If STDIN is given, it would be append to the end of message. | 70 | If STDIN is given, it would be append to the end of message. |
66 | 71 | ||
67 | Special prompt: | 72 | Special input: |
68 | Options If input starts with '.', then a prompt of options shows up. | 73 | . If input starts with '.', then a prompt of options shows up. |
69 | User can modify option value for API calls. | 74 | User can modify option value for API calls. |
70 | 75 | ||
71 | Reference: https://platform.openai.com/docs/api-reference/completions | 76 | Reference: https://platform.openai.com/docs/api-reference/completions |
@@ -129,7 +134,7 @@ _get_content() { | |||
129 | fi | 134 | fi |
130 | [ -n "$content" ] && break | 135 | [ -n "$content" ] && break |
131 | done | 136 | done |
132 | elif [[ "$count" -eq 0 && -z "${content}${data}" ]]; then | 137 | elif [[ "$round" -eq 1 && -z "${content}${data}" ]]; then |
133 | echo -e "No data from STDIN\n" | 138 | echo -e "No data from STDIN\n" |
134 | exit 1; | 139 | exit 1; |
135 | fi | 140 | fi |
@@ -200,6 +205,10 @@ while [ "$#" -gt 0 ]; do | |||
200 | stream=true | 205 | stream=true |
201 | shift 1 | 206 | shift 1 |
202 | ;; | 207 | ;; |
208 | -c|--context) | ||
209 | context=$2 | ||
210 | shift 2 | ||
211 | ;; | ||
203 | -h|--help) | 212 | -h|--help) |
204 | _print_helper_message | 213 | _print_helper_message |
205 | exit 0 | 214 | exit 0 |
@@ -222,12 +231,13 @@ behavior="${behavior:-You are a helpful programming assistant}" | |||
222 | temperature=${temperature:-0.7} | 231 | temperature=${temperature:-0.7} |
223 | max_tokens=${max_tokens:-1024} | 232 | max_tokens=${max_tokens:-1024} |
224 | stream=${stream:-false} | 233 | stream=${stream:-false} |
234 | INDEX= | ||
225 | 235 | ||
226 | # Prepare for chat session | 236 | # Prepare for chat session |
227 | cache=$(mktemp) && touch "$cache" | 237 | cache=$(mktemp) && touch "$cache" |
228 | #trap "rm $cache" EXIT | 238 | #trap "rm $cache" EXIT |
229 | session=() | 239 | session=() |
230 | count=0 | 240 | count=1 |
231 | 241 | ||
232 | # Use while to keep chat session | 242 | # Use while to keep chat session |
233 | while true; do | 243 | while true; do |
@@ -243,6 +253,11 @@ EOF | |||
243 | )" | 253 | )" |
244 | session+=("$user_message") | 254 | session+=("$user_message") |
245 | 255 | ||
256 | # If context is specified, use INDEX to get specified range from session | ||
257 | if [ -n $context ]; then | ||
258 | INDEX="-$([ ${#session[@]} -le $context ] && echo ${#session[@]} || echo $(( $context + 1 )))" | ||
259 | fi | ||
260 | |||
246 | # Create request body | 261 | # Create request body |
247 | # Consider quotes, back slashes, use jq to ensure content texts are OK to put in JSON | 262 | # Consider quotes, back slashes, use jq to ensure content texts are OK to put in JSON |
248 | body="$(cat <<EOF | 263 | body="$(cat <<EOF |
@@ -250,7 +265,7 @@ EOF | |||
250 | "model": "$model", | 265 | "model": "$model", |
251 | "messages": [ | 266 | "messages": [ |
252 | {"role": "system", "content": $(echo "$behavior" | jq -sR .)}, | 267 | {"role": "system", "content": $(echo "$behavior" | jq -sR .)}, |
253 | $(IFS=','; echo "${session[*]}") | 268 | $(IFS=','; echo "${session[*]: $INDEX}") |
254 | ], | 269 | ], |
255 | "temperature": $temperature, | 270 | "temperature": $temperature, |
256 | "max_tokens": $max_tokens, | 271 | "max_tokens": $max_tokens, |
@@ -273,5 +288,5 @@ EOF | |||
273 | )" | 288 | )" |
274 | session+=("$assistant_message") | 289 | session+=("$assistant_message") |
275 | 290 | ||
276 | (( count+=1 )) | 291 | (( round+=1 )) |
277 | done | 292 | done |