Fixes and improvements for runai script

This commit is contained in:
Enrico Ludwig 2024-09-04 12:13:39 +02:00
parent 64b72b49d7
commit 5b41b49ed4

View File

@ -7,8 +7,11 @@ MAX_TOKENS=3072 # 1 to 4096 (higher requires more API credits, but can be mo
# The base prompt describing the rules the AI should follow # The base prompt describing the rules the AI should follow
read -r -d '' BASE_PROMPT << EOM read -r -d '' BASE_PROMPT << EOM
Do not explain anything. Do not explain anything.
Only respond with a command that can be executed on the linux terminal. Only respond with a command that can be executed on the linux terminal, as long as a valid question was provided by the user.
If you can't generate a valid command, always respond with '<<CANCEL>>'.
If the user did not provide a valid question, respond with '<<CANCEL>>'.
Never use code tags. Never use code tags.
Never ask the user for anything.
Always put everything in a single command, e.g. using pipes, semicolons or double ampersands. Always put everything in a single command, e.g. using pipes, semicolons or double ampersands.
Break up the command in multiple lines when using redirections, semicolons or double ampersands, but make sure the command can still be executed (e.g. by using a backslash). Break up the command in multiple lines when using redirections, semicolons or double ampersands, but make sure the command can still be executed (e.g. by using a backslash).
Do not use any formatting except bash escaped newlines. Do not use any formatting except bash escaped newlines.
@ -32,7 +35,6 @@ if [[ "$1" != "-a" && -z "$OPENAI_API_KEY" ]]; then
if [ -z "$OPENAI_API_KEY" ]; then if [ -z "$OPENAI_API_KEY" ]; then
echo "Please set the OPENAI_API_KEY environment variable." echo "Please set the OPENAI_API_KEY environment variable."
exit 1
fi fi
fi fi
fi fi
@ -46,7 +48,7 @@ fi
function run_ai() function run_ai()
{ {
if [ -z "$*" ]; then if [ -z "$*" ]; then
echo "Usage: runai <command>" echo "Usage: runai <prompt>"
return return
fi fi
@ -59,6 +61,11 @@ function run_ai()
command=$(OPENAI_API_KEY=$OPENAI_API_KEY openai api chat.completions.create -m gpt-4o -t 0.4 -M 3072 -g "user" "$BASE_PROMPT $*") command=$(OPENAI_API_KEY=$OPENAI_API_KEY openai api chat.completions.create -m gpt-4o -t 0.4 -M 3072 -g "user" "$BASE_PROMPT $*")
if [ "$command" == "<<CANCEL>>" ]; then
echo -e "\033[1;31mThe AI could not generate a valid command. Please try again with a different prompt.\033[0m"
return
fi
echo -e -n "\e[0m" echo -e -n "\e[0m"
if [ $ALL_YES == false ]; then if [ $ALL_YES == false ]; then
@ -83,10 +90,13 @@ if [ "$1" == "-a" ]; then
# check first, if an alias with the same name already exists # check first, if an alias with the same name already exists
if grep -q "alias runai=" ~/.bashrc; then if grep -q "alias runai=" ~/.bashrc; then
echo "The alias runai already exists." echo "The alias runai already exists."
exit 0 return
else else
echo "alias runai='source $0'" >> ~/.bashrc echo "alias runai='source $(realpath $0)'" >> ~/.bashrc
echo "The alias runai has been set. You can now run the AI by typing runai <command>." echo "The alias runai has been set. You can now run the AI by typing runai <command>."
# source the .bashrc file to make the alias available in the current shell
source ~/.bashrc
fi fi
else else
run_ai "$*" run_ai "$*"