diff --git a/llmhost/backends/mlx.py b/llmhost/backends/mlx.py index 1bda3e8..06de6e6 100644 --- a/llmhost/backends/mlx.py +++ b/llmhost/backends/mlx.py @@ -97,12 +97,15 @@ class Stats: self.gen_total = 0 self.prompt_total = 0 self.requests_total = 0 + self.ttft_sum = 0.0 # seconds from request received to first generated token, + self.ttft_count = 0 # streamed requests only (non-streamed ones arrive all at once) self.ready = False # set once the model has actually answered (see _readiness_probe) def snapshot(self): with self.lock: return {"running": self.running, "gen_total": self.gen_total, - "prompt_total": self.prompt_total, "requests_total": self.requests_total} + "prompt_total": self.prompt_total, "requests_total": self.requests_total, + "ttft_sum": self.ttft_sum, "ttft_count": self.ttft_count} def make_handler(inner_port, served_name, context, key, stats): @@ -152,6 +155,7 @@ def make_handler(inner_port, served_name, context, key, stats): def do_POST(self): if not self._authed(): return + self._t0 = time.monotonic() n = int(self.headers.get("Content-Length") or 0) raw = self.rfile.read(n) if n else b"" strip_usage = False @@ -196,6 +200,7 @@ def make_handler(inner_port, served_name, context, key, stats): self.end_headers() self.close_connection = True buf = b"" + self._first = False while True: chunk = resp.read1(65536) if hasattr(resp, "read1") else resp.read(4096) if not chunk: @@ -239,6 +244,7 @@ def make_handler(inner_port, served_name, context, key, stats): except ValueError: return False u = d.get("usage") + first_token = False with stats.lock: if u: # final usage chunk (stream_options.include_usage) is exact; prefer it stats.prompt_total += int(u.get("prompt_tokens") or 0) @@ -246,6 +252,11 @@ def make_handler(inner_port, served_name, context, key, stats): delta = c.get("delta") or {} if delta.get("content") or delta.get("reasoning") or delta.get("reasoning_content") or delta.get("tool_calls"): stats.gen_total += 1 # mlx streams one token per chunk + first_token = True + if first_token and not self._first and getattr(self, "_t0", None) is not None: + self._first = True + stats.ttft_sum += time.monotonic() - self._t0 + stats.ttft_count += 1 return bool(u) and not d.get("choices") return Proxy diff --git a/llmhost/web/index.html b/llmhost/web/index.html index 781340e..70f5bf5 100644 --- a/llmhost/web/index.html +++ b/llmhost/web/index.html @@ -11,7 +11,7 @@ --accent: #3b6ef5; --accent-weak: #e6edfe; --ok: #1f9d55; --ok-weak: #e3f5ea; --warn: #b7791f; --warn-weak: #fbf1dc; --bad: #d64545; --bad-weak: #fbe5e5; - --c1: #3b6ef5; --c2: #0f9d8a; --c3: #c2620f; --c4: #d13d5e; --c5: #8a5cf0; --c6: #2f9e44; + --c1: #3b6ef5; --c2: #0f9d8a; --c3: #c2620f; --c4: #d13d5e; --c5: #8a5cf0; --c6: #2f9e44; --c7: #b5179e; --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color-scheme: light; } @@ -22,7 +22,7 @@ --accent: #6d93ff; --accent-weak: #1c2640; --ok: #3ccf7f; --ok-weak: #143022; --warn: #e3a94a; --warn-weak: #33280f; --bad: #f07272; --bad-weak: #3a1818; - --c1: #6d93ff; --c2: #3fc9b4; --c3: #f0954a; --c4: #f2708c; --c5: #a98bff; --c6: #5fd27a; + --c1: #6d93ff; --c2: #3fc9b4; --c3: #f0954a; --c4: #f2708c; --c5: #a98bff; --c6: #5fd27a; --c7: #e67ad1; color-scheme: dark; } } @@ -96,7 +96,9 @@ button.danger { color: var(--bad); } .bar i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .4s; } .charts { grid-template-columns: repeat(3, minmax(0, 1fr)); margin-top: 14px; } -@media (max-width: 820px) { .charts { grid-template-columns: minmax(0, 1fr); } } +.charts.four { grid-template-columns: repeat(4, minmax(0, 1fr)); } +@media (max-width: 1100px) { .charts.four { grid-template-columns: repeat(2, minmax(0, 1fr)); } } +@media (max-width: 820px) { .charts, .charts.four { grid-template-columns: minmax(0, 1fr); } } .chart .head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; } .chart .head span { font-size: 12px; color: var(--muted); } .chart .head b { font-size: 13px; font-variant-numeric: tabular-nums; } @@ -171,6 +173,7 @@ pre {