Category selection

Updated at:

This topic describes how to use AI_CHOICE to select one category from the given candidate categories by using a decision model.

Limitations

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

Syntax

AI_CHOICE(
    MODEL => MODEL <model_name>,
    STATE => <state>,
    QUESTION => <question>,
    CRITERIA => <criteria>
    [, DETAILS => <details>]
    [, 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 classified.

QUESTION

STRING

Yes

None

A non-NULL string constant that describes the classification goal.

CRITERIA

MAP<STRING, STRING>

Yes

None

The candidate categories and their descriptions. Each key is a category label and each value is a category description. Both must be string constants. The MAP cannot be empty, and the labels cannot be duplicated.

DETAILS

BOOLEAN

No

FALSE

Specifies whether to return the full answer. The value must be a Boolean literal.

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.

A category description explains the business meaning of a label. For example, you can set the label to "Logistics" and the description to "Delivery, pickup, and parcel collection issues".

Return values

Returns STRING, which indicates the category label selected by the model.

When DETAILS => TRUE is set, the function returns VARIANT, which can contain details such as the category selection result, the confidence level, and the probability of each candidate category. 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

Test statements

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

SELECT
    id,
    AI_CHOICE(
        MODEL => MODEL jev_model,
        STATE => content,
        QUESTION => 'Which team should handle the unresolved issue?',
        CRITERIA => MAP[
            'After-sales', 'Returns, exchanges, and product quality issues',
            'Logistics', 'Delivery, collection, and pickup issues',
            'Other', 'Other customer service requests'
        ]
    ) AS category
FROM tickets;

Sample output

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

id

category

1

After-sales

2

Logistics

Retrieve detailed results

The following example uses the model and the view created in the preceding sections, and retrieves the full answer by setting DETAILS => TRUE.

CREATE TEMPORARY VIEW category_results AS
SELECT
    id,
    AI_CHOICE(
        MODEL => MODEL jev_model,
        STATE => content,
        QUESTION => 'Which team should handle the unresolved issue?',
        CRITERIA => MAP[
            'After-sales', 'Returns, exchanges, and product quality issues',
            'Logistics', 'Delivery, collection, and pickup issues',
            'Other', 'Other customer service requests'
        ],
        DETAILS => TRUE
    ) AS decision_result
FROM tickets;

SELECT
    id,
    decision_result
FROM category_results;

References