Been getting into LLMs and one thing I noticed being in Australia is internet speed still suck here compared to many other countries. Well in the meantime for the llamafile project, here is one useful linux bash script which may be useful for you who already downloaded a llamafile model a while ago, but want to take advantage of the new speedups that justine figured out for llamafile https://justine.lol/matmul/ .

This script facilitates the repackaging and upgrading of llamafile archives generated by Llama.cpp. This is particularly useful for users with limited internet access, by preserving existing gguf and .arg settings while replacing the llamafile engine. This may break in the future if the llamafile contains more than just .gguf and .args but for now it shall do.


This is also posted to gist in case anyone needs it.

#!/bin/bash
# Llama.cpp Repackaging Script
# This script facilitates the repackaging and upgrading of llamafile archives generated by Llama.cpp.
# This is particularly useful for users with limited internet access, by preserving existing gguf and .arg settings while replacing the llamafile engine.
#
# Usage: llamafilerepack [-h] [-f] <old> <new>
#   -h: Display usage information.
#   -f: Skip Version Check.
#   -v: Verbose Mode
#   <old>: The name of the old llamafile archive to be upgraded.
#   <new>: The name of the new llamafile archive to be created.
#
# Example:
#   llamafilerepack old_llamafile new_llamafile
#   This command will upgrade the old_llamafile to a new llamafile named new_llamafile.
#
# Author: Brian Khuu
# Date: 2023-04-06
# https://briankhuu.com/blog/2024/04/06/inplace-upgrading-of-llamafiles-engine-bash-script/
set -e

# Function to display usage information
print_usage() {
    echo "Usage: $0 [OPTION]... <old> <new>"
    echo "Upgrade llamafile archives."
    echo ""
    echo "Options:"
    echo "  -h, --help            display this help and exit"
    echo "  -f, --force           skip version check"
    echo "  -v, --verbose         verbose mode"
    echo ""
    echo "Arguments:"
    echo "  <old>                 the name of the old llamafile archive to be upgraded"
    echo "  <new>                 the name of the new llamafile archive to be created"
    echo ""
    echo "Example:"
    echo "  $0 old_llamafile new_llamafile"
    echo "  This command will upgrade the old_llamafile to a new llamafile named new_llamafile."
}

# Parse command-line options
while getopts "hfv" opt; do
    case $opt in
        h) 
            print_usage
            exit 0 ;;
        f) 
            force_upgrade=true
            echo "Skipping version check." ;;
        v) 
            verbose=true
            echo "Verbose Output Mode." ;;
    esac
done


# Shift the option parameters
shift $((OPTIND -1))

# Remove .llamafile extension from arguments if present
old_llamafile="${1%.llamafile}"
if [ -z "$2" ]; then
    new_llamafile="${1%.llamafile}.updated"
else
    new_llamafile="${2%.llamafile}"
fi

# Obtain versions of old and new llamafiles
old_llamafile_engine_version=$(./"$old_llamafile".llamafile --version)
new_llamafile_engine_version=$(/usr/local/bin/llamafile --version)

# Check if llamafile has been upgraded
echo "== Engine Version Check =="
echo "Engine version from $old_llamafile: $old_llamafile_engine_version"
echo "Engine version from /usr/local/bin/llamafile: $new_llamafile_engine_version"
if [ "$old_llamafile_engine_version" = "$new_llamafile_engine_version" ] && [ "$force_upgrade" != "true" ]; then
    echo "Upgrade not required. Exiting..."
    exit 0
fi

if [ "$verbose" = "true" ]; then
  echo "== Current Content =="
  zipinfo "${old_llamafile}.llamafile"
fi

echo "== Repackaging / Upgrading =="
tempdir=$(mktemp -d)
echo "extracting..."
unzip "${old_llamafile}.llamafile" -d ${tempdir}
echo "repackaging..."
cp /usr/local/bin/llamafile "${new_llamafile}.llamafile"
/usr/local/bin/zipalign -j0 "${new_llamafile}.llamafile" ${tempdir}/*.gguf ${tempdir}/.args

echo "== Completed =="