Embed
Mastra uses the AI SDK's embed and embedMany functions to generate vector embeddings for text inputs, enabling similarity search and RAG workflows.
Supported embedding modelsSupported embedding models的直接链接
Memory and RAG vector query tools accept embedding models with specification versions v1, v2, v3, and v4. Mastra selects the matching AI SDK implementation from the model's specificationVersion. Existing model configurations remain unchanged.
For direct calls, use the matching export from @mastra/core/vector: embedV1, embedV2, embedV3, or embedV4. For example, an AI SDK v7 provider exposes a specification v4 embedding model:
import { openai } from '@ai-sdk/openai'
import { embedV4 } from '@mastra/core/vector'
const { embedding } = await embedV4({
model: openai.embedding('text-embedding-3-small'),
value: 'Your text to embed',
})
This example requires @ai-sdk/openai v4. The model specification version and AI SDK package version are different: AI SDK v4 uses embedding specification v1, v5 uses v2, v6 uses v3, and v7 uses v4.
Single embeddingSingle embedding的直接链接
The embed function generates a vector embedding for a single text input:
import { embed } from 'ai'
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
const result = await embed({
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
value: 'Your text to embed',
maxRetries: 2, // optional, defaults to 2
})
ParametersParameters的直接链接
model:
value:
maxRetries?:
abortSignal?:
headers?:
Return valueReturn value的直接链接
embedding:
Multiple embeddingsMultiple embeddings的直接链接
For embedding multiple texts at once, use the embedMany function:
import { embedMany } from 'ai'
const result = await embedMany({
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
values: ['First text', 'Second text', 'Third text'],
maxRetries: 2, // optional, defaults to 2
})
ParametersParameters的直接链接
model:
values:
maxRetries?:
abortSignal?:
headers?:
Return valueReturn value的直接链接
embeddings:
Example usageExample usage的直接链接
import { embed, embedMany } from 'ai'
// Single embedding
const singleResult = await embed({
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
value: 'What is the meaning of life?',
})
// Multiple embeddings
const multipleResult = await embedMany({
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
values: [
'First question about life',
'Second question about universe',
'Third question about everything',
],
})
For more detailed information about embeddings in the Vercel AI SDK, see: