AI_CLASSIFY は MaxCompute の AI 関数で、モデルを呼び出して、指定された候補リストの中から入力に最も一致するラベルを返します。
構文
STRING AI_CLASSIFY(
STRING <model_name>,
STRING <version_name>,
STRING <input>,
ARRAY<STRING> <labels>
[, STRING <model_parameters>]
);
パラメーター
-
model_name:必須。使用するモデルの名前を指定する文字列です。詳細については、「SQL AI 関数」をご参照ください。
-
version_name:必須。使用するモデルのバージョンを指定する文字列です。デフォルトのバージョンを使用するには、
DEFAULT_VERSIONを指定します。 -
input:必須。分類するテキストを含む文字列です。
-
labels:必須。候補となる文字列ラベルの配列です。このパラメーターには、定数または列名を指定できます。定数を使用する場合、2 ~ 20 個のラベルを含める必要があります。
-
model_parameters:任意。モデルパラメーターを JSON 形式で指定する文字列です。
max_tokens、temperature、top_pなどを指定できます。例:'{"max_tokens": 500, "temperature": 0.6, "top_p": 0.95}'。-
max_tokens:1 回のモデル呼び出しで生成されるトークンの最大数です。MaxCompute パブリックモデルのデフォルト値は 4096 です。
-
temperature:0 ~ 1 の値で、モデルの出力のランダム性を制御します。値が大きいほど、より創造的で多様な出力になり、値が小さいほど、より確定的で保守的な出力になります。
-
top_p:0 ~ 1 の値で、モデルが考慮する候補ラベルの範囲を制限します。値が大きいほど、範囲が広がり多様性が増し、値が小さいほど、範囲が狭まり焦点が絞られた出力になります。
-
戻り値
入力に最も一致する単一のラベルを表す文字列を返します。
-
inputがSTRING型でない場合、またはlabelsがARRAY<STRING>型でない場合は、エラーを返します。 -
labelsが定数で、含まれるラベルが 2 個未満または 20 個を超える場合は、エラーを返します。 -
inputまたはlabelsがNULLまたは空の文字列 ("") の場合は、NULLを返します。
例
例 1:定数テキストの分類
この例では、MaxCompute が提供する qwen3-max パブリックモデルを呼び出して、入力テキストを分類し、最も一致するラベルを返します。
-- SQL ジョブでモデルコンピューティングサービスを使用
SET odps.task.major.version=sql_modelstudio;
SET odps.namespace.schema=true;
SELECT AI_CLASSIFY(
bigdata_public_modelset.default.`qwen3-max`,
DEFAULT_VERSION,
'MaxCompute is a fully managed, high-performance big data computing platform that provides fast and scalable data warehousing and analytics capabilities.',
ARRAY('Technology', 'Sports', 'Finance', 'Healthcare', 'Education')
) AS classified_label;
-- 結果
+------------------+
| classified_label |
+------------------+
| Technology |
+------------------+
例 2:テーブル内のデータの分類
この例では、MaxCompute が提供する Qwen3-4B-GGUF パブリックモデルを呼び出して、テーブル内の複数のテキストデータを一括で分類します。
-- サンプルデータ
CREATE TABLE news_articles (
content STRING
);
INSERT INTO news_articles VALUES
('Artificial intelligence is transforming the healthcare industry with new diagnostic tools.'),
('The stock market hit a new all-time high today, driven by a rally in the tech sector.'),
('The team won the championship after a thrilling overtime game.'),
('Cloud computing allows businesses to scale their infrastructure on demand.');
-- モデルを呼び出してテーブル内のテキストを分類
SET odps.sql.ai.treat.as.common.model=true;
SET odps.namespace.schema=true;
SELECT
content,
AI_CLASSIFY(
bigdata_public_modelset.default.`Qwen3-4B-GGUF`,
DEFAULT_VERSION,
content,
ARRAY('Technology', 'Sports', 'Finance', 'Healthcare')
) AS category
FROM news_articles;
-- 結果
+------------------------------------------------------------------------------------------+------------+
| content | category |
+------------------------------------------------------------------------------------------+------------+
| Artificial intelligence is transforming the healthcare industry with new diagnostic tools. | Healthcare |
| The stock market hit a new all-time high today, driven by a rally in the tech sector. | Finance |
| The team won the championship after a thrilling overtime game. | Sports |
| Cloud computing allows businesses to scale their infrastructure on demand. | Technology |
+------------------------------------------------------------------------------------------+------------+