-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_loaded_models.sh
96 lines (88 loc) · 2.88 KB
/
show_loaded_models.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
#!/bin/bash
# Global Vars
ollama_url=http://localhost
port="11434"
blobs=()
model_name_paths=()
#Parse arguments
while [ "$#" -gt 0 ]; do
case $1 in
-h|--help)
echo
echo " Identifies Ollama models running on this operating system by parsing running processes."
echo
echo " Usage: $0 [options]"
echo
echo " Options:"
echo " -j, --json_output Prints result as a json object. Other output disabled. (Default: false)"
echo " -p, --port [port number] Specify Ollama Server port (Default: 11434)"
echo " -u, --ollama_url [url] Specify Ollama Server URL (Default: http://localhost)"
echo
echo " Dependencies: jq"
exit 0
;;
-j|--json_output)
json_out=1
shift 1
;;
-u|--ollama_url)
ollama_url=$2
shift 2
;;
-p|--port)
port=$2
shift 2
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
compare_running_models_and_modelfiles() {
json_match=()
json_output=()
local matching_models=()
OLDIFS=$IFS
for ((i=0; i<${#model_name_paths[@]}; i++)); do # Iterate over the array of modelname,blob-path
for blob in "${blobs[@]}"; do
IFS=',', read -ra fields <<< "${model_name_paths[i]}" # Split the string into parts
if [ "${fields[1]}" == "$blob" ]; then # Check if current 'field' matches a blob
matching_models+=( '{ "model": "'"${fields[0]}"'", "path": "'"${fields[1]}"'"}') # Add to list of matching models
fi
done
done
if [ -z "$json_out" ]; then
echo -e "\nModel Found: \n $(echo ${matching_models[*]} | jq '.' | sed s/[{}]//g) \n"
else
local json_match="${matching_models[*]}"
json_output=$(echo $json_match | jq -c -s .)
echo "$json_output"
fi
IFS=$OLDIFS
}
get_running_model_paths() {
blobs=$(ps aux | grep -- '--model' | grep -v grep | grep -Po '(?<=--model\s).*' | cut -d ' ' -f1)
if [ -z "$blobs" ]; then
echo -e "\n\n Warning: No running Ollama models detected!\n"
exit 0
fi
}
parse_modelfiles() {
if [ -z "$json_out" ]; then
echo -e "\nConnecting to $ollama_url:$port\n"
if [ -z "$(curl -s $ollama_url:$port)" ]; then
echo -e "Could not connect to Ollama. Check the ollama_url parameter and that the server is running\n"
exit 1
fi
echo $(curl -s "$ollama_url:$port")
fi
local models=( $(curl -s "$ollama_url:$port/api/tags" | jq -r '.models[].name') )
for model in "${models[@]}"; do
local modelfile=$(curl -s "$ollama_url:$port/api/show" -d '{ "name": "'"$model"'", "modelfile": true }' | jq -r '.modelfile')
model_name_paths+=($model,$(echo "$modelfile" | awk '/^FROM/{print $2}'))
done
}
parse_modelfiles
get_running_model_paths
compare_running_models_and_modelfiles