diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2023-04-03 14:18:24 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2023-04-03 19:39:23 +0800 |
| commit | 39ca31f4002297370572e97ad62b675cf1bc205e (patch) | |
| tree | b5273f3833022cd9af9bf195ca993fc1eaae34c2 /bin | |
| parent | d19590d84386454b3a5e73fc98e1d65ea0e5d53c (diff) | |
Add subcommand last
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/gpt/gpt | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/bin/gpt/gpt b/bin/gpt/gpt index a3bfa48..b348a2e 100755 --- a/bin/gpt/gpt +++ b/bin/gpt/gpt | |||
| @@ -6,16 +6,20 @@ | |||
| 6 | # - Cowork with https://github.com/openai/chatgpt-retrieval-plugin | 6 | # - Cowork with https://github.com/openai/chatgpt-retrieval-plugin |
| 7 | # - History for prompt | 7 | # - History for prompt |
| 8 | # - Mac/BSD compatible | 8 | # - Mac/BSD compatible |
| 9 | # - Generate completions for bash/zsh | ||
| 9 | 10 | ||
| 10 | # Necessary commands | 11 | # Necessary commands |
| 11 | stacks=( curl jq sed ) | 12 | stacks=( curl jq sed ) |
| 12 | 13 | ||
| 13 | # User can dynamically change these options for API call | 14 | # User can dynamically change these options for API call |
| 14 | |||
| 15 | configurable_options=( model behavior temperature max_tokens stream context ) | 15 | configurable_options=( model behavior temperature max_tokens stream context ) |
| 16 | |||
| 16 | # If script is interupt by SIGINT, simply print leave message | 17 | # If script is interupt by SIGINT, simply print leave message |
| 17 | trap _print_leave_message INT | 18 | trap _print_leave_message INT |
| 18 | 19 | ||
| 20 | # Where to cache dir | ||
| 21 | CACHE_DIR=${CACHE_DIR:-/tmp} | ||
| 22 | |||
| 19 | # If any stack is missing, exit with 2 | 23 | # If any stack is missing, exit with 2 |
| 20 | _check_stacks() { | 24 | _check_stacks() { |
| 21 | for command in "${stacks[@]}"; do | 25 | for command in "${stacks[@]}"; do |
| @@ -85,6 +89,9 @@ Special Input: | |||
| 85 | 89 | ||
| 86 | .h Ask gpt the usage of this script | 90 | .h Ask gpt the usage of this script |
| 87 | 91 | ||
| 92 | Sub Commands: | ||
| 93 | last Use fzf to choose previous inputs | ||
| 94 | |||
| 88 | Reference: https://platform.openai.com/docs/api-reference/completions | 95 | Reference: https://platform.openai.com/docs/api-reference/completions |
| 89 | EOF | 96 | EOF |
| 90 | } | 97 | } |
| @@ -148,7 +155,8 @@ _get_content() { | |||
| 148 | if [ ! "$SKIP_INPUT" = true ] ; then | 155 | if [ ! "$SKIP_INPUT" = true ] ; then |
| 149 | # Read content from terminal | 156 | # Read content from terminal |
| 150 | while true; do | 157 | while true; do |
| 151 | read -e -r -p "Let's Chat: " content </dev/tty | 158 | read -e -r -p "Let's Chat: " ${PREFILL:+-i "$PREFILL"} content </dev/tty |
| 159 | unset PREFILL | ||
| 152 | if [ "$content" = .c ]; then | 160 | if [ "$content" = .c ]; then |
| 153 | echo -e '\n======\n' | 161 | echo -e '\n======\n' |
| 154 | _configure_options | 162 | _configure_options |
| @@ -201,6 +209,14 @@ _process_completion() { | |||
| 201 | fi | 209 | fi |
| 202 | } | 210 | } |
| 203 | 211 | ||
| 212 | _get_input_by_fzf() { | ||
| 213 | PREFILL=$( | ||
| 214 | find "$CACHE_DIR" -name 'gpt.*' 2>/dev/null | \ | ||
| 215 | while read -r file; do jq -sr '.[0].messages[1].content' "$file" | head -1; done | \ | ||
| 216 | sed -E '/^null$/d' | sort -u | \ | ||
| 217 | fzf | ||
| 218 | ) | ||
| 219 | } | ||
| 204 | 220 | ||
| 205 | #===================================================== | 221 | #===================================================== |
| 206 | # Main Function starts here | 222 | # Main Function starts here |
| @@ -208,7 +224,7 @@ _process_completion() { | |||
| 208 | 224 | ||
| 209 | # Check OPENAI API KEY in env | 225 | # Check OPENAI API KEY in env |
| 210 | # Exit with 6 (configuration issue) if it is not set | 226 | # Exit with 6 (configuration issue) if it is not set |
| 211 | [ -z "$OPENAI_API_KEY" ] && OPENAI_API_KEY=$(cat $XDG_CONFIG_HOME/openai/key) | 227 | [ -z "$OPENAI_API_KEY" ] && OPENAI_API_KEY=$(cat "$XDG_CONFIG_HOME"/openai/key) |
| 212 | [ -z "$OPENAI_API_KEY" ] && { echo API KEY not specified; exit 6; } | 228 | [ -z "$OPENAI_API_KEY" ] && { echo API KEY not specified; exit 6; } |
| 213 | 229 | ||
| 214 | # Parse arguments | 230 | # Parse arguments |
| @@ -258,6 +274,10 @@ while [ "$#" -gt 0 ]; do | |||
| 258 | _print_helper_message | 274 | _print_helper_message |
| 259 | exit 0 | 275 | exit 0 |
| 260 | ;; | 276 | ;; |
| 277 | last) | ||
| 278 | _get_input_by_fzf | ||
| 279 | break | ||
| 280 | ;; | ||
| 261 | --) | 281 | --) |
| 262 | SKIP_INPUT=true | 282 | SKIP_INPUT=true |
| 263 | shift 1 | 283 | shift 1 |
| @@ -287,7 +307,6 @@ stream=${stream:-false} | |||
| 287 | INDEX= | 307 | INDEX= |
| 288 | 308 | ||
| 289 | # Prepare for chat session | 309 | # Prepare for chat session |
| 290 | CACHE_DIR=${CACHE_DIR:-/tmp} | ||
| 291 | cache=$(mktemp -t gpt.XXXXXX -p ${CACHE_DIR}) && touch "$cache" | 310 | cache=$(mktemp -t gpt.XXXXXX -p ${CACHE_DIR}) && touch "$cache" |
| 292 | #trap "rm $cache" EXIT | 311 | #trap "rm $cache" EXIT |
| 293 | session=() | 312 | session=() |