56 lines
2.4 KiB
Bash
Executable File
56 lines
2.4 KiB
Bash
Executable File
#!/bin/bash
|
||
# 启动本地 MLX 模型服务:chat + embedding 均由 oMLX(18080) 提供
|
||
# chat=Qwen3.5-9B-MLX-4bit(4-bit),embedding=BGE-M3(oMLX 原生 BERT 系支持)
|
||
# 首次运行自动检测 .venv:缺失/损坏(如来自其他机器)时按 requirements.txt 重建,就绪则跳过安装
|
||
# 用法: ./scripts/start_models.sh
|
||
set -eu
|
||
cd "$(dirname "$0")/.."
|
||
ROOT=$(pwd)
|
||
VENV="$ROOT/.venv"
|
||
LOG_DIR="$HOME/.omlx/logs"
|
||
CACHE_DIR="$HOME/.omlx/cache"
|
||
mkdir -p "$LOG_DIR" "$CACHE_DIR"
|
||
|
||
# .venv 不可跨机复制(pyvenv.cfg / bin 脚本 shebang 写死本机路径),须在本机重建
|
||
if [ ! -x "$VENV/bin/python" ]; then
|
||
if [ -d "$VENV" ]; then
|
||
echo "[warn] $VENV 存在但 python 不可用(可能来自其他机器),删除重建"
|
||
rm -rf "$VENV"
|
||
fi
|
||
if ! command -v python3 >/dev/null 2>&1; then
|
||
echo "[error] 未找到 python3,请先安装 Python >= 3.11" >&2
|
||
exit 1
|
||
fi
|
||
if ! python3 -c 'import sys; raise SystemExit(0 if sys.version_info >= (3, 11) else 1)' 2>/dev/null; then
|
||
echo "[error] 需要 Python >= 3.11(当前 $(python3 --version 2>&1)),omlx 依赖无法安装" >&2
|
||
exit 1
|
||
fi
|
||
echo "[setup] 创建 venv 并安装依赖(首次/新机器运行,见 scripts/requirements.txt)..."
|
||
python3 -m venv "$VENV"
|
||
"$VENV/bin/pip" install -r "$ROOT/scripts/requirements.txt"
|
||
elif [ ! -x "$VENV/bin/omlx" ]; then
|
||
echo "[setup] venv 存在但依赖未装全,补装 scripts/requirements.txt ..."
|
||
"$VENV/bin/pip" install -r "$ROOT/scripts/requirements.txt"
|
||
else
|
||
echo "[ok] .venv 已就绪,跳过安装"
|
||
fi
|
||
|
||
# oMLX 模型目录:chat(Qwen3.5-9B-MLX-4bit)+ embedding(bge-m3)均为实体目录,oMLX 按目录扫描注册模型
|
||
OMLX_MODEL_DIR="$ROOT/models/mlx/omlx"
|
||
|
||
if lsof -iTCP:18080 -sTCP:LISTEN >/dev/null 2>&1; then
|
||
echo "[skip] oMLX 18080 已被占用"
|
||
else
|
||
# 内存策略写在 ~/.omlx/settings.json(custom 上限 14GB),
|
||
# CLI 不加 --memory-guard,避免覆盖 settings.json 的 custom 配置
|
||
nohup "$VENV/bin/omlx" serve \
|
||
--model-dir "$OMLX_MODEL_DIR" \
|
||
--host 127.0.0.1 --port 18080 \
|
||
--paged-ssd-cache-dir "$CACHE_DIR/omlx-cache" \
|
||
--max-concurrent-requests 8 \
|
||
--log-level info >> "$LOG_DIR/mlx-chat.log" 2>&1 &
|
||
echo "[start] oMLX chat+embedding -> 127.0.0.1:18080 (pid $!)"
|
||
fi
|
||
|
||
echo "日志: $LOG_DIR/mlx-chat.log"
|