evalsense 0.2.0 → 0.3.0
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 +190 -0
- package/README.md +99 -82
- package/dist/{chunk-HDJID3GC.cjs → chunk-DFC6FRTG.cjs} +8 -26
- package/dist/chunk-DFC6FRTG.cjs.map +1 -0
- package/dist/chunk-DGUM43GV.js +10 -0
- package/dist/chunk-DGUM43GV.js.map +1 -0
- package/dist/chunk-JEQ2X3Z6.cjs +12 -0
- package/dist/chunk-JEQ2X3Z6.cjs.map +1 -0
- package/dist/{chunk-5P7LNNO6.js → chunk-JPVZL45G.js} +8 -26
- package/dist/chunk-JPVZL45G.js.map +1 -0
- package/dist/{chunk-Y23VHTD3.cjs → chunk-RZFLCWTW.cjs} +2 -2
- package/dist/chunk-RZFLCWTW.cjs.map +1 -0
- package/dist/{chunk-BRPM6AB6.js → chunk-Z3U6AUWX.js} +2 -2
- package/dist/chunk-Z3U6AUWX.js.map +1 -0
- package/dist/cli.cjs +39 -36
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +37 -34
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +300 -101
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +76 -6
- package/dist/index.d.ts +76 -6
- package/dist/index.js +222 -23
- package/dist/index.js.map +1 -1
- package/dist/metrics/index.cjs +257 -17
- package/dist/metrics/index.cjs.map +1 -1
- package/dist/metrics/index.d.cts +252 -1
- package/dist/metrics/index.d.ts +252 -1
- package/dist/metrics/index.js +240 -2
- package/dist/metrics/index.js.map +1 -1
- package/dist/metrics/opinionated/index.cjs +6 -5
- package/dist/metrics/opinionated/index.js +2 -1
- package/package.json +8 -6
- package/dist/chunk-5P7LNNO6.js.map +0 -1
- package/dist/chunk-BRPM6AB6.js.map +0 -1
- package/dist/chunk-HDJID3GC.cjs.map +0 -1
- package/dist/chunk-Y23VHTD3.cjs.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2026 Mohit Joshi
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
> JS-native LLM evaluation framework with Jest-like API and statistical assertions
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/evalsense)
|
|
6
|
-
[](https://opensource.org/licenses/Apache-2.0)
|
|
7
7
|
|
|
8
8
|
**evalsense** brings classical ML-style statistical evaluation to LLM systems in JavaScript. Instead of evaluating individual test cases, evalsense evaluates entire datasets and computes confusion matrices, precision/recall, F1 scores, and other statistical metrics.
|
|
9
9
|
|
|
10
|
+
> **New in v0.3.0:** Regression assertions (MAE, RMSE, R²) and flexible ID matching for custom identifier fields! [See migration guide](./docs/migration-v0.3.0.md).
|
|
11
|
+
> **New in v0.2.x:** Built-in adapters for OpenAI, Anthropic, and OpenRouter - no boilerplate needed!
|
|
10
12
|
> **New in v0.2.0:** LLM-powered metrics for hallucination, relevance, faithfulness, and toxicity detection. [See migration guide](./docs/migration-v0.2.md).
|
|
11
13
|
|
|
12
14
|
## Why evalsense?
|
|
@@ -57,7 +59,7 @@ function classifySentiment(record) {
|
|
|
57
59
|
|
|
58
60
|
return {
|
|
59
61
|
id: record.id,
|
|
60
|
-
sentiment: hasPositive && !hasNegative ? "positive" : "negative"
|
|
62
|
+
sentiment: hasPositive && !hasNegative ? "positive" : "negative",
|
|
61
63
|
};
|
|
62
64
|
}
|
|
63
65
|
|
|
@@ -109,14 +111,14 @@ describe("Spam classifier", () => {
|
|
|
109
111
|
|
|
110
112
|
const result = await runModel(dataset, (record) => ({
|
|
111
113
|
id: record.id,
|
|
112
|
-
isSpam: classifyEmail(record.text)
|
|
114
|
+
isSpam: classifyEmail(record.text),
|
|
113
115
|
}));
|
|
114
116
|
|
|
115
117
|
expectStats(result)
|
|
116
118
|
.field("isSpam")
|
|
117
119
|
.toHaveAccuracyAbove(0.9)
|
|
118
|
-
.toHavePrecisionAbove(true, 0.85)
|
|
119
|
-
.toHaveRecallAbove(true, 0.85)
|
|
120
|
+
.toHavePrecisionAbove(true, 0.85) // Precision for spam=true
|
|
121
|
+
.toHaveRecallAbove(true, 0.85) // Recall for spam=true
|
|
120
122
|
.toHaveConfusionMatrix();
|
|
121
123
|
});
|
|
122
124
|
});
|
|
@@ -134,13 +136,13 @@ describe("Hallucination detector", () => {
|
|
|
134
136
|
// Your model returns a continuous score
|
|
135
137
|
const result = await runModel(dataset, (record) => ({
|
|
136
138
|
id: record.id,
|
|
137
|
-
hallucinated: computeHallucinationScore(record.output)
|
|
139
|
+
hallucinated: computeHallucinationScore(record.output), // 0.0 to 1.0
|
|
138
140
|
}));
|
|
139
141
|
|
|
140
142
|
// Binarize the score at threshold 0.3
|
|
141
143
|
expectStats(result)
|
|
142
144
|
.field("hallucinated")
|
|
143
|
-
.binarize(0.3)
|
|
145
|
+
.binarize(0.3) // >= 0.3 means hallucinated
|
|
144
146
|
.toHaveRecallAbove(true, 0.7)
|
|
145
147
|
.toHavePrecisionAbove(true, 0.6)
|
|
146
148
|
.toHaveConfusionMatrix();
|
|
@@ -159,7 +161,7 @@ describe("Intent classifier", () => {
|
|
|
159
161
|
|
|
160
162
|
const result = await runModel(dataset, (record) => ({
|
|
161
163
|
id: record.id,
|
|
162
|
-
intent: classifyIntent(record.query)
|
|
164
|
+
intent: classifyIntent(record.query),
|
|
163
165
|
}));
|
|
164
166
|
|
|
165
167
|
expectStats(result)
|
|
@@ -191,12 +193,10 @@ describe("LLM classifier", () => {
|
|
|
191
193
|
const response = await callLLM(record.text);
|
|
192
194
|
return { id: record.id, category: response.category };
|
|
193
195
|
},
|
|
194
|
-
5
|
|
196
|
+
5 // concurrency limit
|
|
195
197
|
);
|
|
196
198
|
|
|
197
|
-
expectStats(result)
|
|
198
|
-
.field("category")
|
|
199
|
-
.toHaveAccuracyAbove(0.9);
|
|
199
|
+
expectStats(result).field("category").toHaveAccuracyAbove(0.9);
|
|
200
200
|
});
|
|
201
201
|
});
|
|
202
202
|
```
|
|
@@ -283,6 +283,7 @@ npx evalsense list tests/
|
|
|
283
283
|
### Core API
|
|
284
284
|
|
|
285
285
|
#### `describe(name, fn)`
|
|
286
|
+
|
|
286
287
|
Groups related evaluation tests (like Jest's describe).
|
|
287
288
|
|
|
288
289
|
```javascript
|
|
@@ -292,6 +293,7 @@ describe("My model", () => {
|
|
|
292
293
|
```
|
|
293
294
|
|
|
294
295
|
#### `evalTest(name, fn)` / `test(name, fn)` / `it(name, fn)`
|
|
296
|
+
|
|
295
297
|
Defines an evaluation test.
|
|
296
298
|
|
|
297
299
|
```javascript
|
|
@@ -303,6 +305,7 @@ evalTest("should have 90% accuracy", async () => {
|
|
|
303
305
|
### Dataset Functions
|
|
304
306
|
|
|
305
307
|
#### `loadDataset(path)`
|
|
308
|
+
|
|
306
309
|
Loads a dataset from a JSON file. Records must have an `id` or `_id` field.
|
|
307
310
|
|
|
308
311
|
```javascript
|
|
@@ -310,44 +313,45 @@ const dataset = loadDataset("./data.json");
|
|
|
310
313
|
```
|
|
311
314
|
|
|
312
315
|
#### `runModel(dataset, modelFn)`
|
|
316
|
+
|
|
313
317
|
Runs a model function on each record sequentially.
|
|
314
318
|
|
|
315
319
|
```javascript
|
|
316
320
|
const result = await runModel(dataset, (record) => ({
|
|
317
321
|
id: record.id,
|
|
318
|
-
prediction: classify(record.text)
|
|
322
|
+
prediction: classify(record.text),
|
|
319
323
|
}));
|
|
320
324
|
```
|
|
321
325
|
|
|
322
326
|
#### `runModelParallel(dataset, modelFn, concurrency)`
|
|
327
|
+
|
|
323
328
|
Runs a model function with parallel execution.
|
|
324
329
|
|
|
325
330
|
```javascript
|
|
326
|
-
const result = await runModelParallel(dataset, modelFn, 10);
|
|
331
|
+
const result = await runModelParallel(dataset, modelFn, 10); // concurrency=10
|
|
327
332
|
```
|
|
328
333
|
|
|
329
334
|
### Assertions
|
|
330
335
|
|
|
331
336
|
#### `expectStats(result)`
|
|
337
|
+
|
|
332
338
|
Creates a statistical assertion chain from model results.
|
|
333
339
|
|
|
334
340
|
```javascript
|
|
335
|
-
expectStats(result)
|
|
336
|
-
.field("prediction")
|
|
337
|
-
.toHaveAccuracyAbove(0.8);
|
|
341
|
+
expectStats(result).field("prediction").toHaveAccuracyAbove(0.8);
|
|
338
342
|
```
|
|
339
343
|
|
|
340
344
|
#### `expectStats(predictions, groundTruth)`
|
|
345
|
+
|
|
341
346
|
Two-argument form for judge validation. Aligns predictions with ground truth by `id` field.
|
|
342
347
|
|
|
343
348
|
```javascript
|
|
344
349
|
// Validate judge outputs against human labels
|
|
345
|
-
expectStats(judgeOutputs, humanLabels)
|
|
346
|
-
.field("label")
|
|
347
|
-
.toHaveAccuracyAbove(0.85);
|
|
350
|
+
expectStats(judgeOutputs, humanLabels).field("label").toHaveAccuracyAbove(0.85);
|
|
348
351
|
```
|
|
349
352
|
|
|
350
353
|
**When to use:**
|
|
354
|
+
|
|
351
355
|
- Validating LLM judges against human labels
|
|
352
356
|
- Evaluating metric quality
|
|
353
357
|
- Testing automated detection systems
|
|
@@ -355,19 +359,21 @@ expectStats(judgeOutputs, humanLabels)
|
|
|
355
359
|
### Field Selection
|
|
356
360
|
|
|
357
361
|
#### `.field(fieldName)`
|
|
362
|
+
|
|
358
363
|
Selects a field for evaluation.
|
|
359
364
|
|
|
360
365
|
```javascript
|
|
361
|
-
expectStats(result).field("sentiment")
|
|
366
|
+
expectStats(result).field("sentiment");
|
|
362
367
|
```
|
|
363
368
|
|
|
364
369
|
#### `.binarize(threshold)`
|
|
370
|
+
|
|
365
371
|
Converts continuous scores to binary (>=threshold is true).
|
|
366
372
|
|
|
367
373
|
```javascript
|
|
368
374
|
expectStats(result)
|
|
369
375
|
.field("score")
|
|
370
|
-
.binarize(0.5)
|
|
376
|
+
.binarize(0.5) // score >= 0.5 is true
|
|
371
377
|
.toHaveAccuracyAbove(0.8);
|
|
372
378
|
```
|
|
373
379
|
|
|
@@ -403,23 +409,20 @@ Distribution assertions validate output distributions **without requiring ground
|
|
|
403
409
|
|
|
404
410
|
```javascript
|
|
405
411
|
// Assert that at least 80% of confidence scores are above 0.7
|
|
406
|
-
expectStats(predictions)
|
|
407
|
-
.field("confidence")
|
|
408
|
-
.toHavePercentageAbove(0.7, 0.8);
|
|
412
|
+
expectStats(predictions).field("confidence").toHavePercentageAbove(0.7, 0.8);
|
|
409
413
|
|
|
410
414
|
// Assert that at least 90% of toxicity scores are below 0.3
|
|
411
|
-
expectStats(predictions)
|
|
412
|
-
.field("toxicity")
|
|
413
|
-
.toHavePercentageBelow(0.3, 0.9);
|
|
415
|
+
expectStats(predictions).field("toxicity").toHavePercentageBelow(0.3, 0.9);
|
|
414
416
|
|
|
415
417
|
// Chain multiple distribution assertions
|
|
416
418
|
expectStats(predictions)
|
|
417
419
|
.field("score")
|
|
418
|
-
.toHavePercentageAbove(0.5, 0.6)
|
|
420
|
+
.toHavePercentageAbove(0.5, 0.6) // At least 60% above 0.5
|
|
419
421
|
.toHavePercentageBelow(0.9, 0.8); // At least 80% below 0.9
|
|
420
422
|
```
|
|
421
423
|
|
|
422
424
|
**Use cases:**
|
|
425
|
+
|
|
423
426
|
- Monitor confidence score distributions
|
|
424
427
|
- Validate schema compliance rates
|
|
425
428
|
- Check output range constraints
|
|
@@ -436,35 +439,35 @@ Validate judge outputs against human-labeled ground truth using the **two-argume
|
|
|
436
439
|
const judgeOutputs = [
|
|
437
440
|
{ id: "1", hallucinated: true },
|
|
438
441
|
{ id: "2", hallucinated: false },
|
|
439
|
-
{ id: "3", hallucinated: true }
|
|
442
|
+
{ id: "3", hallucinated: true },
|
|
440
443
|
];
|
|
441
444
|
|
|
442
445
|
// Human labels (ground truth)
|
|
443
446
|
const humanLabels = [
|
|
444
447
|
{ id: "1", hallucinated: true },
|
|
445
448
|
{ id: "2", hallucinated: false },
|
|
446
|
-
{ id: "3", hallucinated: false }
|
|
449
|
+
{ id: "3", hallucinated: false },
|
|
447
450
|
];
|
|
448
451
|
|
|
449
452
|
// Validate judge performance
|
|
450
453
|
expectStats(judgeOutputs, humanLabels)
|
|
451
454
|
.field("hallucinated")
|
|
452
|
-
.toHaveRecallAbove(true, 0.9)
|
|
453
|
-
.toHavePrecisionAbove(true, 0.7)
|
|
455
|
+
.toHaveRecallAbove(true, 0.9) // Don't miss hallucinations
|
|
456
|
+
.toHavePrecisionAbove(true, 0.7) // Some false positives OK
|
|
454
457
|
.toHaveConfusionMatrix();
|
|
455
458
|
```
|
|
456
459
|
|
|
457
460
|
**Use cases:**
|
|
461
|
+
|
|
458
462
|
- Evaluate LLM-as-judge accuracy
|
|
459
463
|
- Validate heuristic metrics against human labels
|
|
460
464
|
- Test automated detection systems (refusal, policy compliance)
|
|
461
465
|
- Calibrate metric thresholds
|
|
462
466
|
|
|
463
467
|
**Two-argument expectStats:**
|
|
468
|
+
|
|
464
469
|
```javascript
|
|
465
|
-
expectStats(actual, expected)
|
|
466
|
-
.field("fieldName")
|
|
467
|
-
.toHaveAccuracyAbove(0.8);
|
|
470
|
+
expectStats(actual, expected).field("fieldName").toHaveAccuracyAbove(0.8);
|
|
468
471
|
```
|
|
469
472
|
|
|
470
473
|
The first argument is your predictions (judge outputs), the second is ground truth (human labels). Both must have matching `id` fields for alignment.
|
|
@@ -493,6 +496,7 @@ Datasets must be JSON arrays where each record has an `id` or `_id` field:
|
|
|
493
496
|
```
|
|
494
497
|
|
|
495
498
|
**Requirements:**
|
|
499
|
+
|
|
496
500
|
- Each record MUST have `id` or `_id` for alignment
|
|
497
501
|
- Ground truth fields (e.g., `label`, `sentiment`, `category`) are compared against model outputs
|
|
498
502
|
- Model functions must return predictions with matching `id`
|
|
@@ -522,6 +526,7 @@ project/
|
|
|
522
526
|
```
|
|
523
527
|
|
|
524
528
|
Run with:
|
|
529
|
+
|
|
525
530
|
```bash
|
|
526
531
|
npx evalsense run tests/
|
|
527
532
|
```
|
|
@@ -551,26 +556,25 @@ evalsense includes LLM-powered metrics for hallucination detection, relevance as
|
|
|
551
556
|
### Quick Setup
|
|
552
557
|
|
|
553
558
|
```javascript
|
|
554
|
-
import { setLLMClient } from "evalsense/metrics";
|
|
559
|
+
import { setLLMClient, createOpenAIAdapter } from "evalsense/metrics";
|
|
555
560
|
import { hallucination, relevance, faithfulness, toxicity } from "evalsense/metrics/opinionated";
|
|
556
561
|
|
|
557
562
|
// 1. Configure your LLM client (one-time setup)
|
|
558
|
-
setLLMClient(
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
});
|
|
563
|
+
setLLMClient(
|
|
564
|
+
createOpenAIAdapter(process.env.OPENAI_API_KEY, {
|
|
565
|
+
model: "gpt-4-turbo-preview",
|
|
566
|
+
temperature: 0,
|
|
567
|
+
})
|
|
568
|
+
);
|
|
565
569
|
|
|
566
570
|
// 2. Use metrics in evaluations
|
|
567
571
|
const results = await hallucination({
|
|
568
572
|
outputs: [{ id: "1", output: "Paris has 50 million people." }],
|
|
569
|
-
context: ["Paris has approximately 2.1 million residents."]
|
|
573
|
+
context: ["Paris has approximately 2.1 million residents."],
|
|
570
574
|
});
|
|
571
575
|
|
|
572
|
-
console.log(results[0].score);
|
|
573
|
-
console.log(results[0].reasoning);
|
|
576
|
+
console.log(results[0].score); // 0.9 (high hallucination)
|
|
577
|
+
console.log(results[0].reasoning); // "Output claims 50M, context says 2.1M"
|
|
574
578
|
```
|
|
575
579
|
|
|
576
580
|
### Available Metrics
|
|
@@ -589,62 +593,74 @@ Choose between accuracy and cost:
|
|
|
589
593
|
await hallucination({
|
|
590
594
|
outputs,
|
|
591
595
|
context,
|
|
592
|
-
evaluationMode: "per-row"
|
|
596
|
+
evaluationMode: "per-row", // default
|
|
593
597
|
});
|
|
594
598
|
|
|
595
599
|
// Batch: Lower cost, single API call
|
|
596
600
|
await hallucination({
|
|
597
601
|
outputs,
|
|
598
602
|
context,
|
|
599
|
-
evaluationMode: "batch"
|
|
603
|
+
evaluationMode: "batch",
|
|
600
604
|
});
|
|
601
605
|
```
|
|
602
606
|
|
|
603
|
-
### Provider
|
|
607
|
+
### Built-in Provider Adapters
|
|
608
|
+
|
|
609
|
+
evalsense includes ready-to-use adapters for popular LLM providers:
|
|
610
|
+
|
|
611
|
+
**OpenAI (GPT-4, GPT-3.5)**
|
|
604
612
|
|
|
605
|
-
**OpenAI:**
|
|
606
613
|
```javascript
|
|
607
|
-
import
|
|
614
|
+
import { createOpenAIAdapter } from "evalsense/metrics";
|
|
608
615
|
|
|
609
|
-
|
|
610
|
-
setLLMClient(
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
}
|
|
618
|
-
});
|
|
616
|
+
// npm install openai
|
|
617
|
+
setLLMClient(
|
|
618
|
+
createOpenAIAdapter(process.env.OPENAI_API_KEY, {
|
|
619
|
+
model: "gpt-4-turbo-preview", // or "gpt-3.5-turbo" for lower cost
|
|
620
|
+
temperature: 0,
|
|
621
|
+
maxTokens: 4096,
|
|
622
|
+
})
|
|
623
|
+
);
|
|
619
624
|
```
|
|
620
625
|
|
|
621
|
-
**Anthropic
|
|
626
|
+
**Anthropic (Claude)**
|
|
627
|
+
|
|
622
628
|
```javascript
|
|
623
|
-
import
|
|
629
|
+
import { createAnthropicAdapter } from "evalsense/metrics";
|
|
624
630
|
|
|
625
|
-
|
|
626
|
-
setLLMClient(
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
631
|
+
// npm install @anthropic-ai/sdk
|
|
632
|
+
setLLMClient(
|
|
633
|
+
createAnthropicAdapter(process.env.ANTHROPIC_API_KEY, {
|
|
634
|
+
model: "claude-3-5-sonnet-20241022", // or "claude-3-haiku-20240307" for speed
|
|
635
|
+
maxTokens: 4096,
|
|
636
|
+
})
|
|
637
|
+
);
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
**OpenRouter (100+ models from one API)**
|
|
641
|
+
|
|
642
|
+
```javascript
|
|
643
|
+
import { createOpenRouterAdapter } from "evalsense/metrics";
|
|
644
|
+
|
|
645
|
+
// No SDK needed - uses fetch
|
|
646
|
+
setLLMClient(
|
|
647
|
+
createOpenRouterAdapter(process.env.OPENROUTER_API_KEY, {
|
|
648
|
+
model: "anthropic/claude-3.5-sonnet", // or "openai/gpt-3.5-turbo", etc.
|
|
649
|
+
temperature: 0,
|
|
650
|
+
appName: "my-eval-system",
|
|
651
|
+
})
|
|
652
|
+
);
|
|
636
653
|
```
|
|
637
654
|
|
|
638
|
-
**
|
|
655
|
+
**Custom Adapter (for any provider)**
|
|
656
|
+
|
|
639
657
|
```javascript
|
|
640
658
|
setLLMClient({
|
|
641
659
|
async complete(prompt) {
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
return (await response.json()).response;
|
|
647
|
-
}
|
|
660
|
+
// Implement for your LLM provider
|
|
661
|
+
const response = await yourLLM.generate(prompt);
|
|
662
|
+
return response.text;
|
|
663
|
+
},
|
|
648
664
|
});
|
|
649
665
|
```
|
|
650
666
|
|
|
@@ -660,6 +676,7 @@ setLLMClient({
|
|
|
660
676
|
evalsense is built on the principle that **metrics are predictions, not facts**.
|
|
661
677
|
|
|
662
678
|
Instead of treating LLM-as-judge metrics (relevance, hallucination, etc.) as ground truth, evalsense:
|
|
679
|
+
|
|
663
680
|
- Treats them as **weak labels** from a model
|
|
664
681
|
- Validates them statistically against human references when available
|
|
665
682
|
- Computes confusion matrices to reveal bias and systematic errors
|
|
@@ -146,10 +146,7 @@ function getSupport(cm, label) {
|
|
|
146
146
|
}
|
|
147
147
|
function formatConfusionMatrix(cm) {
|
|
148
148
|
const maxLabelLen = Math.max(...cm.labels.map((l) => l.length), 8);
|
|
149
|
-
const colWidth = Math.max(
|
|
150
|
-
...cm.matrix.flat().map((n) => String(n).length),
|
|
151
|
-
maxLabelLen
|
|
152
|
-
);
|
|
149
|
+
const colWidth = Math.max(...cm.matrix.flat().map((n) => String(n).length), maxLabelLen);
|
|
153
150
|
const header = " ".repeat(maxLabelLen + 2) + cm.labels.map((l) => l.padStart(colWidth)).join(" ");
|
|
154
151
|
const rows = cm.labels.map((label, i) => {
|
|
155
152
|
const rowData = cm.matrix[i].map((n) => String(n).padStart(colWidth)).join(" ");
|
|
@@ -261,7 +258,7 @@ var ConsoleReporter = class {
|
|
|
261
258
|
*/
|
|
262
259
|
printHeader(fileCount) {
|
|
263
260
|
this.log("");
|
|
264
|
-
this.log(this.color("bold", `EvalSense v0.
|
|
261
|
+
this.log(this.color("bold", `EvalSense v0.3.0`));
|
|
265
262
|
this.log(this.color("dim", `Running ${fileCount} eval file(s)...`));
|
|
266
263
|
this.log("");
|
|
267
264
|
}
|
|
@@ -296,9 +293,7 @@ var ConsoleReporter = class {
|
|
|
296
293
|
for (const fm of test.fieldMetrics) {
|
|
297
294
|
this.printFieldMetrics(fm);
|
|
298
295
|
}
|
|
299
|
-
if (test.error && test.status === "
|
|
300
|
-
this.log(this.color("red", ` ${test.error.message}`));
|
|
301
|
-
} else if (test.error && test.status === "error") {
|
|
296
|
+
if (test.error && test.status === "error") {
|
|
302
297
|
this.log(this.color("red", ` Error: ${test.error.message}`));
|
|
303
298
|
}
|
|
304
299
|
for (const assertion of test.assertions) {
|
|
@@ -426,23 +421,10 @@ var ConsoleReporter = class {
|
|
|
426
421
|
console.log(message);
|
|
427
422
|
}
|
|
428
423
|
};
|
|
429
|
-
var DEFAULT_PATTERNS = [
|
|
430
|
-
|
|
431
|
-
"**/*.eval.ts",
|
|
432
|
-
"**/*.eval.mjs"
|
|
433
|
-
];
|
|
434
|
-
var DEFAULT_IGNORE = [
|
|
435
|
-
"**/node_modules/**",
|
|
436
|
-
"**/dist/**",
|
|
437
|
-
"**/build/**",
|
|
438
|
-
"**/.git/**"
|
|
439
|
-
];
|
|
424
|
+
var DEFAULT_PATTERNS = ["**/*.eval.js", "**/*.eval.ts", "**/*.eval.mjs"];
|
|
425
|
+
var DEFAULT_IGNORE = ["**/node_modules/**", "**/dist/**", "**/build/**", "**/.git/**"];
|
|
440
426
|
async function discoverEvalFiles(options = {}) {
|
|
441
|
-
const {
|
|
442
|
-
patterns = DEFAULT_PATTERNS,
|
|
443
|
-
ignore = DEFAULT_IGNORE,
|
|
444
|
-
cwd = process.cwd()
|
|
445
|
-
} = options;
|
|
427
|
+
const { patterns = DEFAULT_PATTERNS, ignore = DEFAULT_IGNORE, cwd = process.cwd() } = options;
|
|
446
428
|
const files = [];
|
|
447
429
|
for (const pattern of patterns) {
|
|
448
430
|
const matches = await glob.glob(pattern, {
|
|
@@ -775,5 +757,5 @@ exports.parseReport = parseReport;
|
|
|
775
757
|
exports.recordAssertion = recordAssertion;
|
|
776
758
|
exports.recordFieldMetrics = recordFieldMetrics;
|
|
777
759
|
exports.setCurrentSuite = setCurrentSuite;
|
|
778
|
-
//# sourceMappingURL=chunk-
|
|
779
|
-
//# sourceMappingURL=chunk-
|
|
760
|
+
//# sourceMappingURL=chunk-DFC6FRTG.cjs.map
|
|
761
|
+
//# sourceMappingURL=chunk-DFC6FRTG.cjs.map
|