Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/train/omega-llm/native/serve.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / train / omega-llm / native / serve.om` 2904 lines ยท 64947 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-only 2 note omegallm serve.om (aien-train DRA-119): `ollm serve PORT CK MERGES [MAXCONN] [tplmax=N] 3 note [test=1]`, an OpenAI-compatible HTTP/1.1 server for a v1 BPE checkpoint (V > 256) on 4 note 127.0.0.1:PORT only. aarch64 only: build.sh composes it under one `om2` line after the 5 note omega-mv `e5b32567` units net/http.om, net/json.om and net/jsonw.om (vendored unmodified); the 6 note other targets take noserve.om. It prints `PORT n`, then serves one request at a time, one 7 note request per connection (Connection: close), with no connection limit; MAXCONN (1 or more, 8 note for tests) makes it leave after that many connections. 9 note A connection is served once its first bytes arrive; a silent one waits in one 10 note of 8 pending slots and is closed unanswered 10 s after its accept (svwait). tplmax=N 11 note (256 .. 983040, default 65536) caps the chat template in bytes; test=1 enables the test-only 12 note members ollm_test_output and ollm_test_delay_ms (refused otherwise). 13 note Routes: GET /health {"status":"ok","in_flight":0,"busy":false}; GET /v1/models; 14 note POST /v1/chat/completions with JSON or SSE (stream true) responses; other paths 404, other 15 note methods 405. in_flight counts the generating chat completions (0 or 1, one slot); busy is 16 note in_flight 1. While a completion generates, the listener and pending connections are served 17 note between tokens: GET /health answers {"status":"ok","in_flight":1,"busy":true}, GET 18 note /v1/models the list, and every other request 503 server_busy with Retry-After: 1. A client 19 note that leaves (svgone) stops its generation: nothing more is sent, logged as status 499. 20 note Request: messages (non-empty array of objects with a string role of at most 32 bytes and a 21 note content that is a string, null, or an array of {"type":"text","text":string} parts joined 22 note with no separator; other part types are 400; content may be absent beside tool_calls), 23 note optional max_tokens or max_completion_tokens (1 .. 999999), temperature (0 .. 2 without 24 note exponent, used to 0.001; default 1), seed (unsigned; absent or null: a fresh seed for each 25 note request, svseed), top_k (integer -1 .. 26 note 999999; -1, 0 and K >= V are off), top_p (0 <= p <= 1 without exponent, used to 0.000001; 27 note 0 keeps only the most probable token, 1 is off), stop (null, a string or an array of at most 28 note 8 strings of 1 .. 128 bytes), tools (array of at most 128 29 note {"type":"function","function":{"name":1 .. 64 bytes, ...}}), tool_choice ("none" or "auto"; 30 note "required" and a {"type":"function"} object are 400: nothing here can force a call), stream, 31 note stream_options.include_usage and n (only 1). Members that would change the output and are 32 note not implemented are 400 unless they carry their default (svscal3): response_format other 33 note than {"type":"text"}, a non-empty logit_bias, logprobs true, top_logprobs, frequency_penalty 34 note or presence_penalty other than 0, parallel_tool_calls false, functions and function_call. 35 note Other members (model, user, ...) are ignored. 36 note Interim chat template (until DRA-123 fixes one shared template): when tools is non-empty, 37 note `tools: ` and the request's tools JSON text as sent, then `\n`; then `role: content\n` for 38 note each message in order, an assistant's tool_calls appended to its content as 39 note `{"name":NAME,"arguments":ARGS}` (NAME the JSON string as sent, ARGS 40 note the decoded string or the object's JSON text as sent, {} when absent); then `assistant: `. 41 note At most tplmax bytes, else 400. The context is token 0 then the template's encoding (CT0 42 note tokens, reported as prompt_tokens); CT0 >= T is refused with 400 context_length_exceeded 43 note stating CT0 and T. Output runs at most min(max_tokens, T - CT0) tokens, so the T-token window 44 note never slides (cmdsamp's step osstep, splitmix64 seed stream; with top_k or top_p on, the 45 note kept tokens are the most probable in order, ties to the lower id, up to K and until their 46 note mass reaches top_p, drawn by the same uniform scaled to the kept mass); a generated token 0 47 note ends it with finish_reason stop, the limit with length, the earliest stop string with stop 48 note (the content cut before it; a streamed chunk never sends a byte a stop could still claim). 49 note Tool calls (tools non-empty, tool_choice not "none"): when the content from the first 50 note `` on is 1 .. 8 blocks `{"name":N,"arguments":A}` with only 51 note whitespace between and after, N a declared tool name and A an object, a string or absent, 52 note the answer carries tool_calls (id call__, arguments the object's JSON text or the 53 note string) with finish_reason tool_calls and content the text before, trailing whitespace 54 note dropped, null when empty; otherwise the text is plain content. Streaming holds back a 55 note possible marker and trailing whitespace, then sends the tool_calls in one delta chunk. 56 note Decoded bytes are sent as UTF-8: a sequence cut by a token boundary waits for the next token, 57 note an ill-formed one (or one cut by the end) becomes U+FFFD. One line per routed response (and 58 note per 413 at the head, and per request answered during a generation) goes to stdout: `C status 59 note prompt_tokens completion_tokens finish(0 none, 1 stop, 2 length, 3 tool_calls, 4 client left) 60 note ms`. A body over 65532 JSON tokens is 413 request_too_large. 61 note Exit codes: 2 usage; 27 merges (bpe.om); 71 the port cannot be bound. 62 note wb layout: raw token bytes [0, 8192), content [8192, 73728), stop strings [73728, 74752) (8 x 63 note 128), markers [74752, 74784), message [81920, 82176), response id [82176, 82240), call id 64 note [82240, 82432), tool names [82432, 90624) (128 x 64), template [90624, `1073664`). bd holds the 65 note request, then the tool calls' names and arguments [0, 131072). sv slots: 0 stream, 1 max 66 note tokens, 2 TEMPMILLI, 3 seed state, 4 include_usage, 5 template bytes, 6 CT0, 7 generated, 8 67 note finish, 9 request count, 10 created, 11 start ns, 12 raw bytes, 13 content bytes, 14 content 68 note sent, 15 id length, 16 error code, 17 chunk end, 19 T, 21 last status, 22 test mode, 23 69 note tplmax, 24 forced tokens, 25 forced on, 26 test output bytes, 27 delay ms, 28 top_k, 29 top_p 70 note millionths, 30 tools, 31 tool detection, 32 tool calls, 33 marker position, 34 bd fill, 71 note 35 stop strings, 36 listen fd. 72 note Model id (svmid): read from the loaded checkpoint's header, so it names this model and this 73 note checkpoint: `ollm-ck0M-vV-tT-dD-hH-lL-stepS-fnvX`, M the checkpoint magic (1 OLLMCK01, 74 note 2 OLLMCK02), V T D H L its shapes, S its step field and X the FNV-1a 64 of its payload as the 75 note header records it (ockload has already refused a payload that does not hash to it, exit 14). 76 note The same id is `/v1/models` data[0].id (also while a completion generates) and the `model` 77 note of every completion and chunk; it is kept in the global svmi, its length in svml, and 78 note printed as `MODEL id` before `PORT n`. 79 note 80 note Mutants (tests/omegallm/build.sh --mutant NAME sets one constant to 0; tests/omegallm/ 81 note serve-mutants.sh requires the serve gate to fail on each): OMUTMETH answers a known path's 82 note other methods instead of 405, OMUTPATH answers other paths as /health instead of 404, OMUTBIG 83 note drops the 413 (no head bound, an over-long body is a plain 400), OMUTJSON serves malformed JSON 84 note instead of 400, OMUTLOOP binds 0.0.0.0 instead of 127.0.0.1, OMUTSEED shifts the request seed 85 note by one (parity with `sample`), OMUTMID reports the old fixed id `ollm`. 86 const OMUTMETH ulong 1 87 const OMUTPATH ulong 1 88 const OMUTBIG ulong 1 89 const OMUTJSON ulong 1 90 const OMUTLOOP ulong 1 91 const OMUTSEED ulong 1 92 const OMUTMID ulong 1 93 94 text svtjson "application/json" 95 text svtctyp "Content-Type" 96 text svphlth "/health" 97 text svpmods "/v1/models" 98 text svpchat "/v1/chat/completions" 99 text svpcomp "/v1/completions" 100 text skprompt "prompt" 101 text svtcomp "text_completion" 102 text svcidp "cmpl-ollm-" 103 text svmget "GET" 104 text svmpost "POST" 105 text svthlth "{\"status\":\"ok\",\"in_flight\":0,\"busy\":false}" 106 text svthbsy "{\"status\":\"ok\",\"in_flight\":1,\"busy\":true}" 107 text svtlist "list" 108 text skdata "data" 109 text skowned "owned_by" 110 text svaien "aien" 111 text svt503 "{\"error\":{\"message\":\"a chat completion is in flight and this server runs one at a time: retry after 1 s\",\"type\":\"server_error\",\"param\":null,\"code\":\"server_busy\"}}" 112 text svtra "Retry-After" 113 text svone "1" 114 text svport "PORT " 115 text svmodl "MODEL " 116 text svmid1 "ollm-ck0" 117 text svmidv "-v" 118 text svmidt "-t" 119 text svmidd "-d" 120 text svmidh "-h" 121 text svmidl "-l" 122 text svmids "-step" 123 text svmidf "-fnv" 124 text skmsgs "messages" 125 text skrole "role" 126 text skcont "content" 127 text skstrm "stream" 128 text skmaxt "max_tokens" 129 text skmaxc "max_completion_tokens" 130 text sktemp "temperature" 131 text skseed "seed" 132 text skopts "stream_options" 133 text skincl "include_usage" 134 text skn "n" 135 text skid "id" 136 text skobj "object" 137 text skcrt "created" 138 text skmodel "model" 139 text skchoi "choices" 140 text skidx "index" 141 text skmsg "message" 142 text skdelta "delta" 143 text skfin "finish_reason" 144 text skusage "usage" 145 text skpt "prompt_tokens" 146 text skct "completion_tokens" 147 text sktt "total_tokens" 148 text skerr "error" 149 text sktype "type" 150 text skcode "code" 151 text skparam "param" 152 text sktext "text" 153 text sktopk "top_k" 154 text sktopp "top_p" 155 text sktools "tools" 156 text sktchoi "tool_choice" 157 text sktcall "tool_calls" 158 text skfunc "function" 159 text sknam "name" 160 text skargs "arguments" 161 text sktout "ollm_test_output" 162 text sktdel "ollm_test_delay_ms" 163 text sknone "none" 164 text svauto "auto" 165 text skrfmt "response_format" 166 text sklbias "logit_bias" 167 text sklogp "logprobs" 168 text sktlogp "top_logprobs" 169 text skfpen "frequency_penalty" 170 text skppen "presence_penalty" 171 text skptc "parallel_tool_calls" 172 text skfuncs "functions" 173 text skfcall "function_call" 174 text svollm "ollm" 175 text svasst "assistant" 176 text svcc "chat.completion" 177 text svccc "chat.completion.chunk" 178 text svstop "stop" 179 text svlen "length" 180 text svinv "invalid_request_error" 181 text svsrv "server_error" 182 text svdone "[DONE]" 183 text svidp "chatcmpl-ollm-" 184 text svcallp "call_" 185 text svus "_" 186 text svsep ": " 187 text svnl "\n" 188 text svaprm "assistant: " 189 text svtlp "tools: " 190 text svtco "" 191 text svtcc "" 192 text svtcn "{\"name\":" 193 text svtca ",\"arguments\":" 194 text svtcz "}" 195 text svempt "{}" 196 text svoptt "tplmax=" 197 text svopte "test=1" 198 text secjson "invalid_json" 199 text secreq "invalid_request" 200 text secctx "context_length_exceeded" 201 text secbig "request_too_large" 202 text secnf "not_found" 203 text secmeth "method_not_allowed" 204 text secint "internal_error" 205 text sem1 "request body is not valid JSON (RFC 8259)" 206 text sem2 "the request must be a JSON object" 207 text sem3 "messages must be a non-empty array of objects" 208 text sem4 "message content must be a string, null or an array of text parts (absent only beside tool_calls)" 209 text sem5 "message role must be a string of at most 32 bytes" 210 text sem6a "the chat template is over " 211 text sem6b " bytes (the serve option tplmax=N sets this cap)" 212 text sem7 "temperature must be a number from 0 to 2 without exponent" 213 text sem8 "max_tokens must be an integer from 1 to 999999" 214 text sem9 "seed must be an unsigned integer" 215 text sem10 "stream must be true or false" 216 text sem12 "only n = 1 is supported" 217 text sem13 "the request body is over 983040 bytes" 218 text sem14 "no such path; routes: GET /health, GET /v1/models, POST /v1/chat/completions, POST /v1/completions" 219 text sem15 "method not allowed on this path" 220 text sem16 "response construction failed" 221 text sem17 "message content parts must be {\"type\":\"text\",\"text\":string}; image_url, input_audio, file and other part types are not supported" 222 text sem18 "stop must be null, a string or an array of at most 8 strings of 1 to 128 bytes" 223 text sem19 "top_k must be an integer from -1 to 999999 (-1 and 0 turn it off)" 224 text sem20 "top_p must be a number from 0 to 1 without exponent (resolution 0.000001; 0 keeps only the most probable token)" 225 text sem21 "tools must be an array of at most 128 {\"type\":\"function\",\"function\":{\"name\":string of 1 to 64 bytes}} objects" 226 text sem22 "tool_choice must be null, \"none\" or \"auto\"; \"required\" and a {\"type\":\"function\"} object are refused because this server cannot force a tool call" 227 text sem23 "tool_calls must be an array of {\"function\":{\"name\":string,\"arguments\":string or object}} objects" 228 text sem24 "ollm_test_output (a string of at most 16384 bytes) and ollm_test_delay_ms (0 to 10000) need a server started with test=1" 229 text sem25 "the request body has over 65532 JSON tokens (values, keys and closers), the JSON parser's limit" 230 text sem26 "prompt must be a non-empty string" 231 text sem27 "stream is not supported on /v1/completions: send stream false or leave it out" 232 text sem28 "messages, tools and tool_choice belong to /v1/chat/completions; /v1/completions takes prompt" 233 text sem29 "not supported here: response_format other than text, logit_bias, logprobs, top_logprobs, frequency_penalty, presence_penalty, parallel_tool_calls false, functions, function_call; leave them out or send their defaults" 234 text sem11a "prompt is " 235 text sem11b " tokens including the start token; the context window is " 236 text sem11c " tokens, so no room is left for output" 237 238 note forced tokens (test=1), top_k/top_p marks, stop lengths, tool name lengths, tool call spans 239 global svft ulong 4096 240 global svmk uchar 4096 241 global svstl ulong 8 242 global svtnl ulong 128 243 global svtcx ulong 32 244 note pending connections (svwait): descriptor + 1 (0 free), accept time, record slot; pollfd records; 245 note the svgone peek byte 246 global svpfd ulong 8 247 global svpat ulong 8 248 global svpix ulong 8 249 global svpl uchar 72 250 global svpk uchar 1 251 global svmi uchar 192 252 global svml ulong 1 253 254 note ======== small byte helpers over wb 255 256 note svmcat: copy text t into wb at p, at most up to e; the next p 257 fn svmcat wb edit uchar `1073664` p ulong e ulong t view uchar * to ulong 258 q = p 259 for i ulong len t 260 if ge q e 261 ret q 262 end 263 put wb q get t i 264 q = add q 1 265 end 266 ret q 267 end 268 269 note svmdec: v in decimal into wb at p (at most 20 digits, up to e); the next p 270 fn svmdec wb edit uchar `1073664` p ulong e ulong v ulong to ulong 271 d = new uchar 20 0 272 i = ulong 20 273 x = v 274 for it ulong 20 275 i = sub i 1 276 put d i uchar add rem x 10 48 277 x = div x 10 278 if eq x 0 279 break 280 end 281 end 282 q = p 283 for k ulong sub 20 i 284 if ge q e 285 ret q 286 end 287 put wb q get d add i k 288 q = add q 1 289 end 290 ret q 291 end 292 293 note svat: whether the content at p starts with the pattern wb[po, po + pl) (p + pl <= content) 294 fn svat wb edit uchar `1073664` p ulong po ulong pl ulong to bool 295 for i ulong pl 296 if ne get wb add 8192 add p i get wb add po i 297 ret false 298 end 299 end 300 ret true 301 end 302 303 note svfind: the first position >= s where the pattern wb[po, po + pl) lies wholly in the content 304 note [0, n), or jsnone 305 fn svfind wb edit uchar `1073664` s ulong n ulong po ulong pl ulong to ulong 306 p = s 307 while le add p pl n bound 65537 308 if svat wb p po pl 309 ret p 310 end 311 p = add p 1 312 end 313 ret jsnone 314 end 315 316 note svpsuf: the longest k < pl, k <= n - lo, with content [n - k, n) a prefix of the pattern 317 fn svpsuf wb edit uchar `1073664` lo ulong n ulong po ulong pl ulong to ulong 318 k = sub pl 1 319 if lt sub n lo k 320 k = sub n lo 321 end 322 m = k 323 for it ulong m 324 if svat wb sub n k po k 325 ret k 326 end 327 k = sub k 1 328 end 329 ret 0 330 end 331 332 fn svisws c ulong to bool 333 ret or or eq c 32 eq c 9 or eq c 10 eq c 13 334 end 335 336 note svtrim: h moved back over trailing whitespace of the content, not below lo 337 fn svtrim wb edit uchar `1073664` lo ulong h ulong to ulong 338 if le h lo 339 ret h 340 end 341 q = h 342 for it ulong sub h lo 343 if not svisws ulong get wb add 8191 q 344 break 345 end 346 q = sub q 1 347 end 348 ret q 349 end 350 351 note svskws: p moved over whitespace of the content, not past n 352 fn svskws wb edit uchar `1073664` p ulong n ulong to ulong 353 q = p 354 for it ulong sub n p 355 if not svisws ulong get wb add 8192 q 356 break 357 end 358 q = add q 1 359 end 360 ret q 361 end 362 363 note ======== UTF-8 (RFC 3629 section 4) 364 365 note svu8n: the sequence at a[p], with a[0 .. n) the bytes so far: 1 .. 4 its length when well 366 note formed; 0 when a[p .. n) is a proper prefix of a well-formed sequence; 8 + k when its maximal 367 note ill-formed subpart is k bytes (replaced by one U+FFFD) 368 fn svu8n a view uchar * p ulong n ulong to ulong 369 c = ulong get a p 370 if lt c 128 371 ret 1 372 end 373 k = svu8k c 374 if eq k 0 375 ret 9 376 end 377 for i ulong sub k 1 378 q = add p add i 1 379 if ge q n 380 ret 0 381 end 382 if not svu8ok c i ulong get a q 383 ret add 9 i 384 end 385 end 386 ret k 387 end 388 389 note svu8k: the sequence length a lead byte c >= 128 starts, 0 when it starts none 390 fn svu8k c ulong to ulong 391 if and ge c 194 le c 223 392 ret 2 393 end 394 if and ge c 224 le c 239 395 ret 3 396 end 397 if and ge c 240 le c 244 398 ret 4 399 end 400 ret 0 401 end 402 403 note svu8ok: whether b may be continuation byte i (0 the first) after lead byte c (RFC 3629 table) 404 fn svu8ok c ulong i ulong b ulong to bool 405 lo = ulong 128 406 hi = ulong 191 407 if eq i 0 408 if eq c 224 409 lo = 160 410 end 411 if eq c 237 412 hi = 159 413 end 414 if eq c 240 415 lo = 144 416 end 417 if eq c 244 418 hi = 143 419 end 420 end 421 ret and ge b lo le b hi 422 end 423 424 note svrepl: U+FFFD (EF BF BD) into the content at its end 425 fn svrepl wb edit uchar `1073664` sv edit ulong 64 to void 426 g = add 8192 get sv 13 427 put wb g 239 428 put wb add g 1 191 429 put wb add g 2 189 430 put sv 13 add get sv 13 3 431 end 432 433 note svclean: move the complete sequences of the raw bytes wb[0, sv12) to the content; fin 1 also 434 note replaces a cut tail. The rest moves to the front of the raw bytes. 435 fn svclean wb edit uchar `1073664` sv edit ulong 64 fin ulong to void 436 rn = get sv 12 437 p = ulong 0 438 for it ulong add rn 1 439 if ge p rn 440 break 441 end 442 r = svu8n wb p rn 443 if eq r 0 444 if eq fin 0 445 break 446 end 447 svrepl wb sv 448 p = rn 449 else 450 if gt r 8 451 svrepl wb sv 452 p = add p sub r 8 453 else 454 svmove wb add 8192 get sv 13 p r 455 put sv 13 add get sv 13 r 456 p = add p r 457 end 458 end 459 end 460 svmove wb 0 p sub rn p 461 put sv 12 sub rn p 462 end 463 464 note svmove: wb[d, d + n) = wb[s, s + n), copying forward (d <= s or the ranges apart) 465 fn svmove wb edit uchar `1073664` d ulong s ulong n ulong to void 466 for i ulong n 467 put wb add d i get wb add s i 468 end 469 end 470 471 note ======== responses 472 473 fn svstat cs edit ulong 320 io edit uchar 131072 code ulong t view uchar * to long 474 r = hrspst cs io code 475 r = hfe r hhname cs io svtctyp 476 r = hfe r hhval cs io svtjson 477 r = hfe r hhend cs io 0 len t 478 r = hfe r hput cs io t 479 ret hfe r hwfin cs io 480 end 481 482 note svs503: the fixed server_busy error with Retry-After: 1 483 fn svs503 cs edit ulong 320 io edit uchar 131072 to long 484 r = hrspst cs io 503 485 r = hfe r hhname cs io svtctyp 486 r = hfe r hhval cs io svtjson 487 r = hfe r hhname cs io svtra 488 r = hfe r hhval cs io svone 489 r = hfe r hhend cs io 0 len svt503 490 r = hfe r hput cs io svt503 491 ret hfe r hwfin cs io 492 end 493 494 note svsend: the writer's text ob[1040, 1040 + n) as a JSON response with status code 495 fn svsend cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 code ulong n ulong to long 496 r = hrspst cs io code 497 r = hfe r hhname cs io svtctyp 498 r = hfe r hhval cs io svtjson 499 r = hfe r hhend cs io 0 n 500 r = hfe r hput cs io view part ob 1040 n 501 ret hfe r hwfin cs io 502 end 503 504 fn svwt ob edit uchar 409600 t view uchar * to void 505 js_wsbuf ob t 0 len t 506 end 507 508 note svemsg: the message of error code sv16 into wb[81920 ..); its length 509 fn svemsg wb edit uchar `1073664` sv edit ulong 64 to ulong 510 e = get sv 16 511 b = ulong 81920 512 z = ulong 82176 513 p = b 514 if eq e 1 515 p = svmcat wb p z sem1 516 end 517 if eq e 2 518 p = svmcat wb p z sem2 519 end 520 if eq e 3 521 p = svmcat wb p z sem3 522 end 523 if eq e 4 524 p = svmcat wb p z sem4 525 end 526 if eq e 5 527 p = svmcat wb p z sem5 528 end 529 if eq e 6 530 p = svmcat wb p z sem6a 531 p = svmdec wb p z get sv 23 532 p = svmcat wb p z sem6b 533 end 534 if eq e 7 535 p = svmcat wb p z sem7 536 end 537 if eq e 8 538 p = svmcat wb p z sem8 539 end 540 if eq e 9 541 p = svmcat wb p z sem9 542 end 543 if eq e 10 544 p = svmcat wb p z sem10 545 end 546 if eq e 11 547 p = svmcat wb p z sem11a 548 p = svmdec wb p z get sv 6 549 p = svmcat wb p z sem11b 550 p = svmdec wb p z get sv 19 551 p = svmcat wb p z sem11c 552 end 553 if eq e 12 554 p = svmcat wb p z sem12 555 end 556 if eq e 13 557 p = svmcat wb p z sem13 558 end 559 if eq e 14 560 p = svmcat wb p z sem14 561 end 562 if eq e 15 563 p = svmcat wb p z sem15 564 end 565 if eq e 16 566 p = svmcat wb p z sem16 567 end 568 ret sub svemsg2 wb e p z b 569 end 570 571 note svemsg2: the messages of error codes 17 .. 25 at p; the end 572 fn svemsg2 wb edit uchar `1073664` e ulong p ulong z ulong to ulong 573 if eq e 17 574 ret svmcat wb p z sem17 575 end 576 if eq e 18 577 ret svmcat wb p z sem18 578 end 579 if eq e 19 580 ret svmcat wb p z sem19 581 end 582 if eq e 20 583 ret svmcat wb p z sem20 584 end 585 if eq e 21 586 ret svmcat wb p z sem21 587 end 588 if eq e 22 589 ret svmcat wb p z sem22 590 end 591 if eq e 23 592 ret svmcat wb p z sem23 593 end 594 if eq e 24 595 ret svmcat wb p z sem24 596 end 597 if eq e 25 598 ret svmcat wb p z sem25 599 end 600 if eq e 26 601 ret svmcat wb p z sem26 602 end 603 if eq e 27 604 ret svmcat wb p z sem27 605 end 606 if eq e 28 607 ret svmcat wb p z sem28 608 end 609 if eq e 29 610 ret svmcat wb p z sem29 611 end 612 ret p 613 end 614 615 fn svecode ob edit uchar 409600 e ulong to void 616 if eq e 1 617 svwt ob secjson 618 ret 619 end 620 if eq e 11 621 svwt ob secctx 622 ret 623 end 624 if or eq e 13 eq e 25 625 svwt ob secbig 626 ret 627 end 628 if eq e 14 629 svwt ob secnf 630 ret 631 end 632 if eq e 15 633 svwt ob secmeth 634 ret 635 end 636 if eq e 16 637 svwt ob secint 638 ret 639 end 640 svwt ob secreq 641 end 642 643 note svfail: {"error":{"message","type","param":null,"code"}} for error code sv16 with status st 644 fn svfail cs edit ulong 320 io edit uchar 131072 wb edit uchar `1073664` sv edit ulong 64 st ulong to long 645 ob = new uchar 409600 0 646 put sv 21 st 647 m = svemsg wb sv 648 x = js_winit ob 649 js_wobj ob 650 svwt ob skerr 651 js_wobj ob 652 svwt ob skmsg 653 js_wsbuf ob wb 81920 m 654 svwt ob sktype 655 if eq st 500 656 svwt ob svsrv 657 else 658 svwt ob svinv 659 end 660 svwt ob skparam 661 js_wnull ob 662 svwt ob skcode 663 svecode ob get sv 16 664 js_wobje ob 665 js_wobje ob 666 n = js_wdone ob 667 if lt n 0 668 put cs HKEEP 0 669 ret hrsperr cs io 500 670 end 671 ret svsend cs io ob st ulong n 672 end 673 674 note svhead: {"id", "object" t, "created", "model" opened; the caller adds the rest 675 fn svhead ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 t view uchar * to void 676 x = js_winit ob 677 js_wobj ob 678 svwt ob skid 679 js_wsbuf ob wb 82176 get sv 15 680 svwt ob skobj 681 svwt ob t 682 svwt ob skcrt 683 js_wulong ob get sv 10 684 svwt ob skmodel 685 svmidw ob 686 end 687 688 note svmidw: the model id as a JSON string value (the checkpoint id; OMUTMID 0: the fixed `ollm`) 689 fn svmidw ob edit uchar 409600 to void 690 if eq OMUTMID 0 691 svwt ob svollm 692 else 693 js_wsbeg ob 694 for i ulong get svml 0 695 js_wsb ob get svmi i 696 end 697 js_wsend ob 698 end 699 end 700 701 note svicat: copy text t into svmi at p, at most up to e; the next p 702 fn svicat p ulong e ulong t view uchar * to ulong 703 q = p 704 for i ulong len t 705 if ge q e 706 ret q 707 end 708 put svmi q get t i 709 q = add q 1 710 end 711 ret q 712 end 713 714 note svidec: v in decimal into svmi at p (at most 20 digits, up to e); the next p 715 fn svidec p ulong e ulong v ulong to ulong 716 d = new uchar 20 0 717 i = ulong 20 718 x = v 719 for it ulong 20 720 i = sub i 1 721 put d i uchar add rem x 10 48 722 x = div x 10 723 if eq x 0 724 break 725 end 726 end 727 q = p 728 for it ulong sub 20 i 729 if ge q e 730 ret q 731 end 732 put svmi q get d add i it 733 q = add q 1 734 end 735 ret q 736 end 737 738 note svihex: v as 16 lowercase hex digits into svmi at p, at most up to e; the next p 739 fn svihex p ulong e ulong v ulong to ulong 740 q = p 741 for i ulong 16 742 if ge q e 743 ret q 744 end 745 d = and shr v mul sub 15 i 4 15 746 c = add d 48 747 if gt d 9 748 c = add d 87 749 end 750 put svmi q uchar c 751 q = add q 1 752 end 753 ret q 754 end 755 756 note svmid: the model id of the checkpoint loaded from argv[3] (ockload has run and checked it) 757 note into svmi, its length into svml, then `MODEL id` on stdout. The header is read again for 758 note its payload FNV-1a field, which ockload keeps only while it loads. 759 fn svmid to void 760 hl = ulong 112 761 if eq get og 46 2 762 hl = 120 763 end 764 if eq get og 46 3 765 hl = 144 766 end 767 hn = oprd 3 0 0 144 768 if lt hn hl 769 exit 10 770 end 771 e = ulong 192 772 p = svicat 0 e svmid1 773 p = svidec p e get og 46 774 p = svicat p e svmidv 775 p = svidec p e get og 0 776 p = svicat p e svmidt 777 p = svidec p e get og 1 778 p = svicat p e svmidd 779 p = svidec p e get og 2 780 p = svicat p e svmidh 781 p = svidec p e get og 3 782 p = svicat p e svmidl 783 p = svidec p e get og 5 784 p = svicat p e svmids 785 p = svidec p e get og 41 786 p = svicat p e svmidf 787 p = svihex p e opw8 sub hl 8 788 put svml 0 p 789 for i ulong len svmodl 790 oputc ulong get svmodl i 791 end 792 for i ulong p 793 oputc ulong get svmi i 794 end 795 oputc 10 796 end 797 798 note svmodw: the /v1/models answer, the one model this server answers with (not logged) 799 fn svmodw cs edit ulong 320 io edit uchar 131072 to long 800 ob = new uchar 409600 0 801 x = js_winit ob 802 js_wobj ob 803 svwt ob skobj 804 svwt ob svtlist 805 svwt ob skdata 806 js_warr ob 807 js_wobj ob 808 svwt ob skid 809 svmidw ob 810 svwt ob skobj 811 svwt ob skmodel 812 svwt ob skcrt 813 js_wulong ob 0 814 svwt ob skowned 815 svwt ob svaien 816 js_wobje ob 817 js_warre ob 818 js_wobje ob 819 m = js_wdone ob 820 if lt m 0 821 put cs HKEEP 0 822 ret hrsperr cs io 500 823 end 824 ret svsend cs io ob 200 ulong m 825 end 826 827 note svmods: GET /v1/models, logged 828 fn svmods cs edit ulong 320 io edit uchar 131072 sv edit ulong 64 to long 829 put sv 21 200 830 r = svmodw cs io 831 svlog sv 832 ret r 833 end 834 835 note svbig: the status for a body over the bound, 413 (OMUTBIG 0: 400, the 413 refusal removed) 836 fn svbig to ulong 837 if eq OMUTBIG 0 838 ret 400 839 end 840 ret 413 841 end 842 843 fn svfr ob edit uchar 409600 sv edit ulong 64 to void 844 f = get sv 8 845 if eq f 1 846 svwt ob svstop 847 ret 848 end 849 if eq f 2 850 svwt ob svlen 851 ret 852 end 853 if eq f 3 854 svwt ob sktcall 855 ret 856 end 857 js_wnull ob 858 end 859 860 fn svusage ob edit uchar 409600 sv edit ulong 64 to void 861 svwt ob skusage 862 js_wobj ob 863 svwt ob skpt 864 js_wulong ob get sv 6 865 svwt ob skct 866 js_wulong ob get sv 7 867 svwt ob sktt 868 js_wulong ob add get sv 6 get sv 7 869 js_wobje ob 870 end 871 872 note svwcalls: the tool_calls array (idx 1 adds each call's index, as a stream delta does) 873 fn svwcalls ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 idx ulong to void 874 js_warr ob 875 for i ulong get sv 32 876 b = mul i 4 877 js_wobj ob 878 if eq idx 1 879 svwt ob skidx 880 js_wulong ob i 881 end 882 svwt ob skid 883 p = svmcat wb 82240 82432 svcallp 884 p = svmdec wb p 82432 get sv 9 885 p = svmcat wb p 82432 svus 886 p = svmdec wb p 82432 i 887 js_wsbuf ob wb 82240 sub p 82240 888 svwt ob sktype 889 svwt ob skfunc 890 svwt ob skfunc 891 js_wobj ob 892 svwt ob sknam 893 js_wsbuf ob bd get svtcx b get svtcx add b 1 894 svwt ob skargs 895 js_wsbuf ob bd get svtcx add b 2 get svtcx add b 3 896 js_wobje ob 897 js_wobje ob 898 end 899 js_warre ob 900 end 901 902 note svcfull: the text_completion object of /v1/completions 903 fn svcfull ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 to long 904 svhead ob wb sv svtcomp 905 svwt ob skchoi 906 js_warr ob 907 js_wobj ob 908 svwt ob skidx 909 js_wulong ob 0 910 svwt ob sktext 911 js_wsbuf ob wb 8192 get sv 13 912 svwt ob skfin 913 svfr ob sv 914 js_wobje ob 915 js_warre ob 916 svusage ob sv 917 js_wobje ob 918 ret js_wdone ob 919 end 920 921 note svfull: the chat.completion object (stream false), or svcfull on /v1/completions 922 fn svfull ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 to long 923 if eq get sv 37 1 924 ret svcfull ob wb sv 925 end 926 svhead ob wb sv svcc 927 svwt ob skchoi 928 js_warr ob 929 js_wobj ob 930 svwt ob skidx 931 js_wulong ob 0 932 svwt ob skmsg 933 js_wobj ob 934 svwt ob skrole 935 svwt ob svasst 936 svwt ob skcont 937 if gt get sv 32 0 938 h = svtrim wb 0 get sv 33 939 if eq h 0 940 js_wnull ob 941 else 942 js_wsbuf ob wb 8192 h 943 end 944 svwt ob sktcall 945 svwcalls ob wb sv bd 0 946 else 947 js_wsbuf ob wb 8192 get sv 13 948 end 949 js_wobje ob 950 svwt ob skfin 951 svfr ob sv 952 js_wobje ob 953 js_warre ob 954 svusage ob sv 955 js_wobje ob 956 ret js_wdone ob 957 end 958 959 note svchunk: one chat.completion.chunk; kind 0 the role, 1 the content wb[8192 + sv14, 8192 + sv17), 960 note 2 the finish reason, 3 usage (empty choices). Sends it as one SSE event. 961 fn svchunk cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 kind ulong to long 962 svhead ob wb sv svccc 963 svwt ob skchoi 964 js_warr ob 965 if ne kind 3 966 js_wobj ob 967 svwt ob skidx 968 js_wulong ob 0 969 svwt ob skdelta 970 js_wobj ob 971 if eq kind 0 972 svwt ob skrole 973 svwt ob svasst 974 svwt ob skcont 975 note "" as zero bytes of a non-empty text: under omc `e5b32567` an empty text passed as a view has a 976 note wrong length (omega-mv#775, fixed after this pin), and js_wsbuf then refuses it (jswearg) 977 js_wsbuf ob svasst 0 0 978 end 979 if eq kind 1 980 svwt ob skcont 981 g0 = get sv 14 982 js_wsbuf ob wb add 8192 g0 sub get sv 17 g0 983 end 984 js_wobje ob 985 svwt ob skfin 986 if eq kind 2 987 svfr ob sv 988 else 989 js_wnull ob 990 end 991 js_wobje ob 992 end 993 js_warre ob 994 if eq kind 3 995 svusage ob sv 996 end 997 js_wobje ob 998 ret svsse cs io ob js_wdone ob 999 end 1000 1001 note svsse: the writer's text ob[1040, 1040 + n) as one SSE event; -5 when n < 0 1002 fn svsse cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 n long to long 1003 if lt n 0 1004 ret -5 1005 end 1006 r = hssedat cs io view part ob 1040 ulong n 1007 ret hfe r hsseend cs io 1008 end 1009 1010 note svtchunk: the tool_calls delta chunk (stream true) 1011 fn svtchunk cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 to long 1012 svhead ob wb sv svccc 1013 svwt ob skchoi 1014 js_warr ob 1015 js_wobj ob 1016 svwt ob skidx 1017 js_wulong ob 0 1018 svwt ob skdelta 1019 js_wobj ob 1020 svwt ob sktcall 1021 svwcalls ob wb sv bd 1 1022 js_wobje ob 1023 svwt ob skfin 1024 js_wnull ob 1025 js_wobje ob 1026 js_warre ob 1027 js_wobje ob 1028 ret svsse cs io ob js_wdone ob 1029 end 1030 1031 note ======== request parsing 1032 1033 note svtemp: the temperature token's lexeme at bd[o, o + n) as TEMPMILLI (digits, optional 1034 note fraction, used to 0.001), or 999999 when it is not a number from 0 to 2 1035 fn svtemp bd edit uchar 983040 o ulong n ulong to ulong 1036 ip = ulong 0 1037 fr = ulong 0 1038 fd = ulong 0 1039 dot = false 1040 for i ulong n 1041 c = ulong get bd add o i 1042 if eq c 46 1043 if dot 1044 ret 999999 1045 end 1046 dot = true 1047 else 1048 if or lt c 48 gt c 57 1049 ret 999999 1050 end 1051 if dot 1052 if lt fd 3 1053 fr = add mul fr 10 sub c 48 1054 fd = add fd 1 1055 end 1056 else 1057 if ge ip 100 1058 ret 999999 1059 end 1060 ip = add mul ip 10 sub c 48 1061 end 1062 end 1063 end 1064 for k ulong 3 1065 if lt fd 3 1066 fr = mul fr 10 1067 fd = add fd 1 1068 end 1069 end 1070 m = add mul ip 1000 fr 1071 if gt m 2000 1072 ret 999999 1073 end 1074 ret m 1075 end 1076 1077 note svtopp: the top_p token's lexeme at bd[o, o + n) in millionths (used to 0.000001), or jsnone 1078 note when it is not a number from 0 to 1 1079 fn svtopp bd edit uchar 983040 o ulong n ulong to ulong 1080 ip = ulong 0 1081 fr = ulong 0 1082 fd = ulong 0 1083 dot = false 1084 for i ulong n 1085 c = ulong get bd add o i 1086 if eq c 46 1087 if dot 1088 ret jsnone 1089 end 1090 dot = true 1091 else 1092 if or lt c 48 gt c 57 1093 ret jsnone 1094 end 1095 if dot 1096 if lt fd 6 1097 fr = add mul fr 10 sub c 48 1098 fd = add fd 1 1099 end 1100 else 1101 if ge ip 10 1102 ret jsnone 1103 end 1104 ip = add mul ip 10 sub c 48 1105 end 1106 end 1107 end 1108 for k ulong 6 1109 if lt fd 6 1110 fr = mul fr 10 1111 fd = add fd 1 1112 end 1113 end 1114 m = add mul ip `1000000` fr 1115 if gt m `1000000` 1116 ret jsnone 1117 end 1118 ret m 1119 end 1120 1121 note svmem: the value token of member key in object o, or jsnone (the first when repeated) 1122 fn svmem src view uchar * tp view uint 262144 o ulong key view uchar * to ulong 1123 if ne js_type tp o 1 1124 ret jsnone 1125 end 1126 k = js_first tp o 1127 for it ulong js_nkids tp o 1128 if eq k jsnone 1129 break 1130 end 1131 if js_streq src js_off tp k key 1132 ret add k 1 1133 end 1134 k = js_mnext tp k 1135 end 1136 ret jsnone 1137 end 1138 1139 note svpcat: append text t to the template; false when it would pass tplmax (sv23) 1140 fn svpcat wb edit uchar `1073664` sv edit ulong 64 t view uchar * to bool 1141 pl = get sv 5 1142 if gt add pl len t get sv 23 1143 ret false 1144 end 1145 for i ulong len t 1146 put wb add 90624 add pl i get t i 1147 end 1148 put sv 5 add pl len t 1149 ret true 1150 end 1151 1152 note svpstr: append the decoded string token t to the template 1153 fn svpstr bd edit uchar 983040 tp edit uint 262144 t ulong wb edit uchar `1073664` sv edit ulong 64 to bool 1154 pl = get sv 5 1155 cap = get sv 23 1156 if ge pl cap 1157 ret false 1158 end 1159 n = js_sget bd js_off tp t part wb add 90624 pl sub cap pl 1160 if lt n 0 1161 ret false 1162 end 1163 put sv 5 add pl ulong n 1164 ret true 1165 end 1166 1167 note svpraw: append token t's JSON text as sent (a container's whole span) to the template 1168 fn svpraw bd edit uchar 983040 tp edit uint 262144 t ulong wb edit uchar `1073664` sv edit ulong 64 to bool 1169 ret svpcopy bd js_off tp t js_span tp t wb sv 1170 end 1171 1172 note svpcopy: append bd[o, o + n) to the template; false when it would pass tplmax (sv23) 1173 fn svpcopy bd edit uchar 983040 o ulong n ulong wb edit uchar `1073664` sv edit ulong 64 to bool 1174 d = add 90624 get sv 5 1175 if gt add get sv 5 n get sv 23 1176 ret false 1177 end 1178 for i ulong n 1179 put wb add d i get bd add o i 1180 end 1181 put sv 5 add get sv 5 n 1182 ret true 1183 end 1184 1185 note svparts: a content array of text parts into the template, joined with no separator 1186 fn svparts bd edit uchar 983040 tp edit uint 262144 a ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1187 e = js_first tp a 1188 for it ulong js_nkids tp a 1189 if eq e jsnone 1190 break 1191 end 1192 ty = svmem bd tp e sktype 1193 tx = svmem bd tp e sktext 1194 if or eq ty jsnone eq tx jsnone 1195 ret 17 1196 end 1197 if or ne js_type tp ty 6 ne js_type tp tx 6 1198 ret 17 1199 end 1200 if not js_streq bd js_off tp ty sktext 1201 ret 17 1202 end 1203 if not svpstr bd tp tx wb sv 1204 ret 6 1205 end 1206 e = js_enext tp e 1207 end 1208 ret 0 1209 end 1210 1211 note svtc1r: one assistant tool call e as {"name":N,"arguments":A} 1212 fn svtc1r bd edit uchar 983040 tp edit uint 262144 e ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1213 f = svmem bd tp e skfunc 1214 if eq f jsnone 1215 ret 23 1216 end 1217 nm = svmem bd tp f sknam 1218 if eq nm jsnone 1219 ret 23 1220 end 1221 if ne js_type tp nm 6 1222 ret 23 1223 end 1224 ar = svmem bd tp f skargs 1225 aty = ulong 0 1226 if ne ar jsnone 1227 aty = js_type tp ar 1228 end 1229 if and and ne aty 0 ne aty 6 ne aty 1 1230 ret 23 1231 end 1232 if not svpcat wb sv svtcn 1233 ret 6 1234 end 1235 if not svpraw bd tp nm wb sv 1236 ret 6 1237 end 1238 if not svpcat wb sv svtca 1239 ret 6 1240 end 1241 ok = true 1242 if eq aty 6 1243 ok = svpstr bd tp ar wb sv 1244 end 1245 if eq aty 1 1246 ok = svpraw bd tp ar wb sv 1247 end 1248 if eq aty 0 1249 ok = svpcat wb sv svempt 1250 end 1251 if not ok 1252 ret 6 1253 end 1254 if not svpcat wb sv svtcz 1255 ret 6 1256 end 1257 ret 0 1258 end 1259 1260 note svtcs: an assistant message's tool_calls array a into the template 1261 fn svtcs bd edit uchar 983040 tp edit uint 262144 a ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1262 if ne js_type tp a 3 1263 ret 23 1264 end 1265 e = js_first tp a 1266 for it ulong js_nkids tp a 1267 if eq e jsnone 1268 break 1269 end 1270 if not svpcat wb sv svtco 1271 ret 6 1272 end 1273 r = svtc1r bd tp e wb sv 1274 if ne r 0 1275 ret r 1276 end 1277 e = js_enext tp e 1278 end 1279 ret 0 1280 end 1281 1282 note svmsg1: one message object e into the template; 0 or an error code 1283 fn svmsg1 bd edit uchar 983040 tp edit uint 262144 e ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1284 if ne js_type tp e 1 1285 ret 3 1286 end 1287 ro = svmem bd tp e skrole 1288 if eq ro jsnone 1289 ret 5 1290 end 1291 if or ne js_type tp ro 6 gt js_slen tp ro 32 1292 ret 5 1293 end 1294 co = svmem bd tp e skcont 1295 tc = svmem bd tp e sktcall 1296 if ne tc jsnone 1297 if eq js_type tp tc 10 1298 tc = jsnone 1299 end 1300 end 1301 ct = ulong 10 1302 if ne co jsnone 1303 ct = js_type tp co 1304 end 1305 if and eq co jsnone eq tc jsnone 1306 ret 4 1307 end 1308 if and and ne ct 6 ne ct 10 ne ct 3 1309 ret 4 1310 end 1311 if not svpstr bd tp ro wb sv 1312 ret 6 1313 end 1314 if not svpcat wb sv svsep 1315 ret 6 1316 end 1317 if eq ct 6 1318 if not svpstr bd tp co wb sv 1319 ret 6 1320 end 1321 end 1322 if eq ct 3 1323 r = svparts bd tp co wb sv 1324 if ne r 0 1325 ret r 1326 end 1327 end 1328 if ne tc jsnone 1329 r = svtcs bd tp tc wb sv 1330 if ne r 0 1331 ret r 1332 end 1333 end 1334 if not svpcat wb sv svnl 1335 ret 6 1336 end 1337 ret 0 1338 end 1339 1340 fn svmsgs bd edit uchar 983040 tp edit uint 262144 a ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1341 if ne js_type tp a 3 1342 ret 3 1343 end 1344 n = js_nkids tp a 1345 if eq n 0 1346 ret 3 1347 end 1348 e = js_first tp a 1349 for it ulong n 1350 if eq e jsnone 1351 break 1352 end 1353 r = svmsg1 bd tp e wb sv 1354 if ne r 0 1355 ret r 1356 end 1357 e = js_enext tp e 1358 end 1359 if not svpcat wb sv svaprm 1360 ret 6 1361 end 1362 ret 0 1363 end 1364 1365 note svtool1: one tools entry e; its name into wb[82432 + 64 i ..) 1366 fn svtool1 bd edit uchar 983040 tp edit uint 262144 e ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1367 ty = svmem bd tp e sktype 1368 f = svmem bd tp e skfunc 1369 if or eq ty jsnone eq f jsnone 1370 ret 21 1371 end 1372 if ne js_type tp ty 6 1373 ret 21 1374 end 1375 if not js_streq bd js_off tp ty skfunc 1376 ret 21 1377 end 1378 nm = svmem bd tp f sknam 1379 if eq nm jsnone 1380 ret 21 1381 end 1382 if ne js_type tp nm 6 1383 ret 21 1384 end 1385 ret svtname bd js_off tp nm wb sv 1386 end 1387 1388 note svtname: the tool name string at bd[o ..) into wb[82432 + 64 i ..), i = sv30 1389 fn svtname bd edit uchar 983040 o ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1390 i = get sv 30 1391 if ge i 128 1392 ret 21 1393 end 1394 m = js_sget bd o part wb add 82432 mul i 64 64 1395 if lt m 1 1396 ret 21 1397 end 1398 put svtnl i ulong m 1399 put sv 30 add i 1 1400 ret 0 1401 end 1402 1403 note svtools: the tools value a (jsnone when absent): checked, its names kept, its JSON text 1404 note rendered as the template's first line 1405 fn svtools bd edit uchar 983040 tp edit uint 262144 a ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1406 if eq a jsnone 1407 ret 0 1408 end 1409 ty = js_type tp a 1410 if eq ty 10 1411 ret 0 1412 end 1413 if ne ty 3 1414 ret 21 1415 end 1416 n = js_nkids tp a 1417 if gt n 128 1418 ret 21 1419 end 1420 if eq n 0 1421 ret 0 1422 end 1423 e = js_first tp a 1424 for it ulong n 1425 if eq e jsnone 1426 break 1427 end 1428 r = svtool1 bd tp e wb sv 1429 if ne r 0 1430 ret r 1431 end 1432 e = js_enext tp e 1433 end 1434 if not svpcat wb sv svtlp 1435 ret 6 1436 end 1437 if not svpraw bd tp a wb sv 1438 ret 6 1439 end 1440 if not svpcat wb sv svnl 1441 ret 6 1442 end 1443 ret 0 1444 end 1445 1446 note svopts: stream_options value v: include_usage true sets sv4 1447 fn svopts bd edit uchar 983040 tp edit uint 262144 v ulong sv edit ulong 64 to void 1448 if ne js_type tp v 1 1449 ret 1450 end 1451 k = js_first tp v 1452 for it ulong js_nkids tp v 1453 if eq k jsnone 1454 break 1455 end 1456 if js_streq bd js_off tp k skincl 1457 if eq js_type tp add k 1 8 1458 put sv 4 1 1459 end 1460 end 1461 k = js_mnext tp k 1462 end 1463 end 1464 1465 note svscal: one scalar member (key token k, value k + 1); 0 or an error code 1466 fn svscal bd edit uchar 983040 tp edit uint 262144 k ulong sv edit ulong 64 to ulong 1467 o = js_off tp k 1468 v = add k 1 1469 ty = js_type tp v 1470 vo = js_off tp v 1471 if or js_streq bd o skmaxt js_streq bd o skmaxc 1472 if eq ty 10 1473 ret 0 1474 end 1475 if or ne ty 7 not js_isu bd vo 1476 ret 8 1477 end 1478 x = js_tou bd vo 1479 if or eq x 0 gt x 999999 1480 ret 8 1481 end 1482 put sv 1 x 1483 ret 0 1484 end 1485 if js_streq bd o sktemp 1486 if eq ty 10 1487 ret 0 1488 end 1489 if ne ty 7 1490 ret 7 1491 end 1492 if ne and js_nflags tp v 5 0 1493 ret 7 1494 end 1495 m = svtemp bd vo js_span tp v 1496 if gt m 2000 1497 ret 7 1498 end 1499 put sv 2 m 1500 ret 0 1501 end 1502 if js_streq bd o skseed 1503 if eq ty 10 1504 ret 0 1505 end 1506 if or ne ty 7 not js_isu bd vo 1507 ret 9 1508 end 1509 put sv 3 add js_tou bd vo sub 1 OMUTSEED 1510 ret 0 1511 end 1512 if js_streq bd o skstrm 1513 if eq ty 8 1514 put sv 0 1 1515 ret 0 1516 end 1517 if or eq ty 9 eq ty 10 1518 ret 0 1519 end 1520 ret 10 1521 end 1522 if js_streq bd o skn 1523 if eq ty 10 1524 ret 0 1525 end 1526 if or ne ty 7 not js_isu bd vo 1527 ret 12 1528 end 1529 if ne js_tou bd vo 1 1530 ret 12 1531 end 1532 ret 0 1533 end 1534 if js_streq bd o skopts 1535 svopts bd tp v sv 1536 end 1537 ret 0 1538 end 1539 1540 note svstop1: one stop string token t into wb[73728 + 128 i ..) 1541 fn svstop1 bd edit uchar 983040 tp edit uint 262144 t ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1542 if ne js_type tp t 6 1543 ret 18 1544 end 1545 ret svstopn bd js_off tp t wb sv 1546 end 1547 1548 note svstopn: the stop string at bd[o ..) into wb[73728 + 128 i ..), i = sv35 1549 fn svstopn bd edit uchar 983040 o ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1550 i = get sv 35 1551 if ge i 8 1552 ret 18 1553 end 1554 m = js_sget bd o part wb add 73728 mul i 128 128 1555 if lt m 1 1556 ret 18 1557 end 1558 put svstl i ulong m 1559 put sv 35 add i 1 1560 ret 0 1561 end 1562 1563 note svstopp: the stop value v (null, a string or an array of strings) 1564 fn svstopp bd edit uchar 983040 tp edit uint 262144 v ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1565 ty = js_type tp v 1566 if eq ty 10 1567 ret 0 1568 end 1569 if eq ty 6 1570 ret svstop1 bd tp v wb sv 1571 end 1572 if ne ty 3 1573 ret 18 1574 end 1575 n = js_nkids tp v 1576 if gt n 8 1577 ret 18 1578 end 1579 e = js_first tp v 1580 for it ulong n 1581 if eq e jsnone 1582 break 1583 end 1584 r = svstop1 bd tp e wb sv 1585 if ne r 0 1586 ret r 1587 end 1588 e = js_enext tp e 1589 end 1590 ret 0 1591 end 1592 1593 note svtopk: the top_k value v; -1, 0 and null leave it off 1594 fn svtopk bd edit uchar 983040 tp edit uint 262144 v ulong sv edit ulong 64 to ulong 1595 ty = js_type tp v 1596 if eq ty 10 1597 ret 0 1598 end 1599 if ne ty 7 1600 ret 19 1601 end 1602 vo = js_off tp v 1603 if js_isu bd vo 1604 x = js_tou bd vo 1605 if gt x 999999 1606 ret 19 1607 end 1608 put sv 28 x 1609 ret 0 1610 end 1611 if js_isl bd vo 1612 if eq js_tol bd vo neg long 1 1613 ret 0 1614 end 1615 end 1616 ret 19 1617 end 1618 1619 note svtchoi: the tool_choice value v; "none" turns tool-call detection off, "auto" keeps it. 1620 note "required" and the {"type":"function"} object would have to force a call, which this server 1621 note cannot do, so they are refused (22) rather than answered with free text. 1622 fn svtchoi bd edit uchar 983040 tp edit uint 262144 v ulong sv edit ulong 64 to ulong 1623 ty = js_type tp v 1624 if eq ty 10 1625 ret 0 1626 end 1627 if eq ty 6 1628 o = js_off tp v 1629 if js_streq bd o sknone 1630 put sv 31 0 1631 ret 0 1632 end 1633 if js_streq bd o svauto 1634 ret 0 1635 end 1636 end 1637 ret 22 1638 end 1639 1640 note svnzero: whether the number lexeme bd[o, o + n) is zero (every digit before an exponent is 0) 1641 fn svnzero bd edit uchar 983040 o ulong n ulong to bool 1642 for i ulong n 1643 c = ulong get bd add o i 1644 if or eq c 101 eq c 69 1645 ret true 1646 end 1647 if and ge c 49 le c 57 1648 ret false 1649 end 1650 end 1651 ret true 1652 end 1653 1654 note svnoop: whether value v of an unimplemented member asks for nothing. null always does; md 1 1655 note false; 2 true; 3 a number equal to 0; 4 an empty object; 5 an object whose "type" is "text" 1656 fn svnoop bd edit uchar 983040 tp edit uint 262144 v ulong md ulong to bool 1657 ty = js_type tp v 1658 if eq ty 10 1659 ret true 1660 end 1661 if eq md 1 1662 ret eq ty 9 1663 end 1664 if eq md 2 1665 ret eq ty 8 1666 end 1667 if eq md 3 1668 if ne ty 7 1669 ret false 1670 end 1671 ret svnzero bd js_off tp v js_span tp v 1672 end 1673 if ne ty 1 1674 ret false 1675 end 1676 if eq md 4 1677 ret eq js_nkids tp v 0 1678 end 1679 if ne md 5 1680 ret false 1681 end 1682 t = svmem bd tp v sktype 1683 if eq t jsnone 1684 ret false 1685 end 1686 if ne js_type tp t 6 1687 ret false 1688 end 1689 ret js_streq bd js_off tp t sktext 1690 end 1691 1692 note svunsm: the svnoop mode of the unimplemented member whose key is at bd[o ..): 0 it is not 1693 note one; 1 .. 5 as svnoop; 6 only null is accepted 1694 fn svunsm bd edit uchar 983040 o ulong to ulong 1695 if js_streq bd o sklogp 1696 ret 1 1697 end 1698 if js_streq bd o skptc 1699 ret 2 1700 end 1701 if or js_streq bd o skfpen js_streq bd o skppen 1702 ret 3 1703 end 1704 if js_streq bd o sktlogp 1705 ret 3 1706 end 1707 if js_streq bd o sklbias 1708 ret 4 1709 end 1710 if js_streq bd o skrfmt 1711 ret 5 1712 end 1713 if or js_streq bd o skfuncs js_streq bd o skfcall 1714 ret 6 1715 end 1716 ret 0 1717 end 1718 1719 note svscal3: a member that would change the output and is not implemented is refused (29) 1720 note unless it carries its default, so a client never gets an answer that ignored its request 1721 fn svscal3 bd edit uchar 983040 tp edit uint 262144 k ulong to ulong 1722 md = svunsm bd js_off tp k 1723 if eq md 0 1724 ret 0 1725 end 1726 if svnoop bd tp add k 1 md 1727 ret 0 1728 end 1729 ret 29 1730 end 1731 1732 note svtest: a test-only member (key k), refused unless the server runs with test=1 1733 fn svtest bd edit uchar 983040 tp edit uint 262144 k ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1734 if eq get sv 22 0 1735 ret 24 1736 end 1737 v = add k 1 1738 ty = js_type tp v 1739 vo = js_off tp v 1740 if js_streq bd js_off tp k sktdel 1741 if ne ty 7 1742 ret 24 1743 end 1744 if not js_isu bd vo 1745 ret 24 1746 end 1747 x = js_tou bd vo 1748 if gt x 10000 1749 ret 24 1750 end 1751 put sv 27 x 1752 ret 0 1753 end 1754 if ne ty 6 1755 ret 24 1756 end 1757 m = js_sget bd vo part wb 8192 16384 1758 if lt m 0 1759 ret 24 1760 end 1761 put sv 25 1 1762 put sv 26 ulong m 1763 ret 0 1764 end 1765 1766 note svscal2: the members stop, top_k, top_p, tool_choice and the test-only ones; 0 or a code 1767 fn svscal2 bd edit uchar 983040 tp edit uint 262144 k ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1768 o = js_off tp k 1769 v = add k 1 1770 if js_streq bd o svstop 1771 ret svstopp bd tp v wb sv 1772 end 1773 if js_streq bd o sktopk 1774 ret svtopk bd tp v sv 1775 end 1776 if js_streq bd o sktopp 1777 ty = js_type tp v 1778 if eq ty 10 1779 ret 0 1780 end 1781 if ne ty 7 1782 ret 20 1783 end 1784 if ne and js_nflags tp v 5 0 1785 ret 20 1786 end 1787 m = svtopp bd js_off tp v js_span tp v 1788 if eq m jsnone 1789 ret 20 1790 end 1791 put sv 29 m 1792 ret 0 1793 end 1794 if js_streq bd o sktchoi 1795 ret svtchoi bd tp v sv 1796 end 1797 if or js_streq bd o sktout js_streq bd o sktdel 1798 ret svtest bd tp k wb sv 1799 end 1800 ret 0 1801 end 1802 1803 note svseed: the seed state of a request that names none: the wall clock mixed with the request 1804 note count, so two requests with the same body draw differently. A request's own seed replaces it 1805 note (svscal), and only that is reproducible. 1806 fn svseed sv edit ulong 64 to ulong 1807 t = bitcast ulong realns 1808 ret smx xor t mul add get sv 9 1 `11400714819323198485` 1809 end 1810 1811 note svreset: a request's options to their defaults 1812 fn svreset sv edit ulong 64 to void 1813 put sv 0 0 1814 put sv 1 999999 1815 put sv 2 1000 1816 put sv 3 svseed sv 1817 put sv 4 0 1818 put sv 5 0 1819 for i ulong 12 1820 put sv add 24 i 0 1821 end 1822 put sv 29 `1000000` 1823 put sv 31 1 1824 end 1825 1826 note svparse: the request body bd[0, n) into sv and the template; 0 or an error code 1827 fn svparse bd edit uchar 983040 n ulong wb edit uchar `1073664` sv edit ulong 64 to ulong 1828 tp = new uint 262144 0 1829 x = js_init tp 983040 64 1830 r = js_parse bd tp n 1831 if lt r 0 1832 if eq r neg long jsetok 1833 ret 25 1834 end 1835 if eq OMUTJSON 0 1836 svreset sv 1837 put sv 1 1 1838 ret 0 1839 end 1840 ret 1 1841 end 1842 if ne js_type tp 0 1 1843 ret 2 1844 end 1845 svreset sv 1846 k = js_first tp 0 1847 for it ulong js_nkids tp 0 1848 if eq k jsnone 1849 break 1850 end 1851 e = svscal bd tp k sv 1852 if eq e 0 1853 e = svscal2 bd tp k wb sv 1854 end 1855 if eq e 0 1856 e = svscal3 bd tp k 1857 end 1858 if ne e 0 1859 ret e 1860 end 1861 k = js_mnext tp k 1862 end 1863 if eq get sv 37 1 1864 ret svcprm bd tp wb sv 1865 end 1866 ma = svmem bd tp 0 skmsgs 1867 if eq ma jsnone 1868 ret 3 1869 end 1870 e = svtools bd tp svmem bd tp 0 sktools wb sv 1871 if ne e 0 1872 ret e 1873 end 1874 if eq get sv 30 0 1875 put sv 31 0 1876 end 1877 ret svmsgs bd tp ma wb sv 1878 end 1879 1880 note svcprm: a /v1/completions body after its scalars: prompt (a non-empty string) is the whole 1881 note template, with no role lines and no assistant prefix; stream true, messages, tools and 1882 note tool_choice are refused; 0 or an error code 1883 fn svcprm bd edit uchar 983040 tp edit uint 262144 wb edit uchar `1073664` sv edit ulong 64 to ulong 1884 if eq get sv 0 1 1885 ret 27 1886 end 1887 if ne svmem bd tp 0 skmsgs jsnone 1888 ret 28 1889 end 1890 if ne svmem bd tp 0 sktools jsnone 1891 ret 28 1892 end 1893 if ne svmem bd tp 0 sktchoi jsnone 1894 ret 28 1895 end 1896 pr = svmem bd tp 0 skprompt 1897 if eq pr jsnone 1898 ret 26 1899 end 1900 if or ne js_type tp pr 6 eq js_slen tp pr 0 1901 ret 26 1902 end 1903 put sv 30 0 1904 put sv 31 0 1905 if not svpstr bd tp pr wb sv 1906 ret 6 1907 end 1908 ret 0 1909 end 1910 1911 note ======== encoding 1912 1913 note svemit: token t, the m-th of an encoding: md 0 onto the context, md 1 into the forced tokens 1914 fn svemit md ulong m ulong t ulong to void 1915 if eq md 0 1916 if lt get og 9 get og 1 1917 ocpush t 1918 end 1919 ret 1920 end 1921 if lt m 4096 1922 put svft m t 1923 end 1924 end 1925 1926 note svenc1: the pre-token oarg[i, i + l) encoded and emitted after m tokens; the new count 1927 fn svenc1 i ulong l ulong md ulong m ulong to ulong 1928 for k ulong l 1929 put bqtm k ushort get oarg add i k 1930 end 1931 c = bpenct l 1932 for k ulong c 1933 svemit md add m k ulong get bqtm k 1934 end 1935 ret add m c 1936 end 1937 1938 note svenc: encode src[0, n) of any length as obpenc does, in windows of at most 4000 bytes copied 1939 note to oarg; a window ends at a pre-token boundary at least 64 bytes before its end (a pre-token 1940 note is at most 64 bytes, so the boundaries agree with one pass), the last one at n. Returns the 1941 note token count. 1942 fn svenc src view uchar * n ulong md ulong to ulong 1943 s = ulong 0 1944 m = ulong 0 1945 while lt s n bound 999999 1946 w = sub n s 1947 if gt w 4000 1948 w = 4000 1949 end 1950 for k ulong w 1951 put oarg k get src add s k 1952 end 1953 la = eq add s w n 1954 i = ulong 0 1955 while lt i w bound 4097 1956 if and not la gt add i 64 w 1957 break 1958 end 1959 l = bpclena i w 1960 m = svenc1 i l md m 1961 i = add i l 1962 end 1963 s = add s i 1964 end 1965 ret m 1966 end 1967 1968 note ======== generation 1969 1970 note svgen: sample up to min(sv1, T - CT0) tokens; streams chunks when sv0 is 1. 0, or negative 1971 note when a chunk could not be sent or the client left (svgone: finish 4, status 499). Between 1972 note tokens the listener and pending connections are served (svside), waiting up to 1973 note ollm_test_delay_ms. 1974 fn svgen cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 to long 1975 lim = sub get sv 19 get sv 6 1976 if lt get sv 1 lim 1977 lim = get sv 1 1978 end 1979 put sv 7 0 1980 put sv 8 0 1981 put sv 12 0 1982 put sv 13 0 1983 put sv 14 0 1984 put sv 17 0 1985 tmo = long mul get sv 27 `1000000` 1986 for g ulong lim 1987 svside bd sv tmo 1988 if svgone cs 1989 put sv 8 4 1990 put sv 21 499 1991 ret -104 1992 end 1993 if not svtake wb sv 1994 break 1995 end 1996 r = svpend cs io ob wb sv 0 1997 if lt r 0 1998 ret r 1999 end 2000 end 2001 if eq get sv 8 0 2002 put sv 8 2 2003 end 2004 p = get sv 13 2005 svclean wb sv 1 2006 x = svstopc wb sv p 2007 svtcall wb sv bd 2008 r = svpend cs io ob wb sv 1 2009 if lt r 0 2010 ret r 2011 end 2012 if and eq get sv 0 1 gt get sv 32 0 2013 ret svtchunk cs io ob wb sv bd 2014 end 2015 ret r 2016 end 2017 2018 note svtake: one sampled token into the context and the content; false when it ends the output 2019 note (sv8: 1 token 0 or a stop string, 2 the content would pass its 65536 bytes) 2020 fn svtake wb edit uchar `1073664` sv edit ulong 64 to bool 2021 c = svdraw sv 2022 if eq c 0 2023 put sv 8 1 2024 ret false 2025 end 2026 if not svfits sv c 2027 put sv 8 2 2028 ret false 2029 end 2030 svcap wb sv c 2031 p = get sv 13 2032 svclean wb sv 0 2033 if svstopc wb sv p 2034 ret false 2035 end 2036 ret true 2037 end 2038 2039 note svstopc: the earliest stop string match that ends past the content's old length p cuts the 2040 note content there (finish stop); whether one did 2041 fn svstopc wb edit uchar `1073664` sv edit ulong 64 p ulong to bool 2042 n = get sv 13 2043 b = jsnone 2044 for i ulong get sv 35 2045 l = get svstl i 2046 s = ulong 0 2047 if gt add p 1 l 2048 s = sub add p 1 l 2049 end 2050 q = svfind wb s n add 73728 mul i 128 l 2051 if lt q b 2052 b = q 2053 end 2054 end 2055 if eq b jsnone 2056 ret false 2057 end 2058 put sv 13 b 2059 put sv 12 0 2060 put sv 8 1 2061 ret true 2062 end 2063 2064 note svhold: how far a streamed chunk may send: not into the tail a stop string or the tool-call 2065 note marker could still claim, nor (detection on) past the marker or into trailing whitespace 2066 fn svhold wb edit uchar `1073664` sv edit ulong 64 to ulong 2067 n = get sv 13 2068 lo = get sv 14 2069 h = n 2070 for i ulong get sv 35 2071 k = svpsuf wb lo n add 73728 mul i 128 get svstl i 2072 if lt sub n k h 2073 h = sub n k 2074 end 2075 end 2076 if eq get sv 31 1 2077 q = svfind wb lo n 74752 11 2078 if lt q h 2079 h = q 2080 end 2081 k2 = svpsuf wb lo n 74752 11 2082 if lt sub n k2 h 2083 h = sub n k2 2084 end 2085 h = svtrim wb lo h 2086 end 2087 ret h 2088 end 2089 2090 note svdraw: the next token (cmdsamp's step osstep, or svdrawf with top_k/top_p on, or the forced 2091 note tokens of ollm_test_output then 0), advancing the seed stream when sampling 2092 fn svdraw sv edit ulong 64 to ulong 2093 if eq get sv 25 1 2094 g = get sv 7 2095 if lt g get sv 24 2096 ret get svft g 2097 end 2098 ret 0 2099 end 2100 st = get sv 3 2101 tm = get sv 2 2102 c = ulong 0 2103 if or eq tm 0 not svfon sv 2104 c = osstep st tm 2105 else 2106 c = svdrawf st div f64 tm 1000.0 get sv 28 get sv 29 2107 end 2108 if ne tm 0 2109 put sv 3 add st `11400714819323198485` 2110 end 2111 ret c 2112 end 2113 2114 note svfon: whether top_k (0 < K < V) or top_p (< 1) filters 2115 fn svfon sv edit ulong 64 to bool 2116 k = get sv 28 2117 if and gt k 0 lt k get og 0 2118 ret true 2119 end 2120 ret lt get sv 29 `1000000` 2121 end 2122 2123 note svdrawf: odraw over the kept tokens (svkeep), the uniform scaled to their mass 2124 fn svdrawf st ulong tau f64 k ulong p6 ulong to ulong 2125 svlogit tau 2126 o = get og 27 2127 v = get og 0 2128 cm = svkeep o v k p6 2129 u = mul f64 shr smx st 11 1.`1102230246251565e`-16 2130 r = svpick o v mul u cm 2131 for j ulong v 2132 put svmk j 0 2133 end 2134 ret r 2135 end 2136 2137 note svlogit: the next-token logits divided by tau, then made probabilities (as odraw does) 2138 fn svlogit tau f64 to void 2139 olastf 2140 o = get og 27 2141 v = get og 0 2142 m = bitcast f64 ulong `18442240474082181120` 2143 for c ulong v 2144 x = div get oa add o c tau 2145 put oa add o c x 2146 m = fmax m x 2147 end 2148 sm = osoft o v m 2149 end 2150 2151 note svkeep: mark the most probable tokens (ties to the lower id), at most K (0 < K < V), until 2152 note their mass reaches top_p (p6 < `1000000`); their mass 2153 fn svkeep o ulong v ulong k ulong p6 ulong to f64 2154 lim = v 2155 if and gt k 0 lt k v 2156 lim = k 2157 end 2158 pf = div f64 p6 `1000000`.0 2159 cm = 0.0f64 2160 for j ulong lim 2161 b = svmax o v 2162 put svmk b 1 2163 cm = add cm get oa add o b 2164 if and lt p6 `1000000` ge cm pf 2165 break 2166 end 2167 end 2168 ret cm 2169 end 2170 2171 note svmax: the unmarked token of highest probability, the lower id on ties 2172 fn svmax o ulong v ulong to ulong 2173 b = ulong 0 2174 bv = 0.0f64 2175 fd = false 2176 for c ulong v 2177 if eq get svmk c 0 2178 x = get oa add o c 2179 if or not fd gt x bv 2180 b = c 2181 bv = x 2182 fd = true 2183 end 2184 end 2185 end 2186 ret b 2187 end 2188 2189 note svpick: the first kept token (id order) whose running kept mass passes t; else the last kept 2190 fn svpick o ulong v ulong t f64 to ulong 2191 acc = 0.0f64 2192 la = ulong 0 2193 for c ulong v 2194 if eq get svmk c 1 2195 acc = add acc get oa add o c 2196 la = c 2197 if lt t acc 2198 ret c 2199 end 2200 end 2201 end 2202 ret la 2203 end 2204 2205 note svfits: whether token c's bytes fit the raw buffer (8192) and, as up to three content bytes 2206 note each, the content (65536) 2207 fn svfits sv edit ulong 64 c ulong to bool 2208 tl = obtlen c 2209 rn = add get sv 12 tl 2210 gn = add get sv 13 mul rn 3 2211 ret not or or gt rn 8192 gt tl 4096 gt gn 65536 2212 end 2213 2214 note svcap: token c's bytes onto the raw buffer, c onto the context 2215 fn svcap wb edit uchar `1073664` sv edit ulong 64 c ulong to void 2216 obtcap c 2217 rn = get sv 12 2218 for i ulong get obon 0 2219 put wb add rn i get obo i 2220 end 2221 put sv 12 add rn get obon 0 2222 ocpush c 2223 put sv 7 add get sv 7 1 2224 end 2225 2226 note svpend: when streaming, the content not yet sent (up to svhold; fin 1 all of it, or the text 2227 note before the tool calls) as one chunk; 0 or the send error 2228 fn svpend cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 wb edit uchar `1073664` sv edit ulong 64 fin ulong to long 2229 if ne get sv 0 1 2230 ret 0 2231 end 2232 h = get sv 13 2233 if eq fin 0 2234 h = svhold wb sv 2235 else 2236 if gt get sv 32 0 2237 h = svtrim wb get sv 14 get sv 33 2238 end 2239 end 2240 if le h get sv 14 2241 ret 0 2242 end 2243 put sv 17 h 2244 r = svchunk cs io ob wb sv 1 2245 put sv 14 h 2246 ret r 2247 end 2248 2249 note ======== tool calls in the output 2250 2251 note svtdecl: whether bd[f, f + q) is a declared tool name 2252 fn svtdecl wb edit uchar `1073664` bd edit uchar 983040 f ulong q ulong sv edit ulong 64 to bool 2253 for i ulong get sv 30 2254 if eq get svtnl i q 2255 if svteq wb bd f q add 82432 mul i 64 2256 ret true 2257 end 2258 end 2259 end 2260 ret false 2261 end 2262 2263 note svteq: whether bd[f, f + q) equals wb[b, b + q) 2264 fn svteq wb edit uchar `1073664` bd edit uchar 983040 f ulong q ulong b ulong to bool 2265 for j ulong q 2266 if ne get bd add f j get wb add b j 2267 ret false 2268 end 2269 end 2270 ret true 2271 end 2272 2273 note svtargs: the arguments token ar of a block at wb[b ..) into bd at f (object: its JSON text; 2274 note string: decoded; absent: {}); its length, or negative 2275 fn svtargs wb edit uchar `1073664` bd edit uchar 983040 tp view uint 262144 ar ulong f ulong b ulong to long 2276 if eq ar jsnone 2277 put bd f 123 2278 put bd add f 1 125 2279 ret 2 2280 end 2281 ty = js_type tp ar 2282 if eq ty 6 2283 ret js_sget view part wb b 65536 js_off tp ar part bd f 65536 2284 end 2285 if ne ty 1 2286 ret -1 2287 end 2288 o = add b js_off tp ar 2289 n = js_span tp ar 2290 for i ulong n 2291 put bd add f i get wb add o i 2292 end 2293 ret long n 2294 end 2295 2296 note svtc1: the block JSON at content [s, s + l) as tool call sv32; false when it is not one 2297 fn svtc1 wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 s ulong l ulong to bool 2298 if or eq l 0 gt l 65536 2299 ret false 2300 end 2301 tp = new uint 262144 0 2302 x = js_init tp 65536 64 2303 b = add 8192 s 2304 r = js_parse view part wb b l tp l 2305 if lt r 0 2306 ret false 2307 end 2308 if ne js_type tp 0 1 2309 ret false 2310 end 2311 ret svtc2 wb sv bd tp b 2312 end 2313 2314 note svtc2: the parsed block at wb[b ..) (tape tp) recorded as tool call sv32; whether it is one 2315 fn svtc2 wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 tp view uint 262144 b ulong to bool 2316 nm = svmem view part wb b 65536 tp 0 sknam 2317 if eq nm jsnone 2318 ret false 2319 end 2320 if ne js_type tp nm 6 2321 ret false 2322 end 2323 q = svtnam wb sv bd js_off tp nm b 2324 if lt q 1 2325 ret false 2326 end 2327 f = get sv 34 2328 g = add f ulong q 2329 a = svtargs wb bd tp svmem view part wb b 65536 tp 0 skargs g b 2330 if lt a 0 2331 ret false 2332 end 2333 svtrec sv f ulong q g ulong a 2334 ret true 2335 end 2336 2337 note svtnam: the name string at wb[b + o ..) decoded to bd[sv34 ..); its length when it is a 2338 note declared tool name, else -1 2339 fn svtnam wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 o ulong b ulong to long 2340 f = get sv 34 2341 q = js_sget view part wb b 65536 o part bd f 64 2342 if lt q 1 2343 ret -1 2344 end 2345 if not svtdecl wb bd f ulong q sv 2346 ret -1 2347 end 2348 ret q 2349 end 2350 2351 note svtrec: record tool call sv32's name bd[f, f + q) and arguments bd[g, g + a) 2352 fn svtrec sv edit ulong 64 f ulong q ulong g ulong a ulong to void 2353 m = mul get sv 32 4 2354 put svtcx m f 2355 put svtcx add m 1 q 2356 put svtcx add m 2 g 2357 put svtcx add m 3 a 2358 put sv 34 add g a 2359 put sv 32 add get sv 32 1 2360 end 2361 2362 note svtblk: the block JSON at content p; the position after it, or jsnone 2363 fn svtblk wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 p ulong n ulong to ulong 2364 if gt add p 11 n 2365 ret jsnone 2366 end 2367 if not svat wb p 74752 11 2368 ret jsnone 2369 end 2370 s = add p 11 2371 z = svfind wb s n 74763 12 2372 if eq z jsnone 2373 ret jsnone 2374 end 2375 if not svtc1 wb sv bd s sub z s 2376 ret jsnone 2377 end 2378 ret add z 12 2379 end 2380 2381 note svtcall: with detection on, whether the content from the first marker on is 1 .. 8 tool-call 2382 note blocks with only whitespace around: sv32 calls, sv33 the marker, finish tool_calls 2383 fn svtcall wb edit uchar `1073664` sv edit ulong 64 bd edit uchar 983040 to void 2384 put sv 32 0 2385 put sv 34 0 2386 if eq get sv 31 0 2387 ret 2388 end 2389 n = get sv 13 2390 q = svfind wb 0 n 74752 11 2391 if eq q jsnone 2392 ret 2393 end 2394 p = q 2395 for it ulong 9 2396 p = svskws wb p n 2397 if ge p n 2398 break 2399 end 2400 z = jsnone 2401 if lt get sv 32 8 2402 z = svtblk wb sv bd p n 2403 end 2404 if eq z jsnone 2405 put sv 32 0 2406 ret 2407 end 2408 p = z 2409 end 2410 if lt p n 2411 put sv 32 0 2412 ret 2413 end 2414 put sv 33 q 2415 put sv 8 3 2416 end 2417 2418 note ======== requests during a generation 2419 2420 note svwait: wait up to tmo ns for the listener or a pending connection (svpfd); the connection to 2421 note serve now (bytes arrived, or it closed or failed), or -1. A new connection whose first byte 2422 note does not arrive within 20 ms waits in a free pending slot (8; all taken: new connections stay 2423 note in the listen backlog), so a connection that sends nothing costs the caller 20 ms once, not a 2424 note head deadline. A pending connection is closed unanswered 10 s after its accept. 2425 fn svwait sv edit ulong 64 tmo long to long 2426 now = ulong monons 2427 iv = tmo 2428 n = ulong 0 2429 for i ulong 8 2430 f = get svpfd i 2431 if ne f 0 2432 age = sub now get svpat i 2433 if ge age `10000000000` 2434 y = fclose int sub f 1 2435 put svpfd i 0 2436 else 2437 rm = long sub `10000000000` age 2438 if or lt iv 0 lt rm iv 2439 iv = rm 2440 end 2441 svprec n sub f 1 2442 put svpix n i 2443 n = add n 1 2444 end 2445 end 2446 end 2447 fr = lt n 8 2448 m = n 2449 if fr 2450 svprec n get sv 36 2451 m = add n 1 2452 end 2453 r = pollfds svpl 0 mul m 8 iv 2454 if le r 0 2455 ret -1 2456 end 2457 for j ulong n 2458 if ne get svpl add mul j 8 6 0 2459 s = get svpix j 2460 cp = long sub get svpfd s 1 2461 put svpfd s 0 2462 ret cp 2463 end 2464 end 2465 if not fr 2466 ret -1 2467 end 2468 c = haccept int get sv 36 0 2469 if lt c 0 2470 ret -1 2471 end 2472 w = waitio int c 1 long `20000000` 2473 if ne w 0 2474 ret c 2475 end 2476 for k ulong 8 2477 if eq get svpfd k 0 2478 put svpfd k ulong add c 1 2479 put svpat k ulong monons 2480 ret -1 2481 end 2482 end 2483 y2 = fclose int c 2484 ret -1 2485 end 2486 2487 note svprec: pollfd record j for descriptor fd, events POLLIN 2488 fn svprec j ulong fd ulong to void 2489 b = mul j 8 2490 put svpl b uchar and fd 255 2491 put svpl add b 1 uchar and shr fd 8 255 2492 put svpl add b 2 uchar and shr fd 16 255 2493 put svpl add b 3 uchar and shr fd 24 255 2494 put svpl add b 4 1 2495 put svpl add b 5 0 2496 put svpl add b 6 0 2497 put svpl add b 7 0 2498 end 2499 2500 note svgone: whether the client of the generating request has left: a non-blocking peek (MSG_PEEK | 2501 note MSG_DONTWAIT) of its socket reads end of stream or an error. Request bytes stay unread. A 2502 note client that half-closes its sending side after the request counts as gone. 2503 fn svgone cs edit ulong 320 to bool 2504 r = recv int get cs HFD svpk 0 1 66 2505 if eq r 0 2506 ret true 2507 end 2508 ret and lt r 0 and ne r -11 ne r -4 2509 end 2510 2511 note svside: serve the connections svwait yields (the first wait up to tmo ns, then without waiting; 2512 note at most 9 per call) with svside2. Each pass takes a connection and serves it, so none is ever 2513 note taken from svwait and left unserved. 2514 fn svside bd edit uchar 983040 sv edit ulong 64 tmo long to void 2515 t = tmo 2516 for k ulong 9 2517 c = svwait sv t 2518 if lt c 0 2519 ret 2520 end 2521 svside2 c bd sv 2522 t = long 0 2523 end 2524 end 2525 2526 note svside2: one request while a completion generates: health busy, models, else 503 (logged) 2527 fn svside2 c long bd edit uchar 983040 sv edit ulong 64 to void 2528 cs = new ulong 320 0 2529 io = new uchar 131072 0 2530 r = hinit cs io int c 1 2531 if lt r 0 2532 x = hclose cs 2533 ret 2534 end 2535 note bytes have arrived (svwait), so a head or body that stalls costs at most 250 ms or 1 s 2536 hsettmo cs `250000000` `250000000` 2537 hsetbdl cs `1000000000` 2538 put cs HBCAP 983040 2539 r = hreqrd cs io 2540 if gt r 0 2541 put cs HKEEP 0 2542 st = svbusy cs io bd 2543 svlogs st 2544 else 2545 if lt r 0 2546 x = hreject cs io r 2547 end 2548 end 2549 x = hclose cs 2550 end 2551 2552 fn svbusy cs edit ulong 320 io edit uchar 131072 bd edit uchar 983040 to ulong 2553 if and hispath cs io svphlth hismeth cs io svmget 2554 x = svstat cs io 200 svthbsy 2555 ret 200 2556 end 2557 if and hispath cs io svpmods hismeth cs io svmget 2558 x = svmodw cs io 2559 ret 200 2560 end 2561 note read the body before answering, so the client sees the 503 rather than a reset 2562 n = hbodyall cs io bd 2563 x = svs503 cs io 2564 ret 503 2565 end 2566 2567 fn svlogs st ulong to void 2568 oputc 67 2569 oputc 32 2570 oputu st 2571 for i ulong 4 2572 oputc 32 2573 oputc 48 2574 end 2575 oputc 10 2576 oflush 2577 end 2578 2579 note ======== serving 2580 2581 note svid: response id chatcmpl-ollm- (cmpl-ollm- on /v1/completions) into 2582 note wb[82176 ..), its length into sv15 2583 fn svid wb edit uchar `1073664` sv edit ulong 64 to void 2584 put sv 9 add get sv 9 1 2585 p = ulong 82176 2586 if eq get sv 37 1 2587 p = svmcat wb p 82240 svcidp 2588 else 2589 p = svmcat wb p 82240 svidp 2590 end 2591 p = svmdec wb p 82240 get sv 9 2592 put sv 15 sub p 82176 2593 end 2594 2595 fn svlog sv edit ulong 64 to void 2596 oputc 67 2597 oputc 32 2598 oputu get sv 21 2599 oputc 32 2600 oputu get sv 6 2601 oputc 32 2602 oputu get sv 7 2603 oputc 32 2604 oputu get sv 8 2605 oputc 32 2606 oputu div sub ulong monons get sv 11 `1000000` 2607 oputc 10 2608 oflush 2609 end 2610 2611 note svprep: the context (token 0, then the template's encoding) and the forced tokens; 0 or 11 2612 fn svprep wb edit uchar `1073664` sv edit ulong 64 to ulong 2613 put og 9 0 2614 ocpush 0 2615 np = svenc view part wb 90624 get sv 5 get sv 5 0 2616 put sv 6 add np 1 2617 if ge get sv 6 get sv 19 2618 ret 11 2619 end 2620 if eq get sv 25 1 2621 c = ulong 0 2622 if gt get sv 26 0 2623 c = svenc view part wb 8192 get sv 26 get sv 26 1 2624 end 2625 if gt c 4096 2626 c = 4096 2627 end 2628 put sv 24 c 2629 end 2630 ret 0 2631 end 2632 2633 note svchat: POST /v1/chat/completions 2634 fn svchat cs edit ulong 320 io edit uchar 131072 bd edit uchar 983040 wb edit uchar `1073664` sv edit ulong 64 to long 2635 note the request's clock and log fields come from svroute (svbeg) 2636 n = hbodyall cs io bd 2637 if lt n 0 2638 put cs HKEEP 0 2639 if eq n HEBIG 2640 put sv 16 13 2641 r = svfail cs io wb sv svbig 2642 svlog sv 2643 ret r 2644 end 2645 put sv 21 400 2646 svlog sv 2647 ret hrsperr cs io 400 2648 end 2649 note generation outlasts the 60 s body deadline: only the idle limit applies from here 2650 hsetdl cs 0 2651 e = svparse bd ulong n wb sv 2652 if eq e 0 2653 e = svprep wb sv 2654 end 2655 if ne e 0 2656 put sv 16 e 2657 st = ulong 400 2658 if eq e 25 2659 st = ulong 413 2660 end 2661 r = svfail cs io wb sv st 2662 svlog sv 2663 ret r 2664 end 2665 svid wb sv 2666 put sv 10 ulong div realns `1000000000` 2667 ob = new uchar 409600 0 2668 put sv 21 200 2669 if eq get sv 0 1 2670 r = hrspsse cs io 2671 r = hfe r svchunk cs io ob wb sv 0 2672 if lt r 0 2673 svlog sv 2674 ret r 2675 end 2676 r = svgen cs io ob wb sv bd 2677 if lt r 0 2678 svlog sv 2679 ret r 2680 end 2681 r = svchunk cs io ob wb sv 2 2682 if eq get sv 4 1 2683 r = hfe r svchunk cs io ob wb sv 3 2684 end 2685 r = hfe r hssedat cs io svdone 2686 r = hfe r hsseend cs io 2687 r = hfe r hwfin cs io 2688 svlog sv 2689 ret r 2690 end 2691 r = svgen cs io ob wb sv bd 2692 if lt r 0 2693 svlog sv 2694 ret r 2695 end 2696 m = svfull ob wb sv bd 2697 if lt m 0 2698 put sv 16 16 2699 r = svfail cs io wb sv 500 2700 svlog sv 2701 ret r 2702 end 2703 r = svsend cs io ob 200 ulong m 2704 svlog sv 2705 ret r 2706 end 2707 2708 fn svroute cs edit ulong 320 io edit uchar 131072 bd edit uchar 983040 wb edit uchar `1073664` sv edit ulong 64 to long 2709 svbeg sv 2710 if hispath cs io svpchat 2711 if or hismeth cs io svmpost eq OMUTMETH 0 2712 put sv 37 0 2713 ret svchat cs io bd wb sv 2714 end 2715 ret svend cs io wb sv 15 405 2716 end 2717 if hispath cs io svpcomp 2718 if or hismeth cs io svmpost eq OMUTMETH 0 2719 put sv 37 1 2720 ret svchat cs io bd wb sv 2721 end 2722 ret svend cs io wb sv 15 405 2723 end 2724 if hispath cs io svphlth 2725 if or hismeth cs io svmget eq OMUTMETH 0 2726 ret svok cs io sv svthlth 2727 end 2728 ret svend cs io wb sv 15 405 2729 end 2730 if hispath cs io svpmods 2731 if or hismeth cs io svmget eq OMUTMETH 0 2732 ret svmods cs io sv 2733 end 2734 ret svend cs io wb sv 15 405 2735 end 2736 if eq OMUTPATH 0 2737 ret svok cs io sv svthlth 2738 end 2739 ret svend cs io wb sv 14 404 2740 end 2741 2742 note svbeg: a request's log fields cleared, its clock started 2743 fn svbeg sv edit ulong 64 to void 2744 put sv 11 ulong monons 2745 put sv 6 0 2746 put sv 7 0 2747 put sv 8 0 2748 end 2749 2750 note svend: error code e with status st, logged 2751 fn svend cs edit ulong 320 io edit uchar 131072 wb edit uchar `1073664` sv edit ulong 64 e ulong st ulong to long 2752 put sv 16 e 2753 r = svfail cs io wb sv st 2754 svlog sv 2755 ret r 2756 end 2757 2758 note svok: the fixed JSON text t with status 200, logged 2759 fn svok cs edit ulong 320 io edit uchar 131072 sv edit ulong 64 t view uchar * to long 2760 put sv 21 200 2761 r = svstat cs io 200 t 2762 svlog sv 2763 ret r 2764 end 2765 2766 note svconn: one accepted connection c carries one request, answered with Connection: close. 2767 note Connections are served one at a time, so a kept-alive idle connection would hold the listener: 2768 note allen-chat pools its upstream connection, and its next request queued 30 s behind the pooled 2769 note one (the old idle limit) until this change. svwait hands over a connection only once its first 2770 note bytes arrived; the rest of the head must arrive within 10 s. 2771 fn svconn c long cs edit ulong 320 io edit uchar 131072 bd edit uchar 983040 wb edit uchar `1073664` sv edit ulong 64 to void 2772 r = hinit cs io int c 1 2773 if lt r 0 2774 x = hclose cs 2775 ret 2776 end 2777 hsettmo cs `10000000000` `10000000000` 2778 hsetbdl cs `60000000000` 2779 note the JSON parser takes at most 983040 bytes (jsmaxn); larger Content-Length is 413 at the head 2780 put cs HBCAP 983040 2781 r = hreqrd cs io 2782 if eq r HEBIG 2783 put cs HKEEP 0 2784 svbeg sv 2785 x = svend cs io wb sv 13 svbig 2786 end 2787 if and lt r 0 ne r HEBIG 2788 x = hreject cs io r 2789 end 2790 if gt r 0 2791 put cs HKEEP 0 2792 x = svroute cs io bd wb sv 2793 end 2794 x = hclose cs 2795 end 2796 2797 note svpre: whether the argument in oarg (length n) starts with t 2798 fn svpre n ulong t view uchar * to bool 2799 if lt n len t 2800 ret false 2801 end 2802 for i ulong len t 2803 if ne get oarg i get t i 2804 ret false 2805 end 2806 end 2807 ret true 2808 end 2809 2810 note svdig: the decimal oarg[a, n) (1 .. 7 digits), or 0 2811 fn svdig a ulong n ulong to ulong 2812 if or le n a gt sub n a 7 2813 ret 0 2814 end 2815 v = ulong 0 2816 for i ulong sub n a 2817 c = ulong get oarg add a i 2818 if or lt c 48 gt c 57 2819 ret 0 2820 end 2821 v = add mul v 10 sub c 48 2822 end 2823 ret v 2824 end 2825 2826 note svcli: the options after MERGES: [MAXCONN] (first only), tplmax=N, test=1; MAXCONN, or 2827 note 2^64 - 1 when none was given: the width of the connection count, not a limit anyone reaches 2828 fn svcli sv edit ulong 64 na ulong to ulong 2829 mx = ulong `18446744073709551615` 2830 for j ulong sub na 5 2831 i = add j 5 2832 n = oargp i 2833 if svpre n svoptt 2834 v = svdig 7 n 2835 if or lt v 256 gt v 983040 2836 exit 2 2837 end 2838 put sv 23 v 2839 else 2840 if and eq n len svopte svpre n svopte 2841 put sv 22 1 2842 else 2843 if ne i 5 2844 exit 2 2845 end 2846 mx = oargu 5 2847 if eq mx 0 2848 exit 2 2849 end 2850 end 2851 end 2852 end 2853 ret mx 2854 end 2855 2856 note serve PORT CK MERGES [MAXCONN] [tplmax=N] [test=1] 2857 fn cmdserve to void 2858 na = oargc 2859 if or lt na 5 gt na 8 2860 exit 2 2861 end 2862 port = oargu 2 2863 if or eq port 0 gt port 65535 2864 exit 2 2865 end 2866 sv = new ulong 64 0 2867 put sv 23 65536 2868 mx = svcli sv na 2869 ockloop 3 2870 v = get og 0 2871 obload 4 v 2872 ochipup 2873 cs = new ulong 320 0 2874 io = new uchar 131072 0 2875 bd = new uchar 983040 0 2876 wb = new uchar `1073664` 0 2877 a = new uchar 16 0 2878 put sv 19 get og 1 2879 x = svmcat wb 74752 74763 svtco 2880 x = svmcat wb 74763 74775 svtcc 2881 svmid 2882 r = haddr4 a uint mul `2130706433` OMUTLOOP port 2883 lf = hlisten a 16 2884 if lt lf 0 2885 exit 71 2886 end 2887 put sv 36 ulong lf 2888 for i ulong len svport 2889 oputc ulong get svport i 2890 end 2891 oputu port 2892 oputc 10 2893 oflush 2894 note nc counts served connections; it reaches mx only when MAXCONN was given 2895 nc = ulong 0 2896 while lt nc mx bound `9223372036854775807` 2897 c = svwait sv `60000000000` 2898 if ge c 0 2899 svconn c cs io bd wb sv 2900 nc = add nc 1 2901 end 2902 end 2903 y = fclose int lf 2904 end