共用方式為


在 (預覽版) 中 Power Apps 測試非確定性 AI

注意

預覽功能不供生產時使用,而且可能功能受限。 這些功能是在正式發行前先行推出,讓客戶能夠搶先體驗並提供意見反應。

測試人工智慧驅動的應用程式帶來了獨特的挑戰,因為即使輸入相同,人工智慧輸出也可能在運行之間有所不同。 這種非確定性行為需要特殊的測試方法。 本指南介紹如何有效地驗證應用程式中的 Power Platform AI 元件。

瞭解非確定性輸出

傳統測試依賴於產生一致輸出的確定性輸入。 然而,由於以下原因,人工智慧系統每次通常會產生略有不同的結果:

  • 模型溫度設置和採樣方法
  • 選擇不同版本的模型
  • 自然語言處理的細微變化
  • 置信度分數在正常範圍內波動
  • 可能採取不同路徑的上下文相關推理

測試非確定性人工智慧的策略

Power Apps 測試引擎提供了多種有效測試人工智慧元件的策略:

使用 Preview.AIExecutePrompt 函數

Preview.AIExecutePrompt 函數 可以在測試中受控地執行 AI 提示。 以下範例演示如何使用它:

Response: ParseJSON(
  Preview.AIExecutePrompt("CustomPrompt",
  {
    Context: "You are a helpful assistant.",
    Question: "What is the capital of France?"
  }).Text)

此方法允許您:

  • 使用受控輸入執行提示
  • 解析和驗證結構化回應
  • 將結果與預期值進行比較

實施基於容差的驗證

不要期望完全匹配,而是驗證輸出是否滿足可接受閾值內的條件。 以下代碼是一個範例:

// Validate that the sentiment score is within appropriate range
Assert(Response.SentimentScore >= 0.7, "Sentiment score should be positive")

測試核心功能而不是精確輸出

重點測試是驗證 AI 元件是否實現其基本目的,如以下示例所示:

// Check that the classification happens (not the exact classification)
Assert(
  Response.Category = "Positive" || 
  Response.Category = "Neutral" || 
  Response.Category = "Negative", 
  "Response should include a valid sentiment category"
)

對複雜輸出使用結構驗證

對於複雜的 AI 回應,請驗證響應結構而不是特定內容,如以下範例所示:

// Verify all expected fields exist in the response
Assert(!IsBlank(Response.Rating), "Rating should be present")
Assert(!IsBlank(Response.Explanation), "Explanation should be present")
Assert(!IsBlank(Response.NextSteps), "Next steps should be present")

使用 Preview.AIExecutePrompt

測試引擎提供了 專門用於測試 AI 交互的 Preview.AIExecutePrompt 函數

函數簽名

Preview.AIExecutePrompt(
  PromptName: Text, 
  PromptContext: Record
): Record

參數

  • PromptName:要使用的提示範本的名稱
  • PromptContext:提示的上下文數據,通常包含:
    • 上下文或系統消息
    • 問題或用戶輸入
    • 模型的任何其他參數

傳回值

包含以下內容的記錄:

  • 文字:原始回應文本
  • 其他屬性取決於提示範本和模型

啟用該功能

要使用此功能,請在測試設置中啟用 AI 函數模組:

testSettings:
  extensionModules:
    enable: true
    parameters:
      enableAIFunctions: true

示例:測試情緒分析元件

以下範例演示了如何測試 AI 支援的情緒分析功能:

EvaluateSentimentPrompt(Input: Text): TestResult =
  With({
    Response: ParseJSON(
      Preview.AIExecutePrompt("SentimentAnalyzer",
      {
        Text: Input
      }).Text)
  },
  {
    Result: Response.Sentiment,
    Score: Response.ConfidenceScore,
    IsPositive: Response.Sentiment = "Positive" && Response.ConfidenceScore > 0.7,
    IsValid: !IsBlank(Response.Sentiment) && !IsBlank(Response.ConfidenceScore)
  })

示例:評級評估 AI

以下範例演示了如何測試 Preview.AIExecutePrompt AI 驅動的評級系統:

EvaluateTestQuestionPrompt(Prompt: TestQuestion): TestResult =
  With({
    Response: ParseJSON(
      Preview.AIExecutePrompt("PromptEvaluator",
      {
        Context: "You are a helpful agent asking about external customer service questions.",
        Question: Prompt.Question
      }).Text)
  },If(
    IsError(AssertNotError(Prompt.ExpectedRating=Response.Rating, Prompt.Question & ", Expected " & Prompt.ExpectedRating & ", Actual " & Response.Rating)),
    {PassFail: 1, Summary: Prompt.Question & ", Expected " & Prompt.ExpectedRating & ", Actual " & Response.Rating}, 
    {PassFail: 0, Summary: "Pass " & Prompt.Question}
  ))

可以在 AI 提示示例 探索完整的實現。

AI 元件測試的最佳實踐

要為 AI 驅動的元件創建有效的測試,請執行以下作:

  • 為數位輸出定義可接受的 範圍而不是精確值
  • 創建護欄 以驗證輸出是否符合結構要求
  • 使用各種輸入進行測試, 包括邊緣情況和邊界條件
  • 包括負面測試用例 ,以確保 AI 正確處理無效輸入
  • 按功能而不是特定內容對測試 進行細分
  • 根據元件的臨界性使用適當的公差

AI 測試的常見模式

以下示例說明了在應用程式中 Power Platform 測試 AI 支援的功能的常見方法。 這些模式可幫助你驗證內容分類、邊界條件以及 AI 輸出可能不同的其他方案。

內容分類測試

// Test that a content classifier produces valid categories
ClassifyContent(Text: Text): Record = 
  With({
    Result: ParseJSON(Preview.AIExecutePrompt("Classifier", { Content: Text }).Text)
  },
  Assert(
    Result.Category In ["News", "Opinion", "Advertisement"], 
    "Content should be classified into valid category"
  ))

邊界測試

// Test AI Builder form processing with edge cases
TestFormProcessing(FormType: Text): TestResult =
  With({
    Result: ParseJSON(Preview.AIExecutePrompt("FormProcessor", { FormType: FormType }).Text)
  },
  Assert(
    !IsBlank(Result.Fields),
    "Form processing should extract fields even with poor quality input"
  ))

了解 Power Fx 測試函數
探索 AI 輔助測試創作
瞭解模型上下文協定
瀏覽測試引擎範例目錄