@mastra/evals 1.5.1 → 1.6.0-alpha.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.
Files changed (38) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/dist/docs/SKILL.md +2 -1
  3. package/dist/docs/assets/SOURCE_MAP.json +1 -1
  4. package/dist/docs/references/docs-evals-built-in-scorers.md +4 -2
  5. package/dist/docs/references/docs-evals-overview.md +8 -1
  6. package/dist/docs/references/docs-evals-quick-checks.md +6 -5
  7. package/dist/docs/references/reference-evals-answer-relevancy.md +2 -0
  8. package/dist/docs/references/reference-evals-answer-similarity.md +2 -0
  9. package/dist/docs/references/reference-evals-bias.md +2 -0
  10. package/dist/docs/references/reference-evals-checks.md +6 -4
  11. package/dist/docs/references/reference-evals-completeness.md +2 -0
  12. package/dist/docs/references/reference-evals-content-similarity.md +2 -0
  13. package/dist/docs/references/reference-evals-context-precision.md +2 -0
  14. package/dist/docs/references/reference-evals-context-recall.md +205 -0
  15. package/dist/docs/references/reference-evals-context-relevance.md +2 -0
  16. package/dist/docs/references/reference-evals-faithfulness.md +2 -0
  17. package/dist/docs/references/reference-evals-hallucination.md +3 -0
  18. package/dist/docs/references/reference-evals-keyword-coverage.md +2 -0
  19. package/dist/docs/references/reference-evals-noise-sensitivity.md +2 -0
  20. package/dist/docs/references/reference-evals-prompt-alignment.md +3 -0
  21. package/dist/docs/references/reference-evals-rubric.md +3 -1
  22. package/dist/docs/references/reference-evals-scorer-utils.md +2 -0
  23. package/dist/docs/references/reference-evals-textual-difference.md +2 -0
  24. package/dist/docs/references/reference-evals-tone-consistency.md +2 -0
  25. package/dist/docs/references/reference-evals-tool-call-accuracy.md +2 -0
  26. package/dist/docs/references/reference-evals-toxicity.md +2 -0
  27. package/dist/docs/references/reference-evals-trajectory-accuracy.md +3 -1
  28. package/dist/scorers/llm/context-recall/index.d.ts +20 -0
  29. package/dist/scorers/llm/context-recall/index.d.ts.map +1 -0
  30. package/dist/scorers/llm/context-recall/prompts.d.ts +19 -0
  31. package/dist/scorers/llm/context-recall/prompts.d.ts.map +1 -0
  32. package/dist/scorers/llm/index.d.ts +1 -0
  33. package/dist/scorers/llm/index.d.ts.map +1 -1
  34. package/dist/scorers/prebuilt/index.cjs +261 -0
  35. package/dist/scorers/prebuilt/index.cjs.map +1 -1
  36. package/dist/scorers/prebuilt/index.js +261 -1
  37. package/dist/scorers/prebuilt/index.js.map +1 -1
  38. package/package.json +13 -13
@@ -2306,6 +2306,266 @@ function createContextPrecisionScorer({
2306
2306
  });
2307
2307
  }
2308
2308
 
