How to create an Aito query
  • 17 Jul 2020
  • 6 Minutes To Read
  • Print
  • Share
  • Dark
    Light

How to create an Aito query

  • Print
  • Share
  • Dark
    Light

Before you start

This article helps you to learn how to write Aito queries. Similar to other databases, the Aito query is used to manipulate and retrieve the data. However, the Aito query also gives you the ability to make predictions or decisions based on the existing data.

It's great if you are already familiar with SQL or with database concepts such as data table, data schema, etc. If not, we recommend you to take a look at the following articles:

The examples in this article use the dataset of our grocery store demo app. The data model is presented below.

Your browser does not support Svg.click here to download

You don't need to run the query along the way but in case you are interested, we have created an instance already populated with the data. The credentials of the instance is as follows:

  • Instance URL: https://aito-grocery-store.aito.app
  • API key: bc4Ck3nDwM1ILVjNahNJL8hPEAzCes8t2vGMUyo9

You can take a look at our get started articles for platform specific instruction on how to run a query.

Let's begin!

The structure of an Aito Query

Essentially, most Aito queries (except for the EVALUATE query which we will cover in a different article) follow the following structure:

from

The from keyword specifies the table you want to get the data from. You can also pull the data from the tables that are linked to this table. For instance:

{
  "from": "impressions"
}

allows you to get the data from the impressions table and and the products and sessions tables since these two tables are linked to the impressions table

where

Similar to a SQL query, the where clause can be use to filter the query. For instance, the following query:

{
  "from": "products",
  "where": {
    "tags": { "$has": "vegetable" }
  }
}

would return all products of which tags has vegetable.

Aito can perform inference (which is exposed at the PREDICT, RECOMMEND, and MATCH API) and the where clause in this case is used to provide the context for the inference. In other words, the where clause can be considered as giving "clues" or "hypotheses" for Aito to answer a tough question. For example, the following query:

{
  "from": "impressions",
  "where": {
    "session.user": "veronica",
    "product.tags": {"$has": "vegetable"}
  },
  "predict": "purchase"
}

can be understood as: "Based on all impressions data, given that the user is veronica and the product is a vegetable, what is the likelyhood that it will be purchased?"

Without the hypothesis, Aito can only rely on the statistics to make inference. For instance, with the following query:

{
  "from": "impressions",
  "predict": "purchase"
}

Aito would return the base probability of purchasing in all impressions data.

Please note that the where clause in inference query does not behave as hard filter and combine all the proposition inside. Instead, Aito will automatically determine if it should use one or multiple propositions to perform statistical reasoning. For instance, in the predict query above, Aito might also look at impressions data in which the user is not "veronica" but they bought a vegetable product.
If you would like to avoid this behavior, please use the $atomic operator

API specific keywords

Each API will have one or several unique keywords which can be considered as arguments. The example above use the predict keyword which is required for the PREDICT API. The APIs and its keywords is listed below. For detailed information, please click on the API name to go to its API documentation:

  • PREDICT: Predict the likelihood of a feature(:::(Internal) (TODO)
    explain what is a feature
    :::) given hypotheses (required at least one hypothesis).

    • predict (required) : specifies the fieldname to be predicted
    • exclusiveness (optional, defaults to true): specifies if the it is single-lable or multi-lable prediction (multi-label if given as false)
  • RECOMMEND: Gives recommendation to maximize a goal

    • recommend (required) : specifies the fieldname to be recommened
    • goal (required) : specifies the goal to be maximized
  • SIMILARITY: Find entries that are similar to the given criteria

    • similarity (required) : specifies the criteria to search for similar entries
  • MATCH: Predict the likelihood (behave differently to predict if the predicting field is a linked field TODO: see more here)

    • match (required) : specifies the fieldname to be predicted
    • basedOn (optional) : restricts hypothesis if the fieldname to be predicted is a linked field
  • RELATE: Provides statistical information of the relationship between features

    • relate (required) : specifies the feature to be related to

select

The select clause is used to determine the fields of the the returned hits. If select is not specified, Aito will return the default fields.

You can also add the $why in the select field for PREDICT, RECOMMEND, MATCH, SIMILARITY query to ask Aito to return its explanation.

orderBy

Sort the returned results by an order. For APIs that return a score (PREDICT, RECOMMEND, SIMILARITY, MATCH), the default order is descending of the score. You can sort by any field in the result hit. For detailed information, please refer to the orderBy API documentation

pagination

Similar to SQL, the pagination is based on the offset and limit keyword

  • offset: skip offset rows before beginning to return the results
  • limit: return only limit rows

The logic behind Aito query

Flexibility and Versatility

In a typical machine learning worflow, you perform model selection and evaluation to find the best model before deployment. The model is usually static and only serve a single purpose. With Aito, instead of selecting the model, you select and evalute to find the best query. While you don't have the option to choose the algorithm, you have full control of providing the context to the model by adjusting the where clause, Furthermore, you can solve different kind of problems with the same data simply by changing the keywords and the API endpoint (with the advanced GENERIC QUERY, you don't even have to change the API endpoint). This flexibility allows you to solve different problems without having to maintain multiple models. For instance:

  • Given a product's specification, predict the likelyhood that it would be purchase by a generic user:

    {
      "from": "impressions", 
      "where": {
        "product.name": "bread",
        "product.price": { "$lt": 1 }
      },
      "predict": "purchase"
    }
    
  • Recommend the product that a user would most likely purchase:

    {
      "from": "impressions",
      "where": {
        "session.user": "veronica"
      },
      "recommend": "product",
      "goal": { "purchase": true }
    }
    
  • Find a similar product that a user has bought before:

{
  "from": "impressions",
  "where": {
    "session.user": "veronica",
    "purchase": true
  },
  "similarity": {
    "product.name": "rye bread", 
    "product.tags": "gluten free"
  }
}

Inference query

Aito solves machine learning problem using Bayesian inference. Essentially, based on sampling data and prior knowledge provided by the user, Aito can find the probability of an event. An Aito query describes the sampling data, the prior knowledge and what is the event in terms of the databased model. For example, in the following query:

{
  "from": "impressions",
  "where": {
    "product.tags": "vegetable",
  },
  "predict": "purchase"
}

the sampling data is all entries in the impressions table, the prior knowledge is that the product is a vegetable, and the event is wheter the product would be bought.

Let's revert the problem. We would like to find the tags of the product based on all products data, given then product name is "Chiquita banana". In this case, the prior knowledge can be defined as: { "name": "Chiquita banana" } and the event can be describe as "predict": tags. The final query would be:

{
  "from": "products",
  "where": {
    "name": "Chiquita banana",
  },
  "predict": "tags"
}

In the advanced articles, we will discover how to describe more complex logic, such as narrowing down the sample data.

Was This Article Helpful?