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