aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2023-03-23 10:35:48 +0800
committerHsieh Chin Fan <pham@topo.tw>2023-03-23 10:35:48 +0800
commitca4f040ab9e46ce4e900a5e5a6dc57bb94c41393 (patch)
treefa10ea70e5e29cc1565e5a93e77c92bc570970ba
parent943259404bfd39965c42f2a22a2c0beac479062f (diff)
Improve script
-rwxr-xr-xbin/gpt/gpt21
1 files changed, 12 insertions, 9 deletions
diff --git a/bin/gpt/gpt b/bin/gpt/gpt
index ab4b64c..4fbbc5b 100755
--- a/bin/gpt/gpt
+++ b/bin/gpt/gpt
@@ -2,10 +2,10 @@
2 2
3# TODO 3# TODO
4# - Put previous response into tokens 4# - Put previous response into tokens
5# - Use suggested block to wrap data:
6# https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-openai-api
5 7
6[ -z "$OPENAI_API_KEY" ] && OPENAI_API_KEY=$(token openai) 8# Function for printing helper message
7[ -z "$OPENAI_API_KEY" ] && echo API KEY not specified && exit 1
8
9_print_helper_message() { 9_print_helper_message() {
10 cat <<EOF 10 cat <<EOF
11Usage: gpt [-h] [-m MODEL] [-m4] [-b BEHAVIOR] [-t temperature] 11Usage: gpt [-h] [-m MODEL] [-m4] [-b BEHAVIOR] [-t temperature]
@@ -41,6 +41,11 @@ Reference: https://platform.openai.com/docs/api-reference/completions
41EOF 41EOF
42} 42}
43 43
44# Check OPENAI API KEY
45[ -z "$OPENAI_API_KEY" ] && OPENAI_API_KEY=$(token openai)
46[ -z "$OPENAI_API_KEY" ] && { echo API KEY not specified; exit 1; }
47
48
44# Parse arguments 49# Parse arguments
45while [ "$#" -gt 0 ]; do 50while [ "$#" -gt 0 ]; do
46 case "$1" in 51 case "$1" in
@@ -75,28 +80,26 @@ while [ "$#" -gt 0 ]; do
75 esac 80 esac
76done 81done
77 82
78# Available: 83# Set variables
79# v1/models
80# v1/chat/completions
81ROUTE=v1/chat/completions 84ROUTE=v1/chat/completions
82
83model=${model:-gpt-3.5-turbo} 85model=${model:-gpt-3.5-turbo}
84behavior="${behavior:-You are a helpful programming assistant}" 86behavior="${behavior:-You are a helpful programming assistant}"
85temperature=${temperature:-0.7} 87temperature=${temperature:-0.7}
86n=${n:-1} 88n=${n:-1}
87 89
88# Read content from terminal 90# Read prompt from terminal
89[ -z "$content" ] && read -e -r -p "Let's Chat: " content </dev/tty 91[ -z "$content" ] && read -e -r -p "Let's Chat: " content </dev/tty
90[ -z "$content" ] && { echo -e "No message is given\n"; _print_helper_message; exit 1; } 92[ -z "$content" ] && { echo -e "No message is given\n"; _print_helper_message; exit 1; }
91[ ! -t 0 ] && data="$(cat)" 93[ ! -t 0 ] && data="$(cat)"
92[ -n "$data" ] && content="$(printf "%s\\n\\n%s" "$content" "$data")" 94[ -n "$data" ] && content="$(printf "%s\\n\\n%s" "$content" "$data")"
93 95
94# Create request body 96# Create request body
97# Consider quotes, back slashes, use jq to ensure content texts are OK to put in JSON
95body="$(cat <<EOF 98body="$(cat <<EOF
96{ 99{
97 "model": "$model", 100 "model": "$model",
98 "messages": [ 101 "messages": [
99 {"role": "system", "content": "$behavior"}, 102 {"role": "system", "content": $(echo $behavior | jq -R .)},
100 {"role": "user", "content": $(echo $content | jq -R .)} 103 {"role": "user", "content": $(echo $content | jq -R .)}
101 ], 104 ],
102 "temperature": $temperature, 105 "temperature": $temperature,