@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
82 lines (57 loc) • 2.21 kB
Markdown
# Dataset class
**Added in:** `@mastra/core@1.4.0`
The `Dataset` class provides methods for item CRUD, versioning, and experiment management on a single dataset. Obtained via `mastra.datasets.get()` or `mastra.datasets.create()`.
## Usage examples
### Add items and run an experiment
```typescript
import { Mastra } from '@mastra/core'
const mastra = new Mastra({
/* storage config */
})
const dataset = await mastra.datasets.create({
name: 'QA pairs',
})
// Add items
await dataset.addItems({
items: [
{ input: { question: 'What is AI?' }, groundTruth: { answer: 'Artificial Intelligence' } },
{ input: { question: 'What is ML?' }, groundTruth: { answer: 'Machine Learning' } },
],
})
// Run an experiment
const summary = await dataset.startExperiment({
targetType: 'agent',
targetId: 'my-agent',
scorers: ['accuracy'],
})
console.log(`${summary.succeededCount}/${summary.totalItems} succeeded`)
```
### Manage versions and history
```typescript
import { Mastra } from '@mastra/core'
const mastra = new Mastra({
/* storage config */
})
const dataset = await mastra.datasets.get({ id: 'dataset-id' })
// List versions
const { versions } = await dataset.listVersions()
// List items at a specific version
const items = await dataset.listItems({ version: 2 })
// Get item change history
const history = await dataset.getItemHistory({ itemId: 'item-id' })
```
## Access
`Dataset` isn't instantiated directly. Obtain one via `DatasetsManager`:
```typescript
const dataset = await mastra.datasets.get({ id: 'dataset-id' })
// or
const dataset = await mastra.datasets.create({ name: 'My dataset' })
```
## Properties
For the full dataset record (name, description, schemas, version, timestamps), call [`dataset.getDetails()`](https://mastra.ai/reference/datasets/getDetails).
**id** (`string`): The unique identifier of the dataset. Read-only.
## Related
- [DatasetsManager class](https://mastra.ai/reference/datasets/datasets-manager)
- [dataset.startExperiment()](https://mastra.ai/reference/datasets/startExperiment)
- [dataset.addItems()](https://mastra.ai/reference/datasets/addItems)
- [dataset.listVersions()](https://mastra.ai/reference/datasets/listVersions)