2309
+ // src/scorers/llm/context-recall/prompts.ts
2310
+ var CONTEXT_RECALL_AGENT_INSTRUCTIONS = `You are a precise context recall evaluator. Your job is to determine if the retrieved context contains enough information to support all the claims in a ground-truth reference answer.
2311
+
2312
+ Key Principles:
2313
+ 1. First extract all atomic claims/sentences from the ground-truth answer
2314
+ 2. Then verify each claim against the provided retrieval context
2315
+ 3. A claim is attributable if any part of the retrieval context directly supports it
2316
+ 4. A claim is not attributable if no part of the retrieval context mentions or implies it
2317
+ 5. Focus on information coverage, not exact wording
2318
+ 6. Be strict in attribution - the context must actually contain supporting information
2319
+ 7. Never use prior knowledge in judgments - only use the provided context`;
2320
+ function createClaimExtractionPrompt({ groundTruth }) {
2321
+ return `Extract all atomic claims from the given ground-truth answer. A claim is any single statement that asserts one piece of information.
2322
+
2323
+ Guidelines for claim extraction:
2324
+ - Break down compound statements into individual claims
2325
+ - Each claim should be self-contained and verifiable independently
2326
+ - Include all factual assertions, including numbers, dates, and quantities
2327
+ - Keep relationships between entities intact within each claim
2328
+ - Exclude questions, commands, and purely stylistic text
2329
+ - Preserve the original meaning without adding interpretation
2330
+
2331
+ Example:
2332
+ Text: "Albert Einstein was born on 14 March 1879 in Ulm, Germany. He developed the theory of relativity and won the Nobel Prize in Physics in 1921."
2333
+
2334
+ {
2335
+ "claims": [
2336
+ "Albert Einstein was born on 14 March 1879",
2337
+ "Albert Einstein was born in Ulm, Germany",
2338
+ "Albert Einstein developed the theory of relativity",
2339
+ "Albert Einstein won the Nobel Prize in Physics",
2340
+ "Albert Einstein won the Nobel Prize in 1921"
2341
+ ]
2342
+ }
2343
+
2344
+ Please return only JSON format with "claims" array.
2345
+ Return empty list for empty input.
2346
+
2347
+ Ground-Truth Answer:
2348
+ ${groundTruth}
2349
+
2350
+ JSON:
2351
+ `;
2352
+ }
2353
+ function createClaimAttributionPrompt({ claims, context }) {
2354
+ return `Determine if each claim from the ground-truth answer can be attributed to the provided retrieval context. A claim is attributable if any part of the context directly supports or contains the information in that claim.
2355
+
2356
+ Retrieval Context:
2357
+ ${context.map((ctx, index) => `[${index}] ${ctx}`).join("\n")}
2358
+
2359
+ Number of claims: ${claims.length}
2360
+
2361
+ Claims to verify:
2362
+ ${claims.map((claim, index) => `[${index}] ${claim}`).join("\n")}
2363
+
2364
+ For each claim, provide a verdict and reasoning. The verdict must be one of:
2365
+ - "yes" if the claim can be attributed to information in the retrieval context
2366
+ - "no" if the claim cannot be attributed to any part of the retrieval context
2367
+
2368
+ The number of verdicts MUST MATCH the number of claims exactly.
2369
+
2370
+ Format:
2371
+ {
2372
+ "verdicts": [
2373
+ {
2374
+ "verdict": "yes/no",
2375
+ "reason": "explanation of why this claim is or isn't attributable to the context"
2376
+ }
2377
+ ]
2378
+ }
2379
+
2380
+ Rules:
2381
+ - Only use information from the provided retrieval context
2382
+ - Mark claims as "yes" if the context contains supporting information, even if the wording differs
2383
+ - Mark claims as "no" if the context does not mention or imply the information
2384
+ - Never use prior knowledge in your judgment
2385
+ - Provide clear reasoning that references specific context pieces
2386
+
2387
+ Example:
2388
+ Context:
2389
+ [0] "Albert Einstein was born on 14 March 1879 in Ulm, Germany."
2390
+ [1] "Einstein published his theory of special relativity in 1905."
2391
+
2392
+ Claims:
2393
+ [0] "Albert Einstein was born on 14 March 1879"
2394
+ [1] "Albert Einstein won the Nobel Prize in 1921"
2395
+
2396
+ {
2397
+ "verdicts": [
2398
+ {
2399
+ "verdict": "yes",
2400
+ "reason": "Context node [0] explicitly states Einstein was born on 14 March 1879"
2401
+ },
2402
+ {
2403
+ "verdict": "no",
2404
+ "reason": "No part of the retrieval context mentions Einstein winning the Nobel Prize"
2405
+ }
2406
+ ]
2407
+ }`;
2408
+ }
2409
+ function createContextRecallReasonPrompt({
2410
+ groundTruth,
2411
+ context,
2412
+ score,
2413
+ scale,
2414
+ verdicts
2415
+ }) {
2416
+ return `Explain the context recall score for the retrieval context based on how well it covers the claims in the ground-truth answer.
2417
+
2418
+ Ground-Truth Answer:
2419
+ ${groundTruth}
2420
+
2421
+ Retrieval Context:
2422
+ ${context.map((ctx, index) => `[${index}] ${ctx}`).join("\n")}
2423
+
2424
+ Score: ${score} out of ${scale}
2425
+ Verdicts:
2426
+ ${JSON.stringify(verdicts, null, 2)}
2427
+
2428
+ Context Recall measures what fraction of the ground-truth answer's claims are supported by the retrieved context. The score is calculated as:
2429
+ - Extract all claims from the ground-truth answer
2430
+ - Check each claim against the retrieval context
2431
+ - Score = (attributed claims / total claims) \xD7 scale
2432
+
2433
+ Rules for explanation:
2434
+ - Explain which claims were and were not found in the context
2435
+ - Mention specific context pieces that supported attributed claims
2436
+ - Highlight what information was missing for unattributed claims
2437
+ - Keep explanation concise and focused on coverage gaps
2438
+ - Use the given score, don't recalculate
2439
+
2440
+ Format:
2441
+ "The score is ${score} because {explanation of context recall}"
2442
+
2443
+ Example responses:
2444
+ "The score is 0.67 because 2 out of 3 ground-truth claims are supported by the retrieval context. The claims about Einstein's birthdate and birthplace are covered by context node [0], but the claim about the Nobel Prize is not mentioned in any context node."
2445
+ "The score is 1.0 because all claims in the ground-truth answer are fully supported by the retrieval context."
2446
+ "The score is 0.0 because none of the ground-truth claims could be attributed to the retrieval context."`;
2447
+ }
2448
+
2449
+ // src/scorers/llm/context-recall/index.ts
2450
+ var claimExtractionOutputSchema = {
2451
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2452
+ "type": "object",
2453
+ "properties": {
2454
+ "claims": {
2455
+ "type": "array",
2456
+ "items": {
2457
+ "type": "string"
2458
+ }
2459
+ }
2460
+ },
2461
+ "required": [
2462
+ "claims"
2463
+ ]
2464
+ };
2465
+ var claimAttributionOutputSchema = {
2466
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2467
+ "type": "object",
2468
+ "properties": {
2469
+ "verdicts": {
2470
+ "type": "array",
2471
+ "items": {
2472
+ "type": "object",
2473
+ "properties": {
2474
+ "verdict": {
2475
+ "type": "string"
2476
+ },
2477
+ "reason": {
2478
+ "type": "string"
2479
+ }
2480
+ },
2481
+ "required": [
2482
+ "verdict",
2483
+ "reason"
2484
+ ]
2485
+ }
2486
+ }
2487
+ },
2488
+ "required": [
2489
+ "verdicts"
2490
+ ]
2491
+ };
2492
+ var getContext3 = ({
2493
+ input,
2494
+ output,
2495
+ options
2496
+ }) => {
2497
+ if (options.contextExtractor && chunkIZLA36WC_cjs.isScorerRunInputForAgent(input) && chunkIZLA36WC_cjs.isScorerRunOutputForAgent(output)) {
2498
+ return options.contextExtractor(input, output);
2499
+ }
2500
+ return options.context ?? [];
2501
+ };
2502
+ function createContextRecallScorer({
2503
+ model,
2504
+ options
2505
+ }) {
2506
+ if (!options.context && !options.contextExtractor) {
2507
+ throw new Error("Either context or contextExtractor is required for Context Recall scoring");
2508
+ }
2509
+ if (options.context && options.context.length === 0) {
2510
+ throw new Error("Context array cannot be empty if provided");
2511
+ }
2512
+ return evals.createScorer({
2513
+ id: "context-recall-scorer",
2514
+ name: "Context Recall Scorer",
2515
+ description: "A scorer that evaluates how well retrieved context covers the claims in a ground-truth reference answer",
2516
+ judge: {
2517
+ model,
2518
+ instructions: CONTEXT_RECALL_AGENT_INSTRUCTIONS
2519
+ },
2520
+ type: "agent"
2521
+ }).preprocess({
2522
+ description: "Extract atomic claims from the ground-truth answer",
2523
+ outputSchema: claimExtractionOutputSchema,
2524
+ createPrompt: ({ run }) => {
2525
+ if (!run.groundTruth) {
2526
+ return createClaimExtractionPrompt({ groundTruth: "" });
2527
+ }
2528
+ const groundTruth = typeof run.groundTruth === "string" ? run.groundTruth : JSON.stringify(run.groundTruth);
2529
+ return createClaimExtractionPrompt({ groundTruth });
2530
+ }
2531
+ }).analyze({
2532
+ description: "Check if each ground-truth claim is attributable to the retrieval context",
2533
+ outputSchema: claimAttributionOutputSchema,
2534
+ createPrompt: ({ results, run }) => {
2535
+ const context = getContext3({ input: run.input, output: run.output, options });
2536
+ return createClaimAttributionPrompt({
2537
+ claims: results.preprocessStepResult?.claims || [],
2538
+ context
2539
+ });
2540
+ }
2541
+ }).generateScore(({ results, run }) => {
2542
+ if (!run.groundTruth) {
2543
+ return 0;
2544
+ }
2545
+ const totalClaims = results.preprocessStepResult?.claims?.length ?? 0;
2546
+ if (totalClaims === 0) {
2547
+ return 0;
2548
+ }
2549
+ const verdicts = results.analyzeStepResult?.verdicts || [];
2550
+ const attributedClaims = verdicts.filter((v) => v.verdict.toLowerCase().trim() === "yes").length;
2551
+ const score = Math.min(1, attributedClaims / totalClaims) * (options.scale || 1);
2552
+ return chunkIZLA36WC_cjs.roundToTwoDecimals(score);
2553
+ }).generateReason({
2554
+ description: "Explain the context recall evaluation results",
2555
+ createPrompt: ({ run, results, score }) => {
2556
+ const groundTruth = run.groundTruth ? typeof run.groundTruth === "string" ? run.groundTruth : JSON.stringify(run.groundTruth) : "";
2557
+ const context = getContext3({ input: run.input, output: run.output, options });
2558
+ return createContextRecallReasonPrompt({
2559
+ groundTruth,
2560
+ context,
2561
+ score,
2562
+ scale: options.scale || 1,
2563
+ verdicts: results.analyzeStepResult?.verdicts || []
2564
+ });
2565
+ }
2566
+ });
2567
+ }
2568
+
2309
2569
  // src/scorers/llm/noise-sensitivity/prompts.ts
2310
2570
  var NOISE_SENSITIVITY_INSTRUCTIONS = `You are an expert noise sensitivity evaluator. Your job is to analyze how much irrelevant, distracting, or misleading information (noise) affected the agent's response quality and accuracy.
2311
2571
 
@@ -4361,6 +4621,7 @@ exports.createBiasScorer = createBiasScorer;
4361
4621
  exports.createCompletenessScorer = createCompletenessScorer;
4362
4622
  exports.createContentSimilarityScorer = createContentSimilarityScorer;
4363
4623
  exports.createContextPrecisionScorer = createContextPrecisionScorer;
4624
+ exports.createContextRecallScorer = createContextRecallScorer;
4364
4625
  exports.createContextRelevanceScorerLLM = createContextRelevanceScorerLLM;
4365
4626
  exports.createFaithfulnessScorer = createFaithfulnessScorer;
4366
4627
  exports.createHallucinationScorer = createHallucinationScorer;