Principle:0xK3vin MegaMemory Local Embeddings
| Knowledge Sources | |
|---|---|
| Domains | Embeddings, Privacy, Edge_Computing |
| Last Updated | 2026-10-27 12:00 GMT |
Overview
A technique for generating text embeddings entirely in-process using a locally-loaded ONNX model, avoiding network calls and API keys after the initial model download.
Description
Local embeddings run a neural embedding model inside the application process rather than calling a remote API (e.g., OpenAI, Cohere). The model weights are downloaded once and cached; subsequent inference runs on the local CPU/GPU. This is enabled by runtimes like transformers.js (which loads ONNX-quantized models via @xenova/transformers) or sentence-transformers in Python.
Problem solved: Remote embedding APIs require network access, API keys, per-call costs, and send user data to a third party. For a knowledge graph tool that embeds queries and concepts frequently during coding sessions, these constraints are onerous. Local embeddings eliminate all four: no network, no keys, no per-call cost, and data stays on the machine.
Context: MegaMemory uses Xenova/all-MiniLM-L6-v2 (a 384-dimensional, ONNX-quantized model) loaded via @xenova/transformers. The model is lazily initialized on first use or preloaded by the web server. Embeddings are mean-pooled and L2-normalized, producing 1536-byte Buffers (384 × float32). No external service is contacted.
Usage
Use this principle when:
- You need embeddings without network dependency (offline, air-gapped, privacy-sensitive)
- You want to avoid per-call API costs for frequent embedding operations
- The embedding dimensionality (384) is sufficient for your retrieval quality needs
- Latency from a local CPU model (tens of ms) is acceptable for your use case
Theoretical Basis
A sentence embedding model maps a variable-length text to a fixed-dimensional vector:
Failed to parse (syntax error): {\displaystyle \phi(\text{text}) = \text{normalize}_{L2}\left(\text{mean\_pool}\left(\text{transformer}(\text{tokens})\right)\right) \in \mathbb{R}^{384} }
Key steps:
- Tokenize the input text into subword tokens
- Encode through a transformer encoder (MiniLM, 6 layers)
- Mean-pool the per-token hidden states into a single vector
- L2-normalize so the vector lies on the unit hypersphere (making dot product = cosine similarity)
Quantization: The ONNX model uses int8 quantization, reducing model size (~23 MB → ~8 MB) and inference latency at a small cost in output precision.
Pseudo-code:
# Abstract algorithm (NOT real implementation)
tokens = tokenizer(text)
hidden_states = transformer_encoder(tokens) # [seq_len, 384]
pooled = mean_pool(hidden_states) # [384]
embedding = l2_normalize(pooled) # [384], unit norm
return embedding