Skip to content

Commit c1380ea

Browse files
authored
Merge pull request #40 from 0xacx/add-generate-command-option
WIP: First draft of adding command generation and execution and command ex…
2 parents 9ca871e + 55d8524 commit c1380ea

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

chatgpt.sh

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHAT_INIT_PROMPT="You are ChatGPT, a Large Language Model trained by OpenAI. You
55

66
SYSTEM_PROMPT="You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible. Current date: $(date +%d/%m/%Y). Knowledge cutoff: 9/1/2021."
77

8+
COMMAND_GENERATION_PROMPT="Return a one-line bash command with the functionality I will describe. Return ONLY the command ready to run in the terminal. The command should do the following:"
9+
810
CHATGPT_CYAN_LABEL="\n\033[36mchatgpt \033[0m"
911

1012
# error handling function
@@ -116,7 +118,7 @@ build_user_chat_message() {
116118
chat_message="$chat_message, {\"role\": \"user\", \"content\": \"$escaped_prompt\"}"
117119
fi
118120

119-
request_prompt=$chat_message
121+
request_prompt="$chat_message"
120122
}
121123

122124
# adds the assistant response to the message in (chatml) format
@@ -222,7 +224,7 @@ if [ -n "$prompt" ]; then
222224
pipe_mode_prompt=${prompt}
223225
# if input file_descriptor is a terminal, run on chat mode
224226
elif [ -t 0 ]; then
225-
echo -e "Welcome to chatgpt. You can quit with '\033[36mexit\033[0m'."
227+
echo -e "Welcome to chatgpt. You can quit with '\033[36mexit\033[0m' or '\033[36mq\033[0m'."
226228
# prompt from pipe or redirected stdin, run on pipe mode
227229
else
228230
pipe_mode_prompt+=$(cat -)
@@ -240,7 +242,7 @@ while $running; do
240242
CHATGPT_CYAN_LABEL=""
241243
fi
242244

243-
if [ "$prompt" == "exit" ]; then
245+
if [ "$prompt" == "exit" ] || [ "$prompt" == "q" ]; then
244246
running=false
245247
elif [[ "$prompt" =~ ^image: ]]; then
246248
request_to_image "$prompt"
@@ -275,6 +277,41 @@ while $running; do
275277
handle_error "$models_response"
276278
model_data=$(echo $models_response | jq -r -C '.data[] | select(.id=="'"${prompt#*model:}"'")')
277279
echo -e "${CHATGPT_CYAN_LABEL}Complete details for model: ${prompt#*model:}\n ${model_data}"
280+
elif [[ "$prompt" =~ ^command: ]]; then
281+
# escape quotation marks
282+
escaped_prompt=$(echo "$prompt" | sed 's/"/\\"/g')
283+
# escape new lines
284+
if [[ "$prompt" =~ ^command: ]]; then
285+
escaped_prompt=${prompt#command:}
286+
request_prompt=$COMMAND_GENERATION_PROMPT${escaped_prompt//$'\n'/' '}
287+
fi
288+
build_user_chat_message "$chat_message" "$request_prompt"
289+
request_to_chat "$request_prompt"
290+
handle_error "$response"
291+
response_data=$(echo $response | jq -r '.choices[].message.content')
292+
293+
if [[ "$prompt" =~ ^command: ]]; then
294+
echo -e "${CHATGPT_CYAN_LABEL} ${response_data}\n"
295+
dangerous_commands=("rm" ">" "mv" "mkfs" ":(){:|:&};" "dd" "chmod" "wget" "curl")
296+
297+
for dangerous_command in "${dangerous_commands[@]}"; do
298+
if [[ "$response_data" == *"$dangerous_command"* ]]; then
299+
echo "Warning! This command can change your file system or download external scripts & data. Please do not execute code that you don't understand completely."
300+
fi
301+
done
302+
echo "Would you like to execute it? (Yes/No)"
303+
read run_answer
304+
if [ "$run_answer" == "Yes" ] || [ "$run_answer" == "yes" ] || [ "$run_answer" == "y" ] || [ "$run_answer" == "Y" ]; then
305+
echo -e "\nExecuting command: $response_data\n"
306+
eval $response_data
307+
fi
308+
fi
309+
response_data=$(echo "$response_data" | sed 's/"/\\"/g')
310+
add_assistant_response_to_chat_message "$chat_message" "$response_data"
311+
312+
timestamp=$(date +"%d/%m/%Y %H:%M")
313+
echo -e "$timestamp $prompt \n$response_data \n" >>~/.chatgpt_history
314+
278315
elif [[ "$MODEL" =~ ^gpt- ]]; then
279316
# escape quotation marks
280317
escaped_prompt=$(echo "$prompt" | sed 's/"/\\"/g')

0 commit comments

Comments
 (0)