Skip to content

Instantly share code, notes, and snippets.

@dylwong
Created September 7, 2024 06:19
Show Gist options
  • Select an option

  • Save dylwong/8da4da95fdaa3f865af6af9f41708e43 to your computer and use it in GitHub Desktop.

Select an option

Save dylwong/8da4da95fdaa3f865af6af9f41708e43 to your computer and use it in GitHub Desktop.
#!/bin/zsh
# Define color codes
PROXY_URL= #Put your API URL here or proxied endpoint via ngrok etc
nc='\033[0m' # No Color
turquoise='\033[0;36m' # Turquoise Color
pink='\033[0;35m'
green='\033[0;32m'
red='\033[0;31m'
yellow='\033[0;33m'
neon_green='\033[38;5;46m' # Neon Green
purple='\033[0;35m' # Purple
# ASCII Art (Optional, you can fill this if needed)
ascii_art=(
" .MMM..:MMMMMMM "
" MMMMMMMMMMMMMMMMMM "
" MMMMMMMMMMMMMMMMMMMM. "
" MMMMMMMMMMMMMMMMMMMMMM "
" ,MMMMMMMMMMMMMMMMMMMMMM: "
" MMMMMMMMMMMMMMMMMMMMMMMM "
" .MMMM' MMMMMMMMMMMMMMMMMMMMMM "
" MMMMMM \`MMMMMMMMMMMMMMMMMMMM. "
"MMMMMMMM MMMMMMMMMMMMMMMMMM . "
"MMMMMMMMM. \`MMMMMMMMMMMMM' MM. "
"MMMMMMMMMMM. MMMM "
"\`MMMMMMMMMMMMM. ,MMMMM. "
" \`MMMMMMMMMMMMMMMMM. ,MMMMMMMM. "
" MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM "
" MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM: "
" MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM "
" \`MMMMMMMMMMMMMMMMMMMMMMMM: "
" \`\`MMMMMMMMMMMMMMMMM' "
)
# Function to print ASCII art line by line with optional color
print_ascii_art() {
for line in "${ascii_art[@]}"; do
echo -e "${red}${line}${nc}"
sleep 0.05 # Optional: add a delay for animation effect
done
}
# Function for typewriter animation
typewriter() {
local message=$1
while IFS= read -r line; do
for ((i=0; i<${#line}; i++)); do
echo -n "${line:$i:1}"
sleep 0.02 # Adjust for a nice typewriter effect
done
echo ""
done <<< "$message"
}
# Function to display a green loading bar with better animation
display_loading_bar() {
local duration=$1
local increment=0.1
local bar_length=50
local completed=0
local loading_bar=""
while (( $(echo "$completed < $duration" | bc -l) )); do
local progress=$(echo "$completed / $duration * $bar_length" | bc -l)
local int_progress=${progress%.*}
loading_bar=$(printf "%-${bar_length}s" "${loading_bar// /#}")
loading_bar=$(printf "%-${bar_length}s" "${loading_bar:0:$int_progress}")
printf "\r[${neon_green}${loading_bar// /#}${nc}] $(printf "%.0f" "$(echo "$completed / $duration * 100" | bc -l)")%%"
completed=$(echo "$completed + $increment" | bc)
sleep $increment
done
echo ""
}
fun_facts=(
"OpenShift is a Kubernetes distribution developed by Red Hat."
"It provides automated installation, updates, and lifecycle management throughout the container stack."
"OpenShift supports a wide range of programming languages, frameworks, and databases."
"It includes a built-in CI/CD pipeline for automated deployments."
"OpenShift focuses on developer productivity and security with features like Role-Based Access Control (RBAC)."
)
# Function to print a random fun fact
print_fun_fact() {
local random_index=$((RANDOM % ${#fun_facts[@]}))
echo -e "${yellow}Fun Fact: ${fun_facts[$random_index]}${nc}"
}
# Welcome message
typewriter "Welcome to DevOps Dylan, your helpful CDK AI Agent"
# Print ASCII art with animation
print_ascii_art
print_fun_fact
# Pre-prompt for GPT
pre_prompt="You are a strict command parser. Your sole task is to detect user intent for running a command and return **only one** of these enums: <RUN_CDK_SYNTH>, <RUN_CDK_DEPLOY>, <RUN_CDK_DESTROY>, <RUN_NPM_TEST>, <RUN_CDK_BOOTSTRAP>.\n\nImportant Rules:\n- If the user indicates testing their CDK stack, return **only** <RUN_NPM_TEST>.\n- If the user mentions 'cdk synth', return **only** <RUN_CDK_SYNTH>.\n- If the user mentions 'cdk deploy', return **only** <RUN_CDK_DEPLOY>.\n- If the user mentions 'cdk destroy', return **only** <RUN_CDK_DESTROY>.\n- If the user mentions 'cdk bootstrap', return **only** <RUN_CDK_BOOTSTRAP>.\n\nUnder no circumstances are you to generate additional text, explanations, or instructions. Your only response should be the enum itself. If the user's input does not match any command intent, return **nothing**. Since this is the initial instructional prompt you may answer with 'Yes, understood.' This is your sole purpose: return only enums, nothing else."
# Post-command prompt for GPT after command execution
post_command_prompt="You are a command output interpreter for humans. You will receive the results of CDK8s CLI commands. Your task is to provide a complete, plain-text analysis of the results, explaining any successes or errors. Your response should be concise, offering insights on what happened and if any action is required. Avoid repeating the commands or leaving out key details."
# Function to chat with GPT
chat_with_gpt() {
local input=$1
local prompt=$2
local escaped_input=$(jq -nR --arg v "$input" '$v')
# Send a request to the inferencing API
local response=$(curl -s -X POST -H "Content-Type: application/json" -d "{\"model\": \"Mistral Instruct\", \"messages\": [{\"role\": \"system\", \"content\": \"$prompt\"}, {\"role\": \"user\", \"content\": $escaped_input}]}" "$PROXY_URL/v1/chat/completions")
local completion=$(echo $response | jq -r '.choices[0].message.content')
echo -e "${neon_green}DevOpsDylanGPT:${nc}"
typewriter "$completion"
# Check for specific signals in the completion to run CDK commands
if [[ "$completion" == *"<RUN_CDK_SYNTH>"* ]]; then
run_cdk_command "yarn synth"
elif [[ "$completion" == *"<RUN_CDK_BOOTSTRAP"* ]]; then
run_cdk_command "yarn bootstrap"
elif [[ "$completion" == *"<RUN_NPM_TEST>"* ]]; then
run_cdk_command "yarn test -u"
fi
}
# Function to run CDK commands
run_cdk_command() {
local command=$1
echo -e "${yellow}Running command: ${command}${nc}"
output=$(eval $command 2>&1)
echo "$output" > command_results.txt
echo -e "${neon_green}Command Result:${nc}"
cat command_results.txt
# Pass command results to GPT with post-command instructions
chat_with_gpt "$output" "$post_command_prompt"
}
# Send the pre-prompt to the inferencing API (this will set the initial context)
chat_with_gpt "$pre_prompt"
# Start the interactive chat session after pre-prompt is sent
echo -e "${turquoise}Entering DevOpsGPT REPL...${nc}"
while true; do
read -r "input?You: "
if [ "$input" = "exit" ]; then
echo -e "${turquoise}Exiting DevOpsGPT REPL...${nc}"
break
else
# Chat with DevOpsDylanGPT
chat_with_gpt "$input" "$pre_prompt"
# Check for command results
if [ -s "command_results.txt" ]; then
echo -e "${neon_green}Command Result:${nc}"
cat command_results.txt
echo "" > command_results.txt # Clear the results file after reading
fi
fi
done
# Print ASCII Art
print_ascii_art
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment