Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/scorers/embedding-similarity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Scorer, ScoreContext, ScorerSpec } from "../types.js";
import { localEmbedding } from "../providers/mock.js";
import { result } from "./util.js";
import { parseThreshold, result } from "./util.js";

/** Cosine similarity between two equal-length vectors. */
export function cosineSimilarity(a: number[], b: number[]): number {
Expand Down Expand Up @@ -38,7 +38,14 @@ export const embeddingSimilarityScorer: Scorer = {
if (expected === undefined) {
return result(spec, { score: 0, passed: false, reason: "no expected reference provided" });
}
const threshold = typeof spec.threshold === "number" ? spec.threshold : 0.8;
const threshold = parseThreshold(spec, 0.8);
if (threshold === null) {
return result(spec, {
score: 0,
passed: false,
reason: "invalid threshold (must be a finite number in [0, 1])",
});
}

const embed = ctx.provider.embed
? (t: string) => ctx.provider.embed!(t)
Expand Down
7 changes: 7 additions & 0 deletions src/scorers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ export function clamp01(n: number): number {
return Math.min(1, Math.max(0, n));
}

/** Parse a pass threshold in [0, 1], or null when missing/invalid. */
export function parseThreshold(spec: ScorerSpec, defaultVal: number): number | null {
const t = typeof spec.threshold === "number" ? spec.threshold : defaultVal;
if (!Number.isFinite(t) || t < 0 || t > 1) return null;
return t;
}

/** Read the weight from a spec, defaulting to 1 and rejecting negatives. */
export function specWeight(spec: ScorerSpec): number {
const w = typeof spec.weight === "number" ? spec.weight : 1;
Expand Down
27 changes: 27 additions & 0 deletions tests/scorers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,33 @@ describe("embedding-similarity", () => {
it("cosineSimilarity of orthogonal vectors is 0", () => {
expect(cosineSimilarity([1, 0], [0, 1])).toBe(0);
});
it("rejects NaN threshold", async () => {
const r = await run(
embeddingSimilarityScorer,
{ type: "embedding-similarity", expected: "the quick brown fox", threshold: NaN },
ctx("the quick brown fox"),
);
expect(r.passed).toBe(false);
expect(r.reason).toMatch(/invalid threshold/i);
});
it("rejects negative threshold", async () => {
const r = await run(
embeddingSimilarityScorer,
{ type: "embedding-similarity", expected: "x", threshold: -5 },
ctx("totally unrelated garbage"),
);
expect(r.passed).toBe(false);
expect(r.reason).toMatch(/invalid threshold/i);
});
it("rejects threshold above 1", async () => {
const r = await run(
embeddingSimilarityScorer,
{ type: "embedding-similarity", expected: "the quick brown fox", threshold: 2 },
ctx("the quick brown fox"),
);
expect(r.passed).toBe(false);
expect(r.reason).toMatch(/invalid threshold/i);
});
});

describe("llm-judge (mock)", () => {
Expand Down
Loading