-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli-gpt.sh
executable file
·283 lines (231 loc) · 6.95 KB
/
cli-gpt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/bin/sh
# ======================================================== #
# ======================== cli gpt ======================= #
# ======================================================== #
DEFAULT_SAVE_FILEPATH="$HOME/chat-export.md"
PROMPTS_DIR="$HOME/.config/cli-gpt/system-prompts"
# check if prompts dir exists
if [ ! -d "$PROMPTS_DIR" ]; then
mkdir -p "$PROMPTS_DIR"
touch "$PROMPTS_DIR/default.txt"
echo "You are a helpful assistant. The user is interacting with you through the command line." > "$PROMPTS_DIR/default.txt"
fi
PROMPTS=()
# load all prompts (= filename without extension)
for file in "$PROMPTS_DIR"/*.txt; do
filename=$(basename -- "$file")
filename="${filename%.*}"
PROMPTS+=("$filename")
done
messages="[]"
num_messages=0
last_message_type=""
# ~~~~~~~~~~~~~~~ get flags ~~~~~~~~~~~~~~~ #
SYS_PROMPT_FLAG="-s"
SYS_PROMPT_FLAG_LONG="--system-prompt"
SYS_PROMPT_FLAG_VALUE="default"
LIST_SYS_PROMPTS_FLAG="-ls"
LIST_SYS_PROMPTS_FLAG_LONG="--list-system-prompts"
START_PROMPT_FLAG="-p"
START_PROMPT_FLAG_LONG="--prompt"
START_PROMPT_FLAG_VALUE=""
MODEL_FLAG="-m"
MODEL_FLAG_LONG="--model"
MODEL_FLAG_VALUE="gpt-3.5-turbo"
HELP_FLAG="-h"
HELP_FLAG_LONG="--help"
get_readme() {
curl -s https://raw.githubusercontent.com/AlexW00/cli-gpt/master/commands.md
}
show_help() {
get_readme | gum pager --soft-wrap
}
for arg in "$@"
do
case $arg in
$SYS_PROMPT_FLAG=*|$SYS_PROMPT_FLAG_LONG=*)
if [[ ! " ${PROMPTS[*]} " =~ " ${arg#*=} " ]]; then
echo "Invalid system prompt name: ${arg#*=} (valid prompts: ${PROMPTS[*]})"
exit 1
fi
SYS_PROMPT_FLAG_VALUE="${arg#*=}"
shift # Remove --prompt= from processing
;;
"$LIST_SYS_PROMPTS_FLAG"|"$LIST_SYS_PROMPTS_FLAG_LONG")
echo "Available system prompts: ${PROMPTS[*]}"
exit 0
;;
"$MODEL_FLAG"=*|"$MODEL_FLAG_LONG"=*)
MODEL_FLAG_VALUE="${arg#*=}"
shift # Remove --model= from processing
;;
"$START_PROMPT_FLAG"=*|"$START_PROMPT_FLAG_LONG"=*)
START_PROMPT_FLAG_VALUE="${arg#*=}"
shift # Remove --prompt= from processing
;;
"$HELP_FLAG"|"$HELP_FLAG_LONG")
show_help
exit 0
;;
*)
# unknown option
echo "Unknown option: '$arg'; see help with -h or --help for available options."
exit 1
;;
esac
done
# ~~~~~~~~~~~ validate env vars ~~~~~~~~~~~ #
if [ -z "$OPENAI_API_KEY" ]; then
echo "Please set OPENAI_API_KEY environment variable."
exit 1
fi
# ~~~~~~~~~ validate dependencies ~~~~~~~~~ #
if ! command -v gum &> /dev/null
then
echo "gum could not be found. Please install gum from https://github.com/charmbracelet/gum"
exit
fi
# if linux
if [ "$(uname)" = "Linux" ]; then
# if xclip is not installed
if ! command -v xclip &> /dev/null
then
echo "xclip could not be found. Please install xclip."
exit
fi
fi
# ~~~~~~~~~~~~~~~~~~ code ~~~~~~~~~~~~~~~~~ #
push_message() {
role="$1"
content="$2"
# escape content for json format
content=$(echo "$content" | sed -e 's/"/'\''/g')
messages=$(echo "$messages" | jq ". += [{\"role\": \"$role\", \"content\": \"$content\"}]")
num_messages=$((num_messages+1))
}
get_placeholder() {
# if the chat is empty, return "Your message"
if [ "$num_messages" -eq 1 ]; then
echo "Your message (:h for help)"
else
echo "Another message (:q to quit, :h for help)"
fi
}
prompt_user_message() {
content=""
if [ -n "$START_PROMPT_FLAG_VALUE" ]; then
content="$START_PROMPT_FLAG_VALUE"
START_PROMPT_FLAG_VALUE=""
else
content=$(gum input --placeholder "$(get_placeholder)")
fi
# if message is empty, q, exit, or ctrl-c
if [ -z "$content" ] || [ "$content" = ":q" ] || [ "$content" = "exit" ]; then
last_message_type="exit"
elif [ "$content" = ":h" ]; then
last_message_type="help"
elif [ "$content" = ":s" ]; then
last_message_type="save"
elif [ "$content" = ":c" ]; then
last_message_type="copy"
else
push_message "user" "$content"
last_message_type="message"
fi
}
parse_chatgpt_response(){
# return content of first choice and replace newlines with spaces, double quotes with single quotes
echo "$1" | jq -r '.choices[0].message.content' | sed -e 's/\\n/ /g' | sed -e 's/"/'\''/g'
}
get_chatgpt_response () {
# only show the result of the curl command
gum spin --spinner points --show-output --title "Responding..." -- curl -s https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"model\": \"$MODEL_FLAG_VALUE\",
\"messages\": $messages
}"
}
respond(){
# get response from chatgpt
response=$(get_chatgpt_response)
# parse response
content=$(parse_chatgpt_response "$response")
# push message
push_message "assistant" "$content"
}
messages_to_md() {
md=""
# loop through messages
for row in $(echo "${messages}" | jq -r '.[] | @base64'); do
_jq() {
echo "${row}" | base64 --decode | jq -r "${1}"
}
role=$(_jq '.role')
# if role != system
if [ "$role" != "system" ]; then
content=$(_jq '.content')
md+="[$role]:\n"
md+="$content\n\n"
fi
done
echo -e "$md"
}
save_chat() {
# get filepath
filepath=$(gum input --placeholder "$DEFAULT_SAVE_FILEPATH")
# if filepath is empty, use default
if [ -z "$filepath" ]; then
filepath="$DEFAULT_SAVE_FILEPATH"
fi
# save chat
messages_to_md > "$filepath"
# notify user
echo "Chat saved to $filepath"
}
copy_to_clipboard() {
str="$1"
# on linux, use xclip
if [ "$(uname)" = "Linux" ]; then
echo "$str" | xclip -selection clipboard
# on mac, use pbcopy
elif [ "$(uname)" = "Darwin" ]; then
echo "$str" | pbcopy
fi
echo "Last message copied to clipboard"
}
copy_last_message() {
# get last message
last_message=$(echo "$messages" | jq -r '.[-1].content')
# copy last message to clipboard using xclip
copy_to_clipboard "$last_message"
}
show_messages() {
messages_to_md | gum pager --soft-wrap
}
get_prompt_path() {
PROMPT_NAME="$1"
echo "$PROMPTS_DIR/$PROMPT_NAME.txt"
}
get_prompt() {
PROMPT_NAME="$1"
cat "$(get_prompt_path "$PROMPT_NAME")"
}
push_message "system" "$(get_prompt "$SYS_PROMPT_FLAG_VALUE")"
while true; do
# get user message
prompt_user_message
if [ "$last_message_type" = "message" ]; then
respond
show_messages
elif [ "$last_message_type" = "help" ]; then
show_help
elif [ "$last_message_type" = "save" ]; then
save_chat
elif [ "$last_message_type" = "copy" ]; then
copy_last_message
else
exit
fi
done