Comprehensive decision

Updated at:

This topic describes how to use AI_DECIDE to ask one or more questions about the same input by using a decision model, and return the full answer to each question.

Limitations

For model registration, execution requirements, and error handling, see Overview of decision functions.

Syntax

AI_DECIDE(
    MODEL => MODEL <model_name>,
    STATE => <state>,
    QUESTION => <questions_json>
    [, CONFIG => <config>]
    [, FAIL_ON_ERROR => <fail_on_error>]
)

Parameters

Parameter

Data type

Required

Default value

Description

MODEL

MODEL

Yes

None

A registered decision model. The output column must be of the VARIANT type.

STATE

STRING or VARIANT

Yes

None

The content to be evaluated.

QUESTION

STRING

Yes

None

A string constant of a non-empty JSON object that uses question IDs as keys. The noul, choice, and score question types are supported.

CONFIG

MAP<STRING, STRING>

No

None

Invocation-level configuration. The keys and values must be string constants. For more information about the configuration method, see General invocation.

FAIL_ON_ERROR

BOOLEAN

No

TRUE

Specifies whether the job reports an error when a prediction ultimately fails. The value must be a Boolean literal.

For the definition format of questions, see Question definitions and Model Studio Decision Model API. In "Request parameters", see the questions part.

Return values

Returns the full answer of the VARIANT type, with question IDs as keys.

For example, the return result for the need_help question is as follows.

{
  "need_help": {"type": "noul", "noul": 0.99}
}

The full answer can contain details such as the decision result, the confidence level, and the probability distribution. For the structure and field descriptions, see Model Studio Decision Model API. In "Response parameters", see the answers part.

For the return structure after FAIL_ON_ERROR => FALSE is set, see Error handling.

Returns NULL when STATE is NULL.

Examples

Prepare the model and test data

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

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.');

Example 1: Answer a single question

The following example uses the model and the view created in the preceding sections.

SELECT
    id,
    AI_DECIDE(
        MODEL => MODEL jev_model,
        STATE => content,
        QUESTION => '{
          "need_help": {
            "type": "noul",
            "instructions": "Does the user still have a request that requires customer service?"
          }
        }'
    ) AS decision_result
FROM tickets;

Example 2: Answer multiple questions

The following example determines whether follow-up is required, which team handles the ticket, and the impact level.

CREATE TEMPORARY VIEW ticket_decisions AS
SELECT
    id,
    AI_DECIDE(
        MODEL => MODEL jev_model,
        STATE => content,
        QUESTION => '{
          "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"
            }
          },
          "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 decision_result
FROM tickets;

SELECT
    id,
    TRY_CAST(decision_result['need_help']['noul'] AS DOUBLE) AS need_help,
    TRY_CAST(decision_result['category']['choice'] AS STRING) AS category,
    TRY_CAST(decision_result['impact']['score'] AS DOUBLE) AS impact
FROM ticket_decisions;

The following is only an example of the output format. The actual results are subject to the response of the model.

id

need_help

category

impact

1

0.99

After-sales

1.98

2

0.98

Logistics

1.20

References