#!/usr/bin/env python3 """Start SEC 10-K Analyst 2B: all six roles on one GPU (NVIDIA CUDA or Apple Metal), or on the processor. One 4-bit base model is loaded once; the nine LoRA adapters share it. Serves on localhost: 8186 planner, 8188 figure lookups, 8189 text answers, 8190 section reading, 8191 writer and calculator, 8192 label picker (8194 and 8195 are internal). SEC10K_INSTANCE=N runs another copy beside the first: every port moves up by 100 x (N - 1). SEC10K_DEVICE=cpu runs on the processor (no GPU needed, much slower); SEC10K_THREADS sets its threads. """ import argparse import os import sys import time from pathlib import Path HERE = Path(__file__).resolve().parent ROOT = HERE.parent sys.path[:0] = [str(HERE), str(HERE / "vendor"), str(ROOT / "app" / "workflow")] ENDPOINTS = 8 # 8194, 8192, 8195 and the five chat ports INSTANCE = int(os.environ.get("SEC10K_INSTANCE", "1") or 1) OFFSET = 100 * (INSTANCE - 1) CPU = os.environ.get("SEC10K_DEVICE", "").lower() == "cpu" def resolve(name, out_dir): """Write a copy of a manifest with its paths pointing into this download.""" path = out_dir / name path.write_text((HERE / name).read_text().replace("{ROOT}", ROOT.as_posix())) return path def main(): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("--chat-slots", type=int, choices=(1, 2), default=2, help="chat requests served at once (default 2, as in the published runs; 1 uses less memory)") parser.add_argument("--picker-context", type=int, choices=(4096, 8192), default=8192, help="token limit of the label picker's two-way context (default 8192, as in the published runs; " "4096 uses about 4 GB less GPU memory and still fits every picker input seen in testing)") args = parser.parse_args() run_dir = HERE / ".run" run_dir.mkdir(exist_ok=True) selector = resolve("selector_manifest.json", run_dir) chat = resolve("chat_manifest.json", run_dir) import chat_runtime import runtime import service if args.picker_context != 8192: # The two-way (non-causal) context must hold a whole picker input in one batch, and llama.cpp reserves its score # buffer for that many tokens times the 248K-word vocabulary. A smaller limit shrinks that buffer. create_context = runtime.NativeBackend.create_context def smaller_two_way_context(self, model, *, causal, n_ctx, n_ubatch, **options): if not causal: n_ctx = n_ubatch = args.picker_context return create_context(self, model, causal=causal, n_ctx=n_ctx, n_ubatch=n_ubatch, **options) runtime.NativeBackend.create_context = smaller_two_way_context class Chat(chat_runtime.SharedChatRuntime): def __init__(self, runtime, manifest, pool_size=1, context_limit=8192): super().__init__(runtime, manifest, pool_size=args.chat_slots, context_limit=context_limit) chat_runtime.SharedChatRuntime = Chat started, bound = time.time(), [] class Server(service.ThreadingHTTPServer): def __init__(self, server_address, handler, *rest, **options): host, port = server_address super().__init__((host, port + OFFSET), handler, *rest, **options) def server_bind(self): super().server_bind() bound.append(self.server_address[1]) if len(bound) == ENDPOINTS: copy = f" Copy {INSTANCE}, ports {8186 + OFFSET}-{8195 + OFFSET}." if OFFSET else "" print(f"Ready in {time.time() - started:.0f} s.{copy} In another terminal: python ask.py \"your question\"" " (Ctrl+C here to stop)", flush=True) service.ThreadingHTTPServer = Server where = f"on the processor, {os.environ.get('SEC10K_THREADS', '8')} threads" if CPU else "on the GPU" print(f"Loading the 4-bit base and its nine adapters {where} (chat slots {args.chat_slots}, " f"picker context {args.picker_context}) ...", flush=True) try: service.main(["--manifest", str(selector), "--port", "8194", "--picker-source-dir", str(HERE / "picker_source"), "--alias-port", "8192", "--chat-manifest", str(chat), "--chat-port", "8195", "--chat-aliases", "--chat-context-limit", "65536"]) except KeyboardInterrupt: print("Stopped.") if __name__ == "__main__": main()