Overview of decision functions
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
-
Requires Ververica Runtime (VVR) 11.10 or later.
-
Before you use these functions, activate the Flink AI service (built-in models).
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 |
|
|
Specified in the |
Invoked in the |
|
Decision functions |
The |
Invoked in SQL expressions such as |
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-bodymust not containmodel,state, orquestions.
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: |
|
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 |
|
|
Determines the probability that a condition holds. |
An optional object. Use |
|
|
|
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. |
|
|
|
Scores content by ordered levels. |
A required array, ordered from the lowest level to the highest level. |
|
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
STRINGorVARIANTtype is supported.VARIANTvalues can be JSON strings, objects, or arrays. -
Question definitions: configured by using the
questionsparameter. -
Output column: only one output column is supported. Select a type from the following table as needed.
|
Output column type |
Applicable scenario |
Description |
|
|
Returns the primary answer to a single question. |
|
|
|
Returns the primary answers to one or more questions. |
The field names and their order must match the question IDs in |
|
|
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 |
|
Determines the probability that a given condition holds. |
|
|
Selects one category from the candidate categories. |
|
|
Scores content by custom ordered levels. |
|
|
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 |
|
|
|
|
|
|
-
Successful invocation:
err_msgisNULL. -
Prediction ultimately fails: the answer field is
NULL, anderr_msgcontains 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.