okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0fff115df
main
1import json ,sys ,re ,urllib .request ,time ,glob 2URL = "http://127.0.0.1:8085/v1/chat/completions" 3def ask (msgs ,max_tokens = 256 ): 4body = {"messages" :msgs ,"max_tokens" :max_tokens ,"temperature" :0 ,"chat_template_kwargs" :{"enable_thinking" :False }} 5t = time .time () 6r = urllib .request .urlopen (urllib .request .Request (URL ,json .dumps (body ).encode (),{"content-type" :"application/json" }),timeout = 900 ) 7d = json .load (r );d ["wall" ]= time .time ()- t 8return d 9def show (tag ,d ): 10tm = d ["timings" ];u = d ["usage" ] 11f"[ { 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 ): 13m = re .search (r"```python\n(.*?)```" ,text ,re .S );return m .group (1 )if m else text 14def run_tests (src ,tests ): 15g = {} 16try : 17exec (src ,g );exec (tests ,g );return "PASS" 18except Exception as e :return f"FAIL { e !r } " 19mode = sys .argv [1 ] 20if mode == "short" : 21for i in range (2 ): 22d = ask ([{"role" :"user" ,"content" :"Write a Python function that returns the n-th Fibonacci number" }],256 );show (f"fib run { i } " ,d ) 23d ["choices" ][0 ]["message" ]["content" ]) 24"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" )) 25d = 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 ) 26c = d ["choices" ][0 ]["message" ]["content" ];c ) 27"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" )) 28d = 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 );d ["choices" ][0 ]["message" ]["content" ],"(expected 385)" ) 29if mode == "long" : 30src = "" .join (open (f ).read ()for f in sorted (glob .glob ("<strata checkout>/tools/*.py" ))[:6 ])[:int (sys .argv [2 ])] 31msgs = [{"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." }] 32for i in range (2 ): 33d = ask (msgs ,256 );show (f"long run { i } " ,d ) 34d ["choices" ][0 ]["message" ]["content" ][:1200 ]) 35if mode == "long2" : 36fs = sorted (glob .glob ("<strata checkout>/tools/*.py" )) 37src = "" .join (open (f ).read ()for f in fs [6 :12 ])[:7000 ] 38msgs = [{"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." }] 39d = ask (msgs ,256 );show ("long2 BST" ,d ) 40d2 = ask (msgs + [{"role" :"assistant" ,"content" :d ["choices" ][0 ]["message" ]["content" ]},{"role" :"user" ,"content" :"Now add a height() method." }],256 );show ("long2 turn2" ,d2 ) 41d ["choices" ][0 ]["message" ]["content" ][:600 ])