ExtractParams
ExtractParams 使用 LLM 分析配置从文档块中提取元数据。
示例示例的直接链接
import { MDocument } from '@mastra/rag'
const doc = MDocument.fromText(text)
const chunks = await doc.chunk({
extract: {
title: true, // Extract titles using default settings
summary: true, // Generate summaries using default settings
keywords: true, // Extract keywords using default settings
},
})
// Example output:
// chunks[0].metadata = {
// documentTitle: "AI Systems Overview",
// sectionSummary: "Overview of artificial intelligence concepts and applications",
// excerptKeywords: "KEYWORDS: AI, machine learning, algorithms"
// }
参数参数的直接链接
extract 参数接受以下字段:
title?:
boolean | TitleExtractorsArgs
启用标题提取。设为 true 以使用默认设置,或提供自定义配置。
summary?:
boolean | SummaryExtractArgs
启用摘要提取。设为 true 以使用默认设置,或提供自定义配置。
questions?:
boolean | QuestionAnswerExtractArgs
启用问题生成。设为 true 以使用默认设置,或提供自定义配置。
keywords?:
boolean | KeywordExtractArgs
启用关键词提取。设为 true 以使用默认设置,或提供自定义配置。
schema?:
SchemaExtractArgs
使用 Zod schema 启用结构化元数据提取。
Extractor 参数Extractor 参数的直接链接
TitleExtractorsArgstitleextractorsargs的直接链接
llm?:
MastraLanguageModel
用于标题提取的 AI SDK 语言模型
nodes?:
number
要提取的标题节点数
nodeTemplate?:
string
用于标题节点提取的自定义提示词模板。必须包含 {context} 占位符
combineTemplate?:
string
用于合并标题的自定义提示词模板。必须包含 {context} 占位符
SummaryExtractArgssummaryextractargs的直接链接
llm?:
MastraLanguageModel
用于摘要提取的 AI SDK 语言模型
summaries?:
('self' | 'prev' | 'next')[]
要生成的摘要类型列表。只能包含 'self'(当前块)、'prev'(前一块)或 'next'(后一块)。
promptTemplate?:
string
用于摘要生成的自定义提示词模板。必须包含 {context} 占位符
QuestionAnswerExtractArgsquestionanswerextractargs的直接链接
llm?:
MastraLanguageModel
用于问题生成的 AI SDK 语言模型
questions?:
number
要生成的问题数量
promptTemplate?:
string
用于问题生成的自定义提示词模板。必须同时包含 {context} 和 {numQuestions} 占位符
embeddingOnly?:
boolean
如果为 true,则仅生成嵌入,不生成实际问题
KeywordExtractArgskeywordextractargs的直接链接
llm?:
MastraLanguageModel
用于关键词提取的 AI SDK 语言模型
keywords?:
number
要提取的关键词数量
promptTemplate?:
string
用于关键词提取的自定义提示词模板。必须同时包含 {context} 和 {maxKeywords} 占位符
SchemaExtractArgsschemaextractargs的直接链接
schema:
ZodType
定义要提取数据结构的 Zod schema。
llm?:
MastraLanguageModel
用于提取的 AI SDK 语言模型。
instructions?:
string
向 LLM 说明要提取什么的指令。
metadataKey?:
string
用于嵌套提取结果的键。如果省略,结果会展开到 metadata 对象中。
高级示例高级示例的直接链接
import { MDocument } from '@mastra/rag'
const doc = MDocument.fromText(text)
const chunks = await doc.chunk({
extract: {
// Title extraction with custom settings
title: {
nodes: 2, // Extract 2 title nodes
nodeTemplate: 'Generate a title for this: {context}',
combineTemplate: 'Combine these titles: {context}',
},
// Summary extraction with custom settings
summary: {
summaries: ['self'], // Generate summaries for current chunk
promptTemplate: 'Summarize this: {context}',
},
// Question generation with custom settings
questions: {
questions: 3, // Generate 3 questions
promptTemplate: 'Generate {numQuestions} questions about: {context}',
embeddingOnly: false,
},
// Keyword extraction with custom settings
keywords: {
keywords: 5, // Extract 5 keywords
promptTemplate: 'Extract {maxKeywords} key terms from: {context}',
},
// Schema extraction with Zod
schema: {
schema: z.object({
productName: z.string(),
category: z.enum(['electronics', 'clothing']),
}),
instructions: 'Extract product information.',
metadataKey: 'product',
},
},
})
// Example output:
// chunks[0].metadata = {
// documentTitle: "AI in Modern Computing",
// sectionSummary: "Overview of AI concepts and their applications in computing",
// questionsThisExcerptCanAnswer: "1. What is machine learning?\n2. How do neural networks work?",
// excerptKeywords: "1. Machine learning\n2. Neural networks\n3. Training data",
// product: {
// productName: "Neural Net 2000",
// category: "electronics"
// }
// }
用于标题提取的文档分组用于标题提取的文档分组的直接链接
使用 TitleExtractor 时,可以在每个块的 metadata 字段中指定共享的 docId,以将多个块分组进行标题提取。具有相同 docId 的所有块都会获得相同的提取标题。如果未设置 docId,每个块都会被视为独立文档进行标题提取。
示例:
import { MDocument } from '@mastra/rag'
const doc = new MDocument({
docs: [
{ text: 'chunk 1', metadata: { docId: 'docA' } },
{ text: 'chunk 2', metadata: { docId: 'docA' } },
{ text: 'chunk 3', metadata: { docId: 'docB' } },
],
type: 'text',
})
await doc.extractMetadata({ title: true })
// The first two chunks will share a title, while the third chunk will be assigned a separate title.