Introduction to JavaScript SDK: Embed AI in Web Applications

Cover Image for Introduction to JavaScript SDK: Embed AI in Web Applications

We are excited to introduce to our community a new way of interacting with MindsDB via the JavaScript SDK. You can create, train, and use ML models directly from the JavaScript code. Read along to learn how to leverage the power of AI in your web applications.

In this blog post, we introduce the JavaScript SDK. It enables you to perform most of the MindsDB operations inside the JavaScript code, including connecting databases, training and querying models, and more.

How to Use JavaScript SDK

Before we can use the JS SDK, we must install it by running the following command:

npm install --save mindsdb-js-sdk

Another way is to clone the JS SDK repository and install all dependencies manually.

Once installation of the package succeeds, the next step is to connect MindsDB. You can connect either your local installation or your MindsDB Cloud account, as below.

const MindsDB = require("mindsdb-js-sdk").default;

try {

  await MindsDB.connect({
    user: 'user@email.com',
    password: 'password'
  });
  console.log('connected');

} catch(error) {
  // Failed to authenticate
  console.log(error);
}

Please visit our documentation for details.

Forecasting House Sales using a Time Series Model with JavaScript SDK

JavaScript SDK provides functions to interact with databases, tables, projects, models, and views. These include listing available objects, fetching objects with the get method, creating new objects, making predictions, and more.

Let’s go over an example of creating and training a time series model, and then, using it to make batch predictions.

First, we need to define the training options, including training data, columns used to order and group data, the window clause defining how many rows to look back at, and the horizon clause defining how many data records to forecast.

// Defining training options
const timeSeriesTrainingOptions = {
  integration: 'example_db',
  select: 'SELECT * FROM demo_data.house_sales',
  orderBy: 'saledate',
  groupBy: 'bedrooms',
  window: 8,
  horizon: 4
}

We use the trainModel function to create and train a model. Its first argument is the model name. The second argument stores a column to be predicted. And in the third argument, we pass all training options defined above.

// Creating and training a model
let houseSalesForecastModel = await MindsDB.Models.trainModel(
  'house_sales_model',
  'ma',
  'mindsdb',
  timeSeriesTrainingOptions);

It may take some time to train the model. Here is how to check the model status until it finishes the training phase.

// Waiting for the training to be complete
while (houseSalesForecastModel.status !== 'complete' && houseSalesForecastModel.status !== 'error') {
  houseSalesForecastModel = await MindsDB.Models.getModel('house_sales_model', 'mindsdb');
}

// Checking model's status
console.log('Model status: ' + houseSalesForecastModel.status);

You can also describe a model. It is equivalent to using the DESCRIBE statement.

// Describing a model
const modelDescription = await houseSalesForecastModel.describe();
console.log('Model description:');
console.log(modelDescription

Now that the model is ready, we can make predictions. In the case of time series models, we make batch predictions by joining the data table with the model, where the data table serves as input data for the model.

// Defining query options
const queryOptions = {
  // Join model to this data source
  join: 'example_db.demo_data.house_sales',
  // When using batch queries, the 't' alias is used for the joined data source ('t' is short for training/test)
  // The 'm' alias is used for the trained model to be queried
  where: ['t.saledate > LATEST', 't.bedrooms = 2'],
  limit: 4
}

We use the batchQuery function to query for batch predictions.

// Querying for batch predictions
const rentalPriceForecasts = await houseSalesForecastModel.batchQuery(queryOptions);
console.log('Batch predictions:');
rentalPriceForecasts.forEach(f => {
  console.log(f.value);
  console.log(f.explain);
  console.log(f.data);
})

Visit our documentation to learn more about all functions available in the JS SDK.

What’s Next

If you are already familiar with MinsdDB, we encourage you to try interacting with MindsDB via the JavaScript SDK. It enables you to create and train models and make predictions right inside your web applications.

And if you are new to MindsDB, go ahead and create a demo account to explore MindsDB Cloud. Join our Slack community to ask questions and share feedback.