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 { openai } from "@ai-sdk/openai";
import { MDocument } from "@mastra/rag";
import { embed } from "ai";

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

const chunks = await doc.chunk();

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





View source on GitHub