@fabricorg/experiments-harness-evals 0.0.0-stage → 1.0.1
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/LICENSE +202 -0
- package/README.md +77 -2
- package/dist/index.cjs +148 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +32 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +144 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,78 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @fabricorg/experiments-harness-evals
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Use Fabric Experiments evaluators as Fabric Harness scorers, or collect Harness
|
|
4
|
+
case results and prompt provenance for an Experiments evaluation run. This
|
|
5
|
+
package replaces `@fabric-harness/evals/experiments` from Harness evals 3.0.0.
|
|
6
|
+
Requires Node.js 22.19.0 or newer.
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install @fabricorg/experiments-harness-evals @fabricorg/experiments-evals @fabric-harness/evals
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Use an Experiments evaluator in a Harness suite
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { runEvalSuite } from '@fabric-harness/evals';
|
|
16
|
+
import { codeEvaluator } from '@fabricorg/experiments-evals';
|
|
17
|
+
import { experimentsEvaluatorScorer } from '@fabricorg/experiments-harness-evals';
|
|
18
|
+
|
|
19
|
+
const exactMatch = codeEvaluator('exact-match', '1', ({ output, expectedOutput }) => ({
|
|
20
|
+
score: output === expectedOutput ? 1 : 0,
|
|
21
|
+
}));
|
|
22
|
+
const cases = [{ id: 'greeting', input: 'hello', expected: 'HELLO' }];
|
|
23
|
+
|
|
24
|
+
const result = await runEvalSuite({
|
|
25
|
+
name: 'greetings',
|
|
26
|
+
cases,
|
|
27
|
+
runner: async ({ case: example }) => example.input.toUpperCase(),
|
|
28
|
+
scorers: [experimentsEvaluatorScorer(exactMatch)],
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Pass `options.model` when adapting an LLM judge. Harness records scoring errors
|
|
33
|
+
and null verdicts as unscored results.
|
|
34
|
+
|
|
35
|
+
## Evaluate Harness runs in Experiments
|
|
36
|
+
|
|
37
|
+
Using `cases` and `exactMatch` from the first example, forward each invocation's
|
|
38
|
+
Harness event callback from your agent runner.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { executeEvalRun, type ModelClient } from '@fabricorg/experiments-evals';
|
|
42
|
+
import { buildExperimentsRunItems } from '@fabricorg/experiments-harness-evals';
|
|
43
|
+
|
|
44
|
+
const items = await buildExperimentsRunItems(cases, (example, { onEvent }) =>
|
|
45
|
+
runGreetingAgent(example.input, { onEvent }),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
// executeEvalRun requires a model client in its context, even for code evaluators.
|
|
49
|
+
const model: ModelClient = {
|
|
50
|
+
endpoint: '',
|
|
51
|
+
async complete() {
|
|
52
|
+
throw new Error('This evaluation only runs code evaluators.');
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
const result = await executeEvalRun({
|
|
56
|
+
evaluators: [exactMatch],
|
|
57
|
+
items,
|
|
58
|
+
ctx: { model, nowMs: Date.now },
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The collector reads prompt ID and version from `data.metadata.promptId` and
|
|
63
|
+
`data.metadata.promptVersion`, and model from `data.model` on successful
|
|
64
|
+
`model_attempt` events associated with the last completed prompt. Harness
|
|
65
|
+
maintainers state that the SDK emits these fields starting in version 6.11.0.
|
|
66
|
+
Without those fields the adapter reports no prompt ID or prompt version. When
|
|
67
|
+
no model is observed, it uses the `model` option passed to
|
|
68
|
+
`buildExperimentsRunItems`, if one is set.
|
|
69
|
+
|
|
70
|
+
## License and attribution
|
|
71
|
+
|
|
72
|
+
Licensed under the Apache License, Version 2.0. The adapter and its private
|
|
73
|
+
scorer wrapper are adapted from TechFabric Harness.
|
|
74
|
+
|
|
75
|
+
TechFabric Harness
|
|
76
|
+
Copyright 2026 Fabric
|
|
77
|
+
|
|
78
|
+
This product is licensed under the Apache License, Version 2.0.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var evals = require('@fabric-harness/evals');
|
|
4
|
+
|
|
5
|
+
// src/index.ts
|
|
6
|
+
function namedScorer(name, score) {
|
|
7
|
+
function rethrow(cause) {
|
|
8
|
+
if (cause instanceof evals.EvalUnscoredError && cause.scorerName === name) throw cause;
|
|
9
|
+
const reason = cause instanceof Error ? cause.message : typeof cause === "string" ? cause : "Scoring failed.";
|
|
10
|
+
throw new evals.EvalUnscoredError(name, reason, { cause });
|
|
11
|
+
}
|
|
12
|
+
return (input) => {
|
|
13
|
+
try {
|
|
14
|
+
const result = score(input);
|
|
15
|
+
return result instanceof Promise ? result.catch(rethrow) : result;
|
|
16
|
+
} catch (cause) {
|
|
17
|
+
return rethrow(cause);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
var noModel = {
|
|
22
|
+
endpoint: "",
|
|
23
|
+
async complete() {
|
|
24
|
+
throw new Error("This evaluator requires a model client. Pass options.model.");
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
function experimentsEvaluatorScorer(evaluator, options = {}) {
|
|
28
|
+
const name = evaluator.name;
|
|
29
|
+
return namedScorer(name, async ({ case: evalCase, output }) => {
|
|
30
|
+
if (evaluator.kind === "llm-judge" && options.model === void 0) {
|
|
31
|
+
throw new evals.EvalUnscoredError(
|
|
32
|
+
name,
|
|
33
|
+
`Evaluator "${name}" requires a model client. Pass options.model.`
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
const input = {
|
|
37
|
+
input: evalCase.input,
|
|
38
|
+
output,
|
|
39
|
+
expectedOutput: evalCase.expected,
|
|
40
|
+
...evalCase.metadata === void 0 ? {} : { metadata: evalCase.metadata },
|
|
41
|
+
...options.context === void 0 ? {} : { context: options.context }
|
|
42
|
+
};
|
|
43
|
+
const result = await evaluator.evaluate(input, {
|
|
44
|
+
model: options.model ?? noModel,
|
|
45
|
+
nowMs: options.nowMs ?? Date.now
|
|
46
|
+
});
|
|
47
|
+
if (result.score === null) {
|
|
48
|
+
throw new evals.EvalUnscoredError(name, `Evaluator "${name}" returned no score.`);
|
|
49
|
+
}
|
|
50
|
+
const {
|
|
51
|
+
evaluatorName,
|
|
52
|
+
evaluatorVersion,
|
|
53
|
+
score,
|
|
54
|
+
explanation,
|
|
55
|
+
label,
|
|
56
|
+
tokens,
|
|
57
|
+
latencyMs,
|
|
58
|
+
modelEndpoint,
|
|
59
|
+
promptHash
|
|
60
|
+
} = result;
|
|
61
|
+
return {
|
|
62
|
+
name: evaluatorName,
|
|
63
|
+
score,
|
|
64
|
+
...explanation === void 0 ? {} : { reason: explanation },
|
|
65
|
+
metadata: {
|
|
66
|
+
evaluatorVersion,
|
|
67
|
+
latencyMs,
|
|
68
|
+
...label === void 0 ? {} : { label },
|
|
69
|
+
...tokens === void 0 ? {} : { tokens: { ...tokens } },
|
|
70
|
+
...modelEndpoint === void 0 ? {} : { modelEndpoint },
|
|
71
|
+
...promptHash === void 0 ? {} : { promptHash }
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function observedString(value) {
|
|
77
|
+
return typeof value === "string" && value !== "" ? value : void 0;
|
|
78
|
+
}
|
|
79
|
+
function createExperimentsRunCollector() {
|
|
80
|
+
const models = /* @__PURE__ */ new Map();
|
|
81
|
+
const prompts = /* @__PURE__ */ new Map();
|
|
82
|
+
let completedPrompt = {};
|
|
83
|
+
let completedModel;
|
|
84
|
+
let completedOutput;
|
|
85
|
+
function onEvent(event) {
|
|
86
|
+
const data = event.data;
|
|
87
|
+
const key = JSON.stringify([event.sessionId, data?.promptRunId]);
|
|
88
|
+
if (event.type === "model_attempt") {
|
|
89
|
+
const model = observedString(data?.model);
|
|
90
|
+
if (data?.status === "success" && model !== void 0) models.set(key, model);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (event.type !== "prompt_start" && event.type !== "prompt_end" && event.type !== "prompt_error") {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const metadata = data?.metadata;
|
|
97
|
+
const promptId = observedString(metadata?.promptId);
|
|
98
|
+
const version = metadata?.promptVersion;
|
|
99
|
+
const promptVersion = typeof version === "number" ? version : observedString(version);
|
|
100
|
+
const prompt = {
|
|
101
|
+
...promptId === void 0 ? {} : { promptId },
|
|
102
|
+
...promptVersion === void 0 ? {} : { promptVersion }
|
|
103
|
+
};
|
|
104
|
+
if (Object.keys(prompt).length > 0) prompts.set(JSON.stringify(prompt), prompt);
|
|
105
|
+
if (event.type === "prompt_end") {
|
|
106
|
+
completedPrompt = prompt;
|
|
107
|
+
completedModel = models.get(key);
|
|
108
|
+
completedOutput = data?.result;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
onEvent,
|
|
113
|
+
toRunItem(evalCase, output, fallbackModel) {
|
|
114
|
+
const metadata = { ...evalCase.metadata };
|
|
115
|
+
for (const key of ["promptId", "promptVersion", "model", "prompts"]) delete metadata[key];
|
|
116
|
+
const model = completedModel ?? observedString(fallbackModel);
|
|
117
|
+
return {
|
|
118
|
+
itemId: evalCase.id,
|
|
119
|
+
input: {
|
|
120
|
+
input: evalCase.input,
|
|
121
|
+
output: completedOutput === void 0 ? output : completedOutput,
|
|
122
|
+
expectedOutput: evalCase.expected,
|
|
123
|
+
metadata: {
|
|
124
|
+
...metadata,
|
|
125
|
+
...completedPrompt,
|
|
126
|
+
...model === void 0 ? {} : { model },
|
|
127
|
+
...prompts.size > 1 ? { prompts: [...prompts.values()] } : {}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
async function buildExperimentsRunItems(cases, run, options = {}) {
|
|
135
|
+
const items = [];
|
|
136
|
+
for (const evalCase of cases) {
|
|
137
|
+
const collector = createExperimentsRunCollector();
|
|
138
|
+
const output = await run(evalCase, { onEvent: collector.onEvent });
|
|
139
|
+
items.push(collector.toRunItem(evalCase, output, options.model));
|
|
140
|
+
}
|
|
141
|
+
return items;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
exports.buildExperimentsRunItems = buildExperimentsRunItems;
|
|
145
|
+
exports.createExperimentsRunCollector = createExperimentsRunCollector;
|
|
146
|
+
exports.experimentsEvaluatorScorer = experimentsEvaluatorScorer;
|
|
147
|
+
//# sourceMappingURL=index.cjs.map
|
|
148
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["EvalUnscoredError"],"mappings":";;;;;AAuCA,SAAS,WAAA,CAAY,MAAc,KAAA,EAA+B;AAChE,EAAA,SAAS,QAAQ,KAAA,EAAuB;AACtC,IAAA,IAAI,KAAA,YAAiBA,uBAAA,IAAqB,KAAA,CAAM,UAAA,KAAe,MAAM,MAAM,KAAA;AAC3E,IAAA,MAAM,MAAA,GACJ,iBAAiB,KAAA,GACb,KAAA,CAAM,UACN,OAAO,KAAA,KAAU,WACf,KAAA,GACA,iBAAA;AACR,IAAA,MAAM,IAAIA,uBAAA,CAAkB,IAAA,EAAM,MAAA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AAsB,wBAAA,CACpB,KAAA,EACA,GAAA,EAIA,OAAA,GAA8B,EAAC,EACA;AAC/B,EAAA,MAAM,QAA8B,EAAC;AACrC,EAAA,KAAA,MAAW,YAAY,KAAA,EAAO;AAC5B,IAAA,MAAM,YAAY,6BAAA,EAA8B;AAChD,IAAA,MAAM,MAAA,GAAS,MAAM,GAAA,CAAI,QAAA,EAAU,EAAE,OAAA,EAAS,SAAA,CAAU,SAAS,CAAA;AACjE,IAAA,KAAA,CAAM,KAAK,SAAA,CAAU,SAAA,CAAU,UAAU,MAAA,EAAQ,OAAA,CAAQ,KAAK,CAAC,CAAA;AAAA,EACjE;AACA,EAAA,OAAO,KAAA;AACT","file":"index.cjs","sourcesContent":["// Adapted from @fabric-harness/evals 3.0.0 (Apache-2.0), using the public\n// Experiments types and Harness error class.\nimport { type EvalCase, type EvalScorer, EvalUnscoredError } from '@fabric-harness/evals';\nimport type {\n EvalInput,\n EvalScore,\n Evaluator,\n ModelClient,\n RunItem,\n} from '@fabricorg/experiments-evals';\n\nexport interface ExperimentsScorerOptions {\n model?: ModelClient;\n context?: unknown;\n nowMs?: () => number;\n}\n\ntype ExperimentsEventData = NonNullable<EvalCase['metadata']>;\n\ninterface ExperimentsEvent {\n type: string;\n sessionId?: string;\n data?: ExperimentsEventData;\n}\n\ninterface PromptReference {\n promptId?: string;\n promptVersion?: string | number;\n}\n\nexport type ExperimentsRunItem = RunItem;\n\n/** Event callback and run-item builder for one invocation. */\nexport interface ExperimentsRunCollector {\n onEvent: (event: ExperimentsEvent) => void;\n toRunItem(evalCase: EvalCase, output: unknown, fallbackModel?: string): ExperimentsRunItem;\n}\n\n// Private port of namedScorer from @fabric-harness/evals 3.0.0.\nfunction namedScorer(name: string, score: EvalScorer): EvalScorer {\n function rethrow(cause: unknown): never {\n if (cause instanceof EvalUnscoredError && cause.scorerName === name) throw cause;\n const reason =\n cause instanceof Error\n ? cause.message\n : typeof cause === 'string'\n ? cause\n : 'Scoring failed.';\n throw new EvalUnscoredError(name, reason, { cause });\n }\n\n return (input) => {\n try {\n const result = score(input);\n return result instanceof Promise ? result.catch(rethrow) : result;\n } catch (cause) {\n return rethrow(cause);\n }\n };\n}\n\n// Experiments requires a client in its context even for code evaluators.\nconst noModel: ModelClient = {\n endpoint: '',\n async complete() {\n throw new Error('This evaluator requires a model client. Pass options.model.');\n },\n};\n\n/** Score an Experiments evaluator verdict or throw EvalUnscoredError for a scoring failure. */\nexport function experimentsEvaluatorScorer(\n evaluator: Evaluator,\n options: ExperimentsScorerOptions = {},\n): EvalScorer {\n const name = evaluator.name;\n return namedScorer(name, async ({ case: evalCase, output }) => {\n if (evaluator.kind === 'llm-judge' && options.model === undefined) {\n throw new EvalUnscoredError(\n name,\n `Evaluator \"${name}\" requires a model client. Pass options.model.`,\n );\n }\n const input: EvalInput = {\n input: evalCase.input,\n output,\n expectedOutput: evalCase.expected,\n ...(evalCase.metadata === undefined ? {} : { metadata: evalCase.metadata }),\n ...(options.context === undefined ? {} : { context: options.context }),\n };\n const result: EvalScore = await evaluator.evaluate(input, {\n model: options.model ?? noModel,\n nowMs: options.nowMs ?? Date.now,\n });\n if (result.score === null) {\n throw new EvalUnscoredError(name, `Evaluator \"${name}\" returned no score.`);\n }\n const {\n evaluatorName,\n evaluatorVersion,\n score,\n explanation,\n label,\n tokens,\n latencyMs,\n modelEndpoint,\n promptHash,\n } = result;\n return {\n name: evaluatorName,\n score,\n ...(explanation === undefined ? {} : { reason: explanation }),\n metadata: {\n evaluatorVersion,\n latencyMs,\n ...(label === undefined ? {} : { label }),\n ...(tokens === undefined ? {} : { tokens: { ...tokens } }),\n ...(modelEndpoint === undefined ? {} : { modelEndpoint }),\n ...(promptHash === undefined ? {} : { promptHash }),\n },\n };\n });\n}\n\nfunction observedString(value: unknown): string | undefined {\n return typeof value === 'string' && value !== '' ? value : undefined;\n}\n\n/** Collect prompt provenance for one invocation. Use a fresh collector for each case. */\nexport function createExperimentsRunCollector(): ExperimentsRunCollector {\n const models = new Map<string, string>();\n const prompts = new Map<string, PromptReference>();\n let completedPrompt: PromptReference = {};\n let completedModel: string | undefined;\n let completedOutput: unknown;\n\n function onEvent(event: ExperimentsEvent) {\n const data = event.data;\n const key = JSON.stringify([event.sessionId, data?.promptRunId]);\n if (event.type === 'model_attempt') {\n const model = observedString(data?.model);\n if (data?.status === 'success' && model !== undefined) models.set(key, model);\n return;\n }\n if (\n event.type !== 'prompt_start' &&\n event.type !== 'prompt_end' &&\n event.type !== 'prompt_error'\n ) {\n return;\n }\n\n const metadata = data?.metadata as ExperimentsEventData | undefined;\n const promptId = observedString(metadata?.promptId);\n const version = metadata?.promptVersion;\n const promptVersion = typeof version === 'number' ? version : observedString(version);\n const prompt = {\n ...(promptId === undefined ? {} : { promptId }),\n ...(promptVersion === undefined ? {} : { promptVersion }),\n };\n if (Object.keys(prompt).length > 0) prompts.set(JSON.stringify(prompt), prompt);\n if (event.type === 'prompt_end') {\n completedPrompt = prompt;\n completedModel = models.get(key);\n completedOutput = data?.result;\n }\n }\n\n return {\n onEvent,\n toRunItem(evalCase, output, fallbackModel) {\n const metadata = { ...evalCase.metadata };\n for (const key of ['promptId', 'promptVersion', 'model', 'prompts']) delete metadata[key];\n const model = completedModel ?? observedString(fallbackModel);\n return {\n itemId: evalCase.id,\n input: {\n input: evalCase.input,\n output: completedOutput === undefined ? output : completedOutput,\n expectedOutput: evalCase.expected,\n metadata: {\n ...metadata,\n ...completedPrompt,\n ...(model === undefined ? {} : { model }),\n ...(prompts.size > 1 ? { prompts: [...prompts.values()] } : {}),\n },\n },\n };\n },\n };\n}\n\n/** Prepare inputs for scoring in case order. A failed agent run rejects the batch. */\nexport async function buildExperimentsRunItems<TInput, TExpected>(\n cases: readonly EvalCase<TInput, TExpected>[],\n run: (\n evalCase: EvalCase<TInput, TExpected>,\n options: { onEvent: ExperimentsRunCollector['onEvent'] },\n ) => Promise<unknown>,\n options: { model?: string } = {},\n): Promise<ExperimentsRunItem[]> {\n const items: ExperimentsRunItem[] = [];\n for (const evalCase of cases) {\n const collector = createExperimentsRunCollector();\n const output = await run(evalCase, { onEvent: collector.onEvent });\n items.push(collector.toRunItem(evalCase, output, options.model));\n }\n return items;\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { EvalCase, EvalScorer } from '@fabric-harness/evals';
|
|
2
|
+
import { RunItem, ModelClient, Evaluator } from '@fabricorg/experiments-evals';
|
|
3
|
+
|
|
4
|
+
interface ExperimentsScorerOptions {
|
|
5
|
+
model?: ModelClient;
|
|
6
|
+
context?: unknown;
|
|
7
|
+
nowMs?: () => number;
|
|
8
|
+
}
|
|
9
|
+
type ExperimentsEventData = NonNullable<EvalCase['metadata']>;
|
|
10
|
+
interface ExperimentsEvent {
|
|
11
|
+
type: string;
|
|
12
|
+
sessionId?: string;
|
|
13
|
+
data?: ExperimentsEventData;
|
|
14
|
+
}
|
|
15
|
+
type ExperimentsRunItem = RunItem;
|
|
16
|
+
/** Event callback and run-item builder for one invocation. */
|
|
17
|
+
interface ExperimentsRunCollector {
|
|
18
|
+
onEvent: (event: ExperimentsEvent) => void;
|
|
19
|
+
toRunItem(evalCase: EvalCase, output: unknown, fallbackModel?: string): ExperimentsRunItem;
|
|
20
|
+
}
|
|
21
|
+
/** Score an Experiments evaluator verdict or throw EvalUnscoredError for a scoring failure. */
|
|
22
|
+
declare function experimentsEvaluatorScorer(evaluator: Evaluator, options?: ExperimentsScorerOptions): EvalScorer;
|
|
23
|
+
/** Collect prompt provenance for one invocation. Use a fresh collector for each case. */
|
|
24
|
+
declare function createExperimentsRunCollector(): ExperimentsRunCollector;
|
|
25
|
+
/** Prepare inputs for scoring in case order. A failed agent run rejects the batch. */
|
|
26
|
+
declare function buildExperimentsRunItems<TInput, TExpected>(cases: readonly EvalCase<TInput, TExpected>[], run: (evalCase: EvalCase<TInput, TExpected>, options: {
|
|
27
|
+
onEvent: ExperimentsRunCollector['onEvent'];
|
|
28
|
+
}) => Promise<unknown>, options?: {
|
|
29
|
+
model?: string;
|
|
30
|
+
}): Promise<ExperimentsRunItem[]>;
|
|
31
|
+
|
|
32
|
+
export { type ExperimentsRunCollector, type ExperimentsRunItem, type ExperimentsScorerOptions, buildExperimentsRunItems, createExperimentsRunCollector, experimentsEvaluatorScorer };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { EvalCase, EvalScorer } from '@fabric-harness/evals';
|
|
2
|
+
import { RunItem, ModelClient, Evaluator } from '@fabricorg/experiments-evals';
|
|
3
|
+
|
|
4
|
+
interface ExperimentsScorerOptions {
|
|
5
|
+
model?: ModelClient;
|
|
6
|
+
context?: unknown;
|
|
7
|
+
nowMs?: () => number;
|
|
8
|
+
}
|
|
9
|
+
type ExperimentsEventData = NonNullable<EvalCase['metadata']>;
|
|
10
|
+
interface ExperimentsEvent {
|
|
11
|
+
type: string;
|
|
12
|
+
sessionId?: string;
|
|
13
|
+
data?: ExperimentsEventData;
|
|
14
|
+
}
|
|
15
|
+
type ExperimentsRunItem = RunItem;
|
|
16
|
+
/** Event callback and run-item builder for one invocation. */
|
|
17
|
+
interface ExperimentsRunCollector {
|
|
18
|
+
onEvent: (event: ExperimentsEvent) => void;
|
|
19
|
+
toRunItem(evalCase: EvalCase, output: unknown, fallbackModel?: string): ExperimentsRunItem;
|
|
20
|
+
}
|
|
21
|
+
/** Score an Experiments evaluator verdict or throw EvalUnscoredError for a scoring failure. */
|
|
22
|
+
declare function experimentsEvaluatorScorer(evaluator: Evaluator, options?: ExperimentsScorerOptions): EvalScorer;
|
|
23
|
+
/** Collect prompt provenance for one invocation. Use a fresh collector for each case. */
|
|
24
|
+
declare function createExperimentsRunCollector(): ExperimentsRunCollector;
|
|
25
|
+
/** Prepare inputs for scoring in case order. A failed agent run rejects the batch. */
|
|
26
|
+
declare function buildExperimentsRunItems<TInput, TExpected>(cases: readonly EvalCase<TInput, TExpected>[], run: (evalCase: EvalCase<TInput, TExpected>, options: {
|
|
27
|
+
onEvent: ExperimentsRunCollector['onEvent'];
|
|
28
|
+
}) => Promise<unknown>, options?: {
|
|
29
|
+
model?: string;
|
|
30
|
+
}): Promise<ExperimentsRunItem[]>;
|
|
31
|
+
|
|
32
|
+
export { type ExperimentsRunCollector, type ExperimentsRunItem, type ExperimentsScorerOptions, buildExperimentsRunItems, createExperimentsRunCollector, experimentsEvaluatorScorer };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { EvalUnscoredError } from '@fabric-harness/evals';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
function namedScorer(name, score) {
|
|
5
|
+
function rethrow(cause) {
|
|
6
|
+
if (cause instanceof EvalUnscoredError && cause.scorerName === name) throw cause;
|
|
7
|
+
const reason = cause instanceof Error ? cause.message : typeof cause === "string" ? cause : "Scoring failed.";
|
|
8
|
+
throw new EvalUnscoredError(name, reason, { cause });
|
|
9
|
+
}
|
|
10
|
+
return (input) => {
|
|
11
|
+
try {
|
|
12
|
+
const result = score(input);
|
|
13
|
+
return result instanceof Promise ? result.catch(rethrow) : result;
|
|
14
|
+
} catch (cause) {
|
|
15
|
+
return rethrow(cause);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
var noModel = {
|
|
20
|
+
endpoint: "",
|
|
21
|
+
async complete() {
|
|
22
|
+
throw new Error("This evaluator requires a model client. Pass options.model.");
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
function experimentsEvaluatorScorer(evaluator, options = {}) {
|
|
26
|
+
const name = evaluator.name;
|
|
27
|
+
return namedScorer(name, async ({ case: evalCase, output }) => {
|
|
28
|
+
if (evaluator.kind === "llm-judge" && options.model === void 0) {
|
|
29
|
+
throw new EvalUnscoredError(
|
|
30
|
+
name,
|
|
31
|
+
`Evaluator "${name}" requires a model client. Pass options.model.`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
const input = {
|
|
35
|
+
input: evalCase.input,
|
|
36
|
+
output,
|
|
37
|
+
expectedOutput: evalCase.expected,
|
|
38
|
+
...evalCase.metadata === void 0 ? {} : { metadata: evalCase.metadata },
|
|
39
|
+
...options.context === void 0 ? {} : { context: options.context }
|
|
40
|
+
};
|
|
41
|
+
const result = await evaluator.evaluate(input, {
|
|
42
|
+
model: options.model ?? noModel,
|
|
43
|
+
nowMs: options.nowMs ?? Date.now
|
|
44
|
+
});
|
|
45
|
+
if (result.score === null) {
|
|
46
|
+
throw new EvalUnscoredError(name, `Evaluator "${name}" returned no score.`);
|
|
47
|
+
}
|
|
48
|
+
const {
|
|
49
|
+
evaluatorName,
|
|
50
|
+
evaluatorVersion,
|
|
51
|
+
score,
|
|
52
|
+
explanation,
|
|
53
|
+
label,
|
|
54
|
+
tokens,
|
|
55
|
+
latencyMs,
|
|
56
|
+
modelEndpoint,
|
|
57
|
+
promptHash
|
|
58
|
+
} = result;
|
|
59
|
+
return {
|
|
60
|
+
name: evaluatorName,
|
|
61
|
+
score,
|
|
62
|
+
...explanation === void 0 ? {} : { reason: explanation },
|
|
63
|
+
metadata: {
|
|
64
|
+
evaluatorVersion,
|
|
65
|
+
latencyMs,
|
|
66
|
+
...label === void 0 ? {} : { label },
|
|
67
|
+
...tokens === void 0 ? {} : { tokens: { ...tokens } },
|
|
68
|
+
...modelEndpoint === void 0 ? {} : { modelEndpoint },
|
|
69
|
+
...promptHash === void 0 ? {} : { promptHash }
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
function observedString(value) {
|
|
75
|
+
return typeof value === "string" && value !== "" ? value : void 0;
|
|
76
|
+
}
|
|
77
|
+
function createExperimentsRunCollector() {
|
|
78
|
+
const models = /* @__PURE__ */ new Map();
|
|
79
|
+
const prompts = /* @__PURE__ */ new Map();
|
|
80
|
+
let completedPrompt = {};
|
|
81
|
+
let completedModel;
|
|
82
|
+
let completedOutput;
|
|
83
|
+
function onEvent(event) {
|
|
84
|
+
const data = event.data;
|
|
85
|
+
const key = JSON.stringify([event.sessionId, data?.promptRunId]);
|
|
86
|
+
if (event.type === "model_attempt") {
|
|
87
|
+
const model = observedString(data?.model);
|
|
88
|
+
if (data?.status === "success" && model !== void 0) models.set(key, model);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (event.type !== "prompt_start" && event.type !== "prompt_end" && event.type !== "prompt_error") {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const metadata = data?.metadata;
|
|
95
|
+
const promptId = observedString(metadata?.promptId);
|
|
96
|
+
const version = metadata?.promptVersion;
|
|
97
|
+
const promptVersion = typeof version === "number" ? version : observedString(version);
|
|
98
|
+
const prompt = {
|
|
99
|
+
...promptId === void 0 ? {} : { promptId },
|
|
100
|
+
...promptVersion === void 0 ? {} : { promptVersion }
|
|
101
|
+
};
|
|
102
|
+
if (Object.keys(prompt).length > 0) prompts.set(JSON.stringify(prompt), prompt);
|
|
103
|
+
if (event.type === "prompt_end") {
|
|
104
|
+
completedPrompt = prompt;
|
|
105
|
+
completedModel = models.get(key);
|
|
106
|
+
completedOutput = data?.result;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
onEvent,
|
|
111
|
+
toRunItem(evalCase, output, fallbackModel) {
|
|
112
|
+
const metadata = { ...evalCase.metadata };
|
|
113
|
+
for (const key of ["promptId", "promptVersion", "model", "prompts"]) delete metadata[key];
|
|
114
|
+
const model = completedModel ?? observedString(fallbackModel);
|
|
115
|
+
return {
|
|
116
|
+
itemId: evalCase.id,
|
|
117
|
+
input: {
|
|
118
|
+
input: evalCase.input,
|
|
119
|
+
output: completedOutput === void 0 ? output : completedOutput,
|
|
120
|
+
expectedOutput: evalCase.expected,
|
|
121
|
+
metadata: {
|
|
122
|
+
...metadata,
|
|
123
|
+
...completedPrompt,
|
|
124
|
+
...model === void 0 ? {} : { model },
|
|
125
|
+
...prompts.size > 1 ? { prompts: [...prompts.values()] } : {}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
async function buildExperimentsRunItems(cases, run, options = {}) {
|
|
133
|
+
const items = [];
|
|
134
|
+
for (const evalCase of cases) {
|
|
135
|
+
const collector = createExperimentsRunCollector();
|
|
136
|
+
const output = await run(evalCase, { onEvent: collector.onEvent });
|
|
137
|
+
items.push(collector.toRunItem(evalCase, output, options.model));
|
|
138
|
+
}
|
|
139
|
+
return items;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export { buildExperimentsRunItems, createExperimentsRunCollector, experimentsEvaluatorScorer };
|
|
143
|
+
//# sourceMappingURL=index.js.map
|
|
144
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAuCA,SAAS,WAAA,CAAY,MAAc,KAAA,EAA+B;AAChE,EAAA,SAAS,QAAQ,KAAA,EAAuB;AACtC,IAAA,IAAI,KAAA,YAAiB,iBAAA,IAAqB,KAAA,CAAM,UAAA,KAAe,MAAM,MAAM,KAAA;AAC3E,IAAA,MAAM,MAAA,GACJ,iBAAiB,KAAA,GACb,KAAA,CAAM,UACN,OAAO,KAAA,KAAU,WACf,KAAA,GACA,iBAAA;AACR,IAAA,MAAM,IAAI,iBAAA,CAAkB,IAAA,EAAM,MAAA,EAAQ,EAAE,OAAO,CAAA;AAAA,EACrD;AAEA,EAAA,OAAO,CAAC,KAAA,KAAU;AAChB,IAAA,IAAI;AACF,MAAA,MAAM,MAAA,GAAS,MAAM,KAAK,CAAA;AAC1B,MAAA,OAAO,MAAA,YAAkB,OAAA,GAAU,MAAA,CAAO,KAAA,CAAM,OAAO,CAAA,GAAI,MAAA;AAAA,IAC7D,SAAS,KAAA,EAAO;AACd,MAAA,OAAO,QAAQ,KAAK,CAAA;AAAA,IACtB;AAAA,EACF,CAAA;AACF;AAGA,IAAM,OAAA,GAAuB;AAAA,EA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KAAK,CAAC,CAAA;AAAA,EACjE;AACA,EAAA,OAAO,KAAA;AACT","file":"index.js","sourcesContent":["// Adapted from @fabric-harness/evals 3.0.0 (Apache-2.0), using the public\n// Experiments types and Harness error class.\nimport { type EvalCase, type EvalScorer, EvalUnscoredError } from '@fabric-harness/evals';\nimport type {\n EvalInput,\n EvalScore,\n Evaluator,\n ModelClient,\n RunItem,\n} from '@fabricorg/experiments-evals';\n\nexport interface ExperimentsScorerOptions {\n model?: ModelClient;\n context?: unknown;\n nowMs?: () => number;\n}\n\ntype ExperimentsEventData = NonNullable<EvalCase['metadata']>;\n\ninterface ExperimentsEvent {\n type: string;\n sessionId?: string;\n data?: ExperimentsEventData;\n}\n\ninterface PromptReference {\n promptId?: string;\n promptVersion?: string | number;\n}\n\nexport type ExperimentsRunItem = RunItem;\n\n/** Event callback and run-item builder for one invocation. */\nexport interface ExperimentsRunCollector {\n onEvent: (event: ExperimentsEvent) => void;\n toRunItem(evalCase: EvalCase, output: unknown, fallbackModel?: string): ExperimentsRunItem;\n}\n\n// Private port of namedScorer from @fabric-harness/evals 3.0.0.\nfunction namedScorer(name: string, score: EvalScorer): EvalScorer {\n function rethrow(cause: unknown): never {\n if (cause instanceof EvalUnscoredError && cause.scorerName === name) throw cause;\n const reason =\n cause instanceof Error\n ? cause.message\n : typeof cause === 'string'\n ? cause\n : 'Scoring failed.';\n throw new EvalUnscoredError(name, reason, { cause });\n }\n\n return (input) => {\n try {\n const result = score(input);\n return result instanceof Promise ? result.catch(rethrow) : result;\n } catch (cause) {\n return rethrow(cause);\n }\n };\n}\n\n// Experiments requires a client in its context even for code evaluators.\nconst noModel: ModelClient = {\n endpoint: '',\n async complete() {\n throw new Error('This evaluator requires a model client. Pass options.model.');\n },\n};\n\n/** Score an Experiments evaluator verdict or throw EvalUnscoredError for a scoring failure. */\nexport function experimentsEvaluatorScorer(\n evaluator: Evaluator,\n options: ExperimentsScorerOptions = {},\n): EvalScorer {\n const name = evaluator.name;\n return namedScorer(name, async ({ case: evalCase, output }) => {\n if (evaluator.kind === 'llm-judge' && options.model === undefined) {\n throw new EvalUnscoredError(\n name,\n `Evaluator \"${name}\" requires a model client. Pass options.model.`,\n );\n }\n const input: EvalInput = {\n input: evalCase.input,\n output,\n expectedOutput: evalCase.expected,\n ...(evalCase.metadata === undefined ? {} : { metadata: evalCase.metadata }),\n ...(options.context === undefined ? {} : { context: options.context }),\n };\n const result: EvalScore = await evaluator.evaluate(input, {\n model: options.model ?? noModel,\n nowMs: options.nowMs ?? Date.now,\n });\n if (result.score === null) {\n throw new EvalUnscoredError(name, `Evaluator \"${name}\" returned no score.`);\n }\n const {\n evaluatorName,\n evaluatorVersion,\n score,\n explanation,\n label,\n tokens,\n latencyMs,\n modelEndpoint,\n promptHash,\n } = result;\n return {\n name: evaluatorName,\n score,\n ...(explanation === undefined ? {} : { reason: explanation }),\n metadata: {\n evaluatorVersion,\n latencyMs,\n ...(label === undefined ? {} : { label }),\n ...(tokens === undefined ? {} : { tokens: { ...tokens } }),\n ...(modelEndpoint === undefined ? {} : { modelEndpoint }),\n ...(promptHash === undefined ? {} : { promptHash }),\n },\n };\n });\n}\n\nfunction observedString(value: unknown): string | undefined {\n return typeof value === 'string' && value !== '' ? value : undefined;\n}\n\n/** Collect prompt provenance for one invocation. Use a fresh collector for each case. */\nexport function createExperimentsRunCollector(): ExperimentsRunCollector {\n const models = new Map<string, string>();\n const prompts = new Map<string, PromptReference>();\n let completedPrompt: PromptReference = {};\n let completedModel: string | undefined;\n let completedOutput: unknown;\n\n function onEvent(event: ExperimentsEvent) {\n const data = event.data;\n const key = JSON.stringify([event.sessionId, data?.promptRunId]);\n if (event.type === 'model_attempt') {\n const model = observedString(data?.model);\n if (data?.status === 'success' && model !== undefined) models.set(key, model);\n return;\n }\n if (\n event.type !== 'prompt_start' &&\n event.type !== 'prompt_end' &&\n event.type !== 'prompt_error'\n ) {\n return;\n }\n\n const metadata = data?.metadata as ExperimentsEventData | undefined;\n const promptId = observedString(metadata?.promptId);\n const version = metadata?.promptVersion;\n const promptVersion = typeof version === 'number' ? version : observedString(version);\n const prompt = {\n ...(promptId === undefined ? {} : { promptId }),\n ...(promptVersion === undefined ? {} : { promptVersion }),\n };\n if (Object.keys(prompt).length > 0) prompts.set(JSON.stringify(prompt), prompt);\n if (event.type === 'prompt_end') {\n completedPrompt = prompt;\n completedModel = models.get(key);\n completedOutput = data?.result;\n }\n }\n\n return {\n onEvent,\n toRunItem(evalCase, output, fallbackModel) {\n const metadata = { ...evalCase.metadata };\n for (const key of ['promptId', 'promptVersion', 'model', 'prompts']) delete metadata[key];\n const model = completedModel ?? observedString(fallbackModel);\n return {\n itemId: evalCase.id,\n input: {\n input: evalCase.input,\n output: completedOutput === undefined ? output : completedOutput,\n expectedOutput: evalCase.expected,\n metadata: {\n ...metadata,\n ...completedPrompt,\n ...(model === undefined ? {} : { model }),\n ...(prompts.size > 1 ? { prompts: [...prompts.values()] } : {}),\n },\n },\n };\n },\n };\n}\n\n/** Prepare inputs for scoring in case order. A failed agent run rejects the batch. */\nexport async function buildExperimentsRunItems<TInput, TExpected>(\n cases: readonly EvalCase<TInput, TExpected>[],\n run: (\n evalCase: EvalCase<TInput, TExpected>,\n options: { onEvent: ExperimentsRunCollector['onEvent'] },\n ) => Promise<unknown>,\n options: { model?: string } = {},\n): Promise<ExperimentsRunItem[]> {\n const items: ExperimentsRunItem[] = [];\n for (const evalCase of cases) {\n const collector = createExperimentsRunCollector();\n const output = await run(evalCase, { onEvent: collector.onEvent });\n items.push(collector.toRunItem(evalCase, output, options.model));\n }\n return items;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabricorg/experiments-harness-evals",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Adapt Fabric Harness eval suites and run events to Fabric Experiments evaluators.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"module": "./dist/index.js",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/index.d.cts",
|
|
19
|
+
"default": "./dist/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": ["dist"],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"type-check": "tsc --noEmit",
|
|
27
|
+
"test": "vitest run",
|
|
28
|
+
"clean": "rm -rf dist"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@fabricorg/experiments-evals": "^1.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@fabric-harness/evals": "3.0.0",
|
|
35
|
+
"@fabric-harness/sdk": "6.10.1",
|
|
36
|
+
"tsup": "^8.3.5",
|
|
37
|
+
"typescript": "^5.8.3",
|
|
38
|
+
"vitest": "^4.1.12"
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=22.19.0"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@fabric-harness/evals": "^3.0.0 || ^4.0.0"
|
|
45
|
+
}
|
|
46
|
+
}
|