@memberjunction/testing-engine 6.2.0-edge.1 → 6.2.0-edge.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/dist/decision-eval/corpus.d.ts +94 -0
- package/dist/decision-eval/corpus.d.ts.map +1 -0
- package/dist/decision-eval/corpus.js +144 -0
- package/dist/decision-eval/corpus.js.map +1 -0
- package/dist/decision-eval/discovery-baseline.d.ts +62 -0
- package/dist/decision-eval/discovery-baseline.d.ts.map +1 -0
- package/dist/decision-eval/discovery-baseline.js +90 -0
- package/dist/decision-eval/discovery-baseline.js.map +1 -0
- package/dist/decision-eval/discovery-corpus.d.ts +75 -0
- package/dist/decision-eval/discovery-corpus.d.ts.map +1 -0
- package/dist/decision-eval/discovery-corpus.js +110 -0
- package/dist/decision-eval/discovery-corpus.js.map +1 -0
- package/dist/decision-eval/discovery-mapping.d.ts +18 -0
- package/dist/decision-eval/discovery-mapping.d.ts.map +1 -0
- package/dist/decision-eval/discovery-mapping.js +21 -0
- package/dist/decision-eval/discovery-mapping.js.map +1 -0
- package/dist/decision-eval/discovery-metrics.d.ts +339 -0
- package/dist/decision-eval/discovery-metrics.d.ts.map +1 -0
- package/dist/decision-eval/discovery-metrics.js +401 -0
- package/dist/decision-eval/discovery-metrics.js.map +1 -0
- package/dist/decision-eval/discovery-scorecard.d.ts +96 -0
- package/dist/decision-eval/discovery-scorecard.d.ts.map +1 -0
- package/dist/decision-eval/discovery-scorecard.js +351 -0
- package/dist/decision-eval/discovery-scorecard.js.map +1 -0
- package/dist/decision-eval/discovery-suite.d.ts +201 -0
- package/dist/decision-eval/discovery-suite.d.ts.map +1 -0
- package/dist/decision-eval/discovery-suite.js +180 -0
- package/dist/decision-eval/discovery-suite.js.map +1 -0
- package/dist/decision-eval/discovery-types.d.ts +896 -0
- package/dist/decision-eval/discovery-types.d.ts.map +1 -0
- package/dist/decision-eval/discovery-types.js +211 -0
- package/dist/decision-eval/discovery-types.js.map +1 -0
- package/dist/decision-eval/index.d.ts +18 -0
- package/dist/decision-eval/index.d.ts.map +1 -0
- package/dist/decision-eval/index.js +18 -0
- package/dist/decision-eval/index.js.map +1 -0
- package/dist/decision-eval/metrics.d.ts +330 -0
- package/dist/decision-eval/metrics.d.ts.map +1 -0
- package/dist/decision-eval/metrics.js +574 -0
- package/dist/decision-eval/metrics.js.map +1 -0
- package/dist/decision-eval/output-paths.d.ts +119 -0
- package/dist/decision-eval/output-paths.d.ts.map +1 -0
- package/dist/decision-eval/output-paths.js +258 -0
- package/dist/decision-eval/output-paths.js.map +1 -0
- package/dist/decision-eval/point-mapping.d.ts +50 -0
- package/dist/decision-eval/point-mapping.d.ts.map +1 -0
- package/dist/decision-eval/point-mapping.js +83 -0
- package/dist/decision-eval/point-mapping.js.map +1 -0
- package/dist/decision-eval/scorecard.d.ts +190 -0
- package/dist/decision-eval/scorecard.d.ts.map +1 -0
- package/dist/decision-eval/scorecard.js +384 -0
- package/dist/decision-eval/scorecard.js.map +1 -0
- package/dist/decision-eval/suite.d.ts +244 -0
- package/dist/decision-eval/suite.d.ts.map +1 -0
- package/dist/decision-eval/suite.js +238 -0
- package/dist/decision-eval/suite.js.map +1 -0
- package/dist/decision-eval/types.d.ts +993 -0
- package/dist/decision-eval/types.d.ts.map +1 -0
- package/dist/decision-eval/types.js +227 -0
- package/dist/decision-eval/types.js.map +1 -0
- package/dist/drivers/AgentEvalDriver.d.ts +30 -1
- package/dist/drivers/AgentEvalDriver.d.ts.map +1 -1
- package/dist/drivers/AgentEvalDriver.js +145 -16
- package/dist/drivers/AgentEvalDriver.js.map +1 -1
- package/dist/drivers/DecisionEvalDriver.d.ts +297 -0
- package/dist/drivers/DecisionEvalDriver.d.ts.map +1 -0
- package/dist/drivers/DecisionEvalDriver.js +716 -0
- package/dist/drivers/DecisionEvalDriver.js.map +1 -0
- package/dist/drivers/PinnedDecisionRunner.d.ts +24 -0
- package/dist/drivers/PinnedDecisionRunner.d.ts.map +1 -0
- package/dist/drivers/PinnedDecisionRunner.js +24 -0
- package/dist/drivers/PinnedDecisionRunner.js.map +1 -0
- package/dist/drivers/PromptEvalDriver.d.ts.map +1 -1
- package/dist/drivers/PromptEvalDriver.js +8 -1
- package/dist/drivers/PromptEvalDriver.js.map +1 -1
- package/dist/drivers/RubricCalibrationTestDriver.d.ts +34 -0
- package/dist/drivers/RubricCalibrationTestDriver.d.ts.map +1 -0
- package/dist/drivers/RubricCalibrationTestDriver.js +193 -0
- package/dist/drivers/RubricCalibrationTestDriver.js.map +1 -0
- package/dist/drivers/calibration.d.ts +85 -0
- package/dist/drivers/calibration.d.ts.map +1 -0
- package/dist/drivers/calibration.js +191 -0
- package/dist/drivers/calibration.js.map +1 -0
- package/dist/drivers/index.d.ts +2 -0
- package/dist/drivers/index.d.ts.map +1 -1
- package/dist/drivers/index.js +2 -0
- package/dist/drivers/index.js.map +1 -1
- package/dist/engine/TestEngine.d.ts.map +1 -1
- package/dist/engine/TestEngine.js +14 -4
- package/dist/engine/TestEngine.js.map +1 -1
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -1
- package/dist/oracles/DecisionJudgeOracle.d.ts +97 -0
- package/dist/oracles/DecisionJudgeOracle.d.ts.map +1 -0
- package/dist/oracles/DecisionJudgeOracle.js +230 -0
- package/dist/oracles/DecisionJudgeOracle.js.map +1 -0
- package/dist/oracles/DecisionLabelMatchOracle.d.ts +38 -0
- package/dist/oracles/DecisionLabelMatchOracle.d.ts.map +1 -0
- package/dist/oracles/DecisionLabelMatchOracle.js +96 -0
- package/dist/oracles/DecisionLabelMatchOracle.js.map +1 -0
- package/dist/oracles/DiscoveryLabelMatchOracle.d.ts +39 -0
- package/dist/oracles/DiscoveryLabelMatchOracle.d.ts.map +1 -0
- package/dist/oracles/DiscoveryLabelMatchOracle.js +88 -0
- package/dist/oracles/DiscoveryLabelMatchOracle.js.map +1 -0
- package/dist/oracles/LLMJudgeOracle.d.ts +2 -47
- package/dist/oracles/LLMJudgeOracle.d.ts.map +1 -1
- package/dist/oracles/LLMJudgeOracle.js +80 -149
- package/dist/oracles/LLMJudgeOracle.js.map +1 -1
- package/dist/oracles/RubricOracle.d.ts +51 -0
- package/dist/oracles/RubricOracle.d.ts.map +1 -0
- package/dist/oracles/RubricOracle.js +66 -0
- package/dist/oracles/RubricOracle.js.map +1 -0
- package/dist/oracles/index.d.ts +5 -0
- package/dist/oracles/index.d.ts.map +1 -1
- package/dist/oracles/index.js +5 -0
- package/dist/oracles/index.js.map +1 -1
- package/dist/oracles/inline-rubric.d.ts +25 -0
- package/dist/oracles/inline-rubric.d.ts.map +1 -0
- package/dist/oracles/inline-rubric.js +82 -0
- package/dist/oracles/inline-rubric.js.map +1 -0
- package/dist/oracles/judge-trace.d.ts +76 -0
- package/dist/oracles/judge-trace.d.ts.map +1 -0
- package/dist/oracles/judge-trace.js +125 -0
- package/dist/oracles/judge-trace.js.map +1 -0
- package/dist/oracles/promote-criteria.d.ts +57 -0
- package/dist/oracles/promote-criteria.d.ts.map +1 -0
- package/dist/oracles/promote-criteria.js +104 -0
- package/dist/oracles/promote-criteria.js.map +1 -0
- package/dist/oracles/rubric-resolution.d.ts +69 -0
- package/dist/oracles/rubric-resolution.d.ts.map +1 -0
- package/dist/oracles/rubric-resolution.js +110 -0
- package/dist/oracles/rubric-resolution.js.map +1 -0
- package/dist/types.d.ts +4 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/result-formatter.d.ts +5 -0
- package/dist/utils/result-formatter.d.ts.map +1 -1
- package/dist/utils/result-formatter.js +8 -2
- package/dist/utils/result-formatter.js.map +1 -1
- package/package.json +12 -10
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Decision Judge oracle implementation
|
|
3
|
+
* @module @memberjunction/testing-engine
|
|
4
|
+
*/
|
|
5
|
+
import { IOracle } from './IOracle.js';
|
|
6
|
+
import { OracleInput, OracleConfig, OracleResult } from '../types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Decision Judge Oracle.
|
|
9
|
+
*
|
|
10
|
+
* Judges output against the same criteria, and the same trace, as {@link LLMJudgeOracle}, but asks a
|
|
11
|
+
* typed decision model instead of parsing an LLM's JSON. Each criterion becomes one Likelihood
|
|
12
|
+
* question, "The response satisfies: <criterion>", and every question travels in one
|
|
13
|
+
* `AIDecisionRunner` call. The pass rule and the weighting live here, in code, not in the prompt.
|
|
14
|
+
*
|
|
15
|
+
* Criteria come from `expectedOutput.judgeValidationCriteria`, or else from `config.criteria`, as for
|
|
16
|
+
* the LLM judge. Each is either a string or `{ "criterion": "...", "weight": 2 }`. A weight of 0 keeps a
|
|
17
|
+
* criterion out of the score but not out of the pass rule; at least one weight must be above 0.
|
|
18
|
+
*
|
|
19
|
+
* Configuration:
|
|
20
|
+
* - criteria: Array of criteria, used when the expected output has none
|
|
21
|
+
* - passThreshold: The probability every criterion must reach to pass (default: 0.5). The default is a
|
|
22
|
+
* placeholder: calibration (Task 2.4) sets real values.
|
|
23
|
+
* - promptName: The decision prompt whose model bindings choose the decision model
|
|
24
|
+
* (default: 'Default Decision')
|
|
25
|
+
* - timeoutMS: How long to wait for each decision model call, in milliseconds (default: 120000, two
|
|
26
|
+
* minutes). A call that runs over fails the oracle.
|
|
27
|
+
*
|
|
28
|
+
* Result:
|
|
29
|
+
* - passed: every criterion's probability is at or above `passThreshold`
|
|
30
|
+
* - score: the weighted mean of the probabilities (weights default to 1)
|
|
31
|
+
* - details: `criteriaProbabilities` (one entry per criterion), `passThreshold`, `promptName`,
|
|
32
|
+
* `decisionRunId` (the `MJ: AI Prompt Runs` row), `decisionModel`, and `llmCost`, the decision
|
|
33
|
+
* call's cost, under the key the LLM judge uses for its own. It is saved with the result details
|
|
34
|
+
* (`TestRun.ResultDetails`) but, as for the LLM judge, it is not added to `TestRun.CostUSD`.
|
|
35
|
+
*
|
|
36
|
+
* A failed decision call gives a failed result with the error message. The oracle never throws.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const oracle = new DecisionJudgeOracle();
|
|
41
|
+
* const result = await oracle.evaluate({
|
|
42
|
+
* test,
|
|
43
|
+
* actualOutput: { response: 'Sales by region report created successfully' },
|
|
44
|
+
* expectedOutput: {
|
|
45
|
+
* judgeValidationCriteria: [
|
|
46
|
+
* 'Response accurately answers the user\'s question',
|
|
47
|
+
* { criterion: 'Response includes actionable information', weight: 2 }
|
|
48
|
+
* ]
|
|
49
|
+
* },
|
|
50
|
+
* contextUser
|
|
51
|
+
* }, {
|
|
52
|
+
* passThreshold: 0.7
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare class DecisionJudgeOracle implements IOracle {
|
|
57
|
+
readonly type = "decision-judge";
|
|
58
|
+
/** The decision prompt used when the config names none. */
|
|
59
|
+
static readonly DEFAULT_PROMPT_NAME = "Default Decision";
|
|
60
|
+
/** The per-criterion pass probability used when the config sets none, until calibration (Task 2.4) sets real values. */
|
|
61
|
+
static readonly DEFAULT_PASS_THRESHOLD = 0.5;
|
|
62
|
+
/**
|
|
63
|
+
* Evaluate output with a typed decision: one Likelihood question per criterion, in one call.
|
|
64
|
+
*
|
|
65
|
+
* @param input - Oracle input with the criteria, the expected output and the actual output
|
|
66
|
+
* @param config - Oracle configuration
|
|
67
|
+
* @returns Oracle result with the per-criterion probabilities; failed, never thrown, on any error
|
|
68
|
+
*/
|
|
69
|
+
evaluate(input: OracleInput, config: OracleConfig): Promise<OracleResult>;
|
|
70
|
+
/** The pass threshold, prompt name and timeout, defaulted, or the reason the config is invalid. */
|
|
71
|
+
private readSettings;
|
|
72
|
+
/** The decision prompt with this name, ignoring case and surrounding spaces. */
|
|
73
|
+
private findPrompt;
|
|
74
|
+
/** The one decision call: the judge's trace as the state, a question per criterion, and the timeout. */
|
|
75
|
+
private buildParams;
|
|
76
|
+
/** The trace as text, under the section names of the LLM judge's prompt. */
|
|
77
|
+
private buildState;
|
|
78
|
+
/** One Likelihood question per criterion, keyed by position: a key is a label for code only. */
|
|
79
|
+
private buildQuestions;
|
|
80
|
+
private questionKey;
|
|
81
|
+
/** Turns the decision into the oracle result: the pass rule and the weighted score, in code. */
|
|
82
|
+
private judge;
|
|
83
|
+
/** Each criterion's probability and pass, or the reason an answer cannot be scored. */
|
|
84
|
+
private readOutcomes;
|
|
85
|
+
/**
|
|
86
|
+
* The answer's probability, or the reason it cannot be scored: the answer is missing, is not a
|
|
87
|
+
* Likelihood, or its probability is not a number from 0 to 1. The range check also rejects NaN,
|
|
88
|
+
* which fails both comparisons, and so never lets it reach the pass rule.
|
|
89
|
+
*/
|
|
90
|
+
private readProbability;
|
|
91
|
+
/** The weighted mean of the probabilities. `ReadJudgeCriteria` guarantees a total weight above 0. */
|
|
92
|
+
private weightedMean;
|
|
93
|
+
private summarize;
|
|
94
|
+
private runDetails;
|
|
95
|
+
private failed;
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=DecisionJudgeOracle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DecisionJudgeOracle.d.ts","sourceRoot":"","sources":["../../src/oracles/DecisionJudgeOracle.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAMH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AA2BnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,qBAAa,mBAAoB,YAAW,OAAO;IAC/C,QAAQ,CAAC,IAAI,oBAAoB;IAEjC,2DAA2D;IAC3D,gBAAuB,mBAAmB,sBAAsB;IAEhE,wHAAwH;IACxH,gBAAuB,sBAAsB,OAAO;IAEpD;;;;;;OAMG;IACG,QAAQ,CAAC,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAwB/E,mGAAmG;IACnG,OAAO,CAAC,YAAY;IAgBpB,gFAAgF;IAChF,OAAO,CAAC,UAAU;IAKlB,wGAAwG;IACxG,OAAO,CAAC,WAAW;IAUnB,4EAA4E;IAC5E,OAAO,CAAC,UAAU;IAQlB,gGAAgG;IAChG,OAAO,CAAC,cAAc;IAWtB,OAAO,CAAC,WAAW;IAInB,gGAAgG;IAChG,OAAO,CAAC,KAAK;IAuBb,uFAAuF;IACvF,OAAO,CAAC,YAAY;IAiBpB;;;;OAIG;IACH,OAAO,CAAC,eAAe;IAWvB,qGAAqG;IACrG,OAAO,CAAC,YAAY;IAKpB,OAAO,CAAC,SAAS;IASjB,OAAO,CAAC,UAAU;IAQlB,OAAO,CAAC,MAAM;CAOjB"}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Decision Judge oracle implementation
|
|
3
|
+
* @module @memberjunction/testing-engine
|
|
4
|
+
*/
|
|
5
|
+
import { AIDecisionParams, AIDecisionRunner } from '@memberjunction/ai-prompts';
|
|
6
|
+
import { AIEngine } from '@memberjunction/aiengine';
|
|
7
|
+
import { BuildJudgeTrace, ReadJudgeCriteria, ReadJudgeTimeoutMS } from './judge-trace.js';
|
|
8
|
+
/**
|
|
9
|
+
* Decision Judge Oracle.
|
|
10
|
+
*
|
|
11
|
+
* Judges output against the same criteria, and the same trace, as {@link LLMJudgeOracle}, but asks a
|
|
12
|
+
* typed decision model instead of parsing an LLM's JSON. Each criterion becomes one Likelihood
|
|
13
|
+
* question, "The response satisfies: <criterion>", and every question travels in one
|
|
14
|
+
* `AIDecisionRunner` call. The pass rule and the weighting live here, in code, not in the prompt.
|
|
15
|
+
*
|
|
16
|
+
* Criteria come from `expectedOutput.judgeValidationCriteria`, or else from `config.criteria`, as for
|
|
17
|
+
* the LLM judge. Each is either a string or `{ "criterion": "...", "weight": 2 }`. A weight of 0 keeps a
|
|
18
|
+
* criterion out of the score but not out of the pass rule; at least one weight must be above 0.
|
|
19
|
+
*
|
|
20
|
+
* Configuration:
|
|
21
|
+
* - criteria: Array of criteria, used when the expected output has none
|
|
22
|
+
* - passThreshold: The probability every criterion must reach to pass (default: 0.5). The default is a
|
|
23
|
+
* placeholder: calibration (Task 2.4) sets real values.
|
|
24
|
+
* - promptName: The decision prompt whose model bindings choose the decision model
|
|
25
|
+
* (default: 'Default Decision')
|
|
26
|
+
* - timeoutMS: How long to wait for each decision model call, in milliseconds (default: 120000, two
|
|
27
|
+
* minutes). A call that runs over fails the oracle.
|
|
28
|
+
*
|
|
29
|
+
* Result:
|
|
30
|
+
* - passed: every criterion's probability is at or above `passThreshold`
|
|
31
|
+
* - score: the weighted mean of the probabilities (weights default to 1)
|
|
32
|
+
* - details: `criteriaProbabilities` (one entry per criterion), `passThreshold`, `promptName`,
|
|
33
|
+
* `decisionRunId` (the `MJ: AI Prompt Runs` row), `decisionModel`, and `llmCost`, the decision
|
|
34
|
+
* call's cost, under the key the LLM judge uses for its own. It is saved with the result details
|
|
35
|
+
* (`TestRun.ResultDetails`) but, as for the LLM judge, it is not added to `TestRun.CostUSD`.
|
|
36
|
+
*
|
|
37
|
+
* A failed decision call gives a failed result with the error message. The oracle never throws.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const oracle = new DecisionJudgeOracle();
|
|
42
|
+
* const result = await oracle.evaluate({
|
|
43
|
+
* test,
|
|
44
|
+
* actualOutput: { response: 'Sales by region report created successfully' },
|
|
45
|
+
* expectedOutput: {
|
|
46
|
+
* judgeValidationCriteria: [
|
|
47
|
+
* 'Response accurately answers the user\'s question',
|
|
48
|
+
* { criterion: 'Response includes actionable information', weight: 2 }
|
|
49
|
+
* ]
|
|
50
|
+
* },
|
|
51
|
+
* contextUser
|
|
52
|
+
* }, {
|
|
53
|
+
* passThreshold: 0.7
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export class DecisionJudgeOracle {
|
|
58
|
+
constructor() {
|
|
59
|
+
this.type = 'decision-judge';
|
|
60
|
+
}
|
|
61
|
+
/** The decision prompt used when the config names none. */
|
|
62
|
+
static { this.DEFAULT_PROMPT_NAME = 'Default Decision'; }
|
|
63
|
+
/** The per-criterion pass probability used when the config sets none, until calibration (Task 2.4) sets real values. */
|
|
64
|
+
static { this.DEFAULT_PASS_THRESHOLD = 0.5; }
|
|
65
|
+
/**
|
|
66
|
+
* Evaluate output with a typed decision: one Likelihood question per criterion, in one call.
|
|
67
|
+
*
|
|
68
|
+
* @param input - Oracle input with the criteria, the expected output and the actual output
|
|
69
|
+
* @param config - Oracle configuration
|
|
70
|
+
* @returns Oracle result with the per-criterion probabilities; failed, never thrown, on any error
|
|
71
|
+
*/
|
|
72
|
+
async evaluate(input, config) {
|
|
73
|
+
try {
|
|
74
|
+
const criteria = ReadJudgeCriteria(input, config);
|
|
75
|
+
if (!criteria.Success) {
|
|
76
|
+
return this.failed(criteria.ErrorMessage);
|
|
77
|
+
}
|
|
78
|
+
const settings = this.readSettings(config);
|
|
79
|
+
if (typeof settings === 'string') {
|
|
80
|
+
return this.failed(settings);
|
|
81
|
+
}
|
|
82
|
+
await AIEngine.Instance.Config(false, input.contextUser);
|
|
83
|
+
const prompt = this.findPrompt(settings.promptName);
|
|
84
|
+
if (!prompt) {
|
|
85
|
+
return this.failed(`Decision prompt "${settings.promptName}" not found in AIEngine.Instance.Prompts`);
|
|
86
|
+
}
|
|
87
|
+
const run = await new AIDecisionRunner().ExecuteDecision(this.buildParams(input, prompt, criteria.Value, settings.timeoutMS));
|
|
88
|
+
return this.judge(run, criteria.Value, settings);
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
return this.failed(`Decision judge error: ${error instanceof Error ? error.message : String(error)}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** The pass threshold, prompt name and timeout, defaulted, or the reason the config is invalid. */
|
|
95
|
+
readSettings(config) {
|
|
96
|
+
const passThreshold = config.passThreshold ?? DecisionJudgeOracle.DEFAULT_PASS_THRESHOLD;
|
|
97
|
+
if (typeof passThreshold !== 'number' || !(passThreshold >= 0 && passThreshold <= 1)) {
|
|
98
|
+
return `passThreshold must be a number from 0 to 1, not ${JSON.stringify(passThreshold)}`;
|
|
99
|
+
}
|
|
100
|
+
const promptName = config.promptName ?? DecisionJudgeOracle.DEFAULT_PROMPT_NAME;
|
|
101
|
+
if (typeof promptName !== 'string' || !promptName.trim()) {
|
|
102
|
+
return `promptName must be a non-empty string, not ${JSON.stringify(promptName)}`;
|
|
103
|
+
}
|
|
104
|
+
const timeout = ReadJudgeTimeoutMS(config);
|
|
105
|
+
if (!timeout.Success) {
|
|
106
|
+
return timeout.ErrorMessage;
|
|
107
|
+
}
|
|
108
|
+
return { passThreshold, promptName, timeoutMS: timeout.Value };
|
|
109
|
+
}
|
|
110
|
+
/** The decision prompt with this name, ignoring case and surrounding spaces. */
|
|
111
|
+
findPrompt(promptName) {
|
|
112
|
+
const target = promptName.trim().toLowerCase();
|
|
113
|
+
return AIEngine.Instance.Prompts.find(p => (p.Name ?? '').trim().toLowerCase() === target);
|
|
114
|
+
}
|
|
115
|
+
/** The one decision call: the judge's trace as the state, a question per criterion, and the timeout. */
|
|
116
|
+
buildParams(input, prompt, criteria, timeoutMS) {
|
|
117
|
+
const params = new AIDecisionParams();
|
|
118
|
+
params.prompt = prompt;
|
|
119
|
+
params.contextUser = input.contextUser;
|
|
120
|
+
params.timeoutMS = timeoutMS;
|
|
121
|
+
params.State = this.buildState(BuildJudgeTrace(input));
|
|
122
|
+
params.Questions = this.buildQuestions(criteria);
|
|
123
|
+
return params;
|
|
124
|
+
}
|
|
125
|
+
/** The trace as text, under the section names of the LLM judge's prompt. */
|
|
126
|
+
buildState(trace) {
|
|
127
|
+
return [
|
|
128
|
+
`Input:\n${trace.Input ?? '(none)'}`,
|
|
129
|
+
`Expected Output Requirements:\n${trace.Expected ?? '(none)'}`,
|
|
130
|
+
`Actual Output:\n${trace.Actual ?? '(none)'}`,
|
|
131
|
+
].join('\n\n');
|
|
132
|
+
}
|
|
133
|
+
/** One Likelihood question per criterion, keyed by position: a key is a label for code only. */
|
|
134
|
+
buildQuestions(criteria) {
|
|
135
|
+
const questions = {};
|
|
136
|
+
criteria.forEach((c, index) => {
|
|
137
|
+
questions[this.questionKey(index)] = {
|
|
138
|
+
Kind: 'Likelihood',
|
|
139
|
+
Instructions: `The response satisfies: ${c.Criterion}`,
|
|
140
|
+
};
|
|
141
|
+
});
|
|
142
|
+
return questions;
|
|
143
|
+
}
|
|
144
|
+
questionKey(index) {
|
|
145
|
+
return `criterion_${index + 1}`;
|
|
146
|
+
}
|
|
147
|
+
/** Turns the decision into the oracle result: the pass rule and the weighted score, in code. */
|
|
148
|
+
judge(run, criteria, settings) {
|
|
149
|
+
const runDetails = this.runDetails(run);
|
|
150
|
+
if (!run.success) {
|
|
151
|
+
return this.failed(`Decision judgment failed: ${run.errorMessage || 'unknown error'}`, runDetails);
|
|
152
|
+
}
|
|
153
|
+
const outcomes = this.readOutcomes(run, criteria, settings.passThreshold);
|
|
154
|
+
if (typeof outcomes === 'string') {
|
|
155
|
+
return this.failed(outcomes, runDetails);
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
oracleType: this.type,
|
|
159
|
+
passed: outcomes.every(o => o.passed),
|
|
160
|
+
score: this.weightedMean(outcomes),
|
|
161
|
+
message: this.summarize(outcomes, settings.passThreshold),
|
|
162
|
+
details: {
|
|
163
|
+
criteriaProbabilities: outcomes,
|
|
164
|
+
passThreshold: settings.passThreshold,
|
|
165
|
+
promptName: settings.promptName,
|
|
166
|
+
...runDetails,
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/** Each criterion's probability and pass, or the reason an answer cannot be scored. */
|
|
171
|
+
readOutcomes(run, criteria, passThreshold) {
|
|
172
|
+
const outcomes = [];
|
|
173
|
+
for (const [index, c] of criteria.entries()) {
|
|
174
|
+
const probability = this.readProbability(run.Answers[this.questionKey(index)], `criterion ${index + 1}: ${c.Criterion}`);
|
|
175
|
+
if (typeof probability === 'string') {
|
|
176
|
+
return probability;
|
|
177
|
+
}
|
|
178
|
+
outcomes.push({
|
|
179
|
+
criterion: c.Criterion,
|
|
180
|
+
probability,
|
|
181
|
+
weight: c.Weight,
|
|
182
|
+
passed: probability >= passThreshold,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return outcomes;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* The answer's probability, or the reason it cannot be scored: the answer is missing, is not a
|
|
189
|
+
* Likelihood, or its probability is not a number from 0 to 1. The range check also rejects NaN,
|
|
190
|
+
* which fails both comparisons, and so never lets it reach the pass rule.
|
|
191
|
+
*/
|
|
192
|
+
readProbability(answer, label) {
|
|
193
|
+
if (answer?.Kind !== 'Likelihood') {
|
|
194
|
+
return `The decision returned no Likelihood answer for ${label}`;
|
|
195
|
+
}
|
|
196
|
+
const probability = answer.Probability;
|
|
197
|
+
if (typeof probability !== 'number' || !(probability >= 0 && probability <= 1)) {
|
|
198
|
+
return `The decision returned a probability of ${probability}, not a number from 0 to 1, for ${label}`;
|
|
199
|
+
}
|
|
200
|
+
return probability;
|
|
201
|
+
}
|
|
202
|
+
/** The weighted mean of the probabilities. `ReadJudgeCriteria` guarantees a total weight above 0. */
|
|
203
|
+
weightedMean(outcomes) {
|
|
204
|
+
const totalWeight = outcomes.reduce((sum, o) => sum + o.weight, 0);
|
|
205
|
+
return outcomes.reduce((sum, o) => sum + o.probability * o.weight, 0) / totalWeight;
|
|
206
|
+
}
|
|
207
|
+
summarize(outcomes, passThreshold) {
|
|
208
|
+
const below = outcomes.filter(o => !o.passed);
|
|
209
|
+
if (below.length === 0) {
|
|
210
|
+
return `All ${outcomes.length} criteria are at or above the pass threshold of ${passThreshold}`;
|
|
211
|
+
}
|
|
212
|
+
const list = below.map(o => `"${o.criterion}" (${o.probability.toFixed(2)})`).join(', ');
|
|
213
|
+
return `${below.length} of ${outcomes.length} criteria are below the pass threshold of ${passThreshold}: ${list}`;
|
|
214
|
+
}
|
|
215
|
+
runDetails(run) {
|
|
216
|
+
return {
|
|
217
|
+
decisionRunId: run.promptRun?.ID,
|
|
218
|
+
decisionModel: run.modelInfo?.modelName,
|
|
219
|
+
llmCost: run.cost,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
failed(message, details) {
|
|
223
|
+
const result = { oracleType: this.type, passed: false, score: 0, message };
|
|
224
|
+
if (details) {
|
|
225
|
+
result.details = details;
|
|
226
|
+
}
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
//# sourceMappingURL=DecisionJudgeOracle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DecisionJudgeOracle.js","sourceRoot":"","sources":["../../src/oracles/DecisionJudgeOracle.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,EAAE,gBAAgB,EAAuB,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AACrG,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAEpD,OAAO,EAAE,eAAe,EAA8B,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AA4BnH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,MAAM,OAAO,mBAAmB;IAAhC;QACa,SAAI,GAAG,gBAAgB,CAAC;IA0LrC,CAAC;IAxLG,2DAA2D;aACpC,wBAAmB,GAAG,kBAAkB,AAArB,CAAsB;IAEhE,wHAAwH;aACjG,2BAAsB,GAAG,GAAG,AAAN,CAAO;IAEpD;;;;;;OAMG;IACH,KAAK,CAAC,QAAQ,CAAC,KAAkB,EAAE,MAAoB;QACnD,IAAI,CAAC;YACD,MAAM,QAAQ,GAAG,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAClD,IAAI,CAAC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,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,SAAS,MAAM,CAAC,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACzF,OAAO,GAAG,KAAK,CAAC,MAAM,OAAO,QAAQ,CAAC,MAAM,6CAA6C,aAAa,KAAK,IAAI,EAAE,CAAC;IACtH,CAAC;IAEO,UAAU,CAAC,GAAwB;QACvC,OAAO;YACH,aAAa,EAAE,GAAG,CAAC,SAAS,EAAE,EAAE;YAChC,aAAa,EAAE,GAAG,CAAC,SAAS,EAAE,SAAS;YACvC,OAAO,EAAE,GAAG,CAAC,IAAI;SACpB,CAAC;IACN,CAAC;IAEO,MAAM,CAAC,OAAe,EAAE,OAA4B;QACxD,MAAM,MAAM,GAAiB,EAAE,UAAU,EAAE,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC;QACzF,IAAI,OAAO,EAAE,CAAC;YACV,MAAM,CAAC,OAAO,GAAG,OAAO,CAAC;QAC7B,CAAC;QACD,OAAO,MAAM,CAAC;IAClB,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview `decision-label-match`: does a decision's Likelihood agree with the point's label?
|
|
3
|
+
* @module @memberjunction/testing-engine
|
|
4
|
+
*/
|
|
5
|
+
import { IOracle } from './IOracle.js';
|
|
6
|
+
import { OracleInput, OracleConfig, OracleResult } from '../types.js';
|
|
7
|
+
/**
|
|
8
|
+
* `decision-label-match`: reads one Likelihood's probability from a Decision Eval run's answers
|
|
9
|
+
* (by default `continues`) and compares `probability >= threshold` with `label === positiveLabel`.
|
|
10
|
+
*
|
|
11
|
+
* - The result passes, with score 1, when the two agree, and fails with score 0 otherwise.
|
|
12
|
+
* - `details` carry `{ probability, label, positive, correct, brier }`, so the scorecard can
|
|
13
|
+
* aggregate without re-running anything.
|
|
14
|
+
* - An `ambiguous` label is not scored: the result is `advisory`, so it never gates the run.
|
|
15
|
+
* - A run with no such Likelihood fails, naming why.
|
|
16
|
+
*
|
|
17
|
+
* Configuration: `{ question = 'continues', positiveLabel = 'continue', threshold = 0.5 }`.
|
|
18
|
+
*/
|
|
19
|
+
export declare class DecisionLabelMatchOracle implements IOracle {
|
|
20
|
+
readonly type = "decision-label-match";
|
|
21
|
+
evaluate(input: OracleInput, config: OracleConfig): Promise<OracleResult>;
|
|
22
|
+
/** A scored comparison of the probability with the label. */
|
|
23
|
+
private scored;
|
|
24
|
+
/** An unscored result for an ambiguous label: reported, never gating. */
|
|
25
|
+
private advisory;
|
|
26
|
+
/** 1 when the label is the positive class, else 0. */
|
|
27
|
+
private positiveOf;
|
|
28
|
+
private failed;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A Likelihood's probability from a Decision Eval `ActualOutput`, or null when the output has no
|
|
32
|
+
* readable answers or no Likelihood under that key.
|
|
33
|
+
*
|
|
34
|
+
* @param actualOutput The run's actual output, as the driver recorded it.
|
|
35
|
+
* @param question The Likelihood's key.
|
|
36
|
+
*/
|
|
37
|
+
export declare function ReadLikelihood(actualOutput: unknown, question: string): number | null;
|
|
38
|
+
//# sourceMappingURL=DecisionLabelMatchOracle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DecisionLabelMatchOracle.d.ts","sourceRoot":"","sources":["../../src/oracles/DecisionLabelMatchOracle.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAanE;;;;;;;;;;;GAWG;AACH,qBAAa,wBAAyB,YAAW,OAAO;IACpD,QAAQ,CAAC,IAAI,0BAA0B;IAEjC,QAAQ,CAAC,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAqB/E,6DAA6D;IAC7D,OAAO,CAAC,MAAM;IAed,yEAAyE;IACzE,OAAO,CAAC,QAAQ;IAahB,sDAAsD;IACtD,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,MAAM;CAGjB;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAOrF"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview `decision-label-match`: does a decision's Likelihood agree with the point's label?
|
|
3
|
+
* @module @memberjunction/testing-engine
|
|
4
|
+
*/
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { DecisionEvalAnswerSummarySchema, DecisionEvalExpectedSchema, DecisionLabelMatchConfigSchema, DescribeZodError } from '../decision-eval/types.js';
|
|
7
|
+
/** The part of a Decision Eval `ActualOutput` this oracle reads: the summarized answers. */
|
|
8
|
+
const AnswersOnlySchema = z.object({ Answers: z.record(DecisionEvalAnswerSummarySchema) });
|
|
9
|
+
/**
|
|
10
|
+
* `decision-label-match`: reads one Likelihood's probability from a Decision Eval run's answers
|
|
11
|
+
* (by default `continues`) and compares `probability >= threshold` with `label === positiveLabel`.
|
|
12
|
+
*
|
|
13
|
+
* - The result passes, with score 1, when the two agree, and fails with score 0 otherwise.
|
|
14
|
+
* - `details` carry `{ probability, label, positive, correct, brier }`, so the scorecard can
|
|
15
|
+
* aggregate without re-running anything.
|
|
16
|
+
* - An `ambiguous` label is not scored: the result is `advisory`, so it never gates the run.
|
|
17
|
+
* - A run with no such Likelihood fails, naming why.
|
|
18
|
+
*
|
|
19
|
+
* Configuration: `{ question = 'continues', positiveLabel = 'continue', threshold = 0.5 }`.
|
|
20
|
+
*/
|
|
21
|
+
export class DecisionLabelMatchOracle {
|
|
22
|
+
constructor() {
|
|
23
|
+
this.type = 'decision-label-match';
|
|
24
|
+
}
|
|
25
|
+
async evaluate(input, config) {
|
|
26
|
+
const settings = DecisionLabelMatchConfigSchema.safeParse(config ?? {});
|
|
27
|
+
if (!settings.success) {
|
|
28
|
+
return this.failed(`Invalid configuration: ${DescribeZodError(settings.error)}`);
|
|
29
|
+
}
|
|
30
|
+
const expected = DecisionEvalExpectedSchema.safeParse(input.expectedOutput);
|
|
31
|
+
if (!expected.success) {
|
|
32
|
+
return this.failed(`Expected outcome has no usable label: ${DescribeZodError(expected.error)}`);
|
|
33
|
+
}
|
|
34
|
+
const probability = ReadLikelihood(input.actualOutput, settings.data.question);
|
|
35
|
+
if (expected.data.label === 'ambiguous') {
|
|
36
|
+
return this.advisory(probability, settings.data);
|
|
37
|
+
}
|
|
38
|
+
if (probability === null) {
|
|
39
|
+
return this.failed(`No '${settings.data.question}' Likelihood in the answers`, {
|
|
40
|
+
probability: null, label: expected.data.label, positive: this.positiveOf(expected.data.label, settings.data), correct: false, brier: null
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return this.scored(probability, expected.data.label, settings.data);
|
|
44
|
+
}
|
|
45
|
+
/** A scored comparison of the probability with the label. */
|
|
46
|
+
scored(probability, label, settings) {
|
|
47
|
+
const positive = this.positiveOf(label, settings);
|
|
48
|
+
const predictedPositive = probability >= settings.threshold;
|
|
49
|
+
const correct = predictedPositive === (positive === 1);
|
|
50
|
+
const details = { probability, label, positive, correct, brier: (probability - positive) ** 2 };
|
|
51
|
+
return {
|
|
52
|
+
oracleType: this.type,
|
|
53
|
+
passed: correct,
|
|
54
|
+
score: correct ? 1 : 0,
|
|
55
|
+
message: `${settings.question} = ${probability.toFixed(3)} ${predictedPositive ? '>=' : '<'} ${settings.threshold}; `
|
|
56
|
+
+ `label '${label}'${correct ? '' : ' (disagrees)'}`,
|
|
57
|
+
details
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/** An unscored result for an ambiguous label: reported, never gating. */
|
|
61
|
+
advisory(probability, settings) {
|
|
62
|
+
const details = { probability, label: 'ambiguous', positive: null, correct: null, brier: null };
|
|
63
|
+
const kept = probability === null ? 'no probability' : probability >= settings.threshold ? 'kept with the previous agent' : 'moved away';
|
|
64
|
+
return {
|
|
65
|
+
oracleType: this.type,
|
|
66
|
+
passed: true,
|
|
67
|
+
score: 0,
|
|
68
|
+
advisory: true,
|
|
69
|
+
message: `Ambiguous label: not scored (${kept})`,
|
|
70
|
+
details
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/** 1 when the label is the positive class, else 0. */
|
|
74
|
+
positiveOf(label, settings) {
|
|
75
|
+
return label === settings.positiveLabel ? 1 : 0;
|
|
76
|
+
}
|
|
77
|
+
failed(message, details) {
|
|
78
|
+
return { oracleType: this.type, passed: false, score: 0, message, details };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A Likelihood's probability from a Decision Eval `ActualOutput`, or null when the output has no
|
|
83
|
+
* readable answers or no Likelihood under that key.
|
|
84
|
+
*
|
|
85
|
+
* @param actualOutput The run's actual output, as the driver recorded it.
|
|
86
|
+
* @param question The Likelihood's key.
|
|
87
|
+
*/
|
|
88
|
+
export function ReadLikelihood(actualOutput, question) {
|
|
89
|
+
const parsed = AnswersOnlySchema.safeParse(actualOutput);
|
|
90
|
+
if (!parsed.success) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
const answer = parsed.data.Answers[question];
|
|
94
|
+
return answer?.Kind === 'Likelihood' && typeof answer.Probability === 'number' ? answer.Probability : null;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=DecisionLabelMatchOracle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DecisionLabelMatchOracle.js","sourceRoot":"","sources":["../../src/oracles/DecisionLabelMatchOracle.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EACH,+BAA+B,EA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AI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC7C,OAAO,MAAM,EAAE,IAAI,KAAK,YAAY,IAAI,OAAO,MAAM,CAAC,WAAW,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC;AAC/G,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview `discovery-label-match`: did agent discovery reach the agent the request's label
|
|
3
|
+
* names, or, for a request no specialist should handle, stay out of the way?
|
|
4
|
+
* @module @memberjunction/testing-engine
|
|
5
|
+
*/
|
|
6
|
+
import { IOracle } from './IOracle.js';
|
|
7
|
+
import { OracleInput, OracleConfig, OracleResult } from '../types.js';
|
|
8
|
+
import { type DiscoveryEvalActualOutput, type DiscoveryEvalExpected, type DiscoveryLabelMatchDetails } from '../decision-eval/discovery-types.js';
|
|
9
|
+
/**
|
|
10
|
+
* `discovery-label-match`: scores an agent-discovery run against its label, for either arm.
|
|
11
|
+
*
|
|
12
|
+
* - An `agent` label is correct when the chosen agent is the labelled agent: the Choice's pick for
|
|
13
|
+
* the decision, whatever its confidence, or the first row `Find Candidate Agents` would return
|
|
14
|
+
* for the `semantic-search` baseline.
|
|
15
|
+
* - A `none` label is correct when discovery would not inject a suggestion (the run's `WouldInject`):
|
|
16
|
+
* production's own verdict, `DecisionDiscoveryFromResult`, not confident once the answers are
|
|
17
|
+
* calibrated for the answering model, or that model having no calibration, or the discovery
|
|
18
|
+
* finishing after production's timeout (decision); or no candidate reaching the action's
|
|
19
|
+
* similarity floor (baseline).
|
|
20
|
+
* - A run with no usable answer fails, naming why, with `correct: null`.
|
|
21
|
+
*
|
|
22
|
+
* `details` carry `{ arm, label, kind, expectedAgentId, chosenAgentId, confidence, anyApplies,
|
|
23
|
+
* wouldInject, topScore, correct }`, so the scorecard can aggregate without re-running anything.
|
|
24
|
+
* It takes no configuration.
|
|
25
|
+
*/
|
|
26
|
+
export declare class DiscoveryLabelMatchOracle implements IOracle {
|
|
27
|
+
readonly type = "discovery-label-match";
|
|
28
|
+
evaluate(input: OracleInput, _config: OracleConfig): Promise<OracleResult>;
|
|
29
|
+
private failed;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Scores one discovery run against its label (see {@link DiscoveryLabelMatchOracle}). `correct` is
|
|
33
|
+
* null when the run has no usable answer: `WouldInject` unknown, or, for the decision, no Choice.
|
|
34
|
+
*
|
|
35
|
+
* @param expected The run's label.
|
|
36
|
+
* @param actual The run's actual output.
|
|
37
|
+
*/
|
|
38
|
+
export declare function ScoreDiscoveryRun(expected: DiscoveryEvalExpected, actual: DiscoveryEvalActualOutput): DiscoveryLabelMatchDetails;
|
|
39
|
+
//# sourceMappingURL=DiscoveryLabelMatchOracle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DiscoveryLabelMatchOracle.d.ts","sourceRoot":"","sources":["../../src/oracles/DiscoveryLabelMatchOracle.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAEnE,OAAO,EAGH,KAAK,yBAAyB,EAC9B,KAAK,qBAAqB,EAC1B,KAAK,0BAA0B,EAClC,MAAM,kCAAkC,CAAC;AAE1C;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,yBAA0B,YAAW,OAAO;IACrD,QAAQ,CAAC,IAAI,2BAA2B;IAElC,QAAQ,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAsBhF,OAAO,CAAC,MAAM;CAGjB;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,qBAAqB,EAAE,MAAM,EAAE,yBAAyB,GAAG,0BAA0B,CAiBhI"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview `discovery-label-match`: did agent discovery reach the agent the request's label
|
|
3
|
+
* names, or, for a request no specialist should handle, stay out of the way?
|
|
4
|
+
* @module @memberjunction/testing-engine
|
|
5
|
+
*/
|
|
6
|
+
import { UUIDsEqual } from '@memberjunction/global';
|
|
7
|
+
import { DescribeZodError } from '../decision-eval/types.js';
|
|
8
|
+
import { DiscoveryEvalActualOutputSchema, DiscoveryEvalExpectedSchema } from '../decision-eval/discovery-types.js';
|
|
9
|
+
/**
|
|
10
|
+
* `discovery-label-match`: scores an agent-discovery run against its label, for either arm.
|
|
11
|
+
*
|
|
12
|
+
* - An `agent` label is correct when the chosen agent is the labelled agent: the Choice's pick for
|
|
13
|
+
* the decision, whatever its confidence, or the first row `Find Candidate Agents` would return
|
|
14
|
+
* for the `semantic-search` baseline.
|
|
15
|
+
* - A `none` label is correct when discovery would not inject a suggestion (the run's `WouldInject`):
|
|
16
|
+
* production's own verdict, `DecisionDiscoveryFromResult`, not confident once the answers are
|
|
17
|
+
* calibrated for the answering model, or that model having no calibration, or the discovery
|
|
18
|
+
* finishing after production's timeout (decision); or no candidate reaching the action's
|
|
19
|
+
* similarity floor (baseline).
|
|
20
|
+
* - A run with no usable answer fails, naming why, with `correct: null`.
|
|
21
|
+
*
|
|
22
|
+
* `details` carry `{ arm, label, kind, expectedAgentId, chosenAgentId, confidence, anyApplies,
|
|
23
|
+
* wouldInject, topScore, correct }`, so the scorecard can aggregate without re-running anything.
|
|
24
|
+
* It takes no configuration.
|
|
25
|
+
*/
|
|
26
|
+
export class DiscoveryLabelMatchOracle {
|
|
27
|
+
constructor() {
|
|
28
|
+
this.type = 'discovery-label-match';
|
|
29
|
+
}
|
|
30
|
+
async evaluate(input, _config) {
|
|
31
|
+
const expected = DiscoveryEvalExpectedSchema.safeParse(input.expectedOutput);
|
|
32
|
+
if (!expected.success) {
|
|
33
|
+
return this.failed(`Expected outcome has no usable label: ${DescribeZodError(expected.error)}`);
|
|
34
|
+
}
|
|
35
|
+
const actual = DiscoveryEvalActualOutputSchema.safeParse(input.actualOutput);
|
|
36
|
+
if (!actual.success) {
|
|
37
|
+
return this.failed(`Actual output is not a discovery run's: ${DescribeZodError(actual.error)}`);
|
|
38
|
+
}
|
|
39
|
+
const details = ScoreDiscoveryRun(expected.data, actual.data);
|
|
40
|
+
if (details.correct === null) {
|
|
41
|
+
return this.failed(`No usable answer: ${actual.data.Error ?? actual.data.VerdictReason ?? 'the run chose no agent'}`, details);
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
oracleType: this.type,
|
|
45
|
+
passed: details.correct,
|
|
46
|
+
score: details.correct ? 1 : 0,
|
|
47
|
+
message: describe(details),
|
|
48
|
+
details
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
failed(message, details) {
|
|
52
|
+
return { oracleType: this.type, passed: false, score: 0, message, details };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Scores one discovery run against its label (see {@link DiscoveryLabelMatchOracle}). `correct` is
|
|
57
|
+
* null when the run has no usable answer: `WouldInject` unknown, or, for the decision, no Choice.
|
|
58
|
+
*
|
|
59
|
+
* @param expected The run's label.
|
|
60
|
+
* @param actual The run's actual output.
|
|
61
|
+
*/
|
|
62
|
+
export function ScoreDiscoveryRun(expected, actual) {
|
|
63
|
+
const expectedAgentId = expected.label === 'agent' ? expected.agentId : null;
|
|
64
|
+
const answered = actual.WouldInject !== null && (actual.Arm !== 'decision' || actual.ChosenAgentId !== null);
|
|
65
|
+
return {
|
|
66
|
+
arm: actual.Arm,
|
|
67
|
+
label: expected.label,
|
|
68
|
+
kind: expected.label === 'none' ? expected.kind : null,
|
|
69
|
+
expectedAgentId,
|
|
70
|
+
chosenAgentId: actual.ChosenAgentId,
|
|
71
|
+
confidence: actual.Confidence,
|
|
72
|
+
anyApplies: actual.AnyApplies,
|
|
73
|
+
wouldInject: actual.WouldInject,
|
|
74
|
+
topScore: actual.Baseline?.TopMatch?.Score ?? null,
|
|
75
|
+
correct: !answered ? null : expectedAgentId !== null
|
|
76
|
+
? !!actual.ChosenAgentId && UUIDsEqual(actual.ChosenAgentId, expectedAgentId)
|
|
77
|
+
: actual.WouldInject === false
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/** The result's message. */
|
|
81
|
+
function describe(details) {
|
|
82
|
+
const verdict = details.correct ? '' : ' (disagrees)';
|
|
83
|
+
if (details.label === 'agent') {
|
|
84
|
+
return `chose ${details.chosenAgentId ?? 'no agent'}; labelled ${details.expectedAgentId}${verdict}`;
|
|
85
|
+
}
|
|
86
|
+
return `${details.wouldInject ? 'would suggest' : 'would not suggest'} ${details.chosenAgentId ?? 'an agent'}; labelled none (${details.kind})${verdict}`;
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=DiscoveryLabelMatchOracle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DiscoveryLabelMatchOracle.js","sourceRoot":"","sources":["../../src/oracles/DiscoveryLabelMatchOracle.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAGpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EACH,+BAA+B,EAC/B,2BAA2B,EAI9B,MAAM,kCAAkC,CAAC;AAE1C;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,yBAAyB;IAAtC;QACa,SAAI,GAAG,uBAAuB,CAAC;IA2B5C,CAAC;IAzBG,KAAK,CAAC,QAAQ,CAAC,KAAkB,EAAE,OAAqB;QACpD,MAAM,QAAQ,GAAG,2BAA2B,CAAC,SAAS,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;QAC7E,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;YACpB,OAAO,IAAI,CAAC,MAAM,CAAC,yCAAyC,gBAAgB,CAAC,QA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