ExtractParams
ExtractParamsは、LLM分析を使用してドキュメントチャンクからメタデータを抽出するように設定します。
例
import { MDocument } from "@mastra/rag";
const doc = MDocument.fromText(text);
const chunks = await doc.chunk({
extract: {
title: true, // デフォルト設定を使用してタイトルを抽出
summary: true, // デフォルト設定を使用して要約を生成
keywords: true // デフォルト設定を使用してキーワードを抽出
}
});
// 例の出力:
// chunks[0].metadata = {
// documentTitle: "AI Systems Overview",
// sectionSummary: "人工知能の概念と応用の概要",
// excerptKeywords: "キーワード: AI, 機械学習, アルゴリズム"
// }
パラメーター
extract
パラメーターは以下のフィールドを受け入れます:
title?:
boolean | TitleExtractorsArgs
タイトル抽出を有効にします。デフォルト設定には true を設定するか、カスタム設定を提供します。
summary?:
boolean | SummaryExtractArgs
要約抽出を有効にします。デフォルト設定には true を設定するか、カスタム設定を提供します。
questions?:
boolean | QuestionAnswerExtractArgs
質問生成を有効にします。デフォルト設定には true を設定するか、カスタム設定を提供します。
keywords?:
boolean | KeywordExtractArgs
キーワード抽出を有効にします。デフォルト設定には true を設定するか、カスタム設定を提供します。
抽出引数
TitleExtractorsArgs
llm?:
MastraLanguageModel
タイトル抽出に使用するAI SDK言語モデル
nodes?:
number
抽出するタイトルノードの数
nodeTemplate?:
string
タイトルノード抽出用のカスタムプロンプトテンプレート。{context}プレースホルダーを含む必要があります
combineTemplate?:
string
タイトルを結合するためのカスタムプロンプトテンプレート。{context}プレースホルダーを含む必要があります
SummaryExtractArgs
llm?:
MastraLanguageModel
要約抽出に使用するAI SDK言語モデル
summaries?:
('self' | 'prev' | 'next')[]
生成する要約タイプのリスト。'self'(現在のチャンク)、'prev'(前のチャンク)、または'next'(次のチャンク)のみを含めることができます
promptTemplate?:
string
要約生成用のカスタムプロンプトテンプレート。{context}プレースホルダーを含む必要があります
QuestionAnswerExtractArgs
llm?:
MastraLanguageModel
質問生成に使用するAI SDK言語モデル
questions?:
number
生成する質問の数
promptTemplate?:
string
質問生成用のカスタムプロンプトテンプレート。{context}と{numQuestions}の両方のプレースホルダーを含む必要があります
embeddingOnly?:
boolean
trueの場合、実際の質問なしで埋め込みのみを生成します
KeywordExtractArgs
llm?:
MastraLanguageModel
キーワード抽出に使用するAI SDK言語モデル
keywords?:
number
抽出するキーワードの数
promptTemplate?:
string
キーワード抽出用のカスタムプロンプトテンプレート。{context}と{maxKeywords}の両方のプレースホルダーを含む必要があります
高度な例
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}"
}
}
});
// 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"
// }
タイトル抽出のためのドキュメントグループ化
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 });
// 最初の2つのチャンクは同じタイトルを共有し、3番目のチャンクには別のタイトルが割り当てられます。