Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AI Test Tool] Add Multi-Turn Accuracy & Export #2419

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/Tools/AI Test Toolkit/Results.xlsx
Binary file not shown.
9 changes: 9 additions & 0 deletions src/Tools/AI Test Toolkit/src/AITTestContext.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ codeunit 149044 "AIT Test Context"
AITTestContextImpl.SetTestMetric(TestMetric);
end;

/// <summary>
/// Sets the accuracy of the test.
/// </summary>
/// <param name="Accuracy">The accuracy as a decimal between 0 and 1.</param>
procedure SetAccuracy(Accuracy: Decimal)
begin
AITTestContextImpl.SetAccuracy(Accuracy);
end;

var
AITTestContextImpl: Codeunit "AIT Test Context Impl.";
}
53 changes: 48 additions & 5 deletions src/Tools/AI Test Toolkit/src/AITTestContextImpl.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ codeunit 149043 "AIT Test Context Impl."
var
AITTestSuiteMgt: Codeunit "AIT Test Suite Mgt.";
GlobalTestOutputJson: Codeunit "Test Output Json";
Accuracy: Decimal;
CurrentTurn: Integer;
NumberOfTurns: Integer;
IsMultiTurn: Boolean;
AccuracySetManually: Boolean;
AccuracyErr: Label 'Accuracy must be between 0.0 and 1.0.';
AnswerTok: Label 'answer', Locked = true;
ContextTok: Label 'context', Locked = true;
GroundTruthTok: Label 'ground_truth', Locked = true;
Expand Down Expand Up @@ -146,6 +149,33 @@ codeunit 149043 "AIT Test Context Impl."
SetSuiteTestOutput(CurrentTestOutputJson.ToText());
end;

/// <summary>
/// Sets the accuracy of the test.
/// </summary>
/// <param name="AccuracyPct">The accuracy as a decimal between 0 and 1.</param>
procedure SetAccuracy(AccuracyPct: Decimal)
begin
if (AccuracyPct < 0) or (AccuracyPct > 1.0) then
Error(AccuracyErr);

AccuracySetManually := true;
Accuracy := AccuracyPct;
end;

/// <summary>
/// Gets the accuracy of the test.
/// </summary>
/// <returns>True if the accuracy was set, false otherwise.</returns>
Copy link
Contributor

@t-prda t-prda Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing parameter doc for AccuracyPct

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not clear that this function returns accuracy only when it is being set manually.

procedure GetAccuracy(var AccuracyPct: Decimal): Boolean
begin
if AccuracySetManually then begin
AccuracyPct := Accuracy;
exit(true);
end;

exit(false);
end;

/// <summary>
/// Sets to next turn for multiturn testing.
/// </summary>
Expand All @@ -155,7 +185,7 @@ codeunit 149043 "AIT Test Context Impl."
if not IsMultiTurn then
exit(false);

if CurrentTurn + 1 > NumberOfTurns then
Copy link
Contributor

@t-prda t-prda Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change may cause regression. Have you tested against some of our existing multi-turn scenarios? Just to be 100% sure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see CurrentTurn is now starts with 1.

if CurrentTurn > NumberOfTurns then
exit(false);

CurrentTurn := CurrentTurn + 1;
Expand All @@ -164,14 +194,23 @@ codeunit 149043 "AIT Test Context Impl."
end;

/// <summary>
/// Gets the current turn for multiturn testing. Turns start from turn 0.
/// Gets the current turn for multiturn testing. Turns start from turn 1.
/// </summary>
/// <returns>The current turn number.</returns>
procedure GetCurrentTurn(): Integer
begin
exit(CurrentTurn);
end;

/// <summary>
/// Gets the total number of turns for multiturn testing.
/// </summary>
/// <returns>The total number of turns for the line.</returns>
procedure GetNumberOfTurns(): Integer
begin
exit(NumberOfTurns);
end;

/// <summary>
/// This method starts the scope of the Run Procedure scenario.
/// </summary>
Expand Down Expand Up @@ -205,12 +244,16 @@ codeunit 149043 "AIT Test Context Impl."
TestInput: Codeunit "Test Input";
TurnsInputJson: Codeunit "Test Input Json";
begin
CurrentTurn := 0;
AccuracySetManually := false;
Accuracy := 0;
CurrentTurn := 1;
GlobalTestOutputJson.Initialize();
TurnsInputJson := TestInput.GetTestInput().ElementExists(TurnsTok, IsMultiTurn);

if IsMultiTurn then
NumberOfTurns := TurnsInputJson.GetElementCount() - 1;
NumberOfTurns := TurnsInputJson.GetElementCount()
else
NumberOfTurns := 1;
end;

