Skip to content

Fast local LLM inference on plain CPUs. No GPU, 8 GB of RAM. OpenAI-compatible server.

fix(start): honor bind addresses and API-only mode when opening chat (#66)

Afloat04554d8a9d43
diff --git a/start b/start
index eb9fc41..17fd350 100755
--- a/start
+++ b/start
@@ -7,14 +7,23 @@
set -euo pipefail
HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
PREFIX=${OPENSET_LLM_HOME:-$HOME/.openset-llm}
-PORT=8080; args=("$@")
-for ((i = 0; i < ${#args[@]}; i++)); do [ "${args[i]}" = --port ] && PORT=${args[i+1]:-8080}; done
+HOST=127.0.0.1; PORT=8080; WEBUI=1; args=("$@")
+for ((i = 0; i < ${#args[@]}; i++)); do
+ case ${args[i]} in
+ --host) HOST=${args[i+1]:-127.0.0.1} ;;
+ --port) PORT=${args[i+1]:-8080} ;;
+ --no-webui) WEBUI=0 ;;
+ esac
+done
case "${1:-}" in -h|--help) sed -n '2,6p' "$0"; exit 0 ;; esac
if ! { [ -x "$PREFIX/bin/llama-server" ] && [ -f "$PREFIX/models/model.gguf" ]; }; then
echo "First run: installing (this downloads the model and can take a while)..."
bash "$HERE/install.sh"
fi
-URL=http://127.0.0.1:$PORT
+SHOW=$HOST
+case $HOST in 0.0.0.0) SHOW=127.0.0.1 ;; ::) SHOW=::1 ;; esac
+case $SHOW in *:*) SHOW=[$SHOW] ;; esac
+URL=http://$SHOW:$PORT
open_page() { # Linux desktop, WSL, or just print the address
if command -v xdg-open >/dev/null 2>&1 && [ -n "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ]; then xdg-open "$URL" >/dev/null 2>&1 && return
elif command -v wslview >/dev/null 2>&1; then wslview "$URL" >/dev/null 2>&1 && return
@@ -22,6 +31,7 @@ open_page() { # Linux desktop, WSL, or just print the address
fi
echo "Open $URL in your browser."
}
+if [ "$WEBUI" = 1 ]; then
( ( sleep 1
for _ in $(seq 1 600); do
kill -0 $$ 2>/dev/null || exit 0 # the server (this very process after the exec below) has gone: say nothing
@@ -29,5 +39,6 @@ open_page() { # Linux desktop, WSL, or just print the address
sleep 1
done
echo "Still loading after 600 s. Open $URL once the server says it is listening." ) & ) # detached: no zombie under the server
+fi
# exec: the server takes over this process, so Ctrl+C, kill and closing the terminal all reach it and none leaves it behind
exec bash "$HERE/serve" "$@"
diff --git a/tests/test_start_options.py b/tests/test_start_options.py
new file mode 100644
index 0000000..92be241
--- /dev/null
+++ b/tests/test_start_options.py
@@ -0,0 +1,54 @@
+import json
+import os
+from pathlib import Path
+import shutil
+import subprocess
+import tempfile
+import unittest
+
+ROOT = Path(__file__).resolve().parents[1]
+
+
+class StartBindOptions(unittest.TestCase):
+ def run_start(self, *options):
+ with tempfile.TemporaryDirectory() as tmp:
+ root = Path(tmp)
+ app, tools, prefix = root / 'app', root / 'tools', root / 'prefix'
+ for p in (app, tools, prefix / 'bin', prefix / 'models'):
+ p.mkdir(parents=True, exist_ok=True)
+ shutil.copyfile(ROOT / 'start', app / 'start')
+ def executable(p, content):
+ p.write_text(content)
+ p.chmod(0o755)
+ for name in ('dirname', 'bash', 'seq'):
+ (tools / name).symlink_to(shutil.which(name))
+ executable(prefix / 'bin/llama-server', '#!/bin/sh\nexit 0\n')
+ (prefix / 'models/model.gguf').touch()
+ executable(app / 'serve', '#!/bin/sh\n/bin/sleep 0.2\n')
+ executable(tools / 'sleep', '#!/bin/sh\nexit 0\n')
+ executable(tools / 'curl', '#!/bin/sh\nprintf "%s\\n" "$@" >> "$PROBE_LOG"\nexit 0\n')
+ env = {**os.environ, 'OPENSET_LLM_HOME': str(prefix), 'PATH': str(tools),
+ 'PROBE_LOG': str(root / 'probes'), 'DISPLAY': '', 'WAYLAND_DISPLAY': ''}
+ result = subprocess.run(['bash', str(app / 'start'), *options], env=env,
+ text=True, capture_output=True, timeout=5)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ log = root / 'probes'
+ return log.read_text() if log.exists() else '', result.stdout
+
+ def test_explicit_loopback_bind_is_used_for_readiness(self):
+ log, output = self.run_start('--host', '127.0.0.2', '--port', '9000')
+ self.assertIn('http://127.0.0.2:9000/health', log)
+ self.assertIn('Open http://127.0.0.2:9000', output)
+
+ def test_ipv6_wildcard_uses_bracketed_loopback(self):
+ log, _ = self.run_start('--host', '::', '--port', '9000')
+ self.assertIn('http://[::1]:9000/health', log)
+
+ def test_api_only_mode_does_not_open_or_probe_the_chat_page(self):
+ log, output = self.run_start('--no-webui')
+ self.assertEqual(log, '')
+ self.assertNotIn('Ready:', output)
+
+
+if __name__ == '__main__':
+ unittest.main()