适用于:
Databricks SQL
Databricks Runtime
该 ai_classify() 函数根据提供的自定义标签对文本内容进行分类。 可以使用简单的标签名称进行基本分类,或者添加标签说明和说明,以提高客户支持路由、文档分类和内容分析等用例的准确性。
该函数接受来自其他 AI 函数的文本或 VARIANT 输出,例如 ai_parse_document启用可组合工作流。
有关要循环访问 ai_classify的 UI 版本,请参阅 “分类”。
要求
Apache 2.0 许可证
此时可能使用的基础模型根据 Apache 2.0 许可证(版权 © Apache Software Foundation)获得许可。 客户需负责确保遵守适用的模型许可条款。
Databricks 建议查看这些许可证,以确保遵守任何适用的条款。 如果模型在未来根据 Databricks 的内部基准表现更好,Databricks 可能会更改模型(以及本页中提供的适用许可证列表)。
支持此函数的模型是使用模型服务基础模型 API 提供的。 有关 Databricks 上可用的模型以及控制这些模型的使用的许可证和策略的信息,请参阅 适用的模型条款 。
如果将来出现根据 Databricks 的内部基准性能更好的模型,Databricks 可能会更改模型并更新文档。
- 此函数仅在某些区域中可用,请参阅 AI 函数可用性。
- 此函数在 SQL 经典版Azure Databricks不可用。
- 查看 Databricks SQL 定价页。
- 在 Databricks Runtime 15.1 及更高版本中,Databricks 笔记本(包括作为任务在 Databricks 工作流中运行的笔记本)支持此函数。
- 批处理推理工作负荷需要 Databricks Runtime 15.4 ML LTS 来提高性能。
语法
版本 2 (建议)
ai_classify(
content VARIANT | STRING,
labels STRING,
[options MAP<STRING, STRING>]
) RETURNS VARIANT
版本 1
ai_classify(
content STRING,
labels ARRAY<STRING>,
[options MAP<STRING, STRING>]
) RETURNS STRING
参数
版本 2 (建议)
content:VARIANT或STRING表达式。 接受以下任一:- 原始文本作为
STRING -
VARIANT由另一个 AI 函数(例如ai_parse_document或ai_extract)生成的
- 原始文本作为
labels:定义STRING分类标签的文本。 标签可以是:- 简单标签:标签名称的 JSON 数组。
["urgent", "not_urgent"] - 带说明的标签:JSON 对象将标签名称映射到说明。 标签说明必须为 0-1000 个字符。
{ "billing_error": "Payment, invoice, or refund issues", "product_defect": "Any malfunction, bug, or breakage", "account_issue": "Login failures, password resets" }
每个标签必须为 1-100 个字符。
labels必须至少包含 2 个标签,且不超过 500 个标签。- 简单标签:标签名称的 JSON 数组。
options:包含配置选项的可选MAP<STRING, STRING>选项:-
version:版本切换以支持迁移("1.0"对于 v1 行为,"2.0"对于 v2 行为)。 默认值基于输入类型,但会回退到"1.0"。 -
instructions:用于提高分类质量的任务和域的全局说明。 必须少于 20,000 个字符。 -
multilabel:设置为"true"在应用多个类别时返回多个标签。 默认值为"false"(单标签分类)。
-
版本 1
contentSTRING:包含要分类的文本的表达式。labelsARRAY<STRING>:具有预期输出分类标签的文本。 必须至少包含 2 个元素,且不得包含 20 个以上的元素。 每个标签必须为 1-50 个字符。options:包含配置选项的可选MAP<STRING, STRING>选项:-
version:版本切换以支持迁移("1.0"对于 v1 行为,"2.0"对于 v2 行为)。 默认值基于输入类型,但会回退到"1.0"。
-
返回
版本 2 (建议)
返回一个 VARIANT 包含:
{
"response": ["label_name"], // Array with single label (or multiple if multilabel=true)
"error_message": null // null on success, or error message on failure
}
该 response 字段包含:
- 单标签模式 (默认值):包含一个包含最佳匹配标签的元素的数组
-
多标签模式 (
multilabel: "true"):应用多个类别时具有多个标签的数组 - 标签名称与参数中
labels提供的名称完全匹配
如果NULLcontent为NULL或无法对内容进行分类,则返回。
版本 1
返回一个 STRING。 该值与 labels 参数中提供的字符串之一匹配。
如果NULLcontent为NULL或无法对内容进行分类,则返回。
示例
版本 2 (建议)
简单标签 - 仅标签名称
> SELECT ai_classify(
'My password is leaked.',
'["urgent", "not_urgent"]'
);
{
"response": ["urgent"],
"error": null
}
带说明的标签
> SELECT ai_classify(
'Customer cannot complete checkout due to payment processing error.',
'{
"billing_error": "Payment, invoice, or refund issues",
"product_defect": "Any malfunction, bug, or breakage",
"account_issue": "Login failures, password resets",
"feature_request": "Customer suggestions for improvements"
}'
);
{
"response": ["billing_error"],
"error": null
}
使用全局说明
> SELECT ai_classify(
'User reports app crashes on startup after update.',
'["critical", "high", "medium", "low"]',
MAP('instructions', 'Classify bug severity based on user impact and frequency.')
);
{
"response": ["critical"],
"error": null
}
多标签分类
> SELECT ai_classify(
'Customer wants refund and reports product arrived broken.',
'{
"billing_issue": "Payment or refund requests",
"product_defect": "Damaged or malfunctioning items",
"shipping_issue": "Delivery problems"
}',
MAP('multilabel', 'true')
);
{
"response": ["billing_issue", "product_defect"],
"error": null
}
可组合性 ai_parse_document
> WITH parsed_docs AS (
SELECT
path,
ai_parse_document(
content,
MAP('version', '2.0')
) AS parsed_content
FROM READ_FILES('/Volumes/support/tickets/', format => 'binaryFile')
)
SELECT
path,
ai_classify(
parsed_content,
'["billing_error", "product_defect", "account_issue", "feature_request"]',
MAP('instructions', 'Customer support ticket classification.')
) AS ticket_category
FROM parsed_docs;
批处理分类
> SELECT
description,
ai_classify(
description,
'["clothing", "shoes", "accessories", "furniture", "electronics"]'
) AS category
FROM products
LIMIT 10;
版本 1
> SELECT ai_classify("My password is leaked.", ARRAY("urgent", "not urgent"));
urgent
> SELECT
description,
ai_classify(description, ARRAY('clothing', 'shoes', 'accessories', 'furniture')) AS category
FROM
products
LIMIT 10;
局限性
版本 2 (建议)
版本 2 限制:
此函数在 SQL 经典版Azure Databricks不可用。
此函数不能与 视图一起使用。
每个标签名称必须为 1-100 个字符。
该
labels参数必须包含 2 到 500 个唯一标签。每个标签说明必须为 0-1,000 个字符。
最大上下文大小为 128,000 个令牌。
版本 1
版本 1 限制:
此函数在 SQL 经典版Azure Databricks不可用。
此函数不能与 视图一起使用。
每个标签名称必须为 1 到 50 个字符。
数组
labels必须包含 2 到 20 个标签。输入
content必须小于 128,000 个标记(约 300,000 个字符)。