Skip to Content
リファレンスツールcreateDocumentChunkerTool()

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} オーバーラップでドキュメントをチャンクします
  • 入力スキーマ: 空のオブジェクト(追加の入力は不要)
  • 出力スキーマ: チャンクの配列を含むオブジェクト

関連