createDocumentChunkerTool()
createDocumentChunkerTool()
関数は、ドキュメントをより効率的に処理・取得するために、小さなチャンクに分割するツールを作成します。さまざまなチャンク化戦略や設定可能なパラメータに対応しています。
基本的な使い方
import { createDocumentChunkerTool, MDocument } from "@mastra/rag";
const document = new MDocument({
text: "Your document content here...",
metadata: { source: "user-manual" },
});
const chunker = createDocumentChunkerTool({
doc: document,
params: {
strategy: "recursive",
size: 512,
overlap: 50,
separator: "\n",
},
});
const { chunks } = await chunker.execute();
パラメーター
doc:
MDocument
チャンク化するドキュメント
params?:
ChunkParams
= デフォルトのチャンク化パラメーター
チャンク化のための設定パラメーター
ChunkParams
strategy?:
'recursive'
= 'recursive'
使用するチャンク化戦略
size?:
number
= 512
各チャンクの目標サイズ(トークン数/文字数)
overlap?:
number
= 50
チャンク間で重複させるトークン数/文字数
separator?:
string
= '\n'
チャンクの区切り文字
戻り値
chunks:
DocumentChunk[]
コンテンツとメタデータを含むドキュメントチャンクの配列
カスタムパラメータを使った例
const technicalDoc = new MDocument({
text: longDocumentContent,
metadata: {
type: "technical",
version: "1.0",
},
});
const chunker = createDocumentChunkerTool({
doc: technicalDoc,
params: {
strategy: "recursive",
size: 1024, // Larger chunks
overlap: 100, // More overlap
separator: "\n\n", // Split on double newlines
},
});
const { chunks } = await chunker.execute();
// Process the chunks
chunks.forEach((chunk, index) => {
console.log(`Chunk ${index + 1} length: ${chunk.content.length}`);
});
ツールの詳細
チャンク化ツールは、以下のプロパティを持つMastraツールとして作成されます。
- ツールID:
Document Chunker {strategy} {size}
- 説明:
{strategy}戦略を使用し、サイズ{size}、オーバーラップ{overlap}でドキュメントをチャンク化します
- 入力スキーマ: 空のオブジェクト(追加の入力は不要)
- 出力スキーマ: チャンク配列を含むオブジェクト