/// <summary>
Expand All @@ -223,7 +266,7 @@ codeunit 149043 "AIT Test Context Impl."
TestInput: Codeunit "Test Input";
begin
if IsMultiTurn then
TestInputJson := TestInput.GetTestInput(TurnsTok).ElementAt(CurrentTurn).Element(ElementName)
TestInputJson := TestInput.GetTestInput(TurnsTok).ElementAt(CurrentTurn - 1).Element(ElementName)
else
TestInputJson := TestInput.GetTestInput(ElementName);
end;
Expand Down
40 changes: 40 additions & 0 deletions src/Tools/AI Test Toolkit/src/AITTestRunIteration.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ codeunit 149042 "AIT Test Run Iteration"
GlobalTestMethodLine: Record "Test Method Line";
NoOfInsertedLogEntries: Integer;
GlobalAITokenUsedByLastTestMethodLine: Integer;
GlobalNumberOfTurnsForLastTestMethodLine: Integer;
GlobalNumberOfTurnsPassedForLastTestMethodLine: Integer;
GlobalTestAccuracy: Decimal;
GlobalSessionAITokenUsed: Integer;

trigger OnRun()
Expand Down Expand Up @@ -124,6 +127,21 @@ codeunit 149042 "AIT Test Run Iteration"
exit(GlobalAITokenUsedByLastTestMethodLine);
end;

procedure GetNumberOfTurnsForLastTestMethodLine(): Integer
begin
exit(GlobalNumberOfTurnsForLastTestMethodLine);
end;

procedure GetNumberOfTurnsPassedForLastTestMethodLine(): Integer
begin
exit(GlobalNumberOfTurnsPassedForLastTestMethodLine);
end;

procedure GetAccuracyForLastTestMethodLine(): Decimal
begin
exit(GlobalTestAccuracy);
end;

[InternalEvent(false)]
procedure OnBeforeRunIteration(var AITTestSuite: Record "AIT Test Suite"; var AITTestMethodLine: Record "AIT Test Method Line")
begin
Expand All @@ -144,6 +162,14 @@ codeunit 149042 "AIT Test Run Iteration"

// Update AI Token Consumption
GlobalAITokenUsedByLastTestMethodLine := 0;

// Update Turns
GlobalNumberOfTurnsPassedForLastTestMethodLine := 0;
GlobalNumberOfTurnsForLastTestMethodLine := 1;

// Update Test Accuracy
GlobalTestAccuracy := 0.0;

GlobalSessionAITokenUsed := AOAIToken.GetTotalServerSessionTokensConsumed();

AITContextCU.StartRunProcedureScenario();
Expand All @@ -154,6 +180,7 @@ codeunit 149042 "AIT Test Run Iteration"
var
AITContextCU: Codeunit "AIT Test Context Impl.";
AOAIToken: Codeunit "AOAI Token";
Accuracy: Decimal;
begin
if ActiveAITTestSuite.Code = '' then
exit;
Expand All @@ -166,6 +193,19 @@ codeunit 149042 "AIT Test Run Iteration"
// Update AI Token Consumption
GlobalAITokenUsedByLastTestMethodLine := AOAIToken.GetTotalServerSessionTokensConsumed() - GlobalSessionAITokenUsed;

// Update Turns
GlobalNumberOfTurnsForLastTestMethodLine := AITContextCU.GetNumberOfTurns();
GlobalNumberOfTurnsPassedForLastTestMethodLine := AITContextCU.GetCurrentTurn();

if not IsSuccess then
GlobalNumberOfTurnsPassedForLastTestMethodLine -= 1;

// Update Test Accuracy
if AITContextCU.GetAccuracy(Accuracy) then
GlobalTestAccuracy := Accuracy
else
GlobalTestAccuracy := GlobalNumberOfTurnsPassedForLastTestMethodLine / GlobalNumberOfTurnsForLastTestMethodLine;

