okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221merge f-145-int 194c26881 (model-card sampling defaults, web sampling, answer room, thinking budget) into f-split-int07fe758ac

main
3.3 KiB77 linesraw
1"""serve/test_web_sampling.py - the web page sends no sampling of its own until the user changes it (0.1.45, #2685).
2
3The page used to send temperature 0.6 / top_p 0.95 / top_k 20 with every request, so the server's model-card defaults
4(thinking 1.0, not thinking 0.7 + presence penalty) never applied to it.  Pins, with node, the page's own functions:
5untouched (the shown defaults, or the old stored 0.6) sends nothing, a changed value is sent whole, 0 is greedy.
6Skipped without node.
7
8    python -m unittest serve.test_web_sampling -v
9"""
10from __future__ import annotations
11
12import json
13import re
14import shutil
15import subprocess
16import unittest
17from pathlib import Path
18
19APP = Path(__file__).resolve().parent / "web" / "app.js"
20
21
22def slice_between(src: str, start: str, end: str) -> str:
23    a = src.index(start)
24    return src[a:src.index(end, a)]
25
26
27@unittest.skipUnless(shutil.which("node"), "node is not installed")
28class WebSampling(unittest.TestCase):
29    @classmethod
30    def setUpClass(cls):
31        src = APP.read_text(encoding="utf-8")
32        cls.defaults = slice_between(src, "const DEFAULTS", "let settings")
33        cls.shared = slice_between(src, "function sharedDefaults", "async function saveShared")
34        cls.body = slice_between(src, "  if (!(settings.temperature > 0)) {", "  if (settings.seed)")
35
36    def run_js(self, settings: dict, pieces: str):
37        js = (self.defaults + "function projectionLoaded() { return false; }\n" + self.shared +
38              f"\nconst settings = {json.dumps(settings)};\nconst body = {{}};\n{pieces}\n"
39              "console.log(JSON.stringify({body, shared: sharedDefaults(settings)}));")
40        out = subprocess.run(["node", "-e", js], capture_output=True, text=True, timeout=30)
41        self.assertEqual(out.returncode, 0, out.stderr)
42        return json.loads(out.stdout)
43
44    def sampling(self, **kw):
45        s = {"thinking": "high", "temperature": 1.0, "top_p": 0.95, "top_k": 20, "max": "", "seed": "", "esp": True}
46        s.update(kw)
47        r = self.run_js(s, self.body)
48        return r["body"], r["shared"]
49
50    def test_untouched_sends_nothing(self):
51        for t in (1.0, 0.6):                       # the shown default, and the old stored default
52            body, shared = self.sampling(temperature=t)
53            self.assertEqual(body, {})
54            self.assertEqual(shared, {"reasoning_effort": "high"})
55
56    def test_changed_values_are_sent_whole(self):
57        body, shared = self.sampling(temperature=0.3)
58        self.assertEqual(body, {"temperature": 0.3, "top_p": 0.95, "top_k": 20})
59        self.assertEqual(shared["temperature"], 0.3)
60        body, _ = self.sampling(top_k=40)
61        self.assertEqual(body, {"temperature": 1.0, "top_p": 0.95, "top_k": 40})
62        body, _ = self.sampling(temperature=0.6, top_p=0.9)
63        self.assertEqual(body["temperature"], 0.6)
64
65    def test_zero_is_greedy(self):
66        body, shared = self.sampling(temperature=0)
67        self.assertEqual(body, {"temperature": 0})
68        self.assertEqual(shared["temperature"], 0)
69
70    def test_the_old_default_is_shown_as_the_new_one(self):
71        src = APP.read_text(encoding="utf-8")
72        self.assertRegex(src, r"if \(samplingUntouched\(settings\)\) settings\.temperature = DEFAULTS\.temperature;")
73        self.assertIsNone(re.search(r"temperature: 0\.6", src))
74
75
76if __name__ == "__main__":
77    unittest.main()