interplane.Get Omega
Read in your language

omega-mv

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

Brought by drakestapleton

branch main Branches Tags History

omega-mv / train / omega-llm / native / serve.om

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