跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

DatasetsManager

Added in: @mastra/core@1.4.0

The DatasetsManager class provides the public API for managing datasets, including CRUD operations and cross-dataset experiment comparisons. Access it via mastra.datasets.

Usage examples
Usage examples的直接链接

Create and list datasets
Create and list datasets的直接链接

src/mastra/datasets/manage.ts
import { Mastra } from '@mastra/core'

const mastra = new Mastra({/* storage config */})

// Create a dataset
const dataset = await mastra.datasets.create({
name: 'QA pairs',
description: 'Question-answer evaluation set',
})

// List all datasets
const { datasets } = await mastra.datasets.list()

Get and delete a dataset
Get and delete a dataset的直接链接

src/mastra/datasets/get-delete.ts
import { Mastra } from '@mastra/core'

const mastra = new Mastra({/* storage config */})

// Get by ID
const dataset = await mastra.datasets.get({ id: 'dataset-id' })

// Delete by ID
await mastra.datasets.delete({ id: 'dataset-id' })

Get experiment
Get experiment的直接链接

Retrieves a specific experiment (run) by ID. Unlike dataset.getExperiment(), this works across all datasets without needing a dataset reference first.

import { Mastra } from '@mastra/core'

const mastra = new Mastra({/* storage config */})

// Get experiment directly without knowing the dataset
const experiment = await mastra.datasets.getExperiment({
experimentId: 'exp-id',
})
if (!experiment) throw new Error('Experiment not found')

console.log(`Dataset: ${experiment.datasetId}`)
console.log(`Status: ${experiment.status}`)

Delete experiment
Delete experiment的直接链接

Deletes an experiment directly by ID, including experiments orphaned by dataset deletion. Mastra deletes the experiment's observability traces first, then the experiment and its result records. Unsupported observability storage leaves the traces in place and logs a warning.

await mastra.datasets.deleteExperiment({
experimentId: 'experiment-id',
organizationId: 'organization-id',
projectId: 'project-id',
})

See DatasetsManager.deleteExperiment() for tenancy behavior and trace cascade details.

Compare experiments
Compare experiments的直接链接

src/mastra/datasets/compare.ts
import { Mastra } from '@mastra/core'

const mastra = new Mastra({/* storage config */})

const comparison = await mastra.datasets.compareExperiments({
experimentIds: ['exp-1', 'exp-2'],
baselineId: 'exp-1',
})

Access
Access的直接链接

DatasetsManager isn't instantiated directly. Access it from a Mastra instance:

const mastra = new Mastra({/* storage config */})
const datasetsManager = mastra.datasets