Skip to content

Commit c694c9c

Browse files
committed
Add command generation, exit option with q
2 parents 492e6e7 + 9ca871e commit c694c9c

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,5 @@ This script relies on curl for the requests to the api and jq to parse the json
140140
## Contributors
141141
:pray: Thanks to all the people who used, tested, submitted issues, PRs and proposed changes:
142142

143-
[pfr-dev](https://www.github.com/pfr-dev), [jordantrizz](https://www.github.com/jordantrizz), [se7en-x230](https://www.github.com/se7en-x230), [mountaineerbr](https://www.github.com/mountaineerbr), [oligeo](https://www.github.com/oligeo), [biaocy](https://www.github.com/biaocy), [dmd](https://www.github.com/dmd), [goosegit11](https://www.github.com/goosegit11), [dilatedpupils](https://www.github.com/dilatedpupils), [direster](https://www.github.com/direster)
143+
[pfr-dev](https://www.github.com/pfr-dev), [jordantrizz](https://www.github.com/jordantrizz), [se7en-x230](https://www.github.com/se7en-x230), [mountaineerbr](https://www.github.com/mountaineerbr), [oligeo](https://www.github.com/oligeo), [biaocy](https://www.github.com/biaocy), [dmd](https://www.github.com/dmd), [goosegit11](https://www.github.com/goosegit11), [dilatedpupils](https://www.github.com/dilatedpupils), [direster](https://www.github.com/direster), [rxaviers](https://www.github.com/rxaviers)
144144

chatgpt.sh

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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. Your output must follow this format \`command: the-actual-command\` and should be only the command, no quotes or other text. The command should do the following:"
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:"
99

1010
EXPLAIN_CODE_PROMPT="Explain in great detail, step by step the following code. Identify any dependencies, libraries, software packages. If you find any errors suggest a solution. Try to guess the context and application it is used on. This is the code:"
1111

@@ -215,20 +215,21 @@ CONTEXT=${CONTEXT:-false}
215215
# create history file
216216
if [ ! -f ~/.chatgpt_history ]; then
217217
touch ~/.chatgpt_history
218-
chmod a+rw ~/.chatgpt_history
218+
chmod 600 ~/.chatgpt_history
219219
fi
220220

221221
running=true
222-
# check input source
223-
# if prompt already entered, run on pipe mode (run once, no chat)
224-
# prompt from pipe
225-
if [ -p /dev/stdin ]; then
226-
pipe_mode_prompt+=$(cat -)
227-
# prompt from argument
228-
elif [ -n "$prompt" ]; then
222+
# check input source and determine run mode
223+
224+
# prompt from argument, run on pipe mode (run once, no chat)
225+
if [ -n "$prompt" ]; then
229226
pipe_mode_prompt=${prompt}
230-
else
227+
# if input file_descriptor is a terminal, run on chat mode
228+
elif [ -t 0 ]; then
231229
echo -e "Welcome to chatgpt. You can quit with '\033[36mexit\033[0m' or '\033[36mq\033[0m'."
230+
# prompt from pipe or redirected stdin, run on pipe mode
231+
else
232+
pipe_mode_prompt+=$(cat -)
232233
fi
233234

234235
while $running; do
@@ -278,35 +279,34 @@ while $running; do
278279
handle_error "$models_response"
279280
model_data=$(echo $models_response | jq -r -C '.data[] | select(.id=="'"${prompt#*model:}"'")')
280281
echo -e "${CHATGPT_CYAN_LABEL}Complete details for model: ${prompt#*model:}\n ${model_data}"
281-
elif [[ "$prompt" =~ ^command: ]] || [[ "$prompt" =~ ^explain: ]]; then
282+
elif [[ "$prompt" =~ ^command: ]]; then
282283
# escape quotation marks
283284
escaped_prompt=$(echo "$prompt" | sed 's/"/\\"/g')
284285
# escape new lines
285286
if [[ "$prompt" =~ ^command: ]]; then
286-
escaped_prompt=${prompt#*command:}
287+
escaped_prompt=${prompt#command:}
287288
request_prompt=$COMMAND_GENERATION_PROMPT${escaped_prompt//$'\n'/' '}
288-
elif [[ "$prompt" =~ ^explain: ]]; then
289-
# todo fix explain request
290-
escaped_prompt=${prompt#*explain:}
291-
request_prompt=$EXPLAIN_CODE_PROMPT${escaped_prompt//$'\n'/' '}
292289
fi
293-
294290
build_user_chat_message "$chat_message" "$request_prompt"
295291
request_to_chat "$request_prompt"
296292
handle_error "$response"
297293
response_data=$(echo $response | jq -r '.choices[].message.content')
298294

299295
if [[ "$prompt" =~ ^command: ]]; then
300-
extracted_command=${response_data#*command:}
301-
echo -e "${CHATGPT_CYAN_LABEL} This is the command that you asked: ${extracted_command}\n"
296+
echo -e "${CHATGPT_CYAN_LABEL} ${response_data}\n"
297+
dangerous_commands=("rm" ">" "mv" "mkfs" ":(){:|:&};" "dd" "chmod" "wget" "curl")
298+
299+
for dangerous_command in "${dangerous_commands[@]}"; do
300+
if [[ "$response_data" == *"$dangerous_command"* ]]; then
301+
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."
302+
fi
303+
done
302304
echo "Would you like to execute it? (Yes/No)"
303305
read run_answer
304-
if [ "$run_answer" == "Yes" ] || [ "$run_answer" == "yes" ] || [ "$run_answer" == "y" ] || [ "$run_answer" == "Y" ] || [ "$run_answer" == "ok" ]; then
305-
echo -e "\nExecuting command: $extracted_command\n"
306-
eval $extracted_command
306+
if [ "$run_answer" == "Yes" ] || [ "$run_answer" == "yes" ] || [ "$run_answer" == "y" ] || [ "$run_answer" == "Y" ]; then
307+
echo -e "\nExecuting command: $response_data\n"
308+
eval $response_data
307309
fi
308-
elif [[ "$prompt" == ^explain: ]]; then
309-
echo -e "${CHATGPT_CYAN_LABEL}${response_data}"
310310
fi
311311
response_data=$(echo "$response_data" | sed 's/"/\\"/g')
312312
add_assistant_response_to_chat_message "$chat_message" "$response_data"

0 commit comments

Comments
 (0)