diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2023-03-21 21:49:58 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2023-03-21 21:49:58 +0800 |
commit | 60f85d962924d6d11cc27d8cac6e08af9953c994 (patch) | |
tree | ad5f6573444145dab88ab50f0847d55cf589556c | |
parent | 73e036b244965e91d80de5a232afa7cfa6c3469f (diff) |
Update gpt
-rwxr-xr-x | bin/gpt/gpt | 100 |
1 files changed, 90 insertions, 10 deletions
diff --git a/bin/gpt/gpt b/bin/gpt/gpt index cdbb997..ebb1d4b 100755 --- a/bin/gpt/gpt +++ b/bin/gpt/gpt | |||
@@ -1,32 +1,112 @@ | |||
1 | #! /bin/bash | 1 | #! /bin/bash |
2 | 2 | ||
3 | |||
4 | # TODO | 3 | # TODO |
5 | # 1. read arguments about model or system message | 4 | # - Put previous response into tokens |
6 | # 2. Put previous response into tokens | 5 | |
6 | [ -z "$OPENAI_API_KEY" ] && OPENAI_API_KEY=$(token openai) | ||
7 | [ -z "$OPENAI_API_KEY" ] && echo API KEY not specified && exit 1 | ||
8 | |||
9 | _print_helper_message() { | ||
10 | cat <<EOF | ||
11 | Usage: gpt [-h] [-m MODEL] [-m4] [-b BEHAVIOR] [-t temperature] | ||
12 | [-n NUMBER] [MESSAGE] | ||
13 | |||
14 | Options: | ||
15 | -h, --help show this help message and exit | ||
16 | |||
17 | -m|--model specify model, available: | ||
18 | gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, | ||
19 | gpt-3.5-turbo, gpt-3.5-turbo-0301 | ||
20 | (Defaults to gpt-3.5-turbo) | ||
21 | |||
22 | -m4 Use model gpt-4 | ||
23 | |||
24 | -b|--behavior How model behave on response, for example: | ||
25 | "You are a helpful assistant." | ||
26 | (Defaults to "You are a helpful programming assistant") | ||
27 | |||
28 | -t|--temperature Value between 0 and 2. Higher values like 0.8 will make the | ||
29 | output more random, while lower values like 0.2 will make it | ||
30 | more focused and deterministic. | ||
31 | (Defaults to 0.7) | ||
7 | 32 | ||
8 | OPENAI_API_KEY=$(token openai) | 33 | -n How many chat completion choices to generate for each input message. |
34 | (Defaults to 1) | ||
35 | |||
36 | * The other arguments would be treated as message content. | ||
37 | If no message is specified, user should type it by hands. | ||
38 | If STDIN is given, it would be append to the end of message. | ||
39 | |||
40 | Reference: https://platform.openai.com/docs/api-reference/completions | ||
41 | EOF | ||
42 | } | ||
43 | |||
44 | # Parse arguments | ||
45 | while [ "$#" -gt 0 ]; do | ||
46 | case "$1" in | ||
47 | -m|--model) | ||
48 | model="$2" | ||
49 | shift 2 | ||
50 | ;; | ||
51 | -m4) | ||
52 | model="gpt-4" | ||
53 | shift 1 | ||
54 | ;; | ||
55 | -b|--behavior) | ||
56 | behavior="$2" | ||
57 | shift 2 | ||
58 | ;; | ||
59 | -t|--temperature) | ||
60 | temperature="$2" | ||
61 | shift 2 | ||
62 | ;; | ||
63 | -n) | ||
64 | n="$2" | ||
65 | shift 2 | ||
66 | ;; | ||
67 | -h|--help) | ||
68 | _print_helper_message | ||
69 | exit 0 | ||
70 | ;; | ||
71 | *) | ||
72 | content="$1" | ||
73 | shift 1 | ||
74 | ;; | ||
75 | esac | ||
76 | done | ||
9 | 77 | ||
10 | # Available: | 78 | # Available: |
11 | # v1/models | 79 | # v1/models |
12 | # v1/chat/completions | 80 | # v1/chat/completions |
13 | ROUTE=v1/chat/completions | 81 | ROUTE=v1/chat/completions |
14 | 82 | ||
15 | model=${model:-gpt-4} | 83 | model=${model:-gpt-3.5-turbo} |
16 | read content | 84 | behavior="${behavior:-You are a helpful programming assistant}" |
85 | temperature=${temperature:-0.7} | ||
86 | n=${n:-1} | ||
87 | |||
88 | # Read content from terminal | ||
89 | [ -z "$content" ] && read -r -p "Let's Chat: " content </dev/tty | ||
90 | [ -z "$content" ] && { echo -e "No message is given\n"; _print_helper_message; exit 1; } | ||
91 | [ ! -t 0 ] && data="$(cat)" | ||
92 | [ -n "$data" ] && content="$(printf "%s\\n\\n%s" "$content" "$data")" | ||
93 | |||
94 | # Create request body | ||
17 | body="$(cat <<EOF | 95 | body="$(cat <<EOF |
18 | { | 96 | { |
19 | "model": "$model", | 97 | "model": "$model", |
20 | "messages": [ | 98 | "messages": [ |
21 | {"role": "system", "content": "You are a helpful assistant about programming"}, | 99 | {"role": "system", "content": "$behavior"}, |
22 | {"role": "user", "content": "$content"} | 100 | {"role": "user", "content": $(echo $content | jq -R .)} |
23 | ], | 101 | ], |
24 | "temperature": 0.7 | 102 | "temperature": $temperature, |
103 | "n": $n | ||
25 | } | 104 | } |
26 | EOF | 105 | EOF |
27 | )" | 106 | )" |
28 | 107 | ||
29 | echo | 108 | # Add an empty line between prompt and response |
109 | echo | ||
30 | 110 | ||
31 | # API call | 111 | # API call |
32 | curl https://api.openai.com/$ROUTE \ | 112 | curl https://api.openai.com/$ROUTE \ |