okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
07fe758ac
main
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 8python -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 : 23a = src .index (start ) 24return src [a :src .index (end ,a )] 25 26 27@ unittest . skipUnless ( shutil . which ( "node" ), "node is not installed" ) 28class WebSampling (unittest .TestCase ): 29@classmethod 30def setUpClass (cls ): 31src = APP .read_text (encoding = "utf-8" ) 32cls .defaults = slice_between (src ,"const DEFAULTS" ,"let settings" ) 33cls .shared = slice_between (src ,"function sharedDefaults" ,"async function saveShared" ) 34cls .body = slice_between (src ," if (!(settings.temperature > 0)) {" ," if (settings.seed)" ) 35 36def run_js (self ,settings :dict ,pieces :str ): 37js = (self .defaults + "function projectionLoaded() { return false; }\n" + self .shared + 38f"\nconst settings = { json . dumps ( settings ) } ;\nconst body = {{}};\n { pieces } \n" 39"console.log(JSON.stringify({body, shared: sharedDefaults(settings)}));" ) 40out = subprocess .run (["node" ,"-e" ,js ],capture_output = True ,text = True ,timeout = 30 ) 41self .assertEqual (out .returncode ,0 ,out .stderr ) 42return json .loads (out .stdout ) 43 44def sampling (self ,** kw ): 45s = {"thinking" :"high" ,"temperature" :1.0 ,"top_p" :0.95 ,"top_k" :20 ,"max" :"" ,"seed" :"" ,"esp" :True } 46s .update (kw ) 47r = self .run_js (s ,self .body ) 48return r ["body" ],r ["shared" ] 49 50def test_untouched_sends_nothing (self ): 51for t in (1.0 ,0.6 ):# the shown default, and the old stored default 52body ,shared = self .sampling (temperature = t ) 53self .assertEqual (body , {}) 54self .assertEqual (shared , {"reasoning_effort" :"high" }) 55 56def test_changed_values_are_sent_whole (self ): 57body ,shared = self .sampling (temperature = 0.3 ) 58self .assertEqual (body , {"temperature" :0.3 ,"top_p" :0.95 ,"top_k" :20 }) 59self .assertEqual (shared ["temperature" ],0.3 ) 60body ,_ = self .sampling (top_k = 40 ) 61self .assertEqual (body , {"temperature" :1.0 ,"top_p" :0.95 ,"top_k" :40 }) 62body ,_ = self .sampling (temperature = 0.6 ,top_p = 0.9 ) 63self .assertEqual (body ["temperature" ],0.6 ) 64 65def test_zero_is_greedy (self ): 66body ,shared = self .sampling (temperature = 0 ) 67self .assertEqual (body , {"temperature" :0 }) 68self .assertEqual (shared ["temperature" ],0 ) 69 70def test_the_old_default_is_shown_as_the_new_one (self ): 71src = APP .read_text (encoding = "utf-8" ) 72self .assertRegex (src ,r"if \(samplingUntouched\(settings\)\) settings\.temperature = DEFAULTS\.temperature;" ) 73self .assertIsNone (re .search (r"temperature: 0\.6" ,src )) 74 75 76if __name__ == "__main__" : 77unittest .main ()