> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # Embed Mastra 使用 AI SDK 的 `embed` 和 `embedMany` 函数为文本输入生成向量嵌入,从而支持相似度搜索和 RAG workflow。 ## 单个嵌入 `embed` 函数为单个文本输入生成向量嵌入: ```typescript 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 }) ``` ### 参数 **model** (`EmbeddingModel`): 要使用的嵌入模型(例如 openai.embedding('text-embedding-3-small')) **value** (`string | Record`): 要嵌入的文本内容或对象 **maxRetries** (`number`): 每次嵌入调用的最大重试次数。设为 0 可禁用重试。 (Default: `2`) **abortSignal** (`AbortSignal`): 用于取消请求的可选中止信号 **headers** (`Record`): 请求的附加 HTTP 标头(仅适用于基于 HTTP 的 Provider) ### 返回值 **embedding** (`number[]`): 输入的嵌入向量 ## 多个嵌入 要一次嵌入多个文本,请使用 `embedMany` 函数: ```typescript 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 }) ``` ### 参数 **model** (`EmbeddingModel`): 要使用的嵌入模型(例如 openai.embedding('text-embedding-3-small')) **values** (`string[] | Record[]`): 要嵌入的文本内容或对象数组 **maxRetries** (`number`): 每次嵌入调用的最大重试次数。设为 0 可禁用重试。 (Default: `2`) **abortSignal** (`AbortSignal`): 用于取消请求的可选中止信号 **headers** (`Record`): 请求的附加 HTTP 标头(仅适用于基于 HTTP 的 Provider) ### 返回值 **embeddings** (`number[][]`): 与输入值对应的嵌入向量数组 ## 使用示例 ```typescript 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', ], }) ``` 有关 Vercel AI SDK 中嵌入的更多详细信息,请参阅: - [AI SDK 嵌入概述](https://sdk.vercel.ai/docs/ai-sdk-core/embeddings) - [embed()](https://sdk.vercel.ai/docs/reference/ai-sdk-core/embed) - [embedMany()](https://sdk.vercel.ai/docs/reference/ai-sdk-core/embed-many)