TypeScript Quick Start

This guide will help you get started with using datasets in TypeScript.

Installation

First, install the Autoblocks client:

npm install @autoblocks/client

Setup

Set your Autoblocks API key from the settings page as an environment variable:

export AUTOBLOCKS_API_KEY=...

Add the datasets generate command to your package.json scripts to generate types for your datasets:

"scripts": {
  "gen": "datasets generate",
  "type-check": "npm run gen && tsc --noEmit"
}

Basic Usage

Here’s how to fetch a dataset and use it in your code:

import { AutoblocksAPIClient } from '@autoblocks/client';

const client = new AutoblocksAPIClient({
  apiKey: process.env.AUTOBLOCKS_API_KEY,
});

const main = async () => {
  // Get the latest revision of a dataset
  const dataset = await client.getDataset({
    name: 'My Dataset',
    schemaVersion: '1',
  });

  // Get a specific revision
  const pinnedDataset = await client.getDataset({
    name: 'My Dataset',
    schemaVersion: '1',
    revisionId: '123',
  });

  // Get a subset of the dataset using splits
  const splitDataset = await client.getDataset({
    name: 'My Dataset',
    schemaVersion: '1',
    splits: ['split-1'],
  });

  console.log(dataset);
};

main();

Working with Dataset Splits

Dataset splits allow you to divide your dataset into smaller, more manageable pieces. This is useful for creating subsets of your dataset for different testing scenarios.

// Get a specific split
const trainingSplit = await client.getDataset({
  name: 'My Dataset',
  schemaVersion: '1',
  splits: ['training'],
});

// Get multiple splits
const testSplits = await client.getDataset({
  name: 'My Dataset',
  schemaVersion: '1',
  splits: ['test-1', 'test-2'],
});

Using Datasets with Test Suites

You can use datasets with test suites to associate test results with dataset items:

import { AutoblocksAPIClient } from '@autoblocks/client';
import { runTestSuite } from '@autoblocks/client/testing';

const client = new AutoblocksAPIClient({
  apiKey: process.env.AUTOBLOCKS_API_KEY,
});

interface TestCase {
  input: string;
  datasetItemId: string;
}

const main = async () => {
  const dataset = await client.getDataset({
    name: 'My Dataset',
    schemaVersion: '1',
  });

  await runTestSuite<TestCase, string>({
    id: 'my-test-suite',
    testCases: dataset.items.map((item) => ({
      datasetItemId: item.revisionId,
      ...item.data,
    })),
    testCaseHash: ['input'],
    fn: ({ testCase }) => testCase.input, // Replace with your LLM call
    evaluators: [], // Replace with your evaluators
  });
};

main();

Next Steps