MDocument
The MDocument class processes documents for RAG applications. The main methods are .chunk() and .extractMetadata().
ConstructorConstructor的直接链接
docs:
Array<{ text: string, metadata?: Record<string, any> }>
Array of document chunks with their text content and optional metadata
type:
string
Type of document content. Recognized values are 'text', 'html', 'markdown', 'json', and 'latex'.
Static methodsStatic methods的直接链接
fromText()fromtext的直接链接
Creates a document from plain text content.
static fromText(text: string, metadata?: Record<string, any>): MDocument
fromHTML()fromhtml的直接链接
Creates a document from HTML content.
static fromHTML(html: string, metadata?: Record<string, any>): MDocument
fromMarkdown()frommarkdown的直接链接
Creates a document from Markdown content.
static fromMarkdown(markdown: string, metadata?: Record<string, any>): MDocument
fromJSON()fromjson的直接链接
Creates a document from JSON content.
static fromJSON(json: string, metadata?: Record<string, any>): MDocument
Instance methodsInstance methods的直接链接
chunk()chunk的直接链接
Splits document into chunks and optionally extracts metadata.
async chunk(
params?: ChunkParams,
options?: { observabilityContext?: ObservabilityContext },
): Promise<Chunk[]>
Pass observabilityContext to attach the chunking and metadata extraction spans to an existing trace.
See chunk() reference for detailed options.
getDocs()getdocs的直接链接
Returns array of processed document chunks.
getDocs(): Chunk[]
getText()gettext的直接链接
Returns array of text strings from chunks.
getText(): string[]
getMetadata()getmetadata的直接链接
Returns array of metadata objects from chunks.
getMetadata(): Record<string, any>[]
extractMetadata()extractmetadata的直接链接
Extracts metadata using specified extractors. See ExtractParams reference for details.
async extractMetadata(params: ExtractParams): Promise<MDocument>
ExamplesExamples的直接链接
import { MDocument } from '@mastra/rag'
// Create document from text
const doc = MDocument.fromText('Your content here')
// Split into chunks with metadata extraction
const chunks = await doc.chunk({
strategy: 'markdown',
headers: [
['#', 'title'],
['##', 'section'],
],
extract: {
summary: true, // Extract summaries with default settings
keywords: true, // Extract keywords with default settings
},
})
// Get processed chunks
const docs = doc.getDocs()
const texts = doc.getText()
const metadata = doc.getMetadata()