@scalvert/eval-core 0.5.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 +21 -0
- package/README.md +109 -0
- package/dist/index.d.ts +224 -0
- package/dist/index.js +232 -0
- package/package.json +89 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Steve Calvert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# @scalvert/eval-core
|
|
2
|
+
|
|
3
|
+
[](https://github.com/scalvert/eval-core/actions/workflows/ci-build.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@scalvert/eval-core)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
General-purpose LLM evaluation primitives. Run test cases against a model, score responses with an LLM judge, and compare results against a saved baseline.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @scalvert/eval-core
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Running evaluations
|
|
18
|
+
|
|
19
|
+
The core workflow: provide a `respond` function (calls your model), a `judge` function (scores the response), and a set of test cases.
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { runEval, createAnthropicJudge, type ResponseFn } from '@scalvert/eval-core';
|
|
23
|
+
|
|
24
|
+
const respond: ResponseFn = async (input) => {
|
|
25
|
+
// Call your model here — any provider, any API
|
|
26
|
+
return { response: 'model output', inputTokens: 100, outputTokens: 50 };
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const judge = createAnthropicJudge({
|
|
30
|
+
model: 'claude-sonnet-4-6',
|
|
31
|
+
threshold: 0.7,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const result = await runEval({
|
|
35
|
+
testCases: [{ name: 'greeting', input: 'Say hello', rubric: 'Response is a friendly greeting' }],
|
|
36
|
+
respond,
|
|
37
|
+
judge,
|
|
38
|
+
concurrency: 3,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
console.log(result.passRate); // 0.0–1.0
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Comparing runs against a baseline
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { compareRuns, loadBaseline, saveBaseline } from '@scalvert/eval-core';
|
|
48
|
+
|
|
49
|
+
// Save a run as the baseline
|
|
50
|
+
await saveBaseline(result, 'baseline.json');
|
|
51
|
+
|
|
52
|
+
// Later, compare a new run against it
|
|
53
|
+
const baseline = await loadBaseline('baseline.json');
|
|
54
|
+
if (baseline) {
|
|
55
|
+
const { passRateDelta, regressions, improvements } = compareRuns(newResult, baseline);
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Validating test case JSON
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { TestCaseSchema } from '@scalvert/eval-core';
|
|
63
|
+
import { z } from 'zod';
|
|
64
|
+
|
|
65
|
+
const testCases = z.array(TestCaseSchema).parse(JSON.parse(rawJson));
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Custom judges
|
|
69
|
+
|
|
70
|
+
`createAnthropicJudge` is a convenience — you can pass any function matching `JudgeFn`:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import type { JudgeFn } from '@scalvert/eval-core';
|
|
74
|
+
|
|
75
|
+
const myJudge: JudgeFn = async ({ input, response, rubric }) => {
|
|
76
|
+
// Your own scoring logic
|
|
77
|
+
return { passed: true, score: 0.95, reasoning: 'Looks good' };
|
|
78
|
+
};
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## API
|
|
82
|
+
|
|
83
|
+
### `runEval(options)`
|
|
84
|
+
|
|
85
|
+
Runs test cases with concurrency control, returning a `RunResult` with pass rate, per-case scores, and token usage.
|
|
86
|
+
|
|
87
|
+
### `createAnthropicJudge(config)`
|
|
88
|
+
|
|
89
|
+
Factory that returns a `JudgeFn` using the Anthropic messages API. Scores responses against a rubric and applies a threshold.
|
|
90
|
+
|
|
91
|
+
### `compareRuns(current, baseline)`
|
|
92
|
+
|
|
93
|
+
Returns `passRateDelta`, `regressions` (names that went from pass to fail), and `improvements` (fail to pass).
|
|
94
|
+
|
|
95
|
+
### `saveBaseline(result, filePath)` / `loadBaseline(filePath)`
|
|
96
|
+
|
|
97
|
+
Persist and load `RunResult` objects as JSON. `loadBaseline` returns `null` for missing files and throws on malformed data.
|
|
98
|
+
|
|
99
|
+
### `buildPassMap(result)`
|
|
100
|
+
|
|
101
|
+
Returns a `Map<string, boolean>` of test name to pass/fail status.
|
|
102
|
+
|
|
103
|
+
### `calculateCost(pricing, inputTokens, outputTokens)`
|
|
104
|
+
|
|
105
|
+
Calculates cost from token counts given a `Pricing` object (`{ inputPerMillion, outputPerMillion }`).
|
|
106
|
+
|
|
107
|
+
## License
|
|
108
|
+
|
|
109
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
interface TestCase {
|
|
4
|
+
name: string;
|
|
5
|
+
input: string;
|
|
6
|
+
rubric: string;
|
|
7
|
+
}
|
|
8
|
+
interface TestResult {
|
|
9
|
+
name: string;
|
|
10
|
+
passed: boolean;
|
|
11
|
+
score: number;
|
|
12
|
+
reasoning: string;
|
|
13
|
+
inputTokens: number;
|
|
14
|
+
outputTokens: number;
|
|
15
|
+
costUsd: number;
|
|
16
|
+
durationMs: number;
|
|
17
|
+
}
|
|
18
|
+
interface RunResult {
|
|
19
|
+
runId: string;
|
|
20
|
+
timestamp: string;
|
|
21
|
+
passRate: number;
|
|
22
|
+
results: TestResult[];
|
|
23
|
+
totalInputTokens: number;
|
|
24
|
+
totalOutputTokens: number;
|
|
25
|
+
totalCostUsd: number;
|
|
26
|
+
}
|
|
27
|
+
type JudgeFn = (options: {
|
|
28
|
+
input: string;
|
|
29
|
+
response: string;
|
|
30
|
+
rubric: string;
|
|
31
|
+
}) => Promise<{
|
|
32
|
+
passed: boolean;
|
|
33
|
+
score: number;
|
|
34
|
+
reasoning: string;
|
|
35
|
+
inputTokens?: number;
|
|
36
|
+
outputTokens?: number;
|
|
37
|
+
}>;
|
|
38
|
+
type ResponseFn = (input: string) => Promise<{
|
|
39
|
+
response: string;
|
|
40
|
+
inputTokens?: number;
|
|
41
|
+
outputTokens?: number;
|
|
42
|
+
}>;
|
|
43
|
+
declare const TestCaseSchema: z.ZodObject<{
|
|
44
|
+
name: z.ZodString;
|
|
45
|
+
input: z.ZodString;
|
|
46
|
+
rubric: z.ZodString;
|
|
47
|
+
}, "strip", z.ZodTypeAny, {
|
|
48
|
+
name: string;
|
|
49
|
+
input: string;
|
|
50
|
+
rubric: string;
|
|
51
|
+
}, {
|
|
52
|
+
name: string;
|
|
53
|
+
input: string;
|
|
54
|
+
rubric: string;
|
|
55
|
+
}>;
|
|
56
|
+
declare const TestResultSchema: z.ZodObject<{
|
|
57
|
+
name: z.ZodString;
|
|
58
|
+
passed: z.ZodBoolean;
|
|
59
|
+
score: z.ZodNumber;
|
|
60
|
+
reasoning: z.ZodString;
|
|
61
|
+
inputTokens: z.ZodNumber;
|
|
62
|
+
outputTokens: z.ZodNumber;
|
|
63
|
+
costUsd: z.ZodNumber;
|
|
64
|
+
durationMs: z.ZodNumber;
|
|
65
|
+
}, "strip", z.ZodTypeAny, {
|
|
66
|
+
name: string;
|
|
67
|
+
passed: boolean;
|
|
68
|
+
score: number;
|
|
69
|
+
reasoning: string;
|
|
70
|
+
inputTokens: number;
|
|
71
|
+
outputTokens: number;
|
|
72
|
+
costUsd: number;
|
|
73
|
+
durationMs: number;
|
|
74
|
+
}, {
|
|
75
|
+
name: string;
|
|
76
|
+
passed: boolean;
|
|
77
|
+
score: number;
|
|
78
|
+
reasoning: string;
|
|
79
|
+
inputTokens: number;
|
|
80
|
+
outputTokens: number;
|
|
81
|
+
costUsd: number;
|
|
82
|
+
durationMs: number;
|
|
83
|
+
}>;
|
|
84
|
+
declare const RunResultSchema: z.ZodObject<{
|
|
85
|
+
runId: z.ZodString;
|
|
86
|
+
timestamp: z.ZodString;
|
|
87
|
+
passRate: z.ZodNumber;
|
|
88
|
+
results: z.ZodArray<z.ZodObject<{
|
|
89
|
+
name: z.ZodString;
|
|
90
|
+
passed: z.ZodBoolean;
|
|
91
|
+
score: z.ZodNumber;
|
|
92
|
+
reasoning: z.ZodString;
|
|
93
|
+
inputTokens: z.ZodNumber;
|
|
94
|
+
outputTokens: z.ZodNumber;
|
|
95
|
+
costUsd: z.ZodNumber;
|
|
96
|
+
durationMs: z.ZodNumber;
|
|
97
|
+
}, "strip", z.ZodTypeAny, {
|
|
98
|
+
name: string;
|
|
99
|
+
passed: boolean;
|
|
100
|
+
score: number;
|
|
101
|
+
reasoning: string;
|
|
102
|
+
inputTokens: number;
|
|
103
|
+
outputTokens: number;
|
|
104
|
+
costUsd: number;
|
|
105
|
+
durationMs: number;
|
|
106
|
+
}, {
|
|
107
|
+
name: string;
|
|
108
|
+
passed: boolean;
|
|
109
|
+
score: number;
|
|
110
|
+
reasoning: string;
|
|
111
|
+
inputTokens: number;
|
|
112
|
+
outputTokens: number;
|
|
113
|
+
costUsd: number;
|
|
114
|
+
durationMs: number;
|
|
115
|
+
}>, "many">;
|
|
116
|
+
totalInputTokens: z.ZodNumber;
|
|
117
|
+
totalOutputTokens: z.ZodNumber;
|
|
118
|
+
totalCostUsd: z.ZodNumber;
|
|
119
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
120
|
+
runId: z.ZodString;
|
|
121
|
+
timestamp: z.ZodString;
|
|
122
|
+
passRate: z.ZodNumber;
|
|
123
|
+
results: z.ZodArray<z.ZodObject<{
|
|
124
|
+
name: z.ZodString;
|
|
125
|
+
passed: z.ZodBoolean;
|
|
126
|
+
score: z.ZodNumber;
|
|
127
|
+
reasoning: z.ZodString;
|
|
128
|
+
inputTokens: z.ZodNumber;
|
|
129
|
+
outputTokens: z.ZodNumber;
|
|
130
|
+
costUsd: z.ZodNumber;
|
|
131
|
+
durationMs: z.ZodNumber;
|
|
132
|
+
}, "strip", z.ZodTypeAny, {
|
|
133
|
+
name: string;
|
|
134
|
+
passed: boolean;
|
|
135
|
+
score: number;
|
|
136
|
+
reasoning: string;
|
|
137
|
+
inputTokens: number;
|
|
138
|
+
outputTokens: number;
|
|
139
|
+
costUsd: number;
|
|
140
|
+
durationMs: number;
|
|
141
|
+
}, {
|
|
142
|
+
name: string;
|
|
143
|
+
passed: boolean;
|
|
144
|
+
score: number;
|
|
145
|
+
reasoning: string;
|
|
146
|
+
inputTokens: number;
|
|
147
|
+
outputTokens: number;
|
|
148
|
+
costUsd: number;
|
|
149
|
+
durationMs: number;
|
|
150
|
+
}>, "many">;
|
|
151
|
+
totalInputTokens: z.ZodNumber;
|
|
152
|
+
totalOutputTokens: z.ZodNumber;
|
|
153
|
+
totalCostUsd: z.ZodNumber;
|
|
154
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
155
|
+
runId: z.ZodString;
|
|
156
|
+
timestamp: z.ZodString;
|
|
157
|
+
passRate: z.ZodNumber;
|
|
158
|
+
results: z.ZodArray<z.ZodObject<{
|
|
159
|
+
name: z.ZodString;
|
|
160
|
+
passed: z.ZodBoolean;
|
|
161
|
+
score: z.ZodNumber;
|
|
162
|
+
reasoning: z.ZodString;
|
|
163
|
+
inputTokens: z.ZodNumber;
|
|
164
|
+
outputTokens: z.ZodNumber;
|
|
165
|
+
costUsd: z.ZodNumber;
|
|
166
|
+
durationMs: z.ZodNumber;
|
|
167
|
+
}, "strip", z.ZodTypeAny, {
|
|
168
|
+
name: string;
|
|
169
|
+
passed: boolean;
|
|
170
|
+
score: number;
|
|
171
|
+
reasoning: string;
|
|
172
|
+
inputTokens: number;
|
|
173
|
+
outputTokens: number;
|
|
174
|
+
costUsd: number;
|
|
175
|
+
durationMs: number;
|
|
176
|
+
}, {
|
|
177
|
+
name: string;
|
|
178
|
+
passed: boolean;
|
|
179
|
+
score: number;
|
|
180
|
+
reasoning: string;
|
|
181
|
+
inputTokens: number;
|
|
182
|
+
outputTokens: number;
|
|
183
|
+
costUsd: number;
|
|
184
|
+
durationMs: number;
|
|
185
|
+
}>, "many">;
|
|
186
|
+
totalInputTokens: z.ZodNumber;
|
|
187
|
+
totalOutputTokens: z.ZodNumber;
|
|
188
|
+
totalCostUsd: z.ZodNumber;
|
|
189
|
+
}, z.ZodTypeAny, "passthrough">>;
|
|
190
|
+
|
|
191
|
+
interface Pricing {
|
|
192
|
+
inputPerMillion: number;
|
|
193
|
+
outputPerMillion: number;
|
|
194
|
+
}
|
|
195
|
+
declare function calculateCost(pricing: Pricing, inputTokens: number, outputTokens: number): number;
|
|
196
|
+
|
|
197
|
+
interface RunEvalOptions {
|
|
198
|
+
testCases: TestCase[];
|
|
199
|
+
respond: ResponseFn;
|
|
200
|
+
judge: JudgeFn;
|
|
201
|
+
concurrency?: number;
|
|
202
|
+
pricing?: Pricing;
|
|
203
|
+
}
|
|
204
|
+
declare function runEval(options: RunEvalOptions): Promise<RunResult>;
|
|
205
|
+
|
|
206
|
+
interface AnthropicJudgeConfig {
|
|
207
|
+
apiKey?: string;
|
|
208
|
+
model: string;
|
|
209
|
+
threshold: number;
|
|
210
|
+
}
|
|
211
|
+
declare function createAnthropicJudge(config: AnthropicJudgeConfig): JudgeFn;
|
|
212
|
+
|
|
213
|
+
interface CompareResult {
|
|
214
|
+
passRateDelta: number;
|
|
215
|
+
regressions: string[];
|
|
216
|
+
improvements: string[];
|
|
217
|
+
}
|
|
218
|
+
declare function compareRuns(current: RunResult, baseline: RunResult): CompareResult;
|
|
219
|
+
|
|
220
|
+
declare function saveBaseline(result: RunResult, filePath: string): Promise<void>;
|
|
221
|
+
declare function loadBaseline(filePath: string): Promise<RunResult | null>;
|
|
222
|
+
declare function buildPassMap(result: RunResult): Map<string, boolean>;
|
|
223
|
+
|
|
224
|
+
export { type AnthropicJudgeConfig, type CompareResult, type JudgeFn, type Pricing, type ResponseFn, type RunEvalOptions, type RunResult, RunResultSchema, type TestCase, TestCaseSchema, type TestResult, TestResultSchema, buildPassMap, calculateCost, compareRuns, createAnthropicJudge, loadBaseline, runEval, saveBaseline };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
var TestCaseSchema = z.object({
|
|
4
|
+
name: z.string().min(1),
|
|
5
|
+
input: z.string(),
|
|
6
|
+
rubric: z.string()
|
|
7
|
+
});
|
|
8
|
+
var TestResultSchema = z.object({
|
|
9
|
+
name: z.string(),
|
|
10
|
+
passed: z.boolean(),
|
|
11
|
+
score: z.number().min(0).max(1),
|
|
12
|
+
reasoning: z.string(),
|
|
13
|
+
inputTokens: z.number(),
|
|
14
|
+
outputTokens: z.number(),
|
|
15
|
+
costUsd: z.number(),
|
|
16
|
+
durationMs: z.number()
|
|
17
|
+
});
|
|
18
|
+
var RunResultSchema = z.object({
|
|
19
|
+
runId: z.string(),
|
|
20
|
+
timestamp: z.string(),
|
|
21
|
+
passRate: z.number().min(0).max(1),
|
|
22
|
+
results: z.array(TestResultSchema),
|
|
23
|
+
totalInputTokens: z.number(),
|
|
24
|
+
totalOutputTokens: z.number(),
|
|
25
|
+
totalCostUsd: z.number()
|
|
26
|
+
}).passthrough();
|
|
27
|
+
|
|
28
|
+
// src/cost.ts
|
|
29
|
+
function calculateCost(pricing, inputTokens, outputTokens) {
|
|
30
|
+
return inputTokens / 1e6 * pricing.inputPerMillion + outputTokens / 1e6 * pricing.outputPerMillion;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/runner.ts
|
|
34
|
+
async function runEval(options) {
|
|
35
|
+
const { testCases, respond, judge, concurrency = 3, pricing } = options;
|
|
36
|
+
const runId = `run-${Date.now()}`;
|
|
37
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
38
|
+
const results = [];
|
|
39
|
+
const queue = [...testCases];
|
|
40
|
+
const running = /* @__PURE__ */ new Set();
|
|
41
|
+
async function processCase(testCase) {
|
|
42
|
+
const start = performance.now();
|
|
43
|
+
const respondResult = await respond(testCase.input);
|
|
44
|
+
const judgeResult = await judge({
|
|
45
|
+
input: testCase.input,
|
|
46
|
+
response: respondResult.response,
|
|
47
|
+
rubric: testCase.rubric
|
|
48
|
+
});
|
|
49
|
+
const durationMs = Math.round(performance.now() - start);
|
|
50
|
+
const inputTokens = (respondResult.inputTokens ?? 0) + (judgeResult.inputTokens ?? 0);
|
|
51
|
+
const outputTokens = (respondResult.outputTokens ?? 0) + (judgeResult.outputTokens ?? 0);
|
|
52
|
+
const costUsd = pricing ? calculateCost(pricing, inputTokens, outputTokens) : 0;
|
|
53
|
+
results.push({
|
|
54
|
+
name: testCase.name,
|
|
55
|
+
passed: judgeResult.passed,
|
|
56
|
+
score: judgeResult.score,
|
|
57
|
+
reasoning: judgeResult.reasoning,
|
|
58
|
+
inputTokens,
|
|
59
|
+
outputTokens,
|
|
60
|
+
costUsd,
|
|
61
|
+
durationMs
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
for (const testCase of queue) {
|
|
65
|
+
if (running.size >= concurrency) {
|
|
66
|
+
await Promise.race(running);
|
|
67
|
+
}
|
|
68
|
+
const promise = processCase(testCase).then(() => {
|
|
69
|
+
running.delete(promise);
|
|
70
|
+
});
|
|
71
|
+
running.add(promise);
|
|
72
|
+
}
|
|
73
|
+
await Promise.all(running);
|
|
74
|
+
const passedCount = results.filter((r) => r.passed).length;
|
|
75
|
+
const totalInputTokens = results.reduce((sum, r) => sum + r.inputTokens, 0);
|
|
76
|
+
const totalOutputTokens = results.reduce((sum, r) => sum + r.outputTokens, 0);
|
|
77
|
+
const totalCostUsd = results.reduce((sum, r) => sum + r.costUsd, 0);
|
|
78
|
+
return {
|
|
79
|
+
runId,
|
|
80
|
+
timestamp,
|
|
81
|
+
passRate: testCases.length > 0 ? passedCount / testCases.length : 0,
|
|
82
|
+
results,
|
|
83
|
+
totalInputTokens,
|
|
84
|
+
totalOutputTokens,
|
|
85
|
+
totalCostUsd
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/judge.ts
|
|
90
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
91
|
+
import { z as z2 } from "zod";
|
|
92
|
+
var JudgeResponseSchema = z2.object({
|
|
93
|
+
pass: z2.boolean(),
|
|
94
|
+
score: z2.number().min(0).max(1),
|
|
95
|
+
reasoning: z2.string()
|
|
96
|
+
});
|
|
97
|
+
function createAnthropicJudge(config) {
|
|
98
|
+
const client = new Anthropic({ apiKey: config.apiKey });
|
|
99
|
+
return async ({ input, response, rubric }) => {
|
|
100
|
+
const message = await client.messages.create({
|
|
101
|
+
model: config.model,
|
|
102
|
+
max_tokens: 1024,
|
|
103
|
+
system: 'You are an objective evaluator. Score whether a model response satisfies a rubric. Respond ONLY with JSON: { "pass": boolean, "score": number (0.0\u20131.0), "reasoning": string }',
|
|
104
|
+
messages: [
|
|
105
|
+
{
|
|
106
|
+
role: "user",
|
|
107
|
+
content: `Rubric: ${rubric}
|
|
108
|
+
|
|
109
|
+
User message: ${input}
|
|
110
|
+
|
|
111
|
+
Response to evaluate:
|
|
112
|
+
${response}
|
|
113
|
+
|
|
114
|
+
Does this response satisfy the rubric?`
|
|
115
|
+
}
|
|
116
|
+
]
|
|
117
|
+
});
|
|
118
|
+
const text = message.content[0].type === "text" ? message.content[0].text : "";
|
|
119
|
+
const parsed = parseJudgeResponse(text);
|
|
120
|
+
return {
|
|
121
|
+
passed: parsed.score >= config.threshold,
|
|
122
|
+
score: parsed.score,
|
|
123
|
+
reasoning: parsed.reasoning,
|
|
124
|
+
inputTokens: message.usage.input_tokens,
|
|
125
|
+
outputTokens: message.usage.output_tokens
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function parseJudgeResponse(text) {
|
|
130
|
+
let jsonText = text.trim();
|
|
131
|
+
if (jsonText.startsWith("```json")) {
|
|
132
|
+
jsonText = jsonText.slice(7);
|
|
133
|
+
}
|
|
134
|
+
if (jsonText.startsWith("```")) {
|
|
135
|
+
jsonText = jsonText.slice(3);
|
|
136
|
+
}
|
|
137
|
+
if (jsonText.endsWith("```")) {
|
|
138
|
+
jsonText = jsonText.slice(0, -3);
|
|
139
|
+
}
|
|
140
|
+
jsonText = jsonText.trim();
|
|
141
|
+
let parsed;
|
|
142
|
+
try {
|
|
143
|
+
parsed = JSON.parse(jsonText);
|
|
144
|
+
} catch {
|
|
145
|
+
const jsonMatch = jsonText.match(/\{[\s\S]*"pass"[\s\S]*\}/);
|
|
146
|
+
if (jsonMatch) {
|
|
147
|
+
parsed = JSON.parse(jsonMatch[0]);
|
|
148
|
+
} else {
|
|
149
|
+
throw new Error(`Failed to parse judge response as JSON: ${text}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const result = JudgeResponseSchema.safeParse(parsed);
|
|
153
|
+
if (!result.success) {
|
|
154
|
+
throw new Error(
|
|
155
|
+
`Judge returned invalid response. Expected {pass, score, reasoning} but got: ${jsonText.slice(0, 500)}
|
|
156
|
+
Validation errors: ${JSON.stringify(result.error.issues)}`
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
return result.data;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/compare.ts
|
|
163
|
+
function compareRuns(current, baseline) {
|
|
164
|
+
const baselineMap = /* @__PURE__ */ new Map();
|
|
165
|
+
for (const r of baseline.results) {
|
|
166
|
+
baselineMap.set(r.name, r.passed);
|
|
167
|
+
}
|
|
168
|
+
const currentMap = /* @__PURE__ */ new Map();
|
|
169
|
+
for (const r of current.results) {
|
|
170
|
+
currentMap.set(r.name, r.passed);
|
|
171
|
+
}
|
|
172
|
+
const regressions = [];
|
|
173
|
+
const improvements = [];
|
|
174
|
+
for (const [name, currentPassed] of currentMap) {
|
|
175
|
+
const baselinePassed = baselineMap.get(name);
|
|
176
|
+
if (baselinePassed === void 0) continue;
|
|
177
|
+
if (baselinePassed && !currentPassed) regressions.push(name);
|
|
178
|
+
if (!baselinePassed && currentPassed) improvements.push(name);
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
passRateDelta: current.passRate - baseline.passRate,
|
|
182
|
+
regressions,
|
|
183
|
+
improvements
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// src/baseline.ts
|
|
188
|
+
import { readFile, writeFile, mkdir } from "fs/promises";
|
|
189
|
+
import { dirname } from "path";
|
|
190
|
+
async function saveBaseline(result, filePath) {
|
|
191
|
+
await mkdir(dirname(filePath), { recursive: true });
|
|
192
|
+
await writeFile(filePath, JSON.stringify(result, null, 2), "utf8");
|
|
193
|
+
}
|
|
194
|
+
async function loadBaseline(filePath) {
|
|
195
|
+
let raw;
|
|
196
|
+
try {
|
|
197
|
+
raw = await readFile(filePath, "utf8");
|
|
198
|
+
} catch (error) {
|
|
199
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
const parsed = JSON.parse(raw);
|
|
205
|
+
const result = RunResultSchema.safeParse(parsed);
|
|
206
|
+
if (!result.success) {
|
|
207
|
+
throw new Error(
|
|
208
|
+
`Malformed baseline file: ${filePath}
|
|
209
|
+
Validation errors: ${JSON.stringify(result.error.issues)}`
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
return result.data;
|
|
213
|
+
}
|
|
214
|
+
function buildPassMap(result) {
|
|
215
|
+
const map = /* @__PURE__ */ new Map();
|
|
216
|
+
for (const r of result.results) {
|
|
217
|
+
map.set(r.name, r.passed);
|
|
218
|
+
}
|
|
219
|
+
return map;
|
|
220
|
+
}
|
|
221
|
+
export {
|
|
222
|
+
RunResultSchema,
|
|
223
|
+
TestCaseSchema,
|
|
224
|
+
TestResultSchema,
|
|
225
|
+
buildPassMap,
|
|
226
|
+
calculateCost,
|
|
227
|
+
compareRuns,
|
|
228
|
+
createAnthropicJudge,
|
|
229
|
+
loadBaseline,
|
|
230
|
+
runEval,
|
|
231
|
+
saveBaseline
|
|
232
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@scalvert/eval-core",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "General-purpose LLM evaluation primitives",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"llm",
|
|
7
|
+
"evaluation",
|
|
8
|
+
"testing",
|
|
9
|
+
"judge",
|
|
10
|
+
"baseline"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/scalvert/eval-core#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/scalvert/eval-core/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/scalvert/eval-core.git"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Steve Calvert <steve.calvert@gmail.com>",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"main": "./dist/index.js",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
36
|
+
"format": "prettier --write .",
|
|
37
|
+
"format:check": "prettier --check .",
|
|
38
|
+
"lint": "eslint . && npm run format:check",
|
|
39
|
+
"prepublishOnly": "npm run build",
|
|
40
|
+
"test": "npm run lint && npm run test:vitest",
|
|
41
|
+
"test:vitest": "vitest run",
|
|
42
|
+
"test:watch": "vitest",
|
|
43
|
+
"typecheck": "tsc --noEmit"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@anthropic-ai/sdk": "^0.39.0",
|
|
47
|
+
"zod": "^3.22.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@eslint/js": "^9.24.0",
|
|
51
|
+
"@release-it-plugins/lerna-changelog": "^5.0.0",
|
|
52
|
+
"@types/node": "^22.0.0",
|
|
53
|
+
"@typescript-eslint/eslint-plugin": "^8.29.1",
|
|
54
|
+
"@typescript-eslint/parser": "^8.29.1",
|
|
55
|
+
"eslint": "^9.24.0",
|
|
56
|
+
"eslint-config-prettier": "^10.1.2",
|
|
57
|
+
"eslint-plugin-jsdoc": "^61.5.0",
|
|
58
|
+
"eslint-plugin-prettier": "^5.2.6",
|
|
59
|
+
"eslint-plugin-unicorn": "^62.0.0",
|
|
60
|
+
"prettier": "^3.5.3",
|
|
61
|
+
"release-it": "^15.5.0",
|
|
62
|
+
"tsup": "^8.0.0",
|
|
63
|
+
"typescript": "^5.4.0",
|
|
64
|
+
"vite": "^7.3.0",
|
|
65
|
+
"vitest": "^4.0.16"
|
|
66
|
+
},
|
|
67
|
+
"engines": {
|
|
68
|
+
"node": ">=22"
|
|
69
|
+
},
|
|
70
|
+
"publishConfig": {
|
|
71
|
+
"access": "public",
|
|
72
|
+
"registry": "https://registry.npmjs.org"
|
|
73
|
+
},
|
|
74
|
+
"release-it": {
|
|
75
|
+
"plugins": {
|
|
76
|
+
"@release-it-plugins/lerna-changelog": {
|
|
77
|
+
"infile": "CHANGELOG.md",
|
|
78
|
+
"launchEditor": true
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
"git": {
|
|
82
|
+
"tagName": "v${version}"
|
|
83
|
+
},
|
|
84
|
+
"github": {
|
|
85
|
+
"release": true,
|
|
86
|
+
"tokenRef": "GITHUB_AUTH"
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|