AITContextCU.EndRunProcedureScenario(CurrentTestMethodLine, IsSuccess);
Commit();
end;
Expand Down
51 changes: 51 additions & 0 deletions src/Tools/AI Test Toolkit/src/Logs/AITLogEntries.Page.al
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ page 149033 "AIT Log Entries"
{
StyleExpr = StatusStyleExpr;
}
field(Accuracy; Rec.Accuracy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to expose these in the API as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to create a separate work item, we can uptake the API changes in the same work item.

{
}
field("No. of Turns Passed"; Rec."No. of Turns Passed")
{
Visible = false;
}
field("No. of Turns Executed"; Rec."No. of Turns Executed")
{
Visible = false;
}
field(TurnsText; TurnsText)
{
StyleExpr = TurnsStyleExpr;
Caption = 'No. of Turns Passed';
ToolTip = 'Specifies the number of turns that passed out of the total number of turns.';
}
field("Orig. Status"; Rec."Original Status")
{
Visible = false;
Expand Down Expand Up @@ -253,6 +270,19 @@ page 149033 "AIT Log Entries"
Page.Run(Page::"AIT Test Data Compare", Rec);
end;
}
action("Export Results")
{
Caption = 'Export Results';
Image = Export;
ToolTip = 'Exports the results.';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide a better tool tip. It might be confusing with the existing action of download test output.


trigger OnAction()
var
AITTestSuiteMgt: Codeunit "AIT Test Suite Mgt.";
begin
AITTestSuiteMgt.ExportResults(Rec);
end;
}
}
area(Promoted)
{
Expand All @@ -279,6 +309,9 @@ page 149033 "AIT Log Entries"
actionref("View Test Data_Promoted"; "View Test Data")
{
}
actionref("Export Results_Promoted"; "Export Results")
{
}
}
}
}
Expand All @@ -287,20 +320,26 @@ page 149033 "AIT Log Entries"
ClickToShowLbl: Label 'Show data input';
DoYouWantToDeleteQst: Label 'Do you want to delete all entries within the filter?';
InputText: Text;
TurnsText: Text;
OutputText: Text;
ErrorMessage: Text;
ErrorCallStack: Text;
StatusStyleExpr: Text;
TurnsStyleExpr: Text;
TestRunDuration: Duration;
IsFilteredToErrors: Boolean;
ShowSensitiveData: Boolean;

trigger OnAfterGetRecord()
var
AITTestSuiteMgt: Codeunit "AIT Test Suite Mgt.";
begin
TestRunDuration := Rec."Duration (ms)";
TurnsText := AITTestSuiteMgt.GetTurnsAsText(Rec);
SetInputOutputDataFields();
SetErrorFields();
SetStatusStyleExpr();
SetTurnsStyleExpr();
end;

local procedure SetStatusStyleExpr()
Expand All @@ -315,6 +354,18 @@ page 149033 "AIT Log Entries"
end;
end;

local procedure SetTurnsStyleExpr()
begin
case Rec."No. of Turns Passed" of
Rec."No. of Turns Executed":
TurnsStyleExpr := 'Favorable';
0:
TurnsStyleExpr := 'Unfavorable';
else
TurnsStyleExpr := 'Ambiguous';
end;
end;

local procedure SetErrorFields()
begin
ErrorMessage := '';
Expand Down
15 changes: 15 additions & 0 deletions src/Tools/AI Test Toolkit/src/Logs/AITLogEntry.Table.al
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ table 149034 "AIT Log Entry"
{
Caption = 'Output Data';
}
field(40; "No. of Turns Executed"; Integer)
{
Caption = 'Total number of turns';
ToolTip = 'Specifies the total number of turns executed.';
}
field(41; "No. of Turns Passed"; Integer)
{
Caption = 'Number of turns passed';
ToolTip = 'Specifies the number of turns passed.';
}
field(45; Accuracy; Decimal)
{
Caption = 'Accuracy';
ToolTip = 'Specifies the accuracy of the test line.';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: consider describing how the accuracy can be calculated or what does it exactly mean. Considering we have a status field as well.

}
field(50; "Tokens Consumed"; Integer)
{
Caption = 'Total Tokens Consumed';
Expand Down
73 changes: 73 additions & 0 deletions src/Tools/AI Test Toolkit/src/Logs/AITResults.Report.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------

namespace System.TestTools.AITestToolkit;

report 149030 "AIT Results"
{
Caption = 'AI Test Results';
ApplicationArea = All;
UsageCategory = Tasks;
DefaultLayout = Excel;
ExcelLayout = 'Results.xlsx';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider using a better name for the layout


dataset
{
dataitem(Results; "AIT Log Entry")
{

RequestFilterFields = Version;
RequestFilterHeading = 'AI Test Log Entries';

column(CodeunitID; Results."Codeunit ID")
{
}
column(Name; Results."Codeunit Name")
{
}
column(TestName; Results."Procedure Name")
{
}
column(Status; Results.Status)
{
}
column(Accuracy; Results.Accuracy)
{
}
column(TurnsExecuted; Results."No. of Turns Executed")
{
}
column(TurnsPassed; Results."No. of Turns Passed")
{
}
column(Input; Input)
{
}
column(Output; Output)
{
}
column(Error_Message; ErrorMessage)
{
}
column(Error; ErrorCallstack)
{
}

trigger OnAfterGetRecord()
begin
Input := Results.GetInputBlob();
Output := Results.GetOutputBlob();
ErrorMessage := Results.GetMessage();
ErrorCallstack := Results.GetErrorCallStack();
end;
}
}

var
Input: Text;
Output: Text;
ErrorMessage: Text;
ErrorCallstack: Text;
}
Loading
Loading