Principle:23blocks OS Ai maestro Text Embeddings
| Knowledge Sources | |
|---|---|
| Domains | Memory, Embeddings |
| Last Updated | 2026-10-26 10:00 GMT |
Overview
A local embedding generation pattern that runs an ONNX model in a bounded, recyclable child process, producing L2-normalized Float32Array vectors for semantic similarity search.
Description
Text embeddings are generated by a local ONNX model running in a bounded child process (`EmbeddingClient`). The singleton lives on `globalThis` to avoid duplicate ONNX sessions when Next.js bundles the module more than once. Vectors are L2-normalized Float32Arrays, so cosine similarity reduces to a dot product.
The embedding client is recyclable — it stays alive across calls but is bounded in memory. `vectorToBuffer` and `bufferToVector` serialize vectors for CozoDB storage.
Usage
Use this principle when designing an embedding system where:
- Embeddings must be generated locally (no external API calls)
- The model process must be bounded and recyclable (not spawned per call)
- Vectors are L2-normalized for efficient similarity computation
Theoretical Basis
L2 normalization reduces cosine similarity to dot product:
embedTexts(texts) → [Float32Array(texts.length)] // L2-normalized
cosine(a, b) = sum(a[i] * b[i]) // dot product
The singleton-on-globalThis pattern prevents duplicate ONNX sessions when the module is bundled multiple times by Next.js. Each ONNX session loads the model into memory, and duplicates would double the footprint.