Overview of decision functions

Updated at:

This topic describes how to invoke a decision model in SQL, how to define questions, and how to use decision functions and handle errors.

Limitations

Invocation methods

You can configure questions in a model and then use ML_PREDICT to invoke the model, or specify questions in a decision function.

Invocation method

Where questions are configured

Usage

ML_PREDICT

Specified in the WITH parameter of the model, as the questions key.

Invoked in the FROM clause. Returns the input columns and the prediction result column.

Decision functions

The QUESTION and CRITERIA parameters of the function.

Invoked in SQL expressions such as SELECT and WHERE.

For information about how to configure invocation-level settings, see General invocation. When you use a decision model, note the following requirements.

  • Only asynchronous invocation is supported.

  • When you use ML_PREDICT, do not enable invocation-level or global bundle configurations.

  • Decision functions do not support invocation-level bundle configurations and are not affected by global bundle configurations.

  • extra-body must not contain model, state, or questions.

Question definitions

questions uses a JSON object to define one or more questions. Each key is a custom question ID, and each value is the definition of a question. AI_DECIDE uses the same format for its QUESTION parameter.

Field

Required

Description

type

Yes

The question type. Valid values: noul, choice, and score.

instructions

No

The description of the question or the evaluation criteria. A string, a JSON object, or an array is supported.

criteria

Conditionally required

The candidate categories or scoring levels. The format depends on the question type.

Question type

Purpose

criteria format

Primary answer

noul

Determines the probability that a condition holds.

An optional object. Use false and true to describe the cases in which the condition does not hold and holds, respectively.

DOUBLE, in the range of 0 to 1.

choice

Selects one category from the candidate categories.

A required object. Each key is a category label, and each value is the description of the category.

STRING, which is the category label selected by the model.

score

Scores content by ordered levels.

A required array, ordered from the lowest level to the highest level.

DOUBLE, where N levels map to the score range 0 to N-1. Decimals are allowed.

For question definitions and related limits, see Model Studio Decision Model API. In "Request parameters", see the questions part.

The following example defines two questions: whether customer service follow-up is required, and which team should handle the request.

{
  "need_help": {
    "type": "noul",
    "instructions": "Does the user still have a request that requires customer service?"
  },
  "category": {
    "type": "choice",
    "instructions": "Which team should handle the request?",
    "criteria": {
      "After-sales": "Returns, exchanges, and product quality issues",
      "Logistics": "Delivery, collection, and pickup issues",
      "Other": "Other customer service requests"
    }
  }
}

Use ML_PREDICT

Model configuration and output types

The model must meet the following configuration requirements.

  • Input column: only one column of the STRING or VARIANT type is supported. VARIANT values can be JSON strings, objects, or arrays.

  • Question definitions: configured by using the questions parameter.

  • Output column: only one output column is supported. Select a type from the following table as needed.

Output column type

Applicable scenario

Description

DOUBLE or STRING

Returns the primary answer to a single question.

questions can contain only one question. Use noul and score with DOUBLE, and choice with STRING.

ROW

Returns the primary answers to one or more questions.

The field names and their order must match the question IDs in questions, and each field type must match the corresponding question type.

VARIANT

Returns the full answer.

Retains the full answer and related details of each question.

Complete invocation example

The following example creates test data and a decision model, and returns the full answer of the VARIANT type. The decision function examples that follow reuse the view and model created here.

CREATE TEMPORARY VIEW tickets(id, content) AS VALUES
    (1, 'The product does not power on. I want to return it.'),
    (2, 'The refund has arrived, but no one has picked up the return.'),
    (3, 'The earlier issue is resolved. Thank you.');

CREATE TEMPORARY MODEL jev_model
INPUT (state STRING)
OUTPUT (answer VARIANT)
WITH (
    'provider' = 'dashscope',
    'task' = 'decision',
    'model' = 'decision-model-preview',
    'questions' = '{
      "need_help": {
        "type": "noul",
        "instructions": "Does the user still have a request that requires customer service?"
      },
      "category": {
        "type": "choice",
        "instructions": "Which team should handle the request?",
        "criteria": {
          "After-sales": "Returns, exchanges, and product quality issues",
          "Logistics": "Delivery, collection, and pickup issues",
          "Other": "Other customer service requests"
        }
      }
    }'
);

