import { Harness } from '../harness.mjs'; import { JudgeHarness } from './judgeHarness.mjs'; import { Judge, JudgeContext } from './types.mjs'; import { JsonValue } from '@vitest-evals/core'; /** * Rubric choice returned by a factuality judge model call. * * @example * ```ts * import type { FactualityJudgeChoice } from "vitest-evals"; * * const choice: FactualityJudgeChoice = "C"; * ``` */ type FactualityJudgeChoice = "A" | "B" | "C" | "D" | "E"; /** * Prompt payload sent to the configured judge harness. * * @example * ```ts * import type { FactualityJudgePrompt } from "vitest-evals"; * * const payload: FactualityJudgePrompt = { * system: "Grade factual consistency.", * prompt: "Compare these answers.", * }; * ``` */ type FactualityJudgePrompt = { /** System prompt for the judge model. */ system: string; /** User prompt containing the question, expert answer, submitted answer, and rubric. */ prompt: string; }; /** * Parsed verdict returned by a factuality judge model call. * * @example * ```ts * import type { FactualityJudgeVerdict } from "vitest-evals"; * * const verdict: FactualityJudgeVerdict = { * choice: "C", * rationale: "The submitted answer matches the expert answer.", * }; * ``` */ type FactualityJudgeVerdict = { /** Rubric choice selected by the judge model. */ choice: FactualityJudgeChoice; /** Human-readable explanation for the selected choice. */ rationale: string; }; /** * Expert answer or reference facts accepted by `FactualityJudge()`. * * @example * ```ts * import type { FactualityJudgeExpected } from "vitest-evals"; * * const expected: FactualityJudgeExpected = * "Paris is the capital of France."; * ``` */ type FactualityJudgeExpected = JsonValue; /** * Configuration for the factuality judge. * * The judge harness can be supplied here, by `describeEval({ judgeHarness })`, * or by `expect(...).toSatisfyJudge(..., { judgeHarness })`. Passing it here * keeps the judge self-contained while preserving provider neutrality. * * @example * ```ts * import { FactualityJudge, type JudgeHarness } from "vitest-evals"; * * declare const judgeHarness: JudgeHarness; * * const judge = FactualityJudge({ name: "FactJudge", judgeHarness }); * ``` */ type FactualityJudgeConfig = { /** Stable judge name used in assertion messages and reports. */ name?: string; /** Default judge-side harness used when matcher options do not provide one. */ judgeHarness?: JudgeHarness; /** Expert answer or reference facts used by this judge instance. */ expected?: FactualityJudgeExpected; }; /** * Matcher context accepted by `FactualityJudge()`. * * @example * ```ts * import { aiSdkJudgeHarness } from "@vitest-evals/harness-ai-sdk"; * import { openai } from "@ai-sdk/openai"; * import { expect } from "vitest"; * import { FactualityJudge } from "vitest-evals"; * * const judgeHarness = aiSdkJudgeHarness({ * model: openai("gpt-4.1-mini"), * }); * * await expect(result).toSatisfyJudge(FactualityJudge(), { * expected: "Paris is the capital of France.", * judgeHarness, * }); * ``` */ type FactualityJudgeOptions | undefined = any> = JudgeContext & { /** Expert answer or reference facts. Overrides the judge config default. */ expected?: FactualityJudgeExpected; }; /** * Creates a factuality judge over normalized harness output. * * `FactualityJudge()` compares `input`, `output`, and an expert answer. Bind a * suite-wide expert answer on the judge config, or pass a case-specific * `expected` value to `toSatisfyJudge(...)`. Configure the LLM used for grading * with `judgeHarness` on the judge, suite, or matcher options. * * @param config - Optional judge name and reusable judge harness default. * * @example * ```ts * import { anthropic } from "@ai-sdk/anthropic"; * import { aiSdkJudgeHarness } from "@vitest-evals/harness-ai-sdk"; * import { describeEval, FactualityJudge } from "vitest-evals"; * import { qaHarness } from "./qaHarness"; * * const judgeHarness = aiSdkJudgeHarness({ * model: anthropic("claude-sonnet-4-5"), * temperature: 0, * }); * const factualityJudge = FactualityJudge({ * judgeHarness, * expected: "Paris is the capital of France.", * }); * * describeEval("qa agent", { * harness: qaHarness, * judges: [factualityJudge], * }, (it) => { * it("answers a geography question", async ({ run }) => { * await run("What is the capital of France?"); * }); * }); * ``` */ declare function FactualityJudge(config?: FactualityJudgeConfig): Judge; export { FactualityJudge, type FactualityJudgeChoice, type FactualityJudgeConfig, type FactualityJudgeExpected, type FactualityJudgeOptions, type FactualityJudgePrompt, type FactualityJudgeVerdict };