メインコンテンツへ移動

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 スキーマを使用した構造化メタデータ抽出を有効にします。

Extractor の引数
Extractor の引数への直接リンク

TitleExtractorsArgs
titleextractorsargsへの直接リンク

llm?:

MastraLanguageModel
タイトル抽出に使用する AI SDK 言語モデル

nodes?:

number
抽出するタイトルノードの数

nodeTemplate?:

string
タイトルノード抽出用のカスタムプロンプトテンプレート。{context} プレースホルダーを含める必要があります

combineTemplate?:

string
タイトル結合用のカスタムプロンプトテンプレート。{context} プレースホルダーを含める必要があります

SummaryExtractArgs
summaryextractargsへの直接リンク

llm?:

MastraLanguageModel
要約抽出に使用する AI SDK 言語モデル

summaries?:

('self' | 'prev' | 'next')[]
生成する要約タイプのリスト。'self'(現在のチャンク)、'prev'(前のチャンク)、'next'(次のチャンク)のみを指定できます

promptTemplate?:

string
要約生成用のカスタムプロンプトテンプレート。{context} プレースホルダーを含める必要があります

QuestionAnswerExtractArgs
questionanswerextractargsへの直接リンク

llm?:

MastraLanguageModel
質問生成に使用する AI SDK 言語モデル

questions?:

number
生成する質問の数

promptTemplate?:

string
質問生成用のカスタムプロンプトテンプレート。{context} と {numQuestions} の両方のプレースホルダーを含める必要があります

embeddingOnly?:

boolean
true の場合、実際の質問は生成せず、埋め込みのみを生成します

KeywordExtractArgs
keywordextractargsへの直接リンク

llm?:

MastraLanguageModel
キーワード抽出に使用する AI SDK 言語モデル

keywords?:

number
抽出するキーワードの数

promptTemplate?:

string
キーワード抽出用のカスタムプロンプトテンプレート。{context} と {maxKeywords} の両方のプレースホルダーを含める必要があります

SchemaExtractArgs
schemaextractargsへの直接リンク

schema:

ZodType
抽出するデータの構造を定義する Zod スキーマ。

llm?:

MastraLanguageModel
抽出に使用する AI SDK 言語モデル。

instructions?:

string
抽出対象を LLM に指示する内容。

metadataKey?:

string
抽出結果を格納するネスト先のキー。省略した場合、結果はメタデータオブジェクトに展開されます。

高度な例
高度な例への直接リンク

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.