diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2023-03-25 18:52:45 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2023-03-25 18:52:45 +0800 |
| commit | 7902ab57bcb380d246496970490b77d4eda0efd2 (patch) | |
| tree | e4afc2efcd9408b6bb1108b7fa40ed189fd20fd8 /bin/gpt | |
| parent | 9c711fb5eefca931beeea5c2017fa9c1d85b7f70 (diff) | |
Update
Diffstat (limited to 'bin/gpt')
| -rwxr-xr-x | bin/gpt/gpt | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/bin/gpt/gpt b/bin/gpt/gpt index 5f90864..6468f22 100755 --- a/bin/gpt/gpt +++ b/bin/gpt/gpt | |||
| @@ -134,13 +134,14 @@ _get_content() { | |||
| 134 | echo -e '\n======\n' | 134 | echo -e '\n======\n' |
| 135 | continue | 135 | continue |
| 136 | elif [ "$content" = .h ]; then | 136 | elif [ "$content" = .h ]; then |
| 137 | content="What does the following script used for? how to use it?" | 137 | content="What does the following script used for? Show me how to use it?" |
| 138 | echo -n "$content" | 138 | echo -n "$content" |
| 139 | content="$content\n\n$(cat $0)" | 139 | content="$content\n\n$(cat "$0")" |
| 140 | elif [ -n "$content" ]; then | ||
| 141 | break | ||
| 140 | else | 142 | else |
| 141 | continue | 143 | continue |
| 142 | fi | 144 | fi |
| 143 | [ -n "$content" ] && break | ||
| 144 | done | 145 | done |
| 145 | # Exit 1 when "--skip" is specified, but no STDIN and CONTENT is given | 146 | # Exit 1 when "--skip" is specified, but no STDIN and CONTENT is given |
| 146 | elif [[ "$round" -eq 1 && -z "${content}${data}" ]]; then | 147 | elif [[ "$round" -eq 1 && -z "${content}${data}" ]]; then |
| @@ -148,14 +149,13 @@ _get_content() { | |||
| 148 | exit 1; | 149 | exit 1; |
| 149 | fi | 150 | fi |
| 150 | 151 | ||
| 151 | # If it is the first round, append STDIN (If it exists) at the end | 152 | # Use STDIN as content if "--skip" is given |
| 152 | if [[ ! "$SKIP_INPUT" = true || -n "$data" ]] ; then | 153 | if [ "$SKIP_INPUT" = true ] && [ -z "$content" ] ; then |
| 153 | content="$(printf "%s\\n\\n%s" "$content" "$data")" | ||
| 154 | echo -e "\n$data" | ||
| 155 | # Or only use STDIN as content | ||
| 156 | else | ||
| 157 | content="$data" | 154 | content="$data" |
| 158 | echo "$content" | 155 | echo -e "$content" |
| 156 | else | ||
| 157 | content="$(printf "%s${data:+\\n\\n}%s" "$content" "$data")" | ||
| 158 | [ -n "$data" ] && echo "$data" | ||
| 159 | fi | 159 | fi |
| 160 | } | 160 | } |
| 161 | 161 | ||
| @@ -246,11 +246,12 @@ INDEX= | |||
| 246 | cache=$(mktemp) && touch "$cache" | 246 | cache=$(mktemp) && touch "$cache" |
| 247 | #trap "rm $cache" EXIT | 247 | #trap "rm $cache" EXIT |
| 248 | session=() | 248 | session=() |
| 249 | count=1 | 249 | round=1 |
| 250 | 250 | ||
| 251 | # Use while to keep chat session | 251 | # Use while to keep chat session |
| 252 | while true; do | 252 | while true; do |
| 253 | _get_content | 253 | _get_content |
| 254 | echo | ||
| 254 | 255 | ||
| 255 | # User must input by hands in the following rounds | 256 | # User must input by hands in the following rounds |
| 256 | SKIP_INPUT=false | 257 | SKIP_INPUT=false |
| @@ -264,7 +265,7 @@ EOF | |||
| 264 | 265 | ||
| 265 | # If context is specified, use INDEX to get specified range from session | 266 | # If context is specified, use INDEX to get specified range from session |
| 266 | if [ -n "$context" ]; then | 267 | if [ -n "$context" ]; then |
| 267 | INDEX="-$([ "${#session[@]}" -le "$context" ] && echo ${#session[@]} || echo $(( $context + 1 )))" | 268 | INDEX="-$([ "${#session[@]}" -le "$context" ] && echo ${#session[@]} || echo $(( "$context" + 1 )))" |
| 268 | fi | 269 | fi |
| 269 | 270 | ||
| 270 | # Create request body | 271 | # Create request body |
| @@ -286,7 +287,7 @@ EOF | |||
| 286 | echo "$body" >>"$cache" | 287 | echo "$body" >>"$cache" |
| 287 | 288 | ||
| 288 | # Use Yellow color to print completion from GPT | 289 | # Use Yellow color to print completion from GPT |
| 289 | echo -en '\n\e[33m' | 290 | echo -en '\e[33m' |
| 290 | _API_call | _process_completion | 291 | _API_call | _process_completion |
| 291 | echo -e '\e[0m\n' | 292 | echo -e '\e[0m\n' |
| 292 | 293 | ||