SELECT
    id,
    answer
FROM ML_PREDICT(
    TABLE tickets,
    MODEL jev_model,
    DESCRIPTOR(content)
);

A full answer can contain the determination result, confidence, and probability distribution. For the structure and field descriptions, see Model Studio Decision Model API. In "Response parameters", see the answers part. For the general syntax of ML_PREDICT, see General invocation.

Use decision functions

A decision function requires the model to have exactly one output column of the VARIANT type. Each invocation uses the question definitions specified in the function parameters, so the questions parameter of the model can be omitted.

Function

Purpose

Probability assessment (AI_PROB)

Determines the probability that a given condition holds.

Category selection (AI_CHOICE)

Selects one category from the candidate categories.

Business scoring (AI_SCORE)

Scores content by custom ordered levels.

Comprehensive decision (AI_DECIDE)

Defines one or more questions in JSON and returns the answer to each question.

The following example reuses the tickets view created earlier. For the parameters and return values of each function, see the corresponding topic.

CREATE TEMPORARY MODEL jev_model
INPUT (state STRING)
OUTPUT (answer VARIANT)
WITH (
    'provider' = 'dashscope',
    'task' = 'decision',
    'model' = 'decision-model-preview'
);

SELECT
    id,
    AI_PROB(
        MODEL => MODEL jev_model,
        STATE => content,
        QUESTION => 'Does the user still have a request that requires customer service?'
    ) AS need_help,
    AI_CHOICE(
        MODEL => MODEL jev_model,
        STATE => content,
        QUESTION => 'Which team should handle the request?',
        CRITERIA => MAP['After-sales', 'Returns, exchanges, and product quality issues', 'Logistics', 'Delivery, collection, and pickup issues', 'Other', 'Other customer service requests']
    ) AS category,
    AI_SCORE(
        MODEL => MODEL jev_model,
        STATE => content,
        QUESTION => 'How much does the issue affect the user?',
        CRITERIA => ARRAY['No impact on usage', 'Some features affected', 'Unable to continue using the product']
    ) AS impact,
    AI_DECIDE(
        MODEL => MODEL jev_model,
        STATE => content,
        QUESTION => '{
          "need_help": {"type": "noul", "instructions": "Is customer service follow-up still required?"},
          "impact": {"type": "score", "instructions": "How much does the issue affect the user?", "criteria": ["No impact on usage", "Some features affected", "Unable to continue using the product"]}
        }'
    ) AS decisions
FROM tickets;

Error handling

For the conditions that enable error handling, retry configurations, and error categories, see General invocation. The following describes the return structures of the four decision functions.

After FAIL_ON_ERROR => FALSE is set, an error message field is added to the function result. The return types are as follows.

Invocation form

Return type when FAIL_ON_ERROR is FALSE

AI_PROB, AI_CHOICE, and AI_SCORE return the primary answer (of type T)

ROW<answer T, err_msg VARIANT>

AI_DECIDE, or the other three decision functions with DETAILS => TRUE set

ROW<answer VARIANT, err_msg VARIANT>

  • Successful invocation: err_msg is NULL.

  • Prediction ultimately fails: the answer field is NULL, and err_msg contains the error message.

  • Syntax errors and parameter validation errors still cause SQL validation to fail.

The following example returns the probability and the error cause.

CREATE TEMPORARY VIEW followup_results AS
SELECT
    id,
    AI_PROB(
        MODEL => MODEL jev_model,
        STATE => content,
        QUESTION => 'Does the user still have a request that requires customer service?',
        FAIL_ON_ERROR => FALSE
    ) AS decision_result
FROM tickets;

SELECT
    id,
    decision_result.answer AS probability,
    TRY_CAST(decision_result.err_msg['message'] AS STRING) AS error_message
FROM followup_results;

Determine whether a prediction failed based on whether the error field is NULL. Do not rely only on whether the answer is NULL.

References