diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2023-03-24 00:04:01 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2023-03-24 00:05:29 +0800 |
commit | f8d6037a989e40425f2faa09c6eb892de0c83509 (patch) | |
tree | 22c41cd15d55096ac1f3c8eaa9b1fb2838b93a8a | |
parent | ef15e9716a9d5803671e6a4741cacfa42aaf97ed (diff) |
Update
-rwxr-xr-x | bin/gpt/gpt | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/bin/gpt/gpt b/bin/gpt/gpt index 4d0f651..1fa78d7 100755 --- a/bin/gpt/gpt +++ b/bin/gpt/gpt | |||
@@ -5,18 +5,35 @@ | |||
5 | # https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-openai-api | 5 | # https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-openai-api |
6 | # - Print token usage when exit | 6 | # - Print token usage when exit |
7 | 7 | ||
8 | # Necessary commands | ||
9 | stacks=( curl jq ) | ||
10 | |||
8 | # User can dynamically change these options for API call | 11 | # User can dynamically change these options for API call |
9 | configurable_options=( behavior temperature max_tokens ) | 12 | configurable_options=( behavior temperature max_tokens ) |
10 | 13 | ||
11 | # If script is interupt by SIGINT, simply print leave message | 14 | # If script is interupt by SIGINT, simply print leave message |
12 | trap _print_leave_message INT | 15 | trap _print_leave_message INT |
13 | 16 | ||
17 | _check_stacks() { | ||
18 | # Print all available options | ||
19 | index=0 | ||
20 | for command in "${stacks[@]}"; do | ||
21 | which "$command" &>/dev/null && continue | ||
22 | echo "$command" is needed | ||
23 | exit 1 | ||
24 | done | ||
25 | } | ||
26 | |||
14 | # Function for printing helper message | 27 | # Function for printing helper message |
15 | _print_helper_message() { | 28 | _print_helper_message() { |
16 | cat <<EOF | 29 | cat <<EOF |
17 | Usage: gpt [-h] [-m MODEL] [-m4] [-b BEHAVIOR] [-t temperature] | 30 | Usage: gpt [-h] [-m MODEL] [-m4] [-b BEHAVIOR] [-t temperature] |
18 | [-M MAX_TOKENS] [-s] [MESSAGE] | 31 | [-M MAX_TOKENS] [-s] [MESSAGE] |
19 | 32 | ||
33 | Env: | ||
34 | OPENAI_API_KEY (Required) | ||
35 | Generate API key from https://platform.openai.com/account/api-keys | ||
36 | |||
20 | Options: | 37 | Options: |
21 | -h, --help show this help message and exit | 38 | -h, --help show this help message and exit |
22 | 39 | ||
@@ -111,10 +128,9 @@ _get_content() { | |||
111 | } | 128 | } |
112 | 129 | ||
113 | # Check OPENAI API KEY"Department:" 3 1 "" 3 15 25 0 | 130 | # Check OPENAI API KEY"Department:" 3 1 "" 3 15 25 0 |
114 | [ -z "$OPENAI_API_KEY" ] && OPENAI_API_KEY=$(token openai) | 131 | [ -z "$OPENAI_API_KEY" ] && which token &>/dev/null && OPENAI_API_KEY=$(token openai) |
115 | [ -z "$OPENAI_API_KEY" ] && { echo API KEY not specified; exit 1; } | 132 | [ -z "$OPENAI_API_KEY" ] && { echo API KEY not specified; exit 1; } |
116 | 133 | ||
117 | |||
118 | # Parse arguments | 134 | # Parse arguments |
119 | while [ "$#" -gt 0 ]; do | 135 | while [ "$#" -gt 0 ]; do |
120 | case "$1" in | 136 | case "$1" in |
@@ -154,6 +170,9 @@ while [ "$#" -gt 0 ]; do | |||
154 | esac | 170 | esac |
155 | done | 171 | done |
156 | 172 | ||
173 | # Make sure necessary commands exist | ||
174 | _check_stacks | ||
175 | |||
157 | # Set variables in API calls | 176 | # Set variables in API calls |
158 | ROUTE=v1/chat/completions | 177 | ROUTE=v1/chat/completions |
159 | model=${model:-gpt-3.5-turbo} | 178 | model=${model:-gpt-3.5-turbo} |