ai_decide 函数

适用于:勾选“是” Databricks SQL 勾选“是” Databricks Runtime

Important

此功能在 Beta 版中。

该 ai_decide() 函数会根据文本或结构化数据评估一个或多个问题。 对于每个问题,它返回概率、指定标准中的选择或有序量表上的得分。 用它来路由支持工单,评估事件是否需要关注,或使用你定义的评分标准优先处理工作。

通话中的所有问题都使用相同的输入,称为 state。 你可以在一次调用中结合不同类型的问题,并在下游SQL查询中使用返回的答案。

数据安全

文档数据在 Databricks 安全外围内进行处理。 Databricks 不存储传递给 AI 函数调用的参数,但保留了元数据的运行细节,例如 Databricks 运行时版本。

要求

Apache 2.0 许可证

此时可能使用的基础模型根据 Apache 2.0 许可证(版权 © Apache Software Foundation)获得许可。 客户需负责确保遵守适用的模型许可条款。

Databricks 建议查看这些许可证,以确保遵守任何适用的条款。 如果模型在未来根据 Databricks 的内部基准表现更好,Databricks 可能会更改模型(以及本页中提供的适用许可证列表)。

支持此函数的模型是使用模型服务基础模型 API 提供的。 有关 Databricks 上可用的模型以及控制这些模型的使用的许可证和策略的信息,请参阅 适用的模型条款 。

如果模型根据Azure Databricks的内部基准性能更好,Databricks 可能会更改模型并更新文档。

Syntax

ai_decide(state, questions [, options])

Arguments

  • state: 或VARIANTSTRING表达式,包含回答问题所需的内容、上下文和示例。 该数值会因行而异。 接受以下任一:

    • 包含 STRING 纯文本或 JSON 编码的对象或数组。 JSON 对象和数组被解释为结构化数据。 其他字符串被视为文本。
    • VARIANT由另一个 AI 函数(例如ai_parse_document或ai_extract)生成的
  • questions: 一个包含非空 JSON 对象的常量 STRING 表达式。 每个键是一个非空的问题ID,每个值是一个问题定义。 每一行的定义都是一样的。 参见 问题定义。

  • options:一个可选的常数 MAP<STRING, STRING> 表达式。 支持的选项为 version,值 '1.0'为。 1.0版本是默认版本。 要明确指定,请使用 map('version', '1.0')。

问题定义

每个题目定义都要求typeinstructions和,加上criteria题型要求时的 。 必填字段不能是 null。 其他字段不支持。

  • type:其中一个 noul, choice或 score。
  • instructions:用于评估状态的指令,以字符串、JSON 对象或 JSON 数组的形式表示。
  • criteria:定义如何回答的描述。 所需的形状取决于 type。

Noul 问题

用 noul 来估算问题为真的概率。 结果是一个0到1的数字。

可选 criteria 对象可以包含 true 和 false 键来描述这些结果。 每个描述可以是字符串、JSON 对象或 JSON 数组。 不需要这些描述时可以省略 criteria 。 如果 被提供,必 criteria 定是对象,且不能是 null。

{
  "needs_escalation": {
    "type": "noul",
    "instructions": "Does this ticket need immediate escalation?",
    "criteria": {
      "true": "An active service outage blocks the customer from working.",
      "false": "The customer can continue working or has a workaround."
    }
  }
}

选择题

使用 choice 该标签从一组命名标准中选择一个。

所需的 criteria 对象将1到255个非空标签映射到描述。 每个描述可以是字符串、JSON 对象、JSON 数组或 null。 当标签本身就能描述选择时使用 null 。

{
  "team": {
    "type": "choice",
    "instructions": "Which team should handle this ticket?",
    "criteria": {
      "shipping": "Delivery or shipment issues",
      "billing": "Payment or invoice issues",
      "technical_support": "Product technical problems"
    }
  }
}

分数问题

用 score 来根据有序尺度评估状态。

所需的 criteria 数组包含2到10个描述,按大小到最高排序。 每个描述可以是字符串、JSON 对象或 JSON 数组。 第一个准则有指数 0,下一个标准有指数 1,依此类推。

结果就是这些指数的概率加权平均值。 它可以是分数的,范围从 0 到标准数减一。 例如,0.3在三个判定1.5条件下,概0.1率 、 、 0.6 以及 的概率得到 。

