diff --git a/config/models.json b/config/models.json new file mode 100644 index 0000000..52cb57d --- /dev/null +++ b/config/models.json @@ -0,0 +1,74 @@ +{ + "models": { + "Qwen3.5-REAP-30-IQ4": { + "profile": "reap", + "port": 8087, + "context": 240000, + "args": [ + "-hf", "mradermacher/Qwen3.5-122B-A10B-REAP-30-GGUF:IQ4_XS", + "--alias", "Qwen3.5-REAP-30-IQ4", + "-c", "240000", + "-b", "512", + "-ub", "128", + "-fa", "on", + "--host", "0.0.0.0", + "--port", "8087" + ] + }, + "Qwen3.5-35B-A3B-Q4": { + "profile": "qwen35", + "port": 8082, + "context": 240000, + "args": [ + "-hf", "lmstudio-community/Qwen3.5-35B-A3B-GGUF:Q4_K_M", + "--host", "0.0.0.0", + "--port", "8082", + "-ngl", "999", + "-c", "240000" + ] + }, + "Qwen3-Coder-30B-A3B-Q4": { + "profile": "coder", + "port": 8080, + "context": 128000, + "args": [ + "-hf", "second-state/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M", + "--alias", "Qwen3-Coder-30B-A3B-Q4", + "--host", "0.0.0.0", + "--port", "8080", + "--jinja", + "-ngl", "999", + "-c", "128000" + ] + }, + "GLM-4.7-Flash-Q4": { + "profile": "glm", + "port": 8086, + "context": 128000, + "args": [ + "-m", "/home/theroot/models/glm47/GLM-4.7-Flash-Q4_K_M.gguf", + "--alias", "GLM-4.7-Flash-Q4", + "-c", "128000", + "-b", "512", + "-ub", "128", + "-ngl", "99", + "-fa", "on", + "--host", "0.0.0.0", + "--port", "8086" + ] + }, + "Qwen3.8-27B-Q8": { + "profile": "qwen38", + "port": 8083, + "context": 240000, + "args": [ + "-m", "/home/theroot/models/qwen3.8/Qwen3.8-27B-Q8_0.gguf", + "--alias", "Qwen3.8-27B-Q8", + "-c", "240000", + "-ngl", "999", + "--host", "0.0.0.0", + "--port", "8083" + ] + } + } +} diff --git a/modelctl b/modelctl new file mode 100755 index 0000000..1858ea8 --- /dev/null +++ b/modelctl @@ -0,0 +1,261 @@ +#!/usr/bin/env python3 + +import json +import os +import signal +import subprocess +import sys +import time +import urllib.request + +CONFIG = "/usr/local/etc/modelctl/models.json" +LLAMA = "/home/theroot/llama.cpp/build/bin/llama-server" +PID_FILE = "/tmp/modelctl.pid" + + +def load_config(): + with open(CONFIG, "r", encoding="utf-8") as f: + return json.load(f) + + +def get_models(): + return load_config()["models"] + + +def get_pid(): + try: + with open(PID_FILE, "r", encoding="utf-8") as f: + return int(f.read().strip()) + except (FileNotFoundError, ValueError): + return None + + +def write_pid(pid): + with open(PID_FILE, "w", encoding="utf-8") as f: + f.write(str(pid)) + + +def clear_pid(): + try: + os.unlink(PID_FILE) + except FileNotFoundError: + pass + + +def process_alive(pid): + try: + os.kill(pid, 0) + return True + except OSError: + return False + + +def stop_server(): + pid = get_pid() + + if not pid or not process_alive(pid): + clear_pid() + return + + print(f"Останавливаю llama-server PID {pid}...") + + try: + os.kill(pid, signal.SIGTERM) + except ProcessLookupError: + clear_pid() + return + + for _ in range(30): + if not process_alive(pid): + clear_pid() + return + time.sleep(1) + + if process_alive(pid): + print("Не остановился, SIGKILL...") + try: + os.kill(pid, signal.SIGKILL) + except ProcessLookupError: + pass + + clear_pid() + + +def get_loaded_model(port): + try: + with urllib.request.urlopen( + f"http://127.0.0.1:{port}/v1/models", + timeout=3, + ) as response: + data = json.loads(response.read()) + + items = data.get("data", []) + + if items: + return items[0].get("id") + + except Exception: + pass + + return None + + +def wait_ready(port, expected): + print(f"Жду {expected} на порту {port}...") + + for _ in range(300): + if get_loaded_model(port) == expected: + print(f"READY: port {port}") + return True + + time.sleep(1) + + print("ERROR: модель не поднялась") + return False + + +def find_model(profile): + for name, info in get_models().items(): + if info.get("profile") == profile: + return name, info + + return None, None + + +def start_model(profile): + name, info = find_model(profile) + + if info is None: + print(f"Неизвестный профиль: {profile}") + return 1 + + command = [LLAMA] + info["args"] + + print(f"Запускаю {name}...") + print(" ".join(command)) + + log_dir = "/var/log/modelctl" + os.makedirs(log_dir, exist_ok=True) + + log_path = os.path.join( + log_dir, + f"{name}.log", + ) + + log_file = open( + log_path, + "a", + encoding="utf-8", + buffering=1, + ) + + process = subprocess.Popen( + command, + stdout=log_file, + stderr=subprocess.STDOUT, + start_new_session=True, + ) + + write_pid(process.pid) + + if not wait_ready(info["port"], name): + try: + process.terminate() + except ProcessLookupError: + pass + + clear_pid() + return 1 + + return 0 + + +def command_list(json_output=False): + config = load_config() + + if json_output: + print( + json.dumps( + config, + ensure_ascii=False, + indent=2, + ) + ) + return + + for name, info in config["models"].items(): + print( + f"{name} | " + f"profile={info['profile']} | " + f"port={info['port']} | " + f"context={info['context']}" + ) + + +def status(): + pid = get_pid() + + if not pid or not process_alive(pid): + clear_pid() + print("llama-server: STOPPED") + return 0 + + print(f"llama-server: RUNNING (PID {pid})") + + for name, info in get_models().items(): + loaded = get_loaded_model(info["port"]) + + if loaded: + print(f"model: {loaded}") + print(f"port: {info['port']}") + break + + return 0 + + +def main(): + if len(sys.argv) < 2: + print("Использование:") + print(" modelctl status") + print(" modelctl stop") + print(" modelctl list") + print(" modelctl list --json") + print(" modelctl load ") + return 1 + + command = sys.argv[1] + + if command == "status": + return status() + + if command == "stop": + stop_server() + print("llama-server: STOPPED") + return 0 + + if command == "list": + command_list( + len(sys.argv) > 2 and sys.argv[2] == "--json" + ) + return 0 + + if command == "load": + if len(sys.argv) != 3: + print("Использование: modelctl load ") + return 1 + + profile = sys.argv[2] + + if find_model(profile)[1] is None: + print(f"Неизвестный профиль: {profile}") + return 1 + + stop_server() + return start_model(profile) + + print(f"Неизвестная команда: {command}") + return 1 + + +if __name__ == "__main__": + sys.exit(main())