Skip to main content

Embed Text Chunk

When working with individual text chunks, you need to convert them into numerical vectors for similarity search. The embed method transforms a single text chunk into an embedding using your chosen provider and model.

import { MDocument } from "@mastra/rag";
import { embed } from "ai";
import { ModelRouterEmbeddingModel } from "@mastra/core/llm";

const doc = MDocument.fromText("Your text content...");

const chunks = await doc.chunk();

const { embedding } = await embed({
model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
value: chunks[0].text,
});





View source on GitHub