ai_decide 函數

適用於:勾選為「是」Databricks SQL 勾選為「是」Databricks Runtime

Important

這項功能位於 測試版 (Beta) 中。 工作區管理員可以從 「預覽 」頁面控制對此功能的存取。 請參閱 管理 Azure Databricks 預覽。

此 ai_decide() 函式會根據文字或結構化資料評估一個或多個問題。 對於每個問題,它會回傳機率、指定標準中的選擇,或是有序量表上的分數。 用它來路由支援工單、評估活動是否需要關注,或用你定義的評分標準優先處理工作。

通話中的所有問題都使用相同的輸入,稱為 state。 你可以在一次通話中結合不同類型的問題,並在下游 SQL 查詢中使用回傳的答案。

數據安全性

您的文件數據會在 Databricks 安全性周邊內處理。 Databricks 不儲存傳遞給 AI 函式呼叫的參數,但會保留元資料執行細節,例如 Databricks 執行時版本。

要求

Apache 2.0 授權

目前可能使用的底層模型依照 Apache 2.0 授權,版權屬於 © Apache 軟體基金會。 客戶應負責確保遵循適用的模型授權。

Databricks 建議檢閱這些授權,以確保符合任何適用的條款。 如果未來模型根據 Databricks 的內部基準檢驗而表現更好,Databricks 可能會變更模型(以及此頁面上提供的適用授權清單)。

支撐此功能的模型是透過 Model Serving Foundation 模型 API 提供。 請參閱 適用模型條款 ,了解 Databricks 上可用的模型,以及管理這些模型使用的授權與政策。

如果根據 Azure Databricks 內部基準測試,出現了表現更好的模型,Databricks 可能會修改模型並更新文件。

語法

ai_decide(state, questions [, options])

Arguments

  • state: A VARIANT 或 STRING 表達式,包含回答問題所需的內容、上下文及範例。 分值會因行而異。 接受以下任一:

    • 一個 STRING 包含純文字或 JSON 編碼的物件或陣列。 JSON 物件與陣列被解讀為結構化資料。 其他字串則視為文字。
    • A VARIANT 是由另一個 AI 函數(例如 ai_parse_document 或 ai_extract)產生的
  • questions: 一個包含非空 JSON 物件的常數 STRING 表達式。 每個鍵都是非空的問題 ID,每個值則是問題定義。 每一列的定義都相同。 請參見 問題定義。

  • options:一個可選的常數 MAP<STRING, STRING> 表達式。 支援的選項為 version,其值 '1.0'為 。 預設版本是 1.0。 若要明確指定,請使用 map('version', '1.0')。

問題定義

每個題型定義都要求 type 和 instructions,加上 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在三個條件下,的0.1機率 、 、 0.6 的機率會得到 1.5。

{
  "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

返回 a VARIANT ,域為 response、 metadata、 error_message。 欄位包含 response 一個 answers 物件,每個問題只有一條條目。 問題編號與參數中 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 描述失敗。 無效的論元類型或問題定義不一致也可能造成查詢錯誤。

範例

以下範例說明如何在一通電話中評估多個問題。 產生的答案在不同通話中可能有所不同。

評估多種題型

此範例評估產品清單的分類、回收材料使用情況及雨天健行的適用性:

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
}