Skip to content

Commit 492e6e7

Browse files
committed
First draft of adding command generation and execution and command explanation
1 parent 192a672 commit 492e6e7

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

chatgpt.sh

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ 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:"
9+
10+
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:"
11+
812
CHATGPT_CYAN_LABEL="\n\033[36mchatgpt \033[0m"
913

1014
# error handling function
@@ -116,7 +120,7 @@ build_user_chat_message() {
116120
chat_message="$chat_message, {\"role\": \"user\", \"content\": \"$escaped_prompt\"}"
117121
fi
118122

119-
request_prompt=$chat_message
123+
request_prompt="$chat_message"
120124
}
121125

122126
# adds the assistant response to the message in (chatml) format
@@ -224,7 +228,7 @@ if [ -p /dev/stdin ]; then
224228
elif [ -n "$prompt" ]; then
225229
pipe_mode_prompt=${prompt}
226230
else
227-
echo -e "Welcome to chatgpt. You can quit with '\033[36mexit\033[0m'."
231+
echo -e "Welcome to chatgpt. You can quit with '\033[36mexit\033[0m' or '\033[36mq\033[0m'."
228232
fi
229233

230234
while $running; do
@@ -239,7 +243,7 @@ while $running; do
239243
CHATGPT_CYAN_LABEL=""
240244
fi
241245

242-
if [ "$prompt" == "exit" ]; then
246+
if [ "$prompt" == "exit" ] || [ "$prompt" == "q" ]; then
243247
running=false
244248
elif [[ "$prompt" =~ ^image: ]]; then
245249
request_to_image "$prompt"
@@ -274,6 +278,42 @@ while $running; do
274278
handle_error "$models_response"
275279
model_data=$(echo $models_response | jq -r -C '.data[] | select(.id=="'"${prompt#*model:}"'")')
276280
echo -e "${CHATGPT_CYAN_LABEL}Complete details for model: ${prompt#*model:}\n ${model_data}"
281+
elif [[ "$prompt" =~ ^command: ]] || [[ "$prompt" =~ ^explain: ]]; then
282+
# escape quotation marks
283+
escaped_prompt=$(echo "$prompt" | sed 's/"/\\"/g')
284+
# escape new lines
285+
if [[ "$prompt" =~ ^command: ]]; then
286+
escaped_prompt=${prompt#*command:}
287+
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'/' '}
292+
fi
293+
294+
build_user_chat_message "$chat_message" "$request_prompt"
295+
request_to_chat "$request_prompt"
296+
handle_error "$response"
297+
response_data=$(echo $response | jq -r '.choices[].message.content')
298+
299+
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"
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" ] || [ "$run_answer" == "ok" ]; then
305+
echo -e "\nExecuting command: $extracted_command\n"
306+
eval $extracted_command
307+
fi
308+
elif [[ "$prompt" == ^explain: ]]; then
309+
echo -e "${CHATGPT_CYAN_LABEL}${response_data}"
310+
fi
311+
response_data=$(echo "$response_data" | sed 's/"/\\"/g')
312+
add_assistant_response_to_chat_message "$chat_message" "$response_data"
313+
314+
timestamp=$(date +"%d/%m/%Y %H:%M")
315+
echo -e "$timestamp $prompt \n$response_data \n" >>~/.chatgpt_history
316+
277317
elif [[ "$MODEL" =~ ^gpt- ]]; then
278318
# escape quotation marks
279319
escaped_prompt=$(echo "$prompt" | sed 's/"/\\"/g')

0 commit comments

Comments
 (0)