#!/bin/bash # Flip the missionMATCH generation runtime the MEGAMIND gateway proxies, then verify it. # # gateway-backend.sh ollama the local Ollama binary (default, no Python, Metal accelerated) # gateway-backend.sh mlx the external MLX server on 8081 (Apple Silicon, fastest on an M chip) # gateway-backend.sh megamind the MEGAMIND reasoning brain on 9999 (answers from its own learned corpus) # # Embeddings ALWAYS stay on Ollama's bge model so the stored vectors and the boot vector gate keep matching; # only generation is selectable. The running mm-api reads the gateway per request, so a flip is picked up with # no mm-api redeploy. If the selected backend fails at request time the gateway falls back to Ollama. set -u BACKEND=${1:-ollama} case "$BACKEND" in ollama|mlx|megamind) ;; *) echo "usage: $0 ollama|mlx|megamind"; exit 2 ;; esac # When flipping to MLX, make sure a known good small MLX server is up on 8081 first (a 4 bit 3B model that is # fast on an M chip and leaves room for the Ollama faithfulness model in 16GB). If it is not running, start it; # the gateway's ollama fallback covers requests while the model loads. if [ "$BACKEND" = "mlx" ]; then MLXBIN="$HOME/mlx-train-venv/bin/mlx_lm.server" MLXMODEL=mlx-community/Qwen2.5-3B-Instruct-4bit if pgrep -f "mlx_lm.server.*--port 8081" >/dev/null 2>&1; then echo "mlx server already up on 8081" elif [ -x "$MLXBIN" ]; then echo "starting mlx server ($MLXMODEL) on 8081" nohup "$MLXBIN" --model "$MLXMODEL" --host 0.0.0.0 --port 8081 > "$HOME/mlx-server.log" 2>&1 /dev/null sleep 1 # No MEGAMIND_ADDR: the gateway binds the Tailscale interface by itself (loopback when none is up). MEGAMIND_SECRETS_FILE="$SECRETS_FILE" MEGAMIND_GENERATE_BACKEND="$BACKEND" \ nohup "$HOME/megamind-gateway" > "$HOME/megamind-gateway.log" 2>&1 /dev/null || tailscale ip -4 2>/dev/null || echo 127.0.0.1) if ! curl -fsS -m 5 "http://$TSIP:8090/healthz" >/dev/null 2>&1; then echo "gateway did not come up on 8090, check $HOME/megamind-gateway.log" exit 1 fi echo "gateway restarted with generation backend=$BACKEND" head -1 "$HOME/megamind-gateway.log"