diff --git a/serve/server.py b/serve/server.py index d874ab8b..3e5755d2 100644 --- a/serve/server.py +++ b/serve/server.py @@ -10,6 +10,7 @@ data, http(s) URLs or local file paths) go through `strata-vision` (the model's embeddings (`GENI`). JPEG/PNG/BMP/GIF go straight in; WebP, TIFF, AVIF, ... (agents like omp send WebP) are converted to PNG first with Pillow. Requests whose prompt plus max tokens exceed the engine's context are REJECTED with 400, never truncated. +An unset (or 0, or -1) max tokens means "unlimited": whatever the prompt leaves of the context. The engine boundary is `Engine.generate(prompt_ids, max_new, sampling, cancel) -> iterator of token ids`. `StrataEngine` keeps one `strata --serve` process resident (weights, expert arena and VRAM tier load once) and @@ -42,6 +43,7 @@ from serve.frontend import (ChatTemplate, Event, OutputParser, anthropic_to_mess IM_END = "<|im_end|>" IMAGE_PAD = "<|image_pad|>" +CTX_SLACK = 8 # `strata --serve` rejects prompt + max_new + 8 > context: keep the same margin here # ------------------------------------------------------------------------------------------------ engines @@ -321,7 +323,9 @@ class Service: self.stop_ids = set(tokenizer.encode(IM_END, parse_special=True) + tokenizer.encode("<|endoftext|>", parse_special=True)) - def prepare(self, messages, tools, kwargs, max_new): + def prepare(self, messages, tools, kwargs, max_new=None): + """-> (ids, thinking, max_new). An unset or non-positive max_new (some clients send -1) means "unlimited": + the rest of the context.""" prompt = self.template.render(messages, tools=tools, **kwargs) ids = self.tok.encode(prompt, parse_special=True) self.embeddings.path = None @@ -352,10 +356,16 @@ class Service: for path, _ in encoded: f.write(path.read_bytes()) self.embeddings.path = combined - if len(ids) + max_new > self.engine.max_context: + room = self.engine.max_context - CTX_SLACK - len(ids) + if max_new is None or max_new <= 0: + if room < 1: + raise ValueError(f"prompt ({len(ids)} tokens) leaves no room to answer in the context " + f"({self.engine.max_context}); requests are never truncated") + max_new = room + elif max_new > room: raise ValueError(f"prompt ({len(ids)} tokens) + max tokens ({max_new}) exceeds the context " f"({self.engine.max_context}); requests are never truncated") - return ids, kwargs.get("enable_thinking", True) is not False + return ids, kwargs.get("enable_thinking", True) is not False, max_new def _note(self, n, evs): with self.status_lock: @@ -703,10 +713,8 @@ def make_handler(svc: Service): def _openai(self, req): messages, tools, kw = openai_to_messages(req) - max_new = int(req.get("max_completion_tokens") or req.get("max_tokens") or 1024) - if max_new <= 0: # a non-positive budget (some clients send -1) means "unset" - max_new = 1024 - ids, thinking = svc.prepare(messages, tools, kw, max_new) + max_new = int(req.get("max_completion_tokens") or req.get("max_tokens") or 0) # 0/-1: the rest of the context + ids, thinking, max_new = svc.prepare(messages, tools, kw, max_new) _debug_req("openai", req, messages, tools, max_new, thinking, len(ids)) cancel = threading.Event() chunks = openai_chunks(svc, req, ids, thinking, tools, max_new, cancel) @@ -727,10 +735,8 @@ def make_handler(svc: Service): def _anthropic(self, req): messages, tools, kw = anthropic_to_messages(req) - max_new = int(req.get("max_tokens") or 1024) - if max_new <= 0: # a non-positive budget (some clients send -1) means "unset" - max_new = 1024 - ids, thinking = svc.prepare(messages, tools, kw, max_new) + max_new = int(req.get("max_tokens") or 0) # 0/-1: the rest of the context + ids, thinking, max_new = svc.prepare(messages, tools, kw, max_new) _debug_req("anthropic", req, messages, tools, max_new, thinking, len(ids)) cancel = threading.Event() events = anthropic_events(svc, req, ids, thinking, tools, max_new, cancel) diff --git a/serve/test_server.py b/serve/test_server.py new file mode 100644 index 00000000..937e3bd5 --- /dev/null +++ b/serve/test_server.py @@ -0,0 +1,130 @@ +"""serve/test_server.py - the max tokens budget over both APIs, against the mock engine (no GPU, no pack). + + python -m unittest serve.test_server -v +""" +from __future__ import annotations + +import json +import os +import sys +import unittest +import urllib.error +import urllib.request +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +from serve.frontend import ChatTemplate # noqa: E402 +from serve.server import CTX_SLACK, ByteTokenizer, MockEngine, Service, serve # noqa: E402 + +ROOT = Path(__file__).resolve().parents[1] +CTX = 4096 +ANSWER = "x" * 2000 # longer than the old 1024 fallback: one token per byte + + +class RecordingEngine(MockEngine): + def generate(self, ids, max_new, sampling, cancel, embeddings=None): + self.last_max_new = max_new + yield from super().generate(ids, max_new, sampling, cancel, embeddings) + + +class MaxTokens(unittest.TestCase): + @classmethod + def setUpClass(cls): + tok = ByteTokenizer() + cls.engine = RecordingEngine(tok, "\n\n" + ANSWER, max_context=CTX) + cls.svc = Service(cls.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) + cls.httpd = serve(cls.svc, port=0) + cls.base = f"http://127.0.0.1:{cls.httpd.server_address[1]}" + + @classmethod + def tearDownClass(cls): + cls.httpd.shutdown() + cls.httpd.server_close() + + def post(self, path, body): + req = urllib.request.Request(self.base + path, data=json.dumps(body).encode(), + headers={"Content-Type": "application/json"}) + try: + with urllib.request.urlopen(req, timeout=30) as r: + return r.status, json.loads(r.read()) + except urllib.error.HTTPError as e: + with e: + return e.code, json.loads(e.read()) + + def call(self, api, text="hi", **budget): + """-> (status, body, prompt tokens, completion tokens); `budget` is merged into the request as given.""" + msgs = [{"role": "user", "content": text}] + if api == "openai": + s, b = self.post("/v1/chat/completions", {"model": "m", "messages": msgs, **budget}) + u = b.get("usage", {}) + return s, b, u.get("prompt_tokens"), u.get("completion_tokens") + s, b = self.post("/v1/messages", {"model": "m", "messages": msgs, **budget}) + u = b.get("usage", {}) + return s, b, u.get("input_tokens"), u.get("output_tokens") + + def test_unset_budget_is_the_rest_of_the_context(self): + cases = {"openai": [{"max_tokens": -1}, {"max_tokens": 0}, {}, {"max_tokens": None}, + {"max_completion_tokens": -1}, {"max_completion_tokens": None, "max_tokens": None}], + "anthropic": [{"max_tokens": -1}, {"max_tokens": 0}, {}, {"max_tokens": None}]} + for api, budgets in cases.items(): + for budget in budgets: + with self.subTest(api=api, budget=budget): + s, b, pt, ct = self.call(api, **budget) + self.assertEqual(s, 200, b) + self.assertEqual(self.engine.last_max_new, CTX - CTX_SLACK - pt) + self.assertGreater(ct, 1024) # the whole answer, not cut at the old 1024 fallback + + def test_explicit_budget_is_honoured(self): + for api, budget in [("openai", {"max_tokens": 50}), ("openai", {"max_completion_tokens": 50}), + ("openai", {"max_completion_tokens": 50, "max_tokens": 9}), + ("anthropic", {"max_tokens": 50}), ("openai", {"max_tokens": 1500}), + ("anthropic", {"max_tokens": 1500})]: + with self.subTest(api=api, budget=budget): + want = budget.get("max_completion_tokens") or budget["max_tokens"] + s, b, _, ct = self.call(api, **budget) + self.assertEqual(s, 200, b) + self.assertEqual(self.engine.last_max_new, want) + self.assertEqual(ct, want) + + def test_explicit_budget_over_the_context_is_rejected(self): + for api in ("openai", "anthropic"): + with self.subTest(api=api): + s, b, _, _ = self.call(api, max_tokens=CTX) + self.assertEqual(s, 400) + self.assertIn("exceeds the context", b["error"]["message"]) + + def test_unset_budget_with_a_near_full_prompt(self): + _, _, pt0, _ = self.call("openai", max_tokens=1) + overhead = pt0 - len("hi") # the template's tokens around the user text + for api in ("openai", "anthropic"): + _, _, pa, _ = self.call(api, max_tokens=1) + over = pa - pt0 # the Anthropic template may differ slightly + with self.subTest(api=api, room=5): # a few tokens left: the budget is exactly those + text = "y" * (CTX - CTX_SLACK - overhead - over - 5) + s, b, pt, ct = self.call(api, text=text, max_tokens=-1) + self.assertEqual(s, 200, b) + self.assertEqual(self.engine.last_max_new, 5) + self.assertEqual(ct, 5) + with self.subTest(api=api, room=0): # nothing left: rejected, not truncated + text = "y" * (CTX - CTX_SLACK - overhead - over) + s, b, _, _ = self.call(api, text=text) + self.assertEqual(s, 400, b) + self.assertIn("no room to answer", b["error"]["message"]) + + def test_debug_log_shows_the_resolved_budget(self): + import contextlib + import io + os.environ["STRATA_DEBUG"] = "1" + try: + for api in ("openai", "anthropic"): + with self.subTest(api=api): + out = io.StringIO() + with contextlib.redirect_stdout(out): + _, _, pt, _ = self.call(api, max_tokens=-1) + self.assertIn(f"max_new={CTX - CTX_SLACK - pt} ", out.getvalue()) + finally: + del os.environ["STRATA_DEBUG"] + + +if __name__ == "__main__": + unittest.main()