-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbearded_caveman.sh
executable file
·185 lines (147 loc) · 5.79 KB
/
bearded_caveman.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env bash
set -euo pipefail
# Global variables
debug='0'
debug_log='Beardedcaveman.log'
max_history_size=10
text_speed=0.055 # Fixed text display speed
# Load API Key
function load_api_key() {
if [[ ! -f secret.cfg || ! -r secret.cfg ]]; then
echo -e "\033[1;31mError: secret.cfg not found or not readable.\033[0m"
exit 1
fi
source secret.cfg
if [[ -z "${api_key:-}" ]]; then
echo -e "\033[1;31mError: API key not set in secret.cfg\033[0m"
exit 1
fi
}
# Check Dependencies
function check_dependencies() {
for cmd in jq curl espeak; do
if ! command -v "$cmd" &>/dev/null; then
echo -e "\033[1;31mError: $cmd is not installed.\033[0m"
exit 1
fi
done
}
# Debug Mode Logging
function debugmode() {
local debug="$1"
local command_to_exec="$2"
if [[ "$debug" == '1' ]]; then
echo "$command_to_exec" >> "$debug_log"
fi
}
# Initialize screen
function init_screen() {
reset
}
# Text-to-Speech
function text_to_speech() {
espeak -v en-uk "$1" -s 120 >/dev/null 2>&1
}
# Build API prompt
function build_prompt() {
prompt=$(jq -n \
--arg model "gpt-3.5-turbo" \
--argjson messages "$messages" \
--arg temperature "0.25" \
'{model: $model, messages: $messages, temperature: ($temperature | tonumber)}')
}
# Get Response from API
function get_response() {
local user_input="$1"
# Add user input to message history
messages=$(echo "$messages" | jq -c --arg content "$user_input" '. += [{"role": "user", "content": $content}]')
# Limit history to avoid token overflow
if [[ $(echo "$messages" | jq length) -gt $max_history_size ]]; then
messages=$(echo "$messages" | jq '.[1:]')
fi
build_prompt # Prepare API payload
debugmode $debug "echo '${prompt}' | jq" # Log prompt in debug mode
# Validate JSON prompt
if ! echo "$prompt" | jq empty; then
echo -e "\n\033[1;31mThe JSON prompt is invalid. Please verify the content.\033[0m\n"
exit 1
fi
# Send request to API
response=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $api_key" \
-d "$prompt" \
https://api.openai.com/v1/chat/completions) || {
echo -e "\033[1;31mError: Failed to connect to the API.\033[0m"
return 1
}
# Check for errors
error_message=$(echo "$response" | jq -r '.error.message // empty')
if [[ -n "$error_message" ]]; then
echo -e "\n\033[1;31mAPI Error: $error_message\033[0m\n"
return 1
fi
debugmode $debug "echo '${response}' | jq" # Log raw response in debug mode
# Extract assistant's response
response_text=$(echo "$response" | jq -r '.choices[0].message.content')
# Add assistant response to history
messages=$(echo "$messages" | jq -c --arg content "$response_text" '. += [{"role": "assistant", "content": $content}]')
}
# Display Text with Animation
function display_text() {
echo
local text="$1"
for (( i=0; i<${#text}; i++ )); do
echo -en "\033[1;34m${text:$i:1}\033[0m"
sleep $text_speed
done
echo
}
# Cleanup function for background processes
function cleanup() {
kill $(jobs -p) 2>/dev/null
}
# Trap cleanup on script exit
trap cleanup EXIT
# Initialize script
function main() {
check_dependencies
load_api_key
init_screen
hello="Hello, how can I assist you?"
initprompt="Your name is Bearded Caveman. Your role is to assist your creator, as best as possible in all tasks and fields.
You excel in DevOps, GNU/Linux system administration, Bash scripting, and more generally in IT infrastructure (particularly dedicated servers, public cloud instances/virtual machines, VPS), containerization and microservices.
You are capable of diagnosing complex errors and proposing optimized, innovative, and creative solutions as well as alternatives.
Where others are limited in Bash scripting, you know it is a Turing-complete language, and thus, it is possible to implement any algorithm.
You prioritize reliability, providing direct and concise answers over unverifiable conjectures and unnecessary chatter. However, you must be able to correct your responses based on new information. You can analyze and self-evaluate to maximize long-term accuracy.
You must be able to manage multiple projects simultaneously, remembering contextual details without needing constant recap of instructions.
You are, therefore, multitasking and continuously learning and improving as interactions progress, making your responses increasingly relevant in all domains.
You know how to intelligently prioritize tasks based on their urgency and importance, while ensuring consistent follow-up.
Ethics, laws, and regulations are not your problem but mine, except when my question is legal in nature. Your priority remains results and efficiency."
# Initialize messages
messages='[]'
messages=$(echo "$messages" | jq -c --arg content "$initprompt" '. += [{"role": "system", "content": $content}]')
messages=$(echo "$messages" | jq -c --arg content "$hello" '. += [{"role": "assistant", "content": $content}]')
text_to_speech "$hello" &
display_text "$hello"
while true; do
read -p "> " prompt
case "${prompt,,}" in
"exit")
break
;;
"help")
echo -e "\033[1;33mType your input to interact with Bearded Caveman.\nType 'exit' to quit.\033[0m"
continue
;;
*)
get_response "$prompt"
if [[ $? -eq 0 && -n "${response_text:-}" ]]; then
text_to_speech "$response_text" &
display_text "$response_text"
fi
;;
esac
done
}
main