适用于:
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 上可用的模型以及控制这些模型的使用的许可证和策略的信息,请参阅 适用的模型条款 。
如果模型根据Azure Databricks的内部基准性能更好,Databricks 可能会更改模型并更新文档。
此函数仅在某些区域中可用,请参阅 AI 函数可用性。
对于具有 增强安全性和合规性加载项的工作区,
- 有关适当的
ai_classify,请参阅区域支持。 - 有关如何在工作区上启用预览版,请参阅“管理Azure Databricks预览”。
- 有关适当的
此函数在 Pro 或经典 SQL 仓库上不可用。
需要 Databricks Runtime 18.2 或更高版本。
笔记本和 Databricks 工作流需要无服务器计算。
Tip
Databricks 建议将 版本 2.1 用于 ai_classify. 版本 1.0 是不支持这些功能的旧接口,不建议用于新的或生产工作负荷。
版本 2.0 支持:
- 用于改进准确性的标签说明
- 多标签分类
- 全局说明
- 与版本 1.0 中的 20 个标签相比,最多 500 个标签
-
VARIANT来自上游 AI 函数的输入,例如ai_parse_document - 返回包含错误信息的结构化
VARIANT
版本 2.1 还支持:
- 每个返回标签的置信度分数,启用
enableConfidenceScores - 解释每个返回的标签的合理原理,使用
启用
在版本 2.1 中 response ,每个分类都是具有键而不是纯标签字符串的每个标签对象 value 。 即使禁用了新选项,此输出形状更改也适用于所有版本 2.1 调用。 从版本 2.0 迁移到 2.1 不兼容。 更新查询和下游代码以从 value 键读取标签,而不是直接读取标签。
若要显式固定版本,请传递 options => map('version', '2.1')。
语法
版本 2.1(建议)
ai_classify(content, labels [, options])
版本 2
ai_classify(content, labels [, options])
版本 1 (旧版)
ai_classify(content, labels [, options])
参数
版本 2.1(建议)
content:VARIANT或STRING表达式。 接受以下任一:- 原始文本作为
STRING -
VARIANT由另一个 AI 函数(例如ai_parse_document或ai_extract)生成的
- 原始文本作为
labels:定义STRING分类标签的表达式。 这可以是字符串文本或任何计算结果为一STRING个 SQL 表达式,包括 Delta 表列。 标签可以是:- 简单标签:标签名称的 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 个标签。 有关大于 500 个标签的分类,请参阅 具有 500 多个标签的分类。- 简单标签:标签名称的 JSON 数组。
options:包含配置选项的可选MAP<STRING, STRING>选项:-
version:版本切换以支持迁移("1.0"或"2.0""2.1")。 默认值基于输入类型,但回退到"1.0"。 -
instructions:用于提高分类质量的任务和域的全局说明。 必须少于 20,000 个字符。 -
multilabel:设置为"true"在应用多个类别时返回多个标签。 默认值为"false"(单标签分类)。 -
enableConfidenceScores:设置为"true"为每个返回的标签包含一个confidence_score(0–1)。 需要版本"2.1"。 默认值为"false"。 -
enableRationales:设置为"true"包含解释每个返回标签的简短理由,这些理由以输入文本为依据。 需要版本"2.1"。 默认值为"false"。
-
版本 2
content:VARIANT或STRING表达式。 接受以下任一:- 原始文本作为
STRING -
VARIANT由另一个 AI 函数(例如ai_parse_document或ai_extract)生成的
- 原始文本作为
labels:定义STRING分类标签的表达式。 这可以是字符串文本或任何计算结果为一STRING个 SQL 表达式,包括 Delta 表列。 标签可以是:- 简单标签:标签名称的 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 个标签。 有关大于 500 个标签的分类,请参阅 具有 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.1(建议)
返回一个 VARIANT 包含:
{
"response": [{ "value": "label_name", "confidence_score": 0.97, "rationale": "Short justification for the label." }],
"metadata": {
"version": "2.1"
},
"error_message": null // null on success, or error message on failure
}
每个 response 元素都是具有 value 键而不是纯标签字符串的每个标签对象。
"true"何时enableConfidenceScores,每个对象还包括一个 confidence_score (0–1)。 当 enableRationales 是 "true"时,每个对象还包括一个 rationale。 启用这两个选项后,每个对象中的键顺序为 value, confidence_score然后 rationale。
该 response 字段包含:
- 单标签模式 (默认值):包含一个包含最佳匹配标签的元素的数组
-
多标签模式 (
multilabel: "true"):应用多个类别时具有多个标签的数组 - 标签名称与参数中
labels提供的名称完全匹配
如果NULLcontent为NULL或无法对内容进行分类,则返回。
版本 2
返回一个 VARIANT 包含:
{
"response": ["label_name"], // Array with single label (or multiple if multilabel=true)
"metadata": {
"version": "2.0"
},
"error_message": null // null on success, or error message on failure
}
该 response 字段包含:
- 单标签模式 (默认值):包含一个包含最佳匹配标签的元素的数组
-
多标签模式 (
multilabel: "true"):应用多个类别时具有多个标签的数组 - 标签名称与参数中
labels提供的名称完全匹配
如果NULLcontent为NULL或无法对内容进行分类,则返回。
版本 1 (旧版)
返回一个 STRING。 该值与 labels 参数中提供的字符串之一匹配。
如果NULLcontent为NULL或无法对内容进行分类,则返回。
示例
版本 2.1(建议)
简单标签 - 仅标签名称
在版本 2.1 中,每个分类都是具有键而不是纯字符串的每个标签对象 value ,即使新选项被禁用也是如此。
> SELECT ai_classify(
'My password is leaked.',
'["urgent", "not_urgent"]',
MAP('version', '2.1')
);
{
"response": [{"value": "urgent"}],
"metadata": {
"version": "2.1"
},
"error_message": null
}
具有置信度分数
> SELECT ai_classify(
'My password is leaked.',
'["urgent", "not_urgent"]',
MAP('version', '2.1', 'enableConfidenceScores', 'true')
);
{
"response": [{"value": "urgent", "confidence_score": 0.97}],
"metadata": {
"version": "2.1"
},
"error_message": null
}
使用理由
> SELECT ai_classify(
'My password is leaked.',
'["urgent", "not_urgent"]',
MAP('version', '2.1', 'enableRationales', 'true')
);
{
"response": [{"value": "urgent", "rationale": "A leaked password is a security incident requiring immediate action."}],
"metadata": {
"version": "2.1"
},
"error_message": null
}
具有置信度分数和理由(多标签)
> SELECT ai_classify(
'Customer wants refund and reports product arrived broken.',
'{
"billing_issue": "Payment or refund requests",
"product_defect": "Damaged or malfunctioning items"
}',
MAP('version', '2.1', 'multilabel', 'true', 'enableConfidenceScores', 'true', 'enableRationales', 'true')
);
{
"response": [
{"value": "billing_issue", "confidence_score": 0.91, "rationale": "Customer explicitly requests a refund."},
{"value": "product_defect", "confidence_score": 0.88, "rationale": "Reports the product arrived broken."}
],
"metadata": {
"version": "2.1"
},
"error_message": 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('version', '2.1', 'instructions', 'Customer support ticket classification.')
) AS ticket_category
FROM parsed_docs;
批处理分类
> SELECT
description,
ai_classify(
description,
'["clothing", "shoes", "accessories", "furniture", "electronics"]',
MAP('version', '2.1')
) AS category
FROM products
LIMIT 10;
具有 500 多个标签的分类
若要使用 500 多个标签进行分类,建议嵌入文档和标签,检索每个文档的顶部 K 标签,然后在较小的子集上运行 ai_classify 。
有关分步演练 ,请参阅教程:使用 500 多个标签对文档进行分类 。
版本 2
简单标签 - 仅标签名称
> SELECT ai_classify(
'My password is leaked.',
'["urgent", "not_urgent"]'
);
{
"response": ["urgent"],
"metadata": {
"version": "2.0"
},
"error_message": 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"],
"metadata": {
"version": "2.0"
},
"error_message": null
}
Delta 表中的标签
通过将标签转换为 JSON 字符串,从 Delta 表传递标签。 例如,给定具有架构 news_topics(topic STRING, description STRING)的标签表,可以将标签传递到 ai_classify 如下所示:
SELECT
ai_classify(
"Leicester City Wins Premier League Title at 5000-1 Odds",
l.labels,
MAP('version', '2.0')
) AS classification
FROM (
SELECT to_json(map_from_entries(collect_list(struct(topic, description)))) AS labels
FROM news_topics
) l;
使用全局说明
> 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"],
"metadata": {
"version": "2.0"
},
"error_message": 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('version', '2.0','multilabel', 'true')
);
{
"response": ["billing_issue", "product_defect"],
"metadata": {
"version": "2.0"
},
"error_message": null
}
版本 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.1(建议)
版本 2.1 限制:
此函数在 SQL 经典版Azure Databricks不可用。
此函数不能与 视图一起使用。
每个标签名称必须为 1-100 个字符。
该
labels参数必须包含 2 到 500 个唯一标签。每个标签说明必须为 0-1,000 个字符。
最大上下文大小为 128,000 个令牌。
版本 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 个字符)。