Skip to content

Instantly share code, notes, and snippets.

@mofosyne
Last active April 17, 2024 18:27
Show Gist options
  • Save mofosyne/46c63934305d5a5321c7e9fd83f4ef3e to your computer and use it in GitHub Desktop.
Save mofosyne/46c63934305d5a5321c7e9fd83f4ef3e to your computer and use it in GitHub Desktop.
This script facilitates the repackaging and upgrading of llamafile archives generated by Llama.cpp
#!/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 =="
@mofosyne
Copy link
Author

Created this gist as people may find it easier to read/use via github over my personal blog.

But if you want to read the writeup, it's in https://briankhuu.com/blog/2024/04/06/inplace-upgrading-of-llamafiles-engine-bash-script/

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