{
  "urgency": {
    "type": "score",
    "instructions": "How urgent is this ticket?",
    "criteria": [
      "Routine request with no time pressure",
      "Time-sensitive issue with a workaround",
      "Critical issue that blocks the customer"
    ]
  }
}

Returns

返回 VARIANT a,域为 response、 metadata、和 error_message。 response该字段包含一个answers对象,每个问题只能有一条条目。 问题ID与参数中 questions 提供的完全一致。 每个答案都包含 type 一个与问题类型相匹配的字段: noul、、 choice或 score。 以下示例展示了 response 每种题型的字段:

  • Noul问题: probability A,从0到1,估计该问题为真的概率。

    {
      "answers": {
        "needs_escalation": {
          "type": "noul",
          "probability": 0.1
        }
      }
    }
    
  • 选择题: choice A 包含概率最高的标签,以及 probabilities 一个将每个标签映射到其概率的对象。 confidence 是0到1的数字,表示州政府对评估的支持程度。 这些 probabilities 数值加起来为1。 标签名称与中 criteria提供的完全一致。

    {
      "answers": {
        "team": {
          "type": "choice",
          "choice": "shipping",
          "probabilities": {
            "shipping": 0.8,
            "billing": 0.1,
            "technical_support": 0.1
          },
          "confidence": 0.9
        }
      }
    }
    
  • 评分问题:包含 score 标准索引的概率加权平均, legend 映射字符串索引(如 "0"), probabilities 以及将这些指标映射到概率的对象。 confidence 是0到1的数字,表示州政府对评估的支持程度。 这些 probabilities 数值加起来为1。

    {
      "answers": {
        "urgency": {
          "type": "score",
          "score": 1.5,
          "legend": {
            "0": "Routine request with no time pressure",
            "1": "Time-sensitive issue with a workaround",
            "2": "Critical issue that blocks the customer"
          },
          "probabilities": {
            "0": 0.1,
            "1": 0.3,
            "2": 0.6
          },
          "confidence": 0.8
        }
      }
    }
    

成功时, metadata.version 表示所用函数版本,且 error_message 为 null。

在失效时, response 是 null 和 error_message 描述失效。 无效的论元类型或问题定义不恒定也可能引发查询错误。

Examples

以下示例展示了如何在一次通话中评估多个问题。 生成的答案在不同通话中可能有所不同。

评估多种题型

本例评估了产品列表的分类、回收材料的使用情况以及雨天徒步的适用性:

SELECT ai_decide(
  '{"name": "TrailShell jacket", "description": "Lightweight waterproof hiking jacket made from recycled polyester. Packs into its own pocket."}',
  '{
    "category": {
      "type": "choice",
      "instructions": "Which product category best fits this item?",
      "criteria": {
        "outerwear": "Jackets, coats, and other protective outer layers",
        "footwear": "Shoes, boots, and sandals",
        "accessories": "Bags, hats, and other accessories"
      }
    },
    "recycled_materials": {
      "type": "noul",
      "instructions": "Does the listing state that the product uses recycled materials?"
    },
    "hiking_suitability": {
      "type": "score",
      "instructions": "How suitable is this product for hiking in rainy weather?",
      "criteria": [
        "Not suitable for outdoor use in rain",
        "Offers some protection from rain",
        "Designed for hiking with waterproof protection"
      ]
    }
  }',
  map('version', '1.0')
) AS decision;

示例响应:

{
  "response": {
    "answers": {
      "category": {
        "type": "choice",
        "choice": "outerwear",
        "probabilities": { "outerwear": 0.95, "footwear": 0.01, "accessories": 0.04 },
        "confidence": 0.95
      },
      "recycled_materials": { "type": "noul", "probability": 0.98 },
      "hiking_suitability": {
        "type": "score",
        "score": 1.8,
        "legend": {
          "0": "Not suitable for outdoor use in rain",
          "1": "Offers some protection from rain",
          "2": "Designed for hiking with waterproof protection"
        },
        "probabilities": { "0": 0.05, "1": 0.1, "2": 0.85 },
        "confidence": 0.9
      }
    }
  },
  "metadata": { "version": "1.0" },
  "error_message": null
}