語言

使用 Windows ML 任務做出決策

Important

決策任務屬於實驗性質,不支援在生產環境中使用,且需要 Windows ML 2.9 實驗版或更新版本。

決策任務會將狀態(例如訊息、支援工單或序列化文件)與輸入問題進行評估。 對於每個問題,它會回傳每個選項的機率、所選選項、信心度,以及採取行動或升級處理的建議。 決策是每題一次評估,因此任務不會產生任何文字。

決策任務以已發佈的決策模型資料夾作為模型,Windows ML 則透過 CGC 後端編譯該模型給 GPU。 目前先使用 Laya,也就是 推薦模型中經過驗證的決策模型。 它需要一個 GPU 執行目標,而 CPU 目標會回傳 HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED)。 關於任務如何執行模型,請參閱 「選擇任務如何執行您的模型」。

題型

種類 選項
選擇 至少一個。 每個都有獨特的標籤和可選的描述。
Score 至少一個。 選項 i 為等級 i,其描述中列出該等級的標準。
是/否 None,這會使用模型對兩個答案的預設描述,或恰好兩個(先假,再真)。

一題可以設定從 0 到 1 的 act 門檻值。 答案建議其信心達到閾值時為 ACT,否則建議 ESCALATE。 若沒有閾值,當模型的行為機率至少為 0.5 時,建議是 ACT。

決定

以 LoadModelFromPath 載入模型資料夾,以 CreateDecisionTaskFromModel 建立任務、建立工作階段,並以狀態及其問題呼叫 Decide。 分詞器為選用:使用 nullptr 時,Task 會從模型資料夾建立模型專屬的分詞器。

// Temporary: WinMLRuntime and the other wrapper types in the WinMLSamples.Runtime
// namespaces aren't in the Windows ML package yet. Copy the wrapper files from
// https://github.com/microsoft/WindowsML/tree/main/Samples/Runtime/shared/csharp
// into your project. A future update will include the C# APIs natively.
using WinMLSamples.Runtime;
using WinMLSamples.Runtime.Tasks;

// Load the decision model and create the task on a GPU target.
using WinMLRuntime runtime = WinMLRuntime.Create();
using ExecutionTarget target = runtime.CreateExecutionTarget(
    ExecutionTargetKind.Gpu, ExecutionTargetPreference.Performance);
using Model model = runtime.LoadModelFromPath(modelFolder);
using WinMLTasks tasks = WinMLTasks.Create(runtime);
using DecisionTask task = tasks.Decision.CreateDecisionTaskFromModel(target, model);

// Describe a choice question and evaluate a state against it.
DecisionOption[] options =
[
    new("billing", "A question about an invoice or payment"),
    new("technical", "A problem using the product"),
];
DecisionQuestion question = new()
{
    Kind = DecisionQuestionKind.Choice,
    Instructions = "Which team should handle this support ticket?",
    Options = options,
};
DecisionRequest request = new()
{
    State = "I was charged twice for my subscription.",
    Questions = [question],
};

// A Decider keeps the session on a thread of its own.
await using Decider decider = await Decider.CreateAsync(tasks, task);
using DecisionResult result = await decider.DecideAsync(request, CancellationToken.None);

// Closing the session reports a failure that disposing it would not.
await decider.CloseAsync();

DecisionAnswer answer = result.GetAnswer(0);
float[] probabilities = new float[options.Length];
result.GetProbabilities(0, probabilities);

閱讀答案

Field Meaning
selectedOption 最有可能的選項。 對於是/否題,1 代表真。
confidence 所選選項的機率。
distributionConfidence 1 減去分布的正規化熵:當所有機率都集中在某個選項時為 1,當分布為均勻分布時為 0。 這不是經過校準的機率。
expectedScore 評分類問題則是預期的水準。
actProbability 模型估計該答案可據以採取行動。
recommendation ACT 或 ESCALATE。

長度超過模型序列中問題所留下空間的狀態,其結尾會被截斷。 以序列化文字形式傳遞結構化狀態。

保留已編譯的程式

與其他使用 CGC 後端的任務一樣,第一個工作階段負責編譯 GPU 的程式。 在模型選項中設定 compiledProgramDirectory 選項,以便在後續流程中重複使用它們。 請參閱 快取已編譯程式。

下一步