diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2023-03-23 15:04:19 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2023-03-23 15:04:53 +0800 |
| commit | b0e36772acb8b5e4f722a63255f4414bcb1385ba (patch) | |
| tree | ff4ac5797424fcc06168b5a1192c3fca0b0386e4 /bin/gpt | |
| parent | 905246f893ea63144aefc37302674ea9887e2b00 (diff) | |
Improve gpt
Diffstat (limited to 'bin/gpt')
| -rwxr-xr-x | bin/gpt/gpt | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/bin/gpt/gpt b/bin/gpt/gpt index 63241ac..cd312d6 100755 --- a/bin/gpt/gpt +++ b/bin/gpt/gpt | |||
| @@ -7,12 +7,13 @@ | |||
| 7 | 7 | ||
| 8 | # If script is interupt by SIGINT, simply print leave message | 8 | # If script is interupt by SIGINT, simply print leave message |
| 9 | trap 'echo -e "\nChat Finished, cached file: $cache"; exit 0' INT | 9 | trap 'echo -e "\nChat Finished, cached file: $cache"; exit 0' INT |
| 10 | trap '_configure_completion' TSTP | ||
| 10 | 11 | ||
| 11 | # Function for printing helper message | 12 | # Function for printing helper message |
| 12 | _print_helper_message() { | 13 | _print_helper_message() { |
| 13 | cat <<EOF | 14 | cat <<EOF |
| 14 | Usage: gpt [-h] [-m MODEL] [-m4] [-b BEHAVIOR] [-t temperature] | 15 | Usage: gpt [-h] [-m MODEL] [-m4] [-b BEHAVIOR] [-t temperature] |
| 15 | [-n NUMBER] [MESSAGE] | 16 | [-M MAX_TOKENS] [-s] [MESSAGE] |
| 16 | 17 | ||
| 17 | Options: | 18 | Options: |
| 18 | -h, --help show this help message and exit | 19 | -h, --help show this help message and exit |
| @@ -33,8 +34,10 @@ Options: | |||
| 33 | more focused and deterministic. | 34 | more focused and deterministic. |
| 34 | (Defaults to 0.7) | 35 | (Defaults to 0.7) |
| 35 | 36 | ||
| 36 | -n How many chat completion choices to generate for each input message. | 37 | -M|--max_tokens The maximum number of tokens to generate in the completion. |
| 37 | (Defaults to 1) | 38 | (Defaults to 2048) |
| 39 | |||
| 40 | -s|--skip Skip message, STDIN would be treated as your message | ||
| 38 | 41 | ||
| 39 | * The other arguments would be treated as message content. | 42 | * The other arguments would be treated as message content. |
| 40 | If no message is specified, user should type it by hands. | 43 | If no message is specified, user should type it by hands. |
| @@ -44,7 +47,18 @@ Reference: https://platform.openai.com/docs/api-reference/completions | |||
| 44 | EOF | 47 | EOF |
| 45 | } | 48 | } |
| 46 | 49 | ||
| 47 | # Check OPENAI API KEY | 50 | _configure_completion() { |
| 51 | field="$( | ||
| 52 | dialog --form "Configure Completion" 10 50 3 \ | ||
| 53 | --stdout \ | ||
| 54 | 'model:' 1 1 "$(eval 'echo $model')" 1 15 25 0 \ | ||
| 55 | 'temperature:' 2 1 "$(eval 'echo $temperature')" 2 15 25 0 \ | ||
| 56 | 'max_tokens:' 3 1 "$(eval 'echo $max_tokens')" 3 15 25 0 | ||
| 57 | )" | ||
| 58 | |||
| 59 | } | ||
| 60 | |||
| 61 | # Check OPENAI API KEY"Department:" 3 1 "" 3 15 25 0 | ||
| 48 | [ -z "$OPENAI_API_KEY" ] && OPENAI_API_KEY=$(token openai) | 62 | [ -z "$OPENAI_API_KEY" ] && OPENAI_API_KEY=$(token openai) |
| 49 | [ -z "$OPENAI_API_KEY" ] && { echo API KEY not specified; exit 1; } | 63 | [ -z "$OPENAI_API_KEY" ] && { echo API KEY not specified; exit 1; } |
| 50 | 64 | ||
| @@ -68,8 +82,8 @@ while [ "$#" -gt 0 ]; do | |||
| 68 | temperature="$2" | 82 | temperature="$2" |
| 69 | shift 2 | 83 | shift 2 |
| 70 | ;; | 84 | ;; |
| 71 | -n) | 85 | -m|--max_tokens) |
| 72 | n="$2" | 86 | max_tokens="$2" |
| 73 | shift 2 | 87 | shift 2 |
| 74 | ;; | 88 | ;; |
| 75 | -h|--help) | 89 | -h|--help) |
| @@ -83,12 +97,14 @@ while [ "$#" -gt 0 ]; do | |||
| 83 | esac | 97 | esac |
| 84 | done | 98 | done |
| 85 | 99 | ||
| 86 | # Set variables | 100 | # Set variables in API calls |
| 87 | ROUTE=v1/chat/completions | 101 | ROUTE=v1/chat/completions |
| 88 | model=${model:-gpt-3.5-turbo} | 102 | model=${model:-gpt-3.5-turbo} |
| 89 | behavior="${behavior:-You are a helpful programming assistant}" | 103 | behavior="${behavior:-You are a helpful programming assistant}" |
| 90 | temperature=${temperature:-0.7} | 104 | temperature=${temperature:-0.7} |
| 91 | n=${n:-1} | 105 | max_tokens=${max_tokens:-2048} |
| 106 | |||
| 107 | # Prepare for chat session | ||
| 92 | cache=`mktemp` && touch $cache # && trap "rm $cache" EXIT | 108 | cache=`mktemp` && touch $cache # && trap "rm $cache" EXIT |
| 93 | session=() | 109 | session=() |
| 94 | count=0 | 110 | count=0 |
| @@ -103,7 +119,12 @@ while true; do | |||
| 103 | # Read data from STDIN | 119 | # Read data from STDIN |
| 104 | [ ! -t 0 ] && data="$(cat)" | 120 | [ ! -t 0 ] && data="$(cat)" |
| 105 | # Append data to the end of content | 121 | # Append data to the end of content |
| 106 | [ -n "$data" ] && content="$(printf "%s\\n\\n%s" "$content" "$data")" | 122 | # And print it out |
| 123 | if [ -n "$data" ]; then | ||
| 124 | content="$(printf "%s\\n\\n%s" "$content" "$data")" | ||
| 125 | echo | ||
| 126 | echo "$data" | ||
| 127 | fi | ||
| 107 | 128 | ||
| 108 | # Put user message into session | 129 | # Put user message into session |
| 109 | user_message="$(cat <<EOF | 130 | user_message="$(cat <<EOF |
| @@ -122,8 +143,7 @@ EOF | |||
| 122 | `IFS=',\n'; echo "${session[*]}"` | 143 | `IFS=',\n'; echo "${session[*]}"` |
| 123 | ], | 144 | ], |
| 124 | "temperature": $temperature, | 145 | "temperature": $temperature, |
| 125 | "n": $n, | 146 | "max_tokens": $max_tokens |
| 126 | "max_tokens": 50 | ||
| 127 | } | 147 | } |
| 128 | EOF | 148 | EOF |
| 129 | )" | 149 | )" |