メインコンテンツへ移動

createDocumentChunkerTool()

createDocumentChunkerTool() 関数は、ドキュメントを小さなチャンクに分割し、効率的に処理、取得するための Tool を作成します。さまざまなチャンク分割戦略と設定可能なパラメーターをサポートしています。

基本的な使用方法
基本的な使用方法への直接リンク

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
= Default chunking parameters
チャンク分割の設定パラメーター

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}`)
})

Tool の詳細
Tool の詳細への直接リンク

チャンク分割 Tool は、次のプロパティを持つ Mastra Tool として作成されます。

  • Tool ID: Document Chunker {strategy} {size}
  • 説明: Chunks document using {strategy} strategy with size {size} and {overlap} overlap
  • 入力スキーマ: 空のオブジェクト(追加の入力は不要)
  • 出力スキーマ: チャンクの配列を含むオブジェクト