Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:23blocks OS Ai maestro Text Embeddings

From Leeroopedia
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:

cosine(a,b)=a⋅bwhen ‖a‖=‖b‖=1

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.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment