okami.mom/strata

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

Niko1221Merge branch 'f-140' into f-140-j0fff115df

main
3.1 KiB41 linesraw
1import json,sys,re,urllib.request,time,glob
2URL="http://127.0.0.1:8085/v1/chat/completions"
3def ask(msgs,max_tokens=256):
4    body={"messages":msgs,"max_tokens":max_tokens,"temperature":0,"chat_template_kwargs":{"enable_thinking":False}}
5    t=time.time()
6    r=urllib.request.urlopen(urllib.request.Request(URL,json.dumps(body).encode(),{"content-type":"application/json"}),timeout=900)
7    d=json.load(r); d["wall"]=time.time()-t
8    return d
9def show(tag,d):
10    tm=d["timings"];u=d["usage"]
11    print(f"[{tag}] prompt_n={tm['prompt_n']} cache_n={tm['cache_n']} prompt {tm['prompt_per_second']:.1f} tok/s | decode {tm['predicted_n']} tok {tm['predicted_per_second']:.1f} tok/s | drafts {tm.get('draft_n_accepted')}/{tm.get('draft_n')} | wall {d['wall']:.1f}s | finish {d['choices'][0]['finish_reason']}")
12def code(text):
13    m=re.search(r"```python\n(.*?)```",text,re.S); return m.group(1) if m else text
14def run_tests(src,tests):
15    g={}
16    try:
17        exec(src,g); exec(tests,g); return "PASS"
18    except Exception as e: return f"FAIL {e!r}"
19mode=sys.argv[1]
20if mode=="short":
21    for i in range(2):
22        d=ask([{"role":"user","content":"Write a Python function that returns the n-th Fibonacci number"}],256); show(f"fib run{i}",d)
23    print(d["choices"][0]["message"]["content"])
24    print("fib tests:",run_tests(code(d["choices"][0]["message"]["content"]),"fn=[v for k,v in list(globals().items()) if callable(v) and not k.startswith('_') and k!='exec'][-1]\nassert [fn(i) for i in range(10)]==[0,1,1,2,3,5,8,13,21,34]; assert fn(50)==12586269025"))
25    d=ask([{"role":"user","content":"Write a Python function gcd(a, b) that returns the greatest common divisor using Euclid's algorithm. Only the code."}],200); show("gcd",d)
26    c=d["choices"][0]["message"]["content"]; print(c)
27    print("gcd tests:",run_tests(code(c),"assert gcd(48,18)==6; assert gcd(17,5)==1; assert gcd(0,9)==9; assert gcd(270,192)==6"))
28    d=ask([{"role":"user","content":"What is the output of this Python code? print(sum(i*i for i in range(1,11)))  Answer with just the number."}],20); show("known",d); print(d["choices"][0]["message"]["content"], "(expected 385)")
29if mode=="long":
30    src="".join(open(f).read() for f in sorted(glob.glob("<strata checkout>/tools/*.py"))[:6])[:int(sys.argv[2])]
31    msgs=[{"role":"user","content":"Here is some Python source:\n\n"+src+"\n\nIn one paragraph, say what this code is for, then write a short Python function that reverses a string."}]
32    for i in range(2):
33        d=ask(msgs,256); show(f"long run{i}",d)
34    print(d["choices"][0]["message"]["content"][:1200])
35if mode=="long2":
36    fs=sorted(glob.glob("<strata checkout>/tools/*.py"))
37    src="".join(open(f).read() for f in fs[6:12])[:7000]
38    msgs=[{"role":"user","content":"Here is some Python source:\n\n"+src+"\n\nWrite a Python implementation of a binary search tree class with insert, search, delete and in-order traversal methods, with docstrings."}]
39    d=ask(msgs,256); show("long2 BST",d)
40    d2=ask(msgs+[{"role":"assistant","content":d["choices"][0]["message"]["content"]},{"role":"user","content":"Now add a height() method."}],256); show("long2 turn2",d2)
41    print(d["choices"][0]["message"]["content"][:600])