條件式認知技能

條件式技能可讓需要布爾運算的 Azure AI 搜尋案例,以判斷要指派給輸出的數據。 這些案例包括篩選、指派預設值,以及根據條件合併數據。

下列虛擬程式代碼示範條件式技能完成的工作:

if (condition) 
    { output = whenTrue } 
else 
    { output = whenFalse } 

注意

此技能未系結至 Azure AI 服務。 這是不可計費的,而且沒有 Azure AI 服務的主要需求。

@odata.type

Microsoft.Skills.Util.ConditionalSkill

評估的欄位

此技能很特別,因為其輸入是評估欄位。

下列項目是表示式的有效值:

  • 註釋路徑(表達式中的路徑必須以 “$(” 和 “)” 分隔)
    範例:

        "= $(/document)"
        "= $(/document/content)"
    
  • 常值(字串、數位、true、false、null)
    範例:

       "= 'this is a string'"   // string (note the single quotation marks)
       "= 34"                   // number
       "= true"                 // Boolean
       "= null"                 // null value
    
  • 使用比較運算符的表達式(==、!=、=、=、>>=、)<<
    範例:

        "= $(/document/language) == 'en'"
        "= $(/document/sentiment) >= 0.5"
    
  • 使用布爾運算符的表達式 (&&, ||, !, ^)
    範例:

        "= $(/document/language) == 'en' && $(/document/sentiment) > 0.5"
        "= !true"
    
  • 使用數值運算子的運算式(+、-、*、/、 %)
    範例:

        "= $(/document/sentiment) + 0.5"         // addition
        "= $(/document/totalValue) * 1.10"       // multiplication
        "= $(/document/lengthInMeters) / 0.3049" // division
    

因為條件式技能支持評估,所以您可以在次要轉換案例中使用。 例如,請參閱 技能定義 4

技能輸入

輸入會區分大小寫。

輸入 描述
條件 此輸入是 評估的欄位 ,代表要評估的條件。 此條件應評估為布爾值 (truefalse)。
例如:
“= true”
“= $(/document/language) =='fr'”
“= $(/document/pages/*/language) == $(/document/expectedLanguage)”
whenTrue 此輸入是 評估的欄位 ,表示條件評估為 true 時要傳回的值。 常數位串應該以單引號 (' 和 『) 傳回。
範例值:
“= 'contract'”
“= $(/document/contractType)”
“= $(/document/entities/*)”
whenFalse 此輸入是 評估的欄位 ,表示條件評估為 false 時要傳回的值。
範例值:
“= 'contract'”
“= $(/document/contractType)”
“= $(/document/entities/*)”

技能輸出

有一個單純稱為「輸出」的單一輸出。如果條件為 false 或條件為 true,則會傳回 Value WhenFalse

範例

範例技能定義 1:篩選檔以只傳回法文檔

如果文件的語言是法文,下列輸出會傳回句子陣列(“/document/frenchSentences”。 如果語言不是法文,則值會設定為 null

{
    "@odata.type": "#Microsoft.Skills.Util.ConditionalSkill",
    "context": "/document",
    "inputs": [
        { "name": "condition", "source": "= $(/document/language) == 'fr'" },
        { "name": "whenTrue", "source": "/document/sentences" },
        { "name": "whenFalse", "source": "= null" }
    ],
    "outputs": [ { "name": "output", "targetName": "frenchSentences" } ]
}

如果使用 “/document/frenchSentences” 作為 另一個技能的內容 ,則只有在 “/document/frenchSentences” 未設定為 null 時,才會執行該技能。

範例技能定義 2:為不存在的值設定預設值

下列輸出會建立批注 (“/document/languageWithDefault”),該批注會設定為文件的語言,如果未設定語言,則為 “es”。

{
    "@odata.type": "#Microsoft.Skills.Util.ConditionalSkill",
    "context": "/document",
    "inputs": [
        { "name": "condition", "source": "= $(/document/language) == null" },
        { "name": "whenTrue", "source": "= 'es'" },
        { "name": "whenFalse", "source": "= $(/document/language)" }
    ],
    "outputs": [ { "name": "output", "targetName": "languageWithDefault" } ]
}

範例技能定義 3:將兩個字段的值合併成一個

在此範例中,某些句子具有 frenchSentiment 屬性。 每當 frenchSentiment 屬性為 null時,我們想要使用 englishSentiment 值。 我們會將輸出指派給稱為 情感 的成員(“/document/sentences/*/sentiment”。

{
    "@odata.type": "#Microsoft.Skills.Util.ConditionalSkill",
    "context": "/document/sentences/*",
    "inputs": [
        { "name": "condition", "source": "= $(/document/sentences/*/frenchSentiment) == null" },
        { "name": "whenTrue", "source": "/document/sentences/*/englishSentiment" },
        { "name": "whenFalse", "source": "/document/sentences/*/frenchSentiment" }
    ],
    "outputs": [ { "name": "output", "targetName": "sentiment" } ]
}

轉換範例

範例技能定義 4:單一欄位的數據轉換

在此範例中,我們會收到 介於 0 到 1 之間的情感 。 我們想要將它轉換成 -1 到 1。 我們可以使用條件式技能來執行此次要轉換。

在此範例中,我們不會使用技能的條件式層面,因為條件一律 為 true

{
    "@odata.type": "#Microsoft.Skills.Util.ConditionalSkill",
    "context": "/document/sentences/*",
    "inputs": [
        { "name": "condition", "source": "= true" },
        { "name": "whenTrue", "source": "= $(/document/sentences/*/sentiment) * 2 - 1" },
        { "name": "whenFalse", "source": "= 0" }
    ],
    "outputs": [ { "name": "output", "targetName": "normalizedSentiment" } ]
}

特殊考量

系統會評估某些參數,因此您必須特別小心,才能遵循記載的模式。 表達式必須以等號開頭。 路徑必須以 “$(” 和 “)” 分隔。 請務必將字串放在單引號中。 這有助於評估工具區分字串和實際路徑和運算符。 此外,請務必在運算符周圍放置空格符(例如,路徑中的 “*” 表示與乘法不同)。

下一步