@hazeljs/eval 0.8.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 +192 -0
- package/README.md +184 -0
- package/dist/agent-trajectory.d.ts +9 -0
- package/dist/agent-trajectory.d.ts.map +1 -0
- package/dist/agent-trajectory.js +33 -0
- package/dist/ci-reporter.d.ts +10 -0
- package/dist/ci-reporter.d.ts.map +1 -0
- package/dist/ci-reporter.js +19 -0
- package/dist/dataset-loader.d.ts +3 -0
- package/dist/dataset-loader.d.ts.map +1 -0
- package/dist/dataset-loader.js +45 -0
- package/dist/golden-runner.d.ts +16 -0
- package/dist/golden-runner.d.ts.map +1 -0
- package/dist/golden-runner.js +82 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/llm-judge.d.ts +16 -0
- package/dist/llm-judge.d.ts.map +1 -0
- package/dist/llm-judge.js +39 -0
- package/dist/rag-metrics.d.ts +19 -0
- package/dist/rag-metrics.d.ts.map +1 -0
- package/dist/rag-metrics.js +39 -0
- package/dist/retrieval-metrics.d.ts +12 -0
- package/dist/retrieval-metrics.d.ts.map +1 -0
- package/dist/retrieval-metrics.js +52 -0
- package/dist/types.d.ts +40 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
Copyright 2024 HazelJS Team
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
22
|
+
|
|
23
|
+
1. Definitions.
|
|
24
|
+
|
|
25
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
26
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
27
|
+
|
|
28
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
29
|
+
the copyright owner that is granting the License.
|
|
30
|
+
|
|
31
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
32
|
+
other entities that control, are controlled by, or are under common
|
|
33
|
+
control with that entity. For the purposes of this definition,
|
|
34
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
35
|
+
direction or management of such entity, whether by contract or
|
|
36
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
37
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
38
|
+
|
|
39
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
40
|
+
exercising permissions granted by this License.
|
|
41
|
+
|
|
42
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
43
|
+
including but not limited to software source code, documentation
|
|
44
|
+
source, and configuration files.
|
|
45
|
+
|
|
46
|
+
"Object" form shall mean any form resulting from mechanical
|
|
47
|
+
transformation or translation of a Source form, including but
|
|
48
|
+
not limited to compiled object code, generated documentation,
|
|
49
|
+
and conversions to other media types.
|
|
50
|
+
|
|
51
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
52
|
+
Object form, made available under the License, as indicated by a
|
|
53
|
+
copyright notice that is included in or attached to the work
|
|
54
|
+
(an example is provided in the Appendix below).
|
|
55
|
+
|
|
56
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
57
|
+
form, that is based on (or derived from) the Work and for which the
|
|
58
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
59
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
60
|
+
of this License, Derivative Works shall not include works that remain
|
|
61
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
62
|
+
the Work and Derivative Works thereof.
|
|
63
|
+
|
|
64
|
+
"Contribution" shall mean any work of authorship, including
|
|
65
|
+
the original version of the Work and any modifications or additions
|
|
66
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
67
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
68
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
69
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
70
|
+
means any form of electronic, verbal, or written communication sent
|
|
71
|
+
to the Licensor or its representatives, including but not limited to
|
|
72
|
+
communication on electronic mailing lists, source code control systems,
|
|
73
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
74
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
75
|
+
excluding communication that is conspicuously marked or otherwise
|
|
76
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
77
|
+
|
|
78
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
79
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
80
|
+
subsequently incorporated within the Work.
|
|
81
|
+
|
|
82
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
83
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
84
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
85
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
86
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
87
|
+
Work and such Derivative Works in Source or Object form.
|
|
88
|
+
|
|
89
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
90
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
91
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
92
|
+
(except as stated in this section) patent license to make, have made,
|
|
93
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
94
|
+
where such license applies only to those patent claims licensable
|
|
95
|
+
by such Contributor that are necessarily infringed by their
|
|
96
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
97
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
98
|
+
institute patent litigation against any entity (including a
|
|
99
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
100
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
101
|
+
or contributory patent infringement, then any patent licenses
|
|
102
|
+
granted to You under this License for that Work shall terminate
|
|
103
|
+
as of the date such litigation is filed.
|
|
104
|
+
|
|
105
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
106
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
107
|
+
modifications, and in Source or Object form, provided that You
|
|
108
|
+
meet the following conditions:
|
|
109
|
+
|
|
110
|
+
(a) You must give any other recipients of the Work or
|
|
111
|
+
Derivative Works a copy of this License; and
|
|
112
|
+
|
|
113
|
+
(b) You must cause any modified files to carry prominent notices
|
|
114
|
+
stating that You changed the files; and
|
|
115
|
+
|
|
116
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
117
|
+
that You distribute, all copyright, patent, trademark, and
|
|
118
|
+
attribution notices from the Source form of the Work,
|
|
119
|
+
excluding those notices that do not pertain to any part of
|
|
120
|
+
the Derivative Works; and
|
|
121
|
+
|
|
122
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
123
|
+
distribution, then any Derivative Works that You distribute must
|
|
124
|
+
include a readable copy of the attribution notices contained
|
|
125
|
+
within such NOTICE file, excluding those notices that do not
|
|
126
|
+
pertain to any part of the Derivative Works, in at least one
|
|
127
|
+
of the following places: within a NOTICE text file distributed
|
|
128
|
+
as part of the Derivative Works; within the Source form or
|
|
129
|
+
documentation, if provided along with the Derivative Works; or,
|
|
130
|
+
within a display generated by the Derivative Works, if and
|
|
131
|
+
wherever such third-party notices normally appear. The contents
|
|
132
|
+
of the NOTICE file are for informational purposes only and
|
|
133
|
+
do not modify the License. You may add Your own attribution
|
|
134
|
+
notices within Derivative Works that You distribute, alongside
|
|
135
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
136
|
+
that such additional attribution notices cannot be construed
|
|
137
|
+
as modifying the License.
|
|
138
|
+
|
|
139
|
+
You may add Your own copyright statement to Your modifications and
|
|
140
|
+
may provide additional or different license terms and conditions
|
|
141
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
142
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
143
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
144
|
+
the conditions stated in this License.
|
|
145
|
+
|
|
146
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
147
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
148
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
149
|
+
this License, without any additional terms or conditions.
|
|
150
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
151
|
+
the terms of any separate license agreement you may have executed
|
|
152
|
+
with Licensor regarding such Contributions.
|
|
153
|
+
|
|
154
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
155
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
156
|
+
except as required for reasonable and customary use in describing the
|
|
157
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
158
|
+
|
|
159
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
160
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
161
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
162
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
163
|
+
implied, including, without limitation, any warranties or conditions
|
|
164
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
165
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
166
|
+
appropriateness of using or redistributing the Work and assume any
|
|
167
|
+
risks associated with Your exercise of permissions under this License.
|
|
168
|
+
|
|
169
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
170
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
171
|
+
unless required by applicable law (such as deliberate and grossly
|
|
172
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
173
|
+
liable to You for damages, including any direct, indirect, special,
|
|
174
|
+
incidental, or consequential damages of any character arising as a
|
|
175
|
+
result of this License or out of the use or inability to use the
|
|
176
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
177
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
178
|
+
other commercial damages or losses), even if such Contributor
|
|
179
|
+
has been advised of the possibility of such damages.
|
|
180
|
+
|
|
181
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
182
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
183
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
184
|
+
or other liability obligations and/or rights consistent with this
|
|
185
|
+
License. However, in accepting such obligations, You may act only
|
|
186
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
187
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
188
|
+
defend, and hold each Contributor harmless for any liability
|
|
189
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
190
|
+
of your accepting any such warranty or additional liability.
|
|
191
|
+
|
|
192
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# @hazeljs/eval
|
|
2
|
+
|
|
3
|
+
Evaluation toolkit for HazelJS AI applications: golden datasets, classical IR metrics (precision/recall@k, MRR, NDCG), RAG-style heuristics, agent trajectory scoring, LLM-as-judge helpers, and CI-friendly reporting.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@hazeljs/eval)
|
|
6
|
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @hazeljs/eval
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Peer dependency:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @hazeljs/core
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Features
|
|
21
|
+
|
|
22
|
+
- **Golden datasets** — JSON format with cases (input, optional expected output, tool calls, retrieved IDs)
|
|
23
|
+
- **Retrieval metrics** — `precisionAtK`, `recallAtK`, `meanReciprocalRank`, `ndcgAtK`
|
|
24
|
+
- **RAG helpers** — `evaluateRetrieval`, `answerContextOverlap` (lightweight faithfulness proxy)
|
|
25
|
+
- **Agent trajectories** — `trajectoryScore`, `toolCallAccuracy`
|
|
26
|
+
- **LLM judge** — `parseJudgeScore`, `buildRelevanceJudgePrompt`, `buildFaithfulnessJudgePrompt` (wire to your model)
|
|
27
|
+
- **Runner** — `runGoldenDataset` with configurable concurrency and thresholds
|
|
28
|
+
- **CI** — `reportEvalForCi` with optional non-zero exit via `process.exitCode`
|
|
29
|
+
|
|
30
|
+
## Quick start
|
|
31
|
+
|
|
32
|
+
### Load a dataset and run evals
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { loadGoldenDatasetFromJson, runGoldenDataset, reportEvalForCi } from '@hazeljs/eval';
|
|
36
|
+
|
|
37
|
+
const dataset = loadGoldenDatasetFromJson('./eval/golden.json');
|
|
38
|
+
|
|
39
|
+
const result = await runGoldenDataset(
|
|
40
|
+
dataset,
|
|
41
|
+
async ({ input }) => {
|
|
42
|
+
// Call your RAG, agent, or HazelAI pipeline here
|
|
43
|
+
return {
|
|
44
|
+
output: await myPipeline.answer(input),
|
|
45
|
+
toolCalls: ['search', 'summarize'],
|
|
46
|
+
retrievedIds: ['doc-1', 'doc-2'],
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
{ concurrency: 2, minAverageScore: 0.75 }
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
reportEvalForCi(result, { exitOnFail: true });
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Retrieval metrics only
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { evaluateRetrieval } from '@hazeljs/eval';
|
|
59
|
+
|
|
60
|
+
const metrics = evaluateRetrieval({
|
|
61
|
+
query: 'What is HazelJS?',
|
|
62
|
+
retrievedIds: ['a', 'b', 'c'],
|
|
63
|
+
relevantIds: ['a', 'x'],
|
|
64
|
+
k: 5,
|
|
65
|
+
});
|
|
66
|
+
// metrics.precisionAtK, recallAtK, mrr, ndcgAtK
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Golden dataset JSON shape
|
|
70
|
+
|
|
71
|
+
```json
|
|
72
|
+
{
|
|
73
|
+
"name": "support-bot",
|
|
74
|
+
"version": "1.0.0",
|
|
75
|
+
"cases": [
|
|
76
|
+
{
|
|
77
|
+
"id": "refund-1",
|
|
78
|
+
"input": "How do I get a refund?",
|
|
79
|
+
"expectedOutput": "within 30 days",
|
|
80
|
+
"expectedToolCalls": ["lookup_policy"],
|
|
81
|
+
"expectedRetrievedIds": ["policy-refunds"]
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Real-life example: regression-test a support FAQ (RAG + CI)
|
|
88
|
+
|
|
89
|
+
You ship a **customer support** app backed by **HazelAI** and **@hazeljs/rag**. Before each release, you run the same golden questions and fail the pipeline if quality drops. (Install `@hazeljs/ai` and `@hazeljs/rag` alongside `@hazeljs/eval` for this flow.)
|
|
90
|
+
|
|
91
|
+
**1. Check in a golden dataset** — e.g. `eval/golden.support.json`:
|
|
92
|
+
|
|
93
|
+
```json
|
|
94
|
+
{
|
|
95
|
+
"name": "support-faq",
|
|
96
|
+
"version": "2025.04",
|
|
97
|
+
"cases": [
|
|
98
|
+
{
|
|
99
|
+
"id": "billing-cycle",
|
|
100
|
+
"input": "When am I charged each month?",
|
|
101
|
+
"expectedOutput": "first of the month"
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
"id": "api-rate-limit",
|
|
105
|
+
"input": "What happens if I exceed the API rate limit?",
|
|
106
|
+
"expectedOutput": "429"
|
|
107
|
+
}
|
|
108
|
+
]
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
**2. Add a small eval script** (run with `npx tsx scripts/run-support-eval.ts` or compile into your CLI):
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import * as path from 'path';
|
|
116
|
+
import { HazelAI } from '@hazeljs/ai';
|
|
117
|
+
import { loadGoldenDatasetFromJson, runGoldenDataset, reportEvalForCi } from '@hazeljs/eval';
|
|
118
|
+
|
|
119
|
+
async function main() {
|
|
120
|
+
const datasetPath = path.join(process.cwd(), 'eval/golden.support.json');
|
|
121
|
+
const dataset = loadGoldenDatasetFromJson(datasetPath);
|
|
122
|
+
|
|
123
|
+
const ai = HazelAI.create({
|
|
124
|
+
defaultProvider: 'openai',
|
|
125
|
+
persistence: {
|
|
126
|
+
rag: {
|
|
127
|
+
vectorStore: 'qdrant',
|
|
128
|
+
connectionString: process.env.QDRANT_URL ?? 'http://127.0.0.1:6333',
|
|
129
|
+
indexName: 'support-docs',
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const result = await runGoldenDataset(
|
|
135
|
+
dataset,
|
|
136
|
+
async ({ input }) => {
|
|
137
|
+
const rag = await ai.rag.ask(input, { topK: 8 });
|
|
138
|
+
const retrievedIds = rag.sources.map((s) => s.id);
|
|
139
|
+
return {
|
|
140
|
+
output: rag.answer,
|
|
141
|
+
retrievedIds,
|
|
142
|
+
};
|
|
143
|
+
},
|
|
144
|
+
{ concurrency: 1, minAverageScore: 0.7 }
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
reportEvalForCi(result, { exitOnFail: process.env.CI === 'true' });
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
main().catch((e) => {
|
|
151
|
+
console.error(e);
|
|
152
|
+
process.exitCode = 1;
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
**3. Wire CI** — in GitHub Actions (or similar), after indexes are built and env vars (`OPENAI_API_KEY`, `QDRANT_URL`, …) are set:
|
|
157
|
+
|
|
158
|
+
```yaml
|
|
159
|
+
- name: Run support golden evals
|
|
160
|
+
run: npx tsx scripts/run-support-eval.ts
|
|
161
|
+
env:
|
|
162
|
+
CI: true
|
|
163
|
+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
|
164
|
+
QDRANT_URL: ${{ secrets.QDRANT_URL }}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
If any case scores below `minAverageScore` or the run errors, `reportEvalForCi` sets `process.exitCode = 1` and the job fails, blocking a bad deploy. Adjust the runner to return `toolCalls` too if you evaluate an **agent** (e.g. `ai.agent('support', input)`) instead of plain RAG.
|
|
168
|
+
|
|
169
|
+
## Scripts
|
|
170
|
+
|
|
171
|
+
| Command | Description |
|
|
172
|
+
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
173
|
+
| `npm run build` | Compile TypeScript to `dist/` |
|
|
174
|
+
| `npm test` | Jest with coverage thresholds (85%+ statements/lines/functions/branches on implementation files; `src/index.ts` is excluded as a pure re-export barrel) |
|
|
175
|
+
| `npm run test:ci` | Jest with coverage reports; thresholds can be relaxed via `--no-coverage-threshold` when needed |
|
|
176
|
+
|
|
177
|
+
## License
|
|
178
|
+
|
|
179
|
+
Apache-2.0
|
|
180
|
+
|
|
181
|
+
## Links
|
|
182
|
+
|
|
183
|
+
- [HazelJS documentation](https://hazeljs.ai)
|
|
184
|
+
- [Monorepo source](https://github.com/hazel-js/hazeljs) — `packages/eval`
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent trajectory evaluation — expected tool sequence vs actual.
|
|
3
|
+
*/
|
|
4
|
+
export interface AgentTrajectory {
|
|
5
|
+
toolCalls: string[];
|
|
6
|
+
}
|
|
7
|
+
export declare function trajectoryScore(expected: string[], actual: string[]): number;
|
|
8
|
+
export declare function toolCallAccuracy(expected: string[], actual: string[]): number;
|
|
9
|
+
//# sourceMappingURL=agent-trajectory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-trajectory.d.ts","sourceRoot":"","sources":["../src/agent-trajectory.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,MAAM,EAAE,CAAC;CACrB;AAED,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAY5E;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAS7E"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Agent trajectory evaluation — expected tool sequence vs actual.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.trajectoryScore = trajectoryScore;
|
|
7
|
+
exports.toolCallAccuracy = toolCallAccuracy;
|
|
8
|
+
function trajectoryScore(expected, actual) {
|
|
9
|
+
if (expected.length === 0)
|
|
10
|
+
return 1;
|
|
11
|
+
const exp = [...expected];
|
|
12
|
+
let matched = 0;
|
|
13
|
+
let j = 0;
|
|
14
|
+
for (let i = 0; i < actual.length && j < exp.length; i++) {
|
|
15
|
+
if (actual[i] === exp[j]) {
|
|
16
|
+
matched++;
|
|
17
|
+
j++;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return matched / exp.length;
|
|
21
|
+
}
|
|
22
|
+
function toolCallAccuracy(expected, actual) {
|
|
23
|
+
const expSet = new Set(expected);
|
|
24
|
+
if (expSet.size === 0)
|
|
25
|
+
return 1;
|
|
26
|
+
const actSet = new Set(actual);
|
|
27
|
+
let hit = 0;
|
|
28
|
+
for (const e of expSet) {
|
|
29
|
+
if (actSet.has(e))
|
|
30
|
+
hit++;
|
|
31
|
+
}
|
|
32
|
+
return hit / expSet.size;
|
|
33
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { EvalRunResult } from './types';
|
|
2
|
+
export interface CiReporterOptions {
|
|
3
|
+
/** When true, exit process with code 1 if eval failed */
|
|
4
|
+
exitOnFail?: boolean;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Print a human-readable summary and optionally exit with failure for CI.
|
|
8
|
+
*/
|
|
9
|
+
export declare function reportEvalForCi(result: EvalRunResult, options?: CiReporterOptions): void;
|
|
10
|
+
//# sourceMappingURL=ci-reporter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ci-reporter.d.ts","sourceRoot":"","sources":["../src/ci-reporter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,MAAM,WAAW,iBAAiB;IAChC,yDAAyD;IACzD,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,IAAI,CAgBxF"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.reportEvalForCi = reportEvalForCi;
|
|
4
|
+
/**
|
|
5
|
+
* Print a human-readable summary and optionally exit with failure for CI.
|
|
6
|
+
*/
|
|
7
|
+
function reportEvalForCi(result, options) {
|
|
8
|
+
// eslint-disable-next-line no-console
|
|
9
|
+
console.log(`[hazeljs/eval] ${result.datasetName}@${result.datasetVersion} ` +
|
|
10
|
+
`avg=${result.averageScore.toFixed(3)} passed=${result.passed}`);
|
|
11
|
+
for (const c of result.caseResults) {
|
|
12
|
+
// eslint-disable-next-line no-console
|
|
13
|
+
console.log(` - ${c.caseId}: score=${c.score.toFixed(3)} passed=${c.passed}` +
|
|
14
|
+
(c.error ? ` error=${c.error}` : ''));
|
|
15
|
+
}
|
|
16
|
+
if (options?.exitOnFail && !result.passed) {
|
|
17
|
+
process.exitCode = 1;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataset-loader.d.ts","sourceRoot":"","sources":["../src/dataset-loader.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,CAOrE"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.loadGoldenDatasetFromJson = loadGoldenDatasetFromJson;
|
|
37
|
+
const fs = __importStar(require("fs"));
|
|
38
|
+
function loadGoldenDatasetFromJson(path) {
|
|
39
|
+
const raw = fs.readFileSync(path, 'utf8');
|
|
40
|
+
const data = JSON.parse(raw);
|
|
41
|
+
if (!data.name || !data.version || !Array.isArray(data.cases)) {
|
|
42
|
+
throw new Error(`Invalid golden dataset JSON at ${path}`);
|
|
43
|
+
}
|
|
44
|
+
return data;
|
|
45
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { EvalRunOptions, EvalRunResult, GoldenDataset } from './types';
|
|
2
|
+
export type CaseRunner = (input: {
|
|
3
|
+
id: string;
|
|
4
|
+
input: string;
|
|
5
|
+
/** @deprecated use metadata */
|
|
6
|
+
metadata?: Record<string, unknown>;
|
|
7
|
+
}) => Promise<{
|
|
8
|
+
output: string;
|
|
9
|
+
toolCalls?: string[];
|
|
10
|
+
retrievedIds?: string[];
|
|
11
|
+
}>;
|
|
12
|
+
/**
|
|
13
|
+
* Run a golden dataset against an async runner (your app, agent, or RAG pipeline).
|
|
14
|
+
*/
|
|
15
|
+
export declare function runGoldenDataset(dataset: GoldenDataset, runner: CaseRunner, options?: EvalRunOptions): Promise<EvalRunResult>;
|
|
16
|
+
//# sourceMappingURL=golden-runner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"golden-runner.d.ts","sourceRoot":"","sources":["../src/golden-runner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAc,aAAa,EAAE,MAAM,SAAS,CAAC;AAIxF,MAAM,MAAM,UAAU,GAAG,CAAC,KAAK,EAAE;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,+BAA+B;IAC/B,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC,KAAK,OAAO,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC,CAAC;AAEjF;;GAEG;AACH,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,aAAa,EACtB,MAAM,EAAE,UAAU,EAClB,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,aAAa,CAAC,CAyFxB"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.runGoldenDataset = runGoldenDataset;
|
|
4
|
+
const agent_trajectory_1 = require("./agent-trajectory");
|
|
5
|
+
const retrieval_metrics_1 = require("./retrieval-metrics");
|
|
6
|
+
/**
|
|
7
|
+
* Run a golden dataset against an async runner (your app, agent, or RAG pipeline).
|
|
8
|
+
*/
|
|
9
|
+
async function runGoldenDataset(dataset, runner, options) {
|
|
10
|
+
const caseResults = [];
|
|
11
|
+
const concurrency = Math.max(1, options?.concurrency ?? 1);
|
|
12
|
+
const chunks = [];
|
|
13
|
+
for (let i = 0; i < dataset.cases.length; i += concurrency) {
|
|
14
|
+
chunks.push(dataset.cases.slice(i, i + concurrency));
|
|
15
|
+
}
|
|
16
|
+
for (const batch of chunks) {
|
|
17
|
+
const batchOut = await Promise.all(batch.map(async (c) => {
|
|
18
|
+
try {
|
|
19
|
+
const result = await runner({
|
|
20
|
+
id: c.id,
|
|
21
|
+
input: c.input,
|
|
22
|
+
metadata: c.metadata,
|
|
23
|
+
});
|
|
24
|
+
let score = 0;
|
|
25
|
+
const details = {};
|
|
26
|
+
if (c.expectedOutput !== undefined) {
|
|
27
|
+
const out = result.output ?? '';
|
|
28
|
+
const exp = c.expectedOutput;
|
|
29
|
+
const passed = out.includes(exp) || out.trim().toLowerCase() === exp.trim().toLowerCase();
|
|
30
|
+
score = passed ? 1 : 0;
|
|
31
|
+
details.outputMatch = passed;
|
|
32
|
+
}
|
|
33
|
+
if (c.expectedToolCalls && c.expectedToolCalls.length > 0) {
|
|
34
|
+
const traj = (0, agent_trajectory_1.trajectoryScore)(c.expectedToolCalls, result.toolCalls ?? []);
|
|
35
|
+
score = score === 0 ? traj : (score + traj) / 2;
|
|
36
|
+
details.trajectoryScore = traj;
|
|
37
|
+
}
|
|
38
|
+
if (c.expectedRetrievedIds && c.expectedRetrievedIds.length > 0) {
|
|
39
|
+
const rel = new Set(c.expectedRetrievedIds);
|
|
40
|
+
const p = (0, retrieval_metrics_1.precisionAtK)(result.retrievedIds ?? [], rel, 5);
|
|
41
|
+
score = score === 0 ? p : (score + p) / 2;
|
|
42
|
+
details.precisionAt5 = p;
|
|
43
|
+
}
|
|
44
|
+
if (c.expectedOutput === undefined &&
|
|
45
|
+
(!c.expectedToolCalls || c.expectedToolCalls.length === 0) &&
|
|
46
|
+
(!c.expectedRetrievedIds || c.expectedRetrievedIds.length === 0)) {
|
|
47
|
+
score = 1;
|
|
48
|
+
}
|
|
49
|
+
const threshold = options?.minAverageScore ?? 0.7;
|
|
50
|
+
return {
|
|
51
|
+
caseId: c.id,
|
|
52
|
+
passed: score >= threshold,
|
|
53
|
+
score,
|
|
54
|
+
details,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
catch (e) {
|
|
58
|
+
return {
|
|
59
|
+
caseId: c.id,
|
|
60
|
+
passed: false,
|
|
61
|
+
score: 0,
|
|
62
|
+
error: e instanceof Error ? e.message : String(e),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}));
|
|
66
|
+
caseResults.push(...batchOut);
|
|
67
|
+
}
|
|
68
|
+
const averageScore = caseResults.length === 0
|
|
69
|
+
? 0
|
|
70
|
+
: caseResults.reduce((s, r) => s + r.score, 0) / caseResults.length;
|
|
71
|
+
const minAvg = options?.minAverageScore ?? 0;
|
|
72
|
+
const passed = averageScore >= minAvg &&
|
|
73
|
+
caseResults.every((r) => r.passed) &&
|
|
74
|
+
!caseResults.some((r) => r.error);
|
|
75
|
+
return {
|
|
76
|
+
datasetName: dataset.name,
|
|
77
|
+
datasetVersion: dataset.version,
|
|
78
|
+
caseResults,
|
|
79
|
+
averageScore,
|
|
80
|
+
passed,
|
|
81
|
+
};
|
|
82
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hazeljs/eval — Evaluation toolkit for AI-native HazelJS apps
|
|
3
|
+
*/
|
|
4
|
+
export type { GoldenCase, GoldenDataset, EvalRunOptions, CaseResult, EvalRunResult } from './types';
|
|
5
|
+
export { precisionAtK, recallAtK, meanReciprocalRank, ndcgAtK } from './retrieval-metrics';
|
|
6
|
+
export { evaluateRetrieval, answerContextOverlap, type RagRetrievalEvalInput, type RagRetrievalEvalResult, } from './rag-metrics';
|
|
7
|
+
export { trajectoryScore, toolCallAccuracy, type AgentTrajectory } from './agent-trajectory';
|
|
8
|
+
export { parseJudgeScore, buildRelevanceJudgePrompt, buildFaithfulnessJudgePrompt, type JudgeScore, type LLMJudgeFn, } from './llm-judge';
|
|
9
|
+
export { runGoldenDataset, type CaseRunner } from './golden-runner';
|
|
10
|
+
export { reportEvalForCi, type CiReporterOptions } from './ci-reporter';
|
|
11
|
+
export { loadGoldenDatasetFromJson } from './dataset-loader';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAEpG,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,kBAAkB,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAE3F,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,KAAK,qBAAqB,EAC1B,KAAK,sBAAsB,GAC5B,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,KAAK,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAE7F,OAAO,EACL,eAAe,EACf,yBAAyB,EACzB,4BAA4B,EAC5B,KAAK,UAAU,EACf,KAAK,UAAU,GAChB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,gBAAgB,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAEpE,OAAO,EAAE,eAAe,EAAE,KAAK,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAExE,OAAO,EAAE,yBAAyB,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @hazeljs/eval — Evaluation toolkit for AI-native HazelJS apps
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.loadGoldenDatasetFromJson = exports.reportEvalForCi = exports.runGoldenDataset = exports.buildFaithfulnessJudgePrompt = exports.buildRelevanceJudgePrompt = exports.parseJudgeScore = exports.toolCallAccuracy = exports.trajectoryScore = exports.answerContextOverlap = exports.evaluateRetrieval = exports.ndcgAtK = exports.meanReciprocalRank = exports.recallAtK = exports.precisionAtK = void 0;
|
|
7
|
+
var retrieval_metrics_1 = require("./retrieval-metrics");
|
|
8
|
+
Object.defineProperty(exports, "precisionAtK", { enumerable: true, get: function () { return retrieval_metrics_1.precisionAtK; } });
|
|
9
|
+
Object.defineProperty(exports, "recallAtK", { enumerable: true, get: function () { return retrieval_metrics_1.recallAtK; } });
|
|
10
|
+
Object.defineProperty(exports, "meanReciprocalRank", { enumerable: true, get: function () { return retrieval_metrics_1.meanReciprocalRank; } });
|
|
11
|
+
Object.defineProperty(exports, "ndcgAtK", { enumerable: true, get: function () { return retrieval_metrics_1.ndcgAtK; } });
|
|
12
|
+
var rag_metrics_1 = require("./rag-metrics");
|
|
13
|
+
Object.defineProperty(exports, "evaluateRetrieval", { enumerable: true, get: function () { return rag_metrics_1.evaluateRetrieval; } });
|
|
14
|
+
Object.defineProperty(exports, "answerContextOverlap", { enumerable: true, get: function () { return rag_metrics_1.answerContextOverlap; } });
|
|
15
|
+
var agent_trajectory_1 = require("./agent-trajectory");
|
|
16
|
+
Object.defineProperty(exports, "trajectoryScore", { enumerable: true, get: function () { return agent_trajectory_1.trajectoryScore; } });
|
|
17
|
+
Object.defineProperty(exports, "toolCallAccuracy", { enumerable: true, get: function () { return agent_trajectory_1.toolCallAccuracy; } });
|
|
18
|
+
var llm_judge_1 = require("./llm-judge");
|
|
19
|
+
Object.defineProperty(exports, "parseJudgeScore", { enumerable: true, get: function () { return llm_judge_1.parseJudgeScore; } });
|
|
20
|
+
Object.defineProperty(exports, "buildRelevanceJudgePrompt", { enumerable: true, get: function () { return llm_judge_1.buildRelevanceJudgePrompt; } });
|
|
21
|
+
Object.defineProperty(exports, "buildFaithfulnessJudgePrompt", { enumerable: true, get: function () { return llm_judge_1.buildFaithfulnessJudgePrompt; } });
|
|
22
|
+
var golden_runner_1 = require("./golden-runner");
|
|
23
|
+
Object.defineProperty(exports, "runGoldenDataset", { enumerable: true, get: function () { return golden_runner_1.runGoldenDataset; } });
|
|
24
|
+
var ci_reporter_1 = require("./ci-reporter");
|
|
25
|
+
Object.defineProperty(exports, "reportEvalForCi", { enumerable: true, get: function () { return ci_reporter_1.reportEvalForCi; } });
|
|
26
|
+
var dataset_loader_1 = require("./dataset-loader");
|
|
27
|
+
Object.defineProperty(exports, "loadGoldenDatasetFromJson", { enumerable: true, get: function () { return dataset_loader_1.loadGoldenDatasetFromJson; } });
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM-as-judge — pluggable interface; wire to @hazeljs/ai or any HTTP API.
|
|
3
|
+
*/
|
|
4
|
+
export type JudgeScore = {
|
|
5
|
+
score: number;
|
|
6
|
+
reasoning?: string;
|
|
7
|
+
};
|
|
8
|
+
export type LLMJudgeFn = (prompt: string) => Promise<string>;
|
|
9
|
+
/**
|
|
10
|
+
* Ask a judge model to score 0–1 given a rubric prompt.
|
|
11
|
+
* Expects JSON in the response: `{ "score": number, "reasoning"?: string }`
|
|
12
|
+
*/
|
|
13
|
+
export declare function parseJudgeScore(raw: string): Promise<JudgeScore>;
|
|
14
|
+
export declare function buildRelevanceJudgePrompt(query: string, answer: string): string;
|
|
15
|
+
export declare function buildFaithfulnessJudgePrompt(context: string, answer: string): string;
|
|
16
|
+
//# sourceMappingURL=llm-judge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm-judge.d.ts","sourceRoot":"","sources":["../src/llm-judge.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,MAAM,UAAU,GAAG;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;AAE7D;;;GAGG;AACH,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAQtE;AAED,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAO/E;AAED,wBAAgB,4BAA4B,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CASpF"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* LLM-as-judge — pluggable interface; wire to @hazeljs/ai or any HTTP API.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.parseJudgeScore = parseJudgeScore;
|
|
7
|
+
exports.buildRelevanceJudgePrompt = buildRelevanceJudgePrompt;
|
|
8
|
+
exports.buildFaithfulnessJudgePrompt = buildFaithfulnessJudgePrompt;
|
|
9
|
+
/**
|
|
10
|
+
* Ask a judge model to score 0–1 given a rubric prompt.
|
|
11
|
+
* Expects JSON in the response: `{ "score": number, "reasoning"?: string }`
|
|
12
|
+
*/
|
|
13
|
+
async function parseJudgeScore(raw) {
|
|
14
|
+
const cleaned = raw
|
|
15
|
+
.replace(/^```(?:json)?\s*/i, '')
|
|
16
|
+
.replace(/\s*```$/i, '')
|
|
17
|
+
.trim();
|
|
18
|
+
const parsed = JSON.parse(cleaned);
|
|
19
|
+
const score = typeof parsed.score === 'number' ? Math.max(0, Math.min(1, parsed.score)) : 0;
|
|
20
|
+
return { score, reasoning: parsed.reasoning };
|
|
21
|
+
}
|
|
22
|
+
function buildRelevanceJudgePrompt(query, answer) {
|
|
23
|
+
return `Rate how relevant the answer is to the query on a scale of 0 to 1.
|
|
24
|
+
Respond with JSON only: {"score": number between 0 and 1, "reasoning": string}
|
|
25
|
+
|
|
26
|
+
Query: ${query}
|
|
27
|
+
|
|
28
|
+
Answer: ${answer}`;
|
|
29
|
+
}
|
|
30
|
+
function buildFaithfulnessJudgePrompt(context, answer) {
|
|
31
|
+
return `Rate whether the answer is grounded in the given context (0 = hallucinated, 1 = fully supported).
|
|
32
|
+
Respond with JSON only: {"score": number between 0 and 1, "reasoning": string}
|
|
33
|
+
|
|
34
|
+
Context:
|
|
35
|
+
${context}
|
|
36
|
+
|
|
37
|
+
Answer:
|
|
38
|
+
${answer}`;
|
|
39
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RAG-oriented evaluation helpers (Ragas-style heuristics without external deps).
|
|
3
|
+
*/
|
|
4
|
+
export interface RagRetrievalEvalInput {
|
|
5
|
+
query: string;
|
|
6
|
+
retrievedIds: string[];
|
|
7
|
+
relevantIds: string[];
|
|
8
|
+
k?: number;
|
|
9
|
+
}
|
|
10
|
+
export interface RagRetrievalEvalResult {
|
|
11
|
+
precisionAtK: number;
|
|
12
|
+
recallAtK: number;
|
|
13
|
+
mrr: number;
|
|
14
|
+
ndcgAtK: number;
|
|
15
|
+
}
|
|
16
|
+
export declare function evaluateRetrieval(input: RagRetrievalEvalInput): RagRetrievalEvalResult;
|
|
17
|
+
/** Token overlap between answer and context (cheap faithfulness proxy) */
|
|
18
|
+
export declare function answerContextOverlap(answer: string, context: string): number;
|
|
19
|
+
//# sourceMappingURL=rag-metrics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rag-metrics.d.ts","sourceRoot":"","sources":["../src/rag-metrics.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,MAAM,WAAW,qBAAqB;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,CAAC,CAAC,EAAE,MAAM,CAAC;CACZ;AAED,MAAM,WAAW,sBAAsB;IACrC,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,qBAAqB,GAAG,sBAAsB,CAStF;AAED,0EAA0E;AAC1E,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAqB5E"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* RAG-oriented evaluation helpers (Ragas-style heuristics without external deps).
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.evaluateRetrieval = evaluateRetrieval;
|
|
7
|
+
exports.answerContextOverlap = answerContextOverlap;
|
|
8
|
+
const retrieval_metrics_1 = require("./retrieval-metrics");
|
|
9
|
+
function evaluateRetrieval(input) {
|
|
10
|
+
const k = input.k ?? 5;
|
|
11
|
+
const rel = new Set(input.relevantIds);
|
|
12
|
+
return {
|
|
13
|
+
precisionAtK: (0, retrieval_metrics_1.precisionAtK)(input.retrievedIds, rel, k),
|
|
14
|
+
recallAtK: (0, retrieval_metrics_1.recallAtK)(input.retrievedIds, rel, k),
|
|
15
|
+
mrr: (0, retrieval_metrics_1.meanReciprocalRank)(input.retrievedIds, rel),
|
|
16
|
+
ndcgAtK: (0, retrieval_metrics_1.ndcgAtK)(input.retrievedIds, rel, k),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** Token overlap between answer and context (cheap faithfulness proxy) */
|
|
20
|
+
function answerContextOverlap(answer, context) {
|
|
21
|
+
const a = new Set(answer
|
|
22
|
+
.toLowerCase()
|
|
23
|
+
.replace(/[^\p{L}\p{N}\s]/gu, ' ')
|
|
24
|
+
.split(/\s+/)
|
|
25
|
+
.filter((w) => w.length > 2));
|
|
26
|
+
const c = new Set(context
|
|
27
|
+
.toLowerCase()
|
|
28
|
+
.replace(/[^\p{L}\p{N}\s]/gu, ' ')
|
|
29
|
+
.split(/\s+/)
|
|
30
|
+
.filter((w) => w.length > 2));
|
|
31
|
+
if (a.size === 0)
|
|
32
|
+
return 0;
|
|
33
|
+
let inter = 0;
|
|
34
|
+
for (const w of a) {
|
|
35
|
+
if (c.has(w))
|
|
36
|
+
inter++;
|
|
37
|
+
}
|
|
38
|
+
return inter / a.size;
|
|
39
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Classic IR metrics for retrieval evaluation (no LLM required).
|
|
3
|
+
*/
|
|
4
|
+
/** Precision@k: fraction of top-k that are relevant */
|
|
5
|
+
export declare function precisionAtK(retrievedIds: string[], relevantIds: Set<string>, k: number): number;
|
|
6
|
+
/** Recall@k: fraction of all relevant docs found in top-k */
|
|
7
|
+
export declare function recallAtK(retrievedIds: string[], relevantIds: Set<string>, k: number): number;
|
|
8
|
+
/** Mean Reciprocal Rank — first relevant position */
|
|
9
|
+
export declare function meanReciprocalRank(retrievedIds: string[], relevantIds: Set<string>): number;
|
|
10
|
+
/** Normalized Discounted Cumulative Gain (binary relevance) */
|
|
11
|
+
export declare function ndcgAtK(retrievedIds: string[], relevantIds: Set<string>, k: number): number;
|
|
12
|
+
//# sourceMappingURL=retrieval-metrics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retrieval-metrics.d.ts","sourceRoot":"","sources":["../src/retrieval-metrics.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,uDAAuD;AACvD,wBAAgB,YAAY,CAAC,YAAY,EAAE,MAAM,EAAE,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAKhG;AAED,6DAA6D;AAC7D,wBAAgB,SAAS,CAAC,YAAY,EAAE,MAAM,EAAE,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAQ7F;AAED,qDAAqD;AACrD,wBAAgB,kBAAkB,CAAC,YAAY,EAAE,MAAM,EAAE,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,MAAM,CAO3F;AAED,+DAA+D;AAC/D,wBAAgB,OAAO,CAAC,YAAY,EAAE,MAAM,EAAE,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAY3F"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Classic IR metrics for retrieval evaluation (no LLM required).
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.precisionAtK = precisionAtK;
|
|
7
|
+
exports.recallAtK = recallAtK;
|
|
8
|
+
exports.meanReciprocalRank = meanReciprocalRank;
|
|
9
|
+
exports.ndcgAtK = ndcgAtK;
|
|
10
|
+
/** Precision@k: fraction of top-k that are relevant */
|
|
11
|
+
function precisionAtK(retrievedIds, relevantIds, k) {
|
|
12
|
+
if (k <= 0)
|
|
13
|
+
return 0;
|
|
14
|
+
const top = retrievedIds.slice(0, k);
|
|
15
|
+
const hits = top.filter((id) => relevantIds.has(id)).length;
|
|
16
|
+
return hits / Math.min(k, top.length || 1);
|
|
17
|
+
}
|
|
18
|
+
/** Recall@k: fraction of all relevant docs found in top-k */
|
|
19
|
+
function recallAtK(retrievedIds, relevantIds, k) {
|
|
20
|
+
if (relevantIds.size === 0)
|
|
21
|
+
return 1;
|
|
22
|
+
const top = new Set(retrievedIds.slice(0, k));
|
|
23
|
+
let hit = 0;
|
|
24
|
+
for (const id of relevantIds) {
|
|
25
|
+
if (top.has(id))
|
|
26
|
+
hit++;
|
|
27
|
+
}
|
|
28
|
+
return hit / relevantIds.size;
|
|
29
|
+
}
|
|
30
|
+
/** Mean Reciprocal Rank — first relevant position */
|
|
31
|
+
function meanReciprocalRank(retrievedIds, relevantIds) {
|
|
32
|
+
for (let i = 0; i < retrievedIds.length; i++) {
|
|
33
|
+
if (relevantIds.has(retrievedIds[i])) {
|
|
34
|
+
return 1 / (i + 1);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
/** Normalized Discounted Cumulative Gain (binary relevance) */
|
|
40
|
+
function ndcgAtK(retrievedIds, relevantIds, k) {
|
|
41
|
+
const top = retrievedIds.slice(0, k);
|
|
42
|
+
const dcg = top.reduce((acc, id, idx) => {
|
|
43
|
+
const rel = relevantIds.has(id) ? 1 : 0;
|
|
44
|
+
return acc + (rel === 0 ? 0 : rel / Math.log2(idx + 2));
|
|
45
|
+
}, 0);
|
|
46
|
+
const idealHits = Math.min(relevantIds.size, k);
|
|
47
|
+
let idcg = 0;
|
|
48
|
+
for (let i = 0; i < idealHits; i++) {
|
|
49
|
+
idcg += 1 / Math.log2(i + 2);
|
|
50
|
+
}
|
|
51
|
+
return idcg === 0 ? 0 : dcg / idcg;
|
|
52
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for @hazeljs/eval
|
|
3
|
+
*/
|
|
4
|
+
export interface GoldenCase {
|
|
5
|
+
id: string;
|
|
6
|
+
input: string;
|
|
7
|
+
/** Expected final answer (substring or exact match depending on matcher) */
|
|
8
|
+
expectedOutput?: string;
|
|
9
|
+
/** Expected tool / function names in order (agent eval) */
|
|
10
|
+
expectedToolCalls?: string[];
|
|
11
|
+
/** Relevant document IDs or content snippets retrieval should surface */
|
|
12
|
+
expectedRetrievedIds?: string[];
|
|
13
|
+
metadata?: Record<string, unknown>;
|
|
14
|
+
}
|
|
15
|
+
export interface GoldenDataset {
|
|
16
|
+
name: string;
|
|
17
|
+
version: string;
|
|
18
|
+
cases: GoldenCase[];
|
|
19
|
+
}
|
|
20
|
+
export interface EvalRunOptions {
|
|
21
|
+
/** Max parallel cases (default 1) */
|
|
22
|
+
concurrency?: number;
|
|
23
|
+
/** Fail CI when average score below threshold */
|
|
24
|
+
minAverageScore?: number;
|
|
25
|
+
}
|
|
26
|
+
export interface CaseResult {
|
|
27
|
+
caseId: string;
|
|
28
|
+
passed: boolean;
|
|
29
|
+
score: number;
|
|
30
|
+
details?: Record<string, unknown>;
|
|
31
|
+
error?: string;
|
|
32
|
+
}
|
|
33
|
+
export interface EvalRunResult {
|
|
34
|
+
datasetName: string;
|
|
35
|
+
datasetVersion: string;
|
|
36
|
+
caseResults: CaseResult[];
|
|
37
|
+
averageScore: number;
|
|
38
|
+
passed: boolean;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,4EAA4E;IAC5E,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,2DAA2D;IAC3D,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC7B,yEAAyE;IACzE,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;IAChC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,UAAU,EAAE,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC7B,qCAAqC;IACrC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iDAAiD;IACjD,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,aAAa;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,EAAE,UAAU,EAAE,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,OAAO,CAAC;CACjB"}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hazeljs/eval",
|
|
3
|
+
"version": "0.8.1",
|
|
4
|
+
"description": "Evaluation toolkit for HazelJS AI apps — golden datasets, RAG metrics, agent trajectories, LLM-as-judge, CI reports",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"test": "jest --coverage --passWithNoTests",
|
|
13
|
+
"test:ci": "jest --coverage --coverageReporters=text --coverageReporters=lcov --no-coverage-threshold",
|
|
14
|
+
"lint": "eslint \"src/**/*.ts\" --ignore-pattern \"**/*.d.ts\"",
|
|
15
|
+
"lint:fix": "eslint \"src/**/*.ts\" --ignore-pattern \"**/*.d.ts\" --fix",
|
|
16
|
+
"clean": "rm -rf dist && find src -type f \\( -name '*.js' -o -name '*.d.ts' -o -name '*.d.ts.map' \\) -delete 2>/dev/null || true"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"zod": "^3.25.76"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@hazeljs/core": ">=0.2.0-beta.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@hazeljs/core": "^0.8.1",
|
|
26
|
+
"@types/jest": "^29.5.14",
|
|
27
|
+
"@types/node": "^20.19.39",
|
|
28
|
+
"@typescript-eslint/eslint-plugin": "^8.58.0",
|
|
29
|
+
"@typescript-eslint/parser": "^8.58.0",
|
|
30
|
+
"eslint": "^8.57.1",
|
|
31
|
+
"eslint-config-prettier": "^9.1.2",
|
|
32
|
+
"eslint-plugin-prettier": "^5.5.5",
|
|
33
|
+
"jest": "^29.7.0",
|
|
34
|
+
"prettier": "^3.8.1",
|
|
35
|
+
"ts-jest": "^29.4.9",
|
|
36
|
+
"typescript": "^5.9.3"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/hazel-js/hazeljs.git",
|
|
44
|
+
"directory": "packages/eval"
|
|
45
|
+
},
|
|
46
|
+
"keywords": [
|
|
47
|
+
"hazeljs",
|
|
48
|
+
"eval",
|
|
49
|
+
"llm",
|
|
50
|
+
"rag",
|
|
51
|
+
"agent",
|
|
52
|
+
"metrics"
|
|
53
|
+
],
|
|
54
|
+
"author": "Muhammad Arslan <muhammad.arslan@hazeljs.ai>",
|
|
55
|
+
"license": "Apache-2.0",
|
|
56
|
+
"gitHead": "8b7685d1250c4622f25d83992f58e13a59bb3dba"
|
|
57
|
+
}
|