Ternlight: 7 MB Browser-Based Embedding Model for Local Semantic Search
Ternlight is a high-performance, client-side embedding model designed to bring semantic search to the web browser without requiring an API or server-side infrastructure. By leveraging WebAssembly (WASM) and ternary quantization-aware training, the project provides a compact footprint (5-7 MB) and near-instant inference times (~2.5ms to 5ms per embedding).
Technical Architecture and Implementation
Ternlight is not a Large Language Model (LLM), but a sentence encoder that transforms text into a 384-dimensional vector. This allows developers to calculate cosine similarity between vectors to determine the semantic relationship between two pieces of text, regardless of whether they share common keywords.
Core Engineering
- Distillation and Quantization: The model was distilled from MiniLM using ternary quantization-aware training to minimize size while maintaining utility.
- Inference Engine: The inference engine was written from scratch in Rust and compiled to WASM with SIMD (Single Instruction, Multiple Data) support to maximize CPU performance.
- Deployment: The project is distributed as a single npm package, eliminating the need for separate model download steps or external server calls.
Model Variants
There are two tiers available via npm:
- @ternlight/base: A 7 MB model providing more capable embeddings with an average inference time of ~5 ms.
- @ternlight/mini: A 5 MB model optimized for size, with an average inference time of ~2.5 ms. This variant uses 256-element vectors internally and projects them up to 384 for compatibility.
Integration and Usage
Developers can implement semantic search in three lines of code using the @ternlight/base package. The library provides embed and similar functions to enable ranked matches based on semantic similarity.
import { embed, similar } from '@ternlight/base';
similar('easy weeknight dinner ideas', recipes, { topK: 3 });
// → ranked matches · ~5 ms · zero network
Use Cases and Community Insights
The community has identified several high-value applications for on-device embeddings, ranging from privacy-focused search to hybrid retrieval systems.
Local Semantic Search and Privacy
Local execution ensures that user queries remain on the device, providing inherent privacy and reducing latency. This is particularly useful for fast, "search-as-you-type" experiences in product bases or documentation sites.
Hybrid Retrieval Systems
Technical discussions suggest combining Ternlight with other browser-based tools to create sophisticated search architectures. For example, combining Ternlight's semantic search with SQLite's FTS5/BM25 (Full Text Search) via tools like absurder-sql allows for Reciprocal Rank Fusion (RRF), enabling hybrid retrieval that combines keyword and semantic matching entirely on the client side.
Distributed Search Ecosystems
Some users suggest integrating Ternlight with portable HNSW (Hierarchical Navigable Small World) search and Parquet files to create a distributed search ecosystem that is not dependent on centralized corporate APIs.
Performance Considerations
While the highly optimized WASM engine provides fast inference, some users have noted that the initial loading of the model can be CPU-intensive, causing system fans to trigger on some devices. Additionally, while the mini variant is highly efficient, some users have reported that the accuracy of similarity search may vary depending on the complex nature of the query.
Sources
Related
- Dispatch
- Dispatch
- Dispatch
- Dispatch
- Project