interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / om / enc.om

482 lines · 8264 bytes Raw Blame History
1note enc.om: client for the embedding encoder (embeddings.rs fetch_embedding).
2note POST <encoder-url>/embed with a 1.5 s budget; 768 numbers become the f32 vector qem.
3note The encoder itself (ONNX bge-base-en-v1.5) is a recorded host dependency (DEPENDENCIES.md).
4note ens: 0 configured (1 yes), 1 port, 2 path len; enh = IPv4 bytes; enp = path prefix
5global ens ulong 4
6global enh uchar 4
7global enp uchar 1024
8global enb uchar 1048576
9global enq uchar 4194304
10global enql ulong 1
11global ena uchar 16
12text en_k1 "{\"modelId\":\"BAAI/bge-base-en-v1.5\",\"modelRevision\":\"cortex-bge-base-en-v1.5-768-v1\",\"priority\":\"recall\",\"text\":"
13text en_post "POST "
14text en_embed "/embed HTTP/1.1"
15text en_host "host: "
16text en_ct "content-type: application/json"
17text en_acc "accept: */*"
18text en_cl "content-length: "
19text en_conn "connection: close"
20text en_http "http://"
21text en_lh "localhost"
22text en_key "embedding"
23text t_sep ": "
24
25note parse --encoder-url bytes in jrs[0..n): http://IPv4|localhost[:port][/path]
26fn enc_config n ulong to void
27put ens 0 0
28if lt n len en_http
29ret
30end
31for i ulong len en_http
32if ne get jrs i get en_http i
33ret
34end
35end
36p = ulong len en_http
37h = p
38while and lt p n and ne get jrs p 58 ne get jrs p 47 bound 999999
39p = add p 1
40end
41if not enc_host h sub p h
42ret
43end
44port = ulong 80
45if and lt p n eq get jrs p 58
46p = add p 1
47port = 0
48while and lt p n ne get jrs p 47 bound 999999
49c = get jrs p
50if or lt c 48 gt c 57
51ret
52end
53port = add mul port 10 ulong sub c 48
54if gt port 65535
55ret
56end
57p = add p 1
58end
59end
60note path without trailing slashes (trim_end_matches("/"))
61e = n
62while and gt e p eq get jrs sub e 1 47 bound 999999
63e = sub e 1
64end
65for i ulong sub e p
66put enp i get jrs add p i
67end
68put ens 2 sub e p
69put ens 1 port
70put ens 0 1
71end
72fn enc_host h ulong n ulong to bool
73if eq n len en_lh
74same = true
75for i ulong n
76if ne get jrs add h i get en_lh i
77same = false
78end
79end
80if same
81put enh 0 127
82put enh 1 0
83put enh 2 0
84put enh 3 1
85ret true
86end
87end
88k = ulong 0
89v = ulong 0
90dg = ulong 0
91for i ulong n
92c = get jrs add h i
93if eq c 46
94if or eq dg 0 ge k 3
95ret false
96end
97put enh k uchar v
98k = add k 1
99v = 0
100dg = 0
101else
102if or lt c 48 gt c 57
103ret false
104end
105v = add mul v 10 ulong sub c 48
106dg = add dg 1
107if gt v 255
108ret false
109end
110end
111end
112if or ne k 3 eq dg 0
113ret false
114end
115put enh 3 uchar v
116ret true
117end
118
119note request body: the JSON payload with text = enq[0..enql)
120fn enc_body to ulong
121put jobl 0 0
122js_raw en_k1
123for i ulong get enql 0
124put jes i get enq i
125end
126js_esc_buf 0 get enql 0
127js_byte 125
128ret get jobl 0
129end
130fn eb_txt o ulong t view uchar * to ulong
131for i ulong len t
132put enb add o i get t i
133end
134ret add o len t
135end
136fn eb_crlf o ulong to ulong
137put enb o 13
138put enb add o 1 10
139ret add o 2
140end
141fn eb_dec o ulong v ulong to ulong
142k = o
143if ge v 10
144k = eb_dec o div v 10
145end
146put enb k uchar add rem v 10 48
147ret add k 1
148end
149note request head for a body of bl bytes; returns head length
150fn enc_head bl ulong to ulong
151o = eb_txt 0 en_post
152for i ulong get ens 2
153put enb add o i get enp i
154end
155o = add o get ens 2
156o = eb_crlf eb_txt o en_embed
157o = eb_txt o en_host
158for k ulong 4
159o = eb_dec o ulong get enh k
160if lt k 3
161put enb o 46
162o = add o 1
163end
164end
165put enb o 58
166o = eb_crlf eb_dec add o 1 get ens 1
167o = eb_crlf eb_txt o en_ct
168o = eb_crlf eb_txt o en_acc
169o = eb_crlf eb_dec eb_txt o en_cl bl
170o = eb_crlf eb_txt o en_conn
171ret eb_crlf o
172end
173
174note fetch the vector for enq; true and qem/qemf set when the encoder answered with 768 numbers
175fn enc_fetch to bool
176put qemf 0 0
177if eq get ens 0 0
178ret false
179end
180dl = add monons 1500000000
181fd = socknew 2 524289 0
182if lt fd 0
183ret false
184end
185ok = enc_talk int fd dl
186x = fclose int fd
187if or not ok ne x 0
188ret false
189end
190ret enc_parse
191end
192note connect (nonblocking, bounded by dl), send head + body, read the whole answer into enb
193fn enc_talk fd int dl long to bool
194put ena 0 2
195put ena 1 0
196p = get ens 1
197put ena 2 uchar shr p 8
198put ena 3 uchar and p 255
199for i ulong 4
200put ena add 4 i get enh i
201end
202r = connect fd ena 0 16
203if and ne r 0 ne r -115
204ret false
205end
206w = waitio fd 4 sub dl monons
207if le w 0
208ret false
209end
210bl = enc_body
211hl = enc_head bl
212if not enc_send fd 0 hl dl
213ret false
214end
215if not enc_sendj fd bl dl
216ret false
217end
218ret enc_recv fd dl
219end
220fn enc_send fd int o ulong n ulong dl long to bool
221k = o
222e = add o n
223while lt k e bound 999999
224w = waitio fd 4 sub dl monons
225if le w 0
226ret false
227end
228s = send fd enb k sub e k 0
229if le s 0
230ret false
231end
232k = add k ulong s
233end
234ret true
235end
236fn enc_sendj fd int n ulong dl long to bool
237k = ulong 0
238while lt k n bound 999999
239w = waitio fd 4 sub dl monons
240if le w 0
241ret false
242end
243s = send fd job k sub n k 0
244if le s 0
245ret false
246end
247k = add k ulong s
248end
249ret true
250end
251note read until the peer closes (we sent connection: close); ens 3 = bytes held
252fn enc_recv fd int dl long to bool
253n = ulong 0
254while true bound 999999
255now = monons
256if ge now dl
257ret false
258end
259w = waitio fd 1 sub dl now
260if le w 0
261ret false
262end
263if ge n len enb
264ret false
265end
266r = recv fd enb n sub len enb n 0
267if lt r 0
268ret false
269end
270if eq r 0
271put ens 3 n
272ret true
273end
274n = add n ulong r
275end
276ret false
277end
278
279fn enc_crlf2 i ulong to bool
280if or ne get enb i 13 ne get enb add i 1 10
281ret false
282end
283ret and eq get enb add i 2 13 eq get enb add i 3 10
284end
285note status 2xx, then the body (content-length or chunked or to EOF) parsed as JSON
286fn enc_parse to bool
287n = get ens 3
288if lt n 12
289ret false
290end
291c = get enb 9
292if ne c 50
293ret false
294end
295he = ulong 0
296i = ulong 0
297while lt add i 3 n bound 999999
298if enc_crlf2 i
299he = add i 4
300break
301end
302i = add i 1
303end
304if eq he 0
305ret false
306end
307bl = enc_body_copy he n
308if eq bl NONE
309ret false
310end
311put jinl 0 bl
312if not js_parse
313ret false
314end
315ret enc_vec
316end
317note copy the body to jin; chunked bodies are de-chunked; returns its length or NONE
318fn enc_body_copy he ulong n ulong to ulong
319if enc_is_chunked he
320ret enc_dechunk he n
321end
322k = sub n he
323if gt k len jin
324ret NONE
325end
326for i ulong k
327put jin i get enb add he i
328end
329ret k
330end
331text en_te "transfer-encoding: chunked"
332fn enc_is_chunked he ulong to bool
333m = len en_te
334i = ulong 0
335while lt add i m he bound 999999
336k = ulong 0
337while lt k m bound 64
338c = get enb add i k
339if and ge c 65 le c 90
340c = add c 32
341end
342if ne c get en_te k
343break
344end
345k = add k 1
346end
347if eq k m
348ret true
349end
350i = add i 1
351end
352ret false
353end
354fn enc_dechunk he ulong n ulong to ulong
355p = he
356o = ulong 0
357while lt p n bound 999999
358sz = ulong 0
359dg = ulong 0
360while lt p n bound 999999
361v = http_hexv get enb p
362if eq v 99
363break
364end
365sz = add mul sz 16 v
366dg = add dg 1
367p = add p 1
368end
369if or eq dg 0 gt sz len jin
370ret NONE
371end
372while and lt p n ne get enb p 10 bound 999999
373p = add p 1
374end
375p = add p 1
376if eq sz 0
377ret o
378end
379if or gt add p sz n gt add o sz len jin
380ret NONE
381end
382for i ulong sz
383put jin add o i get enb add p i
384end
385o = add o sz
386p = add add p sz 2
387end
388ret NONE
389end
390note val.get("embedding") as array; numbers (as_f64) kept, others skipped; exactly 768 needed
391fn enc_vec to bool
392r = get jroot 0
393if ne js_kind r 8
394ret false
395end
396a = js_find r en_key
397if or eq a jnone ne js_kind a 7
398ret false
399end
400k = ulong 0
401for i ulong js_len a
402x = js_child a i
403t = js_kind x
404if and ge t 3 le t 5
405if ge k 768
406ret false
407end
408put qem k f32 enc_num x t
409k = add k 1
410end
411end
412if ne k 768
413ret false
414end
415put qemf 0 1
416ret true
417end
418fn enc_num x ulong t uchar to f64
419v = js_num x
420if eq t 3
421ret f64 v
422end
423if eq t 4
424ret f64 bitcast long v
425end
426ret bitcast f64 v
427end
428
429note recall: the query text; write: "name: content" when content.trim() is not empty, else the name
430fn enc_fetch_q to void
431q = get ap A_Q
432o = rf_off q
433n = rf_len q
434for i ulong n
435put enq i hsg add o i
436end
437put enql 0 n
438ok = enc_fetch
439end
440fn enc_fetch_w to void
441c = get ap A_CONT
442o = rf_off c
443n = rf_len c
444for i ulong n
445put jrs i hc_get add o i
446end
447ru_trim 0 n
448nm = get ap A_NAME
449a = rf_off nm
450m = rf_len nm
451for i ulong m
452put enq i hsg add a i
453end
454k = m
455if gt get jtrl 0 0
456for i ulong len t_sep
457put enq add k i get t_sep i
458end
459k = add k len t_sep
460for i ulong n
461put enq add k i hc_get add o i
462end
463k = add k n
464end
465put enql 0 k
466put ap A_EMB NONE
467if enc_fetch
468put ap A_EMB enc_stage
469end
470end
471note stage qem as 3072 little-endian bytes in hc
472fn enc_stage to ulong
473o = get hct 0
474for i ulong 768
475v = ulong bitcast uint get qem i
476for b ulong 4
477hc_put add add o mul i 4 b uchar and shr v mul b 8 255
478end
479end
480put hct 0 add o 3072
481ret rf_hc o 3072
482end