New ollama model creator module version
This commit is contained in:
@@ -1,49 +1,70 @@
|
|||||||
{ pkgs, ... }: {
|
{ pkgs, ... }: {
|
||||||
systemd.services.init-ollama-model = {
|
systemd.services.init-ollama-model = {
|
||||||
description = "Initialize LLM models with extra context in Ollama Docker";
|
description = "Initialize LLM models with extra context in Ollama Docker";
|
||||||
after = [ "docker-ollama.service" ];
|
|
||||||
|
# On s'assure que Docker tourne avant de lancer ce script
|
||||||
|
after = [ "docker.service" ];
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
|
|
||||||
script = ''
|
script = ''
|
||||||
# Wait for Ollama
|
# Fonction de création asynchrone pour ne pas bloquer le démarrage
|
||||||
while ! ${pkgs.curl}/bin/curl -s http://localhost:11434/api/tags > /dev/null; do
|
(
|
||||||
sleep 2
|
echo "Starting asynchronous Ollama initialization..."
|
||||||
|
|
||||||
|
# Attente d'Ollama (maximum 120 secondes pour éviter une boucle infinie)
|
||||||
|
TIMEOUT=60
|
||||||
|
COUNT=0
|
||||||
|
while ! ${pkgs.curl}/bin/curl -s -f http://127.0.0.1:11434/api/tags > /dev/null; do
|
||||||
|
if [ $COUNT -ge $TIMEOUT ]; then
|
||||||
|
echo "Ollama did not become ready in time. Exiting."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "Waiting for Ollama API to be reachable..."
|
||||||
|
sleep 5
|
||||||
|
COUNT=$((COUNT + 5))
|
||||||
done
|
done
|
||||||
|
|
||||||
create_model_if_missing() {
|
create_model_if_missing() {
|
||||||
local model_name=$1
|
local model_name=$1
|
||||||
local base_model=$2
|
local base_model=$2
|
||||||
if ! ${pkgs.docker}/bin/docker exec ollama ollama list | grep -q "$model_name"; then
|
|
||||||
|
# Vérification robuste via l'API HTTP d'Ollama plutôt que docker exec (évite les conflits de tty)
|
||||||
|
if ! ${pkgs.curl}/bin/curl -s http://127.0.0.1:11434/api/tags | ${pkgs.jq}/bin/jq -e ".models[] | select(.name == \"$model_name\")" > /dev/null; then
|
||||||
echo "$model_name not found, creating from $base_model..."
|
echo "$model_name not found, creating from $base_model..."
|
||||||
|
|
||||||
# We use a custom TEMPLATE block to strip the 'currentDate' function
|
# Utilisation d'un fichier temporaire sur l'hôte pour l'injecter proprement dans Docker
|
||||||
# which is unsupported in Ollama 0.5.7 but present in Devstral's default manifest.
|
TMP_FILE=$(mktemp)
|
||||||
${pkgs.docker}/bin/docker exec ollama sh -c "cat <<EOF > /root/.ollama/$model_name.modelfile
|
cat <<EOF > "$TMP_FILE"
|
||||||
FROM $base_model
|
FROM $base_model
|
||||||
TEMPLATE \"\"\"{{- if .System }}
|
TEMPLATE """{{- if .System }}
|
||||||
[SYSTEM_PROMPT]
|
[SYSTEM_PROMPT]
|
||||||
{{ .System }}
|
{{ .System }}
|
||||||
[/SYSTEM_PROMPT]
|
[/SYSTEM_PROMPT]
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- range .Messages }}
|
{{- range .Messages }}
|
||||||
{{- if eq .Role \"user\" }}
|
{{- if eq .Role "user" }}
|
||||||
[INST]
|
[INST]
|
||||||
{{ .Content }}
|
{{ .Content }}
|
||||||
[/INST]
|
[/INST]
|
||||||
{{- else if eq .Role \"assistant\" }}
|
{{- else if eq .Role "assistant" }}
|
||||||
{{ .Content }}
|
{{ .Content }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- end }}\"\"\"
|
{{- end }}"""
|
||||||
PARAMETER num_ctx 131072
|
PARAMETER num_ctx 131072
|
||||||
PARAMETER num_predict 4096
|
PARAMETER num_predict 4096
|
||||||
PARAMETER num_keep 1024
|
PARAMETER num_keep 1024
|
||||||
PARAMETER repeat_penalty 1.1
|
PARAMETER repeat_penalty 1.1
|
||||||
PARAMETER top_k 40
|
PARAMETER top_k 40
|
||||||
PARAMETER stop \"[INST]\"
|
PARAMETER stop "[INST]"
|
||||||
PARAMETER stop \"[/INST]\"
|
PARAMETER stop "[/INST]"
|
||||||
PARAMETER stop \"</s>\"
|
PARAMETER stop "</s>"
|
||||||
EOF"
|
EOF
|
||||||
${pkgs.docker}/bin/docker exec ollama ollama create "$model_name" -f "/root/.ollama/$model_name.modelfile"
|
|
||||||
${pkgs.docker}/bin/docker exec ollama rm "/root/.ollama/$model_name.modelfile"
|
# Copie et création dans le conteneur
|
||||||
|
${pkgs.docker}/bin/docker cp "$TMP_FILE" ollama:/tmp/model.modelfile
|
||||||
|
${pkgs.docker}/bin/docker exec ollama ollama create "$model_name" -f /tmp/model.modelfile
|
||||||
|
${pkgs.docker}/bin/docker exec ollama rm /tmp/model.modelfile
|
||||||
|
rm -f "$TMP_FILE"
|
||||||
else
|
else
|
||||||
echo "$model_name already exists, skipping."
|
echo "$model_name already exists, skipping."
|
||||||
fi
|
fi
|
||||||
@@ -55,13 +76,12 @@ EOF"
|
|||||||
# Create Devstral
|
# Create Devstral
|
||||||
create_model_if_missing "devstral-small-2:24b-128k" "devstral-small-2:24b"
|
create_model_if_missing "devstral-small-2:24b-128k" "devstral-small-2:24b"
|
||||||
|
|
||||||
# create_model_if_missing "qwen2.5-coder:32b-128k" "qwen2.5-coder:32b"
|
) &
|
||||||
|
|
||||||
# create_model_if_missing "mistral-large-planner:123b" "mistral-large:123b-instruct-v2407-q4_K_S"
|
|
||||||
'';
|
'';
|
||||||
|
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "oneshot";
|
Type = "forking"; # Permet à systemd de savoir que le script passe en arrière-plan via '&'
|
||||||
RemainAfterExit = true;
|
User = "root";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user