Skip to content

Instantly share code, notes, and snippets.

@Tedfulk
Last active October 18, 2024 16:17
Show Gist options
  • Save Tedfulk/24aab8e32e550edf32ceb68a52adf6f7 to your computer and use it in GitHub Desktop.
Save Tedfulk/24aab8e32e550edf32ceb68a52adf6f7 to your computer and use it in GitHub Desktop.

Fish function To update Ollama models For Mac

function update_ollama_models
    echo "Fetching list of Ollama models..."
    set -l jobs
    ollama list | tail -n +2 | while read -l line
        set model_name (echo $line | awk '{print $1}')
        if test -n "$model_name"
            echo "Attempting to update model: $model_name"
            ollama pull $model_name &
            set -a jobs $last_pid
            echo "Started update for model: $model_name"
        else
            echo "No model name extracted from line: $line"
        end
    end

    for job in $jobs
        wait $job
    end
    ollama list | tail -n +2 | awk -F"\t" '{printf $1 "\t" $3 "\t" $4 "\t" $5 RS}'
    echo "Ollama models update complete."
end

Fish function To update Ollama models for linux

function update_ollama_models
    echo "Updating Ollama client..."
    curl -fsSL https://ollama.com/install.sh | sh

    if not type -q ollama
        echo "Ollama command could not be found. Please check the installation."
        return 1
    end

    set -l ollama_proc (ps aux | grep '[o]llama serve')
    if test -z "$ollama_proc"
        echo "Ollama is not running. Starting Ollama serve..."
        ollama serve &
    else
        echo "Ollama serve is already running."
    end

    echo "Fetching list of Ollama models..."
    set -l jobs
    ollama list | tail -n +2 | while read -l line
        set model_name (echo $line | awk '{print $1}')
        if test -n "$model_name"
            echo "Attempting to update model: $model_name"
            ollama pull $model_name &
            set -a jobs $last_pid
            echo "Started update for model: $model_name"
        else
            echo "No model name extracted from line: $line"
        end
    end

    for job in $jobs
        wait $job
    end
    ollama list | tail -n +2 | awk -F"\t" '{printf $1 "\t" $3 "\t" $4 "\t" $5 RS}'
    echo "Ollama models update complete."
end
@megvadulthangya
Copy link

How about an sh scirpt which can log?

#!/bin/bash

LOGFILE="/path_whatewayouwant/ollama_update.log"

Write output to the log file

echo "Starting Ollama update script..." $(date) >> $LOGFILE

echo "Updating Ollama client..." >> $LOGFILE
curl -fsSL https://ollama.com/install.sh | sh >> $LOGFILE 2>&1

if ! command -v ollama &> /dev/null; then
echo "Ollama command could not be found. Please check the installation." >> $LOGFILE
exit 1
else
echo "Ollama command exists." >> $LOGFILE
fi

ollama_proc=$(pgrep -f "ollama serve")
if [ -z "$ollama_proc" ]; then
echo "Ollama is not running. Starting Ollama serve..." >> $LOGFILE
ollama serve >> $LOGFILE 2>&1 &
echo "Ollama serve started." >> $LOGFILE
else
echo "Ollama serve is already running." >> $LOGFILE
fi

echo "Fetching list of Ollama models..." >> $LOGFILE
jobs=()
while read -r line; do
model_name=$(echo "$line" | awk '{print $1}')
if [ -n "$model_name" ]; then
echo "Attempting to update model: $model_name" >> $LOGFILE
ollama pull "$model_name" >> $LOGFILE 2>&1 &
jobs+=($!)
echo "Started update for model: $model_name" >> $LOGFILE
else
echo "No model name extracted from line: $line" >> $LOGFILE
fi
done < <(ollama list | tail -n +2)

for job in "${jobs[@]}"; do
wait "$job"
done

ollama list | tail -n +2 | awk -F"\t" '{printf $1 "\t" $3 "\t" $4 "\t" $5 RS}' >> $LOGFILE
echo "Ollama models update complete." >> $LOGFILE

@Tedfulk
Copy link
Author

Tedfulk commented Oct 18, 2024

@megvadulthangya love it, good idea!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment