In the previous lesson we embedded one string at a time. That works for learning, but if we've got a thousand documents we don't want a thousand round-trips.
embed() accepts a string or a string array. Pass an array, get back an array of vectors.
The array overload of embed() is the same call with text as string[]. Pass a string[] and the batch call returns { embedding: number[][] } like so:
const { embedding: batchEmbeddings } = await embed({
modelId,
text: texts,
});
console.log(`Input: ${texts.length} texts`);
console.log(`Output: ${batchEmbeddings.length} embeddings`);
console.log(`Each embedding dimensions: ${batchEmbeddings[0]!.length}`);Each inner array is one 1024-number vector, in the same order as the input. The model stays loaded, so the second call is much faster than the first (only the embedding step runs).
Note:
batchEmbeddings[0]is the first vector,batchEmbeddings[0][0]is the very first number of that vector. Mind your brackets if you start indexing.
await embed({ modelId, text: texts }) and destructure { embedding: batchEmbeddings }.console.log the input count (texts.length), the output count (batchEmbeddings.length), and the dimension of the first vector (batchEmbeddings[0]!.length).$ Run your code to see results
$