Embedding
Embedding is a rule of a computed dataset. It gives each loaded row a vector of its text, made by an AI SDK embedding model, so that a vector search can find rows by meaning.
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { ComputedDataset } from '@eltjs/elt-sqlite';
import { Embedding } from '@eltjs/embedding';
const model = createOpenAICompatible({
name: 'llama.cpp',
baseURL: 'http://127.0.0.1:8080/v1',
}).embeddingModel('embeddinggemma-2');
new ComputedDataset({
into: destination,
name: 'listing_vectors',
description: 'The vector of each listing.',
from: [listings],
compute: new Embedding({
model,
dimensions: 768,
clock: globalThis,
key: 'id',
fields: ['title', 'description'],
}),
});
dimensions is the number of values in each vector of the model, for example 768 for EmbeddingGemma and 1,536 for text-embedding-3-small. The output column vector is a vector column of these dimensions.
clock waits out the limit of each model call. An app gives globalThis, and a test gives a fake clock.
key is the column that identifies a row. It is the key of the output row.
fields are the columns whose text makes the vector. The rule joins their text with line breaks. A row with no text in these columns has no vector.
- The output column
vector holds a UnitVector of @zukhruf/vectors: float32 values with a length of 1, so the cosine similarity of two vectors is their dot product. Each destination keeps it in its own form. SQLite and Postgres keep the bytes of toSql of @zukhruf/vectors/sqlite, which VectorSearch of @zukhruf/embeddings reads.
- The identity of each row is the provider, the id of the model and the dimensions, for example
openai/text-embedding-3-small (1536 dimensions). One model can give vectors of different dimensions, as the dimensions option of OpenAI's text-embedding-3 models does. When you change the model or the dimensions, the next refresh makes every vector again.
Failures
A model failure stays on the output row, and the next pass asks again (ADR 0007 of elt-sqlite).
- When the model answers a call with HTTP 400, the rule splits the call in halves until it finds each text that the model refuses. Only the row of that text fails. An example is a text over the limit of the model.
- Any other failure fails each row of the call. A split would only send more calls to a model that is down.
- One call has 120 seconds, with its retries. At that limit, each row of the call fails, and the next batch runs.
- A stopped refresh fails no row. The next refresh makes those rows.
- The rule sends 64 rows to
embedMany at a time. When the model refuses a call, each row of that call is stored as failed with the error, and the next refresh asks again. The load does not fail.
- A vector with no usable length, because it is all zeros or holds a value that is not finite, has no unit vector. Its row is stored as
failed with the error, the other rows of the call stay computed, and the next refresh asks again.
- A vector of other dimensions than
dimensions does not fit its column. Its row is stored as failed with an error that names both counts, and the next refresh asks again.
- To search, declare a view that gives
id and vector, and give its name to VectorSearch. A search by publish date, such as nearest, also needs published_at. A query without a date range does not.
Give the model its prompts
Some models want a prompt before each text, and a different prompt for a document and for a search text. EmbeddingGemma wants title: none | text: … before a document and task: search result | query: … before a search text. The rule does not know the model, so it adds no prompt. Wrap the model with wrapEmbeddingModel of the AI SDK, one time for documents and one time for search texts:
import { embed, wrapEmbeddingModel } from 'ai';
const gemma = local.embeddingModel('embeddinggemma-2');
const prompted = (modelId: string, prompt: string) =>
wrapEmbeddingModel({
model: gemma,
modelId,
middleware: {
transformParams: async ({ params }) => ({
...params,
values: params.values.map((text) => `${prompt}${text}`),
}),
},
});
const documents = prompted('embeddinggemma-2/document', 'title: none | text: ');
const searches = prompted(
'embeddinggemma-2/query',
'task: search result | query: ',
);
new Embedding({
model: documents,
dimensions: 768,
clock: globalThis,
key: 'id',
fields: ['title', 'description'],
});
const { embedding } = await embed({ model: searches, value: 'family sedan' });
new VectorSearch({ database, table: 'listing_vectors' }).query(embedding);
Give each wrapped model its own modelId. The identity of a row names the provider and the id of the model, so a new prompt with a new modelId makes every vector again.
On 2026-10-27, 42 OpenSooq listings that source-opensooq-web crawled got their vectors from EmbeddingGemma 2 in one run. nearest of a bicycle listing gave other bicycle listings first. A second run made no vector again.
The package does not start a model or a server. Give it a model that is ready. See when the pipeline refreshes for how a pipeline refreshes the rows.