Implementation:23blocks OS Ai maestro Embed Texts
| Knowledge Sources | |
|---|---|
| Domains | Memory, Embeddings |
| Last Updated | 2026-10-26 10:00 GMT |
Overview
Concrete tool for generating text embeddings via a local ONNX model, provided by AI Maestro's `lib/rag/embeddings.ts` (wrapping `lib/embedding-client.mjs`).
Description
The embedTexts function runs text through a bounded, recyclable child process hosting an ONNX embedding model. The singleton lives on `globalThis` to avoid duplicate ONNX sessions when Next.js bundles the module more than once. Vectors are L2-normalized Float32Arrays. The cosine helper computes similarity between normalized vectors (dot product). vectorToBuffer and bufferToVector serialize vectors for CozoDB storage.
Usage
Call `embedTexts` when generating embeddings for conversation ingestion or search queries. Used by both `indexConversationDelta` and `hybridSearch`/`semanticSearch`.
Code Reference
Source Location
- Repository: ai-maestro
- File: lib/rag/embeddings.ts (L17 embedTexts, L27 cosine, L39 vectorToBuffer, L47 bufferToVector)
- Supporting: lib/embedding-client.mjs (ONNX child process)
Signature
export async function embedTexts(texts: string[]): Promise<Float32Array[]>
export function cosine(a: Float32Array, b: Float32Array): number
export function vectorToBuffer(vec: Float32Array): Buffer
export function bufferToVector(buf: Buffer): Float32Array
Import
import { embedTexts, cosine } from '@/lib/rag/embeddings'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| texts | string[] | Yes | Text strings to embed |
| a, b | Float32Array | Yes (cosine) | L2-normalized vectors |
Outputs
| Name | Type | Description |
|---|---|---|
| embedTexts() returns | Promise<Float32Array[]> | One L2-normalized vector per input text |
| cosine() returns | number | Similarity score (0-1, higher is more similar) |
| vectorToBuffer() returns | Buffer | Serialized for CozoDB storage |
Usage Examples
import { embedTexts, cosine } from '@/lib/rag/embeddings'
// Generate embeddings for a search query
const [queryVec] = await embedTexts(['Deploy the website'])
// Compare with a stored vector
const similarity = cosine(queryVec, storedVec)
console.log(`Similarity: ${similarity}`)
Related Pages
Implements Principle
- Principle:23blocks_OS_Ai_maestro_Text_Embeddings
- Principle:23blocks_OS_Ai_maestro_Conversation_Delta_Indexing
- Principle:23blocks_OS_Ai_maestro_Hybrid_RAG_Search