interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / bench / inference_cost / chat.om

531 lines · 9349 bytes Raw Blame History
1note bench/inference_cost/chat.om -- the JSON edge of driver.sh --run-model.
2note Compose: { printf 'om2\n'; cat seed/json/json.om seed/json/jsonw.om bench/inference_cost/chat.om; } | omc
3note
4note chat req < wire > request.json
5note wire: "version 1", "model M", "max_tokens N", "temperature L" (one JSON number
6note lexeme), "messages K", then K times "msg ROLE NB", a newline, NB content bytes and
7note a newline. ROLE is system, user or assistant. Writes one OpenAI chat-completions
8note request object. Exit 2 bad wire, 3 the JSON writer refused (invalid UTF-8 content).
9note
10note chat resp < body > meta 3> code
11note body: the endpoint's JSON response. meta: "version 1", "model ID" (the id the
12note endpoint returned, bytes outside 33..126 as _, "-" when absent), "prompt_tokens P",
13note "completion_tokens C", "total_tokens T", "code_bytes N". fd 3: the code taken from
14note choices[0].message.content: after the last "</think>", the body of the first ```
15note fence (its info line dropped), else the whole content. A null content is empty
16note code. Exit 4 protocol failure: not one JSON object, an "error" member, no usage
17note or a usage count that is not a non-negative integer, no choices[0].message, or a
18note content that is neither a string nor null. Token counts are only ever copied
19note from usage; nothing is estimated.
20text cwver "version"
21text cwone "1"
22text cwmodel "model"
23text cwmaxt "max_tokens"
24text cwtemp "temperature"
25text cwmsgs "messages"
26text cwmsg "msg"
27text cwrole "role"
28text cwcont "content"
29text cwsys "system"
30text cwuser "user"
31text cwasst "assistant"
32text cwstrm "stream"
33text cwusage "usage"
34text cwptok "prompt_tokens"
35text cwctok "completion_tokens"
36text cwttok "total_tokens"
37text cwchoic "choices"
38text cwmessg "message"
39text cwerror "error"
40text cwreq "req"
41text cwresp "resp"
42text cwthink "</think>"
43text cwfence "```"
44text cwmver "version 1\nmodel "
45text cwnl "\n"
46text cwdash "-"
47text cwcodeb "code_bytes"
48
49fn cw_out fd int b view uchar * off ulong n ulong to void
50p = ulong 0
51note every pass writes at least one byte or exits
52while lt p n bound 999999
53w = write fd b add off p sub n p
54if le w 0
55exit 5
56end
57p = add p ulong w
58end
59end
60
61fn cw_text fd int t view uchar * to void
62cw_out fd t 0 len t
63end
64
65fn cw_putu fd int v ulong to void
66d = new uchar 24 0
67i = ulong 24
68x = v
69note a ulong has at most 20 decimal digits
70for it ulong 21
71i = sub i 1
72put d i uchar add rem x 10 48
73x = div x 10
74if eq x 0
75break
76end
77end
78cw_out fd d i sub 24 i
79end
80
81note cw_line: the label, one space, the number, a newline
82fn cw_line t view uchar * v ulong to void
83sp = new uchar 1 32
84cw_text 1 t
85cw_out 1 sp 0 1
86cw_putu 1 v
87cw_text 1 cwnl
88end
89
90fn cw_slurp b edit uchar * to ulong
91p = ulong 0
92n = len b
93note every pass adds at least one byte to b or leaves the loop
94while lt p n bound 999999
95k = read 0 b p sub n p
96if lt k 0
97exit 2
98end
99if eq k 0
100ret p
101end
102p = add p ulong k
103end
104x = new uchar 1 0
105k = read 0 x 0 1
106if ne k 0
107exit 2
108end
109ret p
110end
111
112fn cw_eqa a view uchar * n ulong t view uchar * to bool
113if ne n len t
114ret false
115end
116for i ulong n
117if ne get a i get t i
118ret false
119end
120end
121ret true
122end
123
124note ---- request wire: cursor cu = pos, word start, word end, length, then saved fields
125
126fn cw_word b view uchar * cu edit ulong * to bool
127p = get cu 0
128n = get cu 3
129while lt p n bound 999999
130c = get b p
131if and ne c 32 ne c 10
132break
133end
134p = add p 1
135end
136s = p
137while lt p n bound 999999
138if le get b p 32
139break
140end
141p = add p 1
142end
143put cu 0 p
144put cu 1 s
145put cu 2 p
146ret gt p s
147end
148
149fn cw_is b view uchar * cu edit ulong * t view uchar * to bool
150s = get cu 1
151e = get cu 2
152if ne sub e s len t
153ret false
154end
155for i ulong len t
156if ne get b add s i get t i
157ret false
158end
159end
160ret true
161end
162
163fn cw_want b view uchar * cu edit ulong * t view uchar * to void
164if not cw_word b cu
165exit 2
166end
167if not cw_is b cu t
168exit 2
169end
170end
171
172fn cw_num b view uchar * cu edit ulong * to ulong
173if not cw_word b cu
174exit 2
175end
176s = get cu 1
177e = get cu 2
178if gt sub e s 19
179exit 2
180end
181v = ulong 0
182for i ulong sub e s
183c = get b add s i
184if or lt c 48 gt c 57
185exit 2
186end
187v = add mul v 10 ulong sub c 48
188end
189ret v
190end
191
192fn cw_onemsg b view uchar * cu edit ulong * ob edit uchar * to void
193cw_want b cu cwmsg
194if not cw_word b cu
195exit 2
196end
197rs = get cu 1
198rl = sub get cu 2 rs
199ok = or or cw_is b cu cwsys cw_is b cu cwuser cw_is b cu cwasst
200if not ok
201exit 2
202end
203nb = cw_num b cu
204p = get cu 2
205n = get cu 3
206if gt nb n
207exit 2
208end
209if gt add p 2 sub n nb
210exit 2
211end
212if ne get b p 10
213exit 2
214end
215c0 = add p 1
216if ne get b add c0 nb 10
217exit 2
218end
219js_wobj ob
220js_wsbuf ob cwrole 0 len cwrole
221js_wsbuf ob b rs rl
222js_wsbuf ob cwcont 0 len cwcont
223js_wsbuf ob b c0 nb
224js_wobje ob
225put cu 0 add c0 add nb 1
226end
227
228fn cw_req b view uchar * n ulong ob edit uchar * to long
229cu = new ulong 16 0
230put cu 3 n
231cw_want b cu cwver
232cw_want b cu cwone
233cw_want b cu cwmodel
234if not cw_word b cu
235exit 2
236end
237put cu 4 get cu 1
238put cu 5 get cu 2
239cw_want b cu cwmaxt
240put cu 6 cw_num b cu
241cw_want b cu cwtemp
242if not cw_word b cu
243exit 2
244end
245put cu 7 get cu 1
246put cu 8 get cu 2
247cw_want b cu cwmsgs
248k = cw_num b cu
249if or eq k 0 gt k 1000
250exit 2
251end
252r = js_winit ob
253if lt r 0
254exit 3
255end
256js_wobj ob
257js_wsbuf ob cwmodel 0 len cwmodel
258js_wsbuf ob b get cu 4 sub get cu 5 get cu 4
259js_wsbuf ob cwmsgs 0 len cwmsgs
260js_warr ob
261for i ulong k
262cw_onemsg b cu ob
263end
264js_warre ob
265js_wsbuf ob cwmaxt 0 len cwmaxt
266js_wulong ob get cu 6
267js_wsbuf ob cwtemp 0 len cwtemp
268js_wnumb ob b get cu 7 sub get cu 8 get cu 7
269js_wsbuf ob cwstrm 0 len cwstrm
270js_wbool ob false
271js_wobje ob
272if cw_word b cu
273exit 2
274end
275ret js_wdone ob
276end
277
278note ---- response
279
280note cw_keq: the decoded key at s[off] equals key number kid (two unsized arrays plus
281note scalars is the most a signature takes here: three arrays and a scalar is error 9)
282fn cw_keq s view uchar * off ulong kid ulong to bool
283if eq kid 0
284ret js_streq s off cwmodel
285end
286if eq kid 1
287ret js_streq s off cwusage
288end
289if eq kid 2
290ret js_streq s off cwptok
291end
292if eq kid 3
293ret js_streq s off cwctok
294end
295if eq kid 4
296ret js_streq s off cwttok
297end
298if eq kid 5
299ret js_streq s off cwchoic
300end
301if eq kid 6
302ret js_streq s off cwmessg
303end
304if eq kid 7
305ret js_streq s off cwcont
306end
307ret js_streq s off cwerror
308end
309
310note cw_mem: the value token of the first member kid of object o, else jsnone
311fn cw_mem s view uchar * t view uint * o ulong kid ulong to ulong
312if ne js_type t o jsobj
313ret jsnone
314end
315m = js_first t o
316while ne m jsnone
317if cw_keq s js_off t m kid
318ret add m 1
319end
320m = js_mnext t m
321end
322ret jsnone
323end
324
325fn cw_count s view uchar * t view uint * u ulong kid ulong to ulong
326v = cw_mem s t u kid
327if eq v jsnone
328exit 4
329end
330if ne js_type t v jsnum
331exit 4
332end
333o = js_off t v
334if not js_isu s o
335exit 4
336end
337ret js_tou s o
338end
339
340fn cw_model s view uchar * t view uint * to void
341mb = new uchar 256 0
342cw_text 1 cwmver
343v = cw_mem s t 0 0
344if eq v jsnone
345cw_text 1 cwdash
346cw_text 1 cwnl
347ret
348end
349if ne js_type t v jsstr
350cw_text 1 cwdash
351cw_text 1 cwnl
352ret
353end
354n = js_sget s js_off t v mb
355if le n 0
356cw_text 1 cwdash
357cw_text 1 cwnl
358ret
359end
360for i ulong ulong n
361c = get mb i
362if or le c 32 ge c 127
363put mb i 95
364end
365end
366cw_out 1 mb 0 ulong n
367cw_text 1 cwnl
368end
369
370fn cw_usage s view uchar * t view uint * to void
371u = cw_mem s t 0 1
372if eq u jsnone
373exit 4
374end
375if ne js_type t u jsobj
376exit 4
377end
378p = cw_count s t u 2
379c = cw_count s t u 3
380a = cw_count s t u 4
381cw_line cwptok p
382cw_line cwctok c
383cw_line cwttok a
384end
385
386fn cw_content s view uchar * t view uint * ob edit uchar * to ulong
387ch = cw_mem s t 0 5
388if eq ch jsnone
389exit 4
390end
391if ne js_type t ch jsarr
392exit 4
393end
394e = js_first t ch
395if eq e jsnone
396exit 4
397end
398m = cw_mem s t e 6
399if eq m jsnone
400exit 4
401end
402if ne js_type t m jsobj
403exit 4
404end
405c = cw_mem s t m 7
406if eq c jsnone
407ret 0
408end
409ty = js_type t c
410if eq ty jsnull
411ret 0
412end
413if ne ty jsstr
414exit 4
415end
416n = js_sget s js_off t c ob
417if lt n 0
418exit 4
419end
420ret ulong n
421end
422
423fn cw_find c view uchar * n ulong st ulong pat view uchar * to ulong
424m = len pat
425if gt m n
426ret jsnone
427end
428i = st
429while le add i m n bound 999999
430hit = true
431for j ulong m
432if ne get c add i j get pat j
433hit = false
434break
435end
436end
437if hit
438ret i
439end
440i = add i 1
441end
442ret jsnone
443end
444
445fn cw_code c view uchar * n ulong to void
446st = ulong 0
447i = ulong 0
448note each pass moves past one "</think>"; content is at most 2 MiB
449while lt i n bound 999999
450f = cw_find c n i cwthink
451if eq f jsnone
452break
453end
454st = add f len cwthink
455i = st
456end
457a = cw_find c n st cwfence
458if eq a jsnone
459cw_line cwcodeb sub n st
460cw_out 3 c st sub n st
461ret
462end
463p = add a len cwfence
464while lt p n bound 999999
465if eq get c p 10
466break
467end
468p = add p 1
469end
470if lt p n
471p = add p 1
472end
473e = cw_find c n p cwfence
474if eq e jsnone
475e = n
476end
477cw_line cwcodeb sub e p
478cw_out 3 c p sub e p
479end
480
481fn cw_resp s view uchar * tape edit uint * ob edit uchar * to int
482if ne js_type tape 0 jsobj
483exit 4
484end
485if ne cw_mem s tape 0 8 jsnone
486exit 4
487end
488c = cw_content s tape ob
489cw_model s tape
490cw_usage s tape
491cw_code ob c
492ret 0
493end
494
495fn main to int
496ib = new uchar 983040 0
497ob = new uchar 2098192 0
498tape = new uint 65536 0
499ab = new uchar 16 0
500if ne argcnt 2
501ret 2
502end
503an = argread 1 ab 0 16
504if lt an 0
505ret 2
506end
507n = cw_slurp ib
508if cw_eqa ab ulong an cwreq
509r = cw_req ib n ob
510if lt r 0
511ret 3
512end
513cw_out 1 ob jswh ulong r
514ret 0
515end
516if cw_eqa ab ulong an cwresp
517if eq n 0
518exit 4
519end
520r = js_init tape n 64
521if lt r 0
522exit 4
523end
524r = js_parse ib tape n
525if lt r 0
526exit 4
527end
528ret cw_resp ib tape ob
529end
530ret 2
531end