-
Notifications
You must be signed in to change notification settings - Fork 0
/
cowllama.sh
227 lines (206 loc) · 6.14 KB
/
cowllama.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/bin/bash
###################
# OLLAMA SETTINGS #
###################
# Available ollama commands (leave empty to disable)
OLLAMA_CONTAINER="ollama"
OLLAMA_BINARY="ollama"
# Available URLs for APIs (leave empty to disable)
OLLAMA_LOCAL_URL="http://localhost:11434"
OLLAMA_DOCKER_URL="http://localhost:11434"
OLLAMA_REMOTE_URL="https://ollama.example.com"
###################
# COWSAY SETTINGS #
###################
# Eyes and tongue of the llama
COWFILE="llama"
EYE="°"
TONGUE="U "
###############################
# DON'T CHANGE ANYTHING BELOW #
###############################
VARGS=($@)
ARGLENGTH=${#VARGS[@]}
VENG=$1
VCMD=$2
VENG_ARGS="${VARGS[@]:1:$ARGLENGTH}"
VMODEL=$3
VPROMPT="${VARGS[@]:3:$ARGLENGTH}"
# take binary into account, but
if [[ "$OLLAMA_BINARY" != "" ]]; then
OLLAMA=$OLLAMA_BINARY
VENGINE="Local"
fi
# default to docker if possible
if [[ "$OLLAMA_CONTAINER" != "" ]]; then
OLLAMA_DOCKER="docker exec -it $OLLAMA_CONTAINER ollama"
OLLAMA=$OLLAMA_DOCKER
VENGINE="Docker"
fi
health() {
case $VENGINE in
Local|Remote|Docker)
echo -e "\nChecking ollama API ..\n";
sleep 0.5
# check if api available
curl $OLLAMA_API/ -I -s 2>&1 >/dev/null && (
curl $OLLAMA_API/ -s | cowsay -W 60 -e "$EYE" -f $COWFILE
) || (
# if not
echo "API not available" | cowthink -W 60 -e "+" -T "$TONGUE" -f $COWFILE 2>/dev/null
);
echo -e "\nAPI: $VENGINE";
;;
*)
echo -e "\nChecking ollama API .."
sleep 0.5
echo -e "DOCKER \t $(curl $OLLAMA_DOCKER_URL/ -s -I &> /dev/null && echo 'Healthy' || echo 'Not Available')";
echo -e "LOCAL \t $(curl $OLLAMA_LOCAL_URL/ -s -I &> /dev/null && echo 'Healthy' || echo 'Not Available')";
echo -e "REMOTE \t $(curl $OLLAMA_REMOTE_URL/ -I &> /dev/null && echo 'Healthy' || echo 'Not Available')";
echo
;;
esac
exit;
}
run() {
case $VENGINE in
Local)
# check if ollama is available
$OLLAMA_BINARY -v 2>&1 >/dev/null && (
# if true
$OLLAMA_BINARY run $VMODEL $VPROMPT
) || (
# if not
echo "ollama not installed locally" | cowthink -W 60 -e "+" -T "$TONGUE" -f $COWFILE 2>/dev/null
);
echo -e "\nAPI: $VENGINE";
;;
Remote)
# check if api available
curl $OLLAMA_API/ -I -s 2>&1 >/dev/null && (
# if true
curl $OLLAMA_API/v1/chat/completions -s -d '{
"model": "'$VMODEL'",
"stream": false,
"messages": [{
"role": "user",
"content": "'"${VPROMPT//\"/\\\"}"'"
}]
}' | jq -r '.choices[0].message.content' | cowsay -W 60 -e "$EYE" -f $COWFILE
) || (
# if not
echo "Ollama not available" | cowthink -W 60 -e "+" -T "$TONGUE" -f $COWFILE 2>/dev/null
);
echo -e "\nAPI: $VENGINE";
;;
Docker)
# check if container available
$OLLAMA_DOCKER -v 2>&1 >/dev/null && (
# if true
$OLLAMA_DOCKER run "$VMODEL" "$VPROMPT"
) || (
# if not
echo "Ollama not available" | cowthink -W 60 -e "+" -T "$TONGUE" -f $COWFILE 2>/dev/null
);
echo -e "\nAPI: $VENGINE";
;;
esac
exit;
}
list() {
# check if api available
curl $OLLAMA_API/ -I -s 2>&1 >/dev/null && (
# if true
echo -e "\nAvailable models:\n";
curl $OLLAMA_API/api/tags -s | jq -r '.models|=sort_by(.name)|.models[].name'
) || (
# if not
echo "Ollama not available" | cowthink -W 60 -e "+" -T "$TONGUE" -f $COWFILE 2>/dev/null
);
echo -e "\nAPI: $VENGINE";
exit;
}
pull() {
# check if api available
curl $OLLAMA_API/ -I -s 2>&1 >/dev/null && (
# if true
echo -e "\nPulling $VMODEL ..";
curl -fsSL -d '{"model": "'$VMODEL'" }' $OLLAMA_API/api/pull |
jq -r '.status' |
sed -u 'i\\o033[2K' |
stdbuf -o0 tr '\n' '\r';
echo
) || (
# if not
echo "Ollama not available" | cowthink -W 60 -e "+" -T "$TONGUE" -f $COWFILE 2>/dev/null
)
echo -e "\nAPI: $VENGINE";
exit;
}
update_all() {
# check if api available
curl $OLLAMA_API/ -I -s 2>&1 >/dev/null && (
#if true
# countdown
countdown=5;
echo -e "Time to Update all $VENGINE models .." | cowthink -e "$EYE" -f $COWFILE;
echo
while [ $countdown -gt 0 ]; do
echo -ne " Updating in $countdown\033[0K\r"
sleep 1;
: $((countdown--))
done
echo -e "\n"
# update all models
curl $OLLAMA_API/api/tags -s | jq -r '.models[].name' | tail -n +2 | awk '{print $1}' | while read -r _model; do
echo -e "\nPulling $_model ..";
curl -fsSL -d '{"model": "'$_model'" }' $OLLAMA_API/api/pull |
jq -rS '.status' |
sed -u 'i\\o033[2K' |
stdbuf -o0 tr '\n' '\r' &&
echo
done
) || (
# if not
echo "Ollama not available" | cowthink -W 60 -e "+" -T "$TONGUE" -f $COWFILE 2>/dev/null
);
echo -e "\nAPI: $VENGINE";
exit;
}
native() {
echo
$OLLAMA $@
echo -e "\nAPI: $VENGINE";
exit;
}
case $VENG in
--local|-L) VENGINE="Local";
OLLAMA_API=$OLLAMA_LOCAL_URL;
;;
--docker|-D) VENGINE="Docker";
OLLAMA_API=$OLLAMA_DOCKER_URL;
;;
--remote|-R) VENGINE="Remote";
OLLAMA_API=$OLLAMA_REMOTE_URL;
;;
--health) VENGINE="";
health;
;;
--update-all) update_all;
;;
*) native "$@";
;;
esac
case $VCMD in
health) health;
;;
run) run;
;;
list) list;
;;
pull) pull;
;;
update-all) update_all;
;;
*) native "$VENG_ARGS";
esac