@mastra/evals 0.0.0-error-handler-fix-20251020202607 → 0.0.0-esbuild-bundle-worker-20260807182016

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 (253) hide show
  1. package/CHANGELOG.md +950 -5
  2. package/LICENSE.md +15 -0
  3. package/README.md +19 -159
  4. package/dist/checks-64AonnEK.js +379 -0
  5. package/dist/checks-64AonnEK.js.map +1 -0
  6. package/dist/checks-DGTgg-nW.cjs +479 -0
  7. package/dist/checks-DGTgg-nW.cjs.map +1 -0
  8. package/dist/checks.cjs +14 -0
  9. package/dist/checks.d.ts +3 -0
  10. package/dist/checks.d.ts.map +1 -0
  11. package/dist/checks.js +2 -0
  12. package/dist/docs/SKILL.md +49 -0
  13. package/dist/docs/assets/SOURCE_MAP.json +6 -0
  14. package/dist/docs/references/docs-evals-built-in-scorers.md +66 -0
  15. package/dist/docs/references/docs-evals-overview.md +163 -0
  16. package/dist/docs/references/docs-evals-quick-checks.md +137 -0
  17. package/dist/docs/references/reference-evals-answer-relevancy.md +107 -0
  18. package/dist/docs/references/reference-evals-answer-similarity.md +99 -0
  19. package/dist/docs/references/reference-evals-bias.md +122 -0
  20. package/dist/docs/references/reference-evals-checks.md +212 -0
  21. package/dist/docs/references/reference-evals-completeness.md +138 -0
  22. package/dist/docs/references/reference-evals-content-similarity.md +103 -0
  23. package/dist/docs/references/reference-evals-context-precision.md +198 -0
  24. package/dist/docs/references/reference-evals-context-recall.md +205 -0
  25. package/dist/docs/references/reference-evals-context-relevance.md +533 -0
  26. package/dist/docs/references/reference-evals-faithfulness.md +116 -0
  27. package/dist/docs/references/reference-evals-hallucination.md +206 -0
  28. package/dist/docs/references/reference-evals-keyword-coverage.md +130 -0
  29. package/dist/docs/references/reference-evals-noise-sensitivity.md +677 -0
  30. package/dist/docs/references/reference-evals-prompt-alignment.md +617 -0
  31. package/dist/docs/references/reference-evals-rubric.md +115 -0
  32. package/dist/docs/references/reference-evals-scorer-utils.md +516 -0
  33. package/dist/docs/references/reference-evals-summarization.md +203 -0
  34. package/dist/docs/references/reference-evals-textual-difference.md +115 -0
  35. package/dist/docs/references/reference-evals-tone-consistency.md +121 -0
  36. package/dist/docs/references/reference-evals-tool-call-accuracy.md +535 -0
  37. package/dist/docs/references/reference-evals-toxicity.md +125 -0
  38. package/dist/docs/references/reference-evals-trajectory-accuracy.md +629 -0
  39. package/dist/index.cjs +12 -91
  40. package/dist/index.cjs.map +1 -1
  41. package/dist/index.d.ts +11 -2
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +13 -87
  44. package/dist/index.js.map +1 -1
  45. package/dist/scorers/code/checks/index.d.ts +214 -0
  46. package/dist/scorers/code/checks/index.d.ts.map +1 -0
  47. package/dist/scorers/code/completeness/index.d.ts +1 -1
  48. package/dist/scorers/code/completeness/index.d.ts.map +1 -1
  49. package/dist/scorers/code/content-similarity/index.d.ts +1 -1
  50. package/dist/scorers/code/content-similarity/index.d.ts.map +1 -1
  51. package/dist/scorers/code/index.d.ts +2 -0
  52. package/dist/scorers/code/index.d.ts.map +1 -1
  53. package/dist/scorers/code/keyword-coverage/index.d.ts +1 -1
  54. package/dist/scorers/code/keyword-coverage/index.d.ts.map +1 -1
  55. package/dist/scorers/code/textual-difference/index.d.ts +1 -1
  56. package/dist/scorers/code/textual-difference/index.d.ts.map +1 -1
  57. package/dist/scorers/code/tone/index.d.ts +1 -1
  58. package/dist/scorers/code/tone/index.d.ts.map +1 -1
  59. package/dist/scorers/code/tool-call-accuracy/index.d.ts +2 -2
  60. package/dist/scorers/code/tool-call-accuracy/index.d.ts.map +1 -1
  61. package/dist/scorers/code/trajectory/index.d.ts +164 -0
  62. package/dist/scorers/code/trajectory/index.d.ts.map +1 -0
  63. package/dist/scorers/index.d.ts +1 -0
  64. package/dist/scorers/index.d.ts.map +1 -1
  65. package/dist/scorers/llm/answer-relevancy/index.d.ts +2 -1
  66. package/dist/scorers/llm/answer-relevancy/index.d.ts.map +1 -1
  67. package/dist/scorers/llm/answer-similarity/index.d.ts +3 -2
  68. package/dist/scorers/llm/answer-similarity/index.d.ts.map +1 -1
  69. package/dist/scorers/llm/bias/index.d.ts +2 -2
  70. package/dist/scorers/llm/bias/index.d.ts.map +1 -1
  71. package/dist/scorers/llm/context-precision/index.d.ts +4 -3
  72. package/dist/scorers/llm/context-precision/index.d.ts.map +1 -1
  73. package/dist/scorers/llm/context-recall/index.d.ts +20 -0
  74. package/dist/scorers/llm/context-recall/index.d.ts.map +1 -0
  75. package/dist/scorers/llm/context-recall/prompts.d.ts +19 -0
  76. package/dist/scorers/llm/context-recall/prompts.d.ts.map +1 -0
  77. package/dist/scorers/llm/context-relevance/index.d.ts +5 -4
  78. package/dist/scorers/llm/context-relevance/index.d.ts.map +1 -1
  79. package/dist/scorers/llm/faithfulness/index.d.ts +4 -1
  80. package/dist/scorers/llm/faithfulness/index.d.ts.map +1 -1
  81. package/dist/scorers/llm/hallucination/index.d.ts +20 -3
  82. package/dist/scorers/llm/hallucination/index.d.ts.map +1 -1
  83. package/dist/scorers/llm/index.d.ts +4 -0
  84. package/dist/scorers/llm/index.d.ts.map +1 -1
  85. package/dist/scorers/llm/noise-sensitivity/index.d.ts +3 -2
  86. package/dist/scorers/llm/noise-sensitivity/index.d.ts.map +1 -1
  87. package/dist/scorers/llm/prompt-alignment/index.d.ts +7 -7
  88. package/dist/scorers/llm/prompt-alignment/index.d.ts.map +1 -1
  89. package/dist/scorers/llm/rubric/index.d.ts +71 -0
  90. package/dist/scorers/llm/rubric/index.d.ts.map +1 -0
  91. package/dist/scorers/llm/rubric/prompts.d.ts +37 -0
  92. package/dist/scorers/llm/rubric/prompts.d.ts.map +1 -0
  93. package/dist/scorers/llm/summarization/index.d.ts +55 -0
  94. package/dist/scorers/llm/summarization/index.d.ts.map +1 -0
  95. package/dist/scorers/llm/summarization/prompts.d.ts +48 -0
  96. package/dist/scorers/llm/summarization/prompts.d.ts.map +1 -0
  97. package/dist/scorers/llm/tool-call-accuracy/index.d.ts +4 -4
  98. package/dist/scorers/llm/tool-call-accuracy/index.d.ts.map +1 -1
  99. package/dist/scorers/llm/toxicity/index.d.ts +2 -1
  100. package/dist/scorers/llm/toxicity/index.d.ts.map +1 -1
  101. package/dist/scorers/llm/trajectory/index.d.ts +58 -0
  102. package/dist/scorers/llm/trajectory/index.d.ts.map +1 -0
  103. package/dist/scorers/llm/trajectory/prompts.d.ts +20 -0
  104. package/dist/scorers/llm/trajectory/prompts.d.ts.map +1 -0
  105. package/dist/scorers/prebuilt/index.cjs +4546 -0
  106. package/dist/scorers/prebuilt/index.cjs.map +1 -0
  107. package/dist/scorers/prebuilt/index.d.ts +3 -0
  108. package/dist/scorers/prebuilt/index.d.ts.map +1 -0
  109. package/dist/scorers/prebuilt/index.js +4503 -0
  110. package/dist/scorers/prebuilt/index.js.map +1 -0
  111. package/dist/scorers/utils.cjs +967 -58
  112. package/dist/scorers/utils.cjs.map +1 -1
  113. package/dist/scorers/utils.d.ts +563 -16
  114. package/dist/scorers/utils.d.ts.map +1 -1
  115. package/dist/scorers/utils.js +939 -2
  116. package/dist/scorers/utils.js.map +1 -1
  117. package/package.json +42 -68
  118. package/dist/attachListeners.d.ts +0 -4
  119. package/dist/attachListeners.d.ts.map +0 -1
  120. package/dist/chunk-7QAUEU4L.cjs +0 -10
  121. package/dist/chunk-7QAUEU4L.cjs.map +0 -1
  122. package/dist/chunk-EMMSS5I5.cjs +0 -37
  123. package/dist/chunk-EMMSS5I5.cjs.map +0 -1
  124. package/dist/chunk-G3PMV62Z.js +0 -33
  125. package/dist/chunk-G3PMV62Z.js.map +0 -1
  126. package/dist/chunk-IUSAD2BW.cjs +0 -19
  127. package/dist/chunk-IUSAD2BW.cjs.map +0 -1
  128. package/dist/chunk-KHEXN75Q.js +0 -134
  129. package/dist/chunk-KHEXN75Q.js.map +0 -1
  130. package/dist/chunk-QKR2PMLZ.cjs +0 -148
  131. package/dist/chunk-QKR2PMLZ.cjs.map +0 -1
  132. package/dist/chunk-QTWX6TKR.js +0 -8
  133. package/dist/chunk-QTWX6TKR.js.map +0 -1
  134. package/dist/chunk-YGTIO3J5.js +0 -17
  135. package/dist/chunk-YGTIO3J5.js.map +0 -1
  136. package/dist/dist-E463LOAZ.js +0 -16729
  137. package/dist/dist-E463LOAZ.js.map +0 -1
  138. package/dist/dist-KKO3PIUK.cjs +0 -16751
  139. package/dist/dist-KKO3PIUK.cjs.map +0 -1
  140. package/dist/evaluation.d.ts +0 -8
  141. package/dist/evaluation.d.ts.map +0 -1
  142. package/dist/magic-string.es-7ORA5OGR.js +0 -1305
  143. package/dist/magic-string.es-7ORA5OGR.js.map +0 -1
  144. package/dist/magic-string.es-NZ2XWFKN.cjs +0 -1311
  145. package/dist/magic-string.es-NZ2XWFKN.cjs.map +0 -1
  146. package/dist/metrics/index.d.ts +0 -4
  147. package/dist/metrics/index.d.ts.map +0 -1
  148. package/dist/metrics/judge/index.cjs +0 -12
  149. package/dist/metrics/judge/index.cjs.map +0 -1
  150. package/dist/metrics/judge/index.d.ts +0 -7
  151. package/dist/metrics/judge/index.d.ts.map +0 -1
  152. package/dist/metrics/judge/index.js +0 -3
  153. package/dist/metrics/judge/index.js.map +0 -1
  154. package/dist/metrics/llm/answer-relevancy/index.d.ts +0 -16
  155. package/dist/metrics/llm/answer-relevancy/index.d.ts.map +0 -1
  156. package/dist/metrics/llm/answer-relevancy/metricJudge.d.ts +0 -20
  157. package/dist/metrics/llm/answer-relevancy/metricJudge.d.ts.map +0 -1
  158. package/dist/metrics/llm/answer-relevancy/prompts.d.ts +0 -19
  159. package/dist/metrics/llm/answer-relevancy/prompts.d.ts.map +0 -1
  160. package/dist/metrics/llm/bias/index.d.ts +0 -14
  161. package/dist/metrics/llm/bias/index.d.ts.map +0 -1
  162. package/dist/metrics/llm/bias/metricJudge.d.ts +0 -14
  163. package/dist/metrics/llm/bias/metricJudge.d.ts.map +0 -1
  164. package/dist/metrics/llm/bias/prompts.d.ts +0 -14
  165. package/dist/metrics/llm/bias/prompts.d.ts.map +0 -1
  166. package/dist/metrics/llm/context-position/index.d.ts +0 -16
  167. package/dist/metrics/llm/context-position/index.d.ts.map +0 -1
  168. package/dist/metrics/llm/context-position/metricJudge.d.ts +0 -20
  169. package/dist/metrics/llm/context-position/metricJudge.d.ts.map +0 -1
  170. package/dist/metrics/llm/context-position/prompts.d.ts +0 -17
  171. package/dist/metrics/llm/context-position/prompts.d.ts.map +0 -1
  172. package/dist/metrics/llm/context-precision/index.d.ts +0 -16
  173. package/dist/metrics/llm/context-precision/index.d.ts.map +0 -1
  174. package/dist/metrics/llm/context-precision/metricJudge.d.ts +0 -20
  175. package/dist/metrics/llm/context-precision/metricJudge.d.ts.map +0 -1
  176. package/dist/metrics/llm/context-precision/prompts.d.ts +0 -17
  177. package/dist/metrics/llm/context-precision/prompts.d.ts.map +0 -1
  178. package/dist/metrics/llm/context-relevancy/index.d.ts +0 -16
  179. package/dist/metrics/llm/context-relevancy/index.d.ts.map +0 -1
  180. package/dist/metrics/llm/context-relevancy/metricJudge.d.ts +0 -16
  181. package/dist/metrics/llm/context-relevancy/metricJudge.d.ts.map +0 -1
  182. package/dist/metrics/llm/context-relevancy/prompts.d.ts +0 -13
  183. package/dist/metrics/llm/context-relevancy/prompts.d.ts.map +0 -1
  184. package/dist/metrics/llm/contextual-recall/index.d.ts +0 -16
  185. package/dist/metrics/llm/contextual-recall/index.d.ts.map +0 -1
  186. package/dist/metrics/llm/contextual-recall/metricJudge.d.ts +0 -16
  187. package/dist/metrics/llm/contextual-recall/metricJudge.d.ts.map +0 -1
  188. package/dist/metrics/llm/contextual-recall/prompts.d.ts +0 -13
  189. package/dist/metrics/llm/contextual-recall/prompts.d.ts.map +0 -1
  190. package/dist/metrics/llm/faithfulness/index.d.ts +0 -16
  191. package/dist/metrics/llm/faithfulness/index.d.ts.map +0 -1
  192. package/dist/metrics/llm/faithfulness/metricJudge.d.ts +0 -22
  193. package/dist/metrics/llm/faithfulness/metricJudge.d.ts.map +0 -1
  194. package/dist/metrics/llm/faithfulness/prompts.d.ts +0 -20
  195. package/dist/metrics/llm/faithfulness/prompts.d.ts.map +0 -1
  196. package/dist/metrics/llm/hallucination/index.d.ts +0 -16
  197. package/dist/metrics/llm/hallucination/index.d.ts.map +0 -1
  198. package/dist/metrics/llm/hallucination/metricJudge.d.ts +0 -22
  199. package/dist/metrics/llm/hallucination/metricJudge.d.ts.map +0 -1
  200. package/dist/metrics/llm/hallucination/prompts.d.ts +0 -17
  201. package/dist/metrics/llm/hallucination/prompts.d.ts.map +0 -1
  202. package/dist/metrics/llm/index.cjs +0 -2481
  203. package/dist/metrics/llm/index.cjs.map +0 -1
  204. package/dist/metrics/llm/index.d.ts +0 -12
  205. package/dist/metrics/llm/index.d.ts.map +0 -1
  206. package/dist/metrics/llm/index.js +0 -2469
  207. package/dist/metrics/llm/index.js.map +0 -1
  208. package/dist/metrics/llm/prompt-alignment/index.d.ts +0 -33
  209. package/dist/metrics/llm/prompt-alignment/index.d.ts.map +0 -1
  210. package/dist/metrics/llm/prompt-alignment/metricJudge.d.ts +0 -20
  211. package/dist/metrics/llm/prompt-alignment/metricJudge.d.ts.map +0 -1
  212. package/dist/metrics/llm/prompt-alignment/prompts.d.ts +0 -17
  213. package/dist/metrics/llm/prompt-alignment/prompts.d.ts.map +0 -1
  214. package/dist/metrics/llm/summarization/index.d.ts +0 -19
  215. package/dist/metrics/llm/summarization/index.d.ts.map +0 -1
  216. package/dist/metrics/llm/summarization/metricJudge.d.ts +0 -34
  217. package/dist/metrics/llm/summarization/metricJudge.d.ts.map +0 -1
  218. package/dist/metrics/llm/summarization/prompts.d.ts +0 -30
  219. package/dist/metrics/llm/summarization/prompts.d.ts.map +0 -1
  220. package/dist/metrics/llm/toxicity/index.d.ts +0 -14
  221. package/dist/metrics/llm/toxicity/index.d.ts.map +0 -1
  222. package/dist/metrics/llm/toxicity/metricJudge.d.ts +0 -14
  223. package/dist/metrics/llm/toxicity/metricJudge.d.ts.map +0 -1
  224. package/dist/metrics/llm/toxicity/prompts.d.ts +0 -10
  225. package/dist/metrics/llm/toxicity/prompts.d.ts.map +0 -1
  226. package/dist/metrics/llm/types.d.ts +0 -7
  227. package/dist/metrics/llm/types.d.ts.map +0 -1
  228. package/dist/metrics/llm/utils.d.ts +0 -14
  229. package/dist/metrics/llm/utils.d.ts.map +0 -1
  230. package/dist/metrics/nlp/completeness/index.d.ts +0 -21
  231. package/dist/metrics/nlp/completeness/index.d.ts.map +0 -1
  232. package/dist/metrics/nlp/content-similarity/index.d.ts +0 -18
  233. package/dist/metrics/nlp/content-similarity/index.d.ts.map +0 -1
  234. package/dist/metrics/nlp/index.cjs +0 -203
  235. package/dist/metrics/nlp/index.cjs.map +0 -1
  236. package/dist/metrics/nlp/index.d.ts +0 -6
  237. package/dist/metrics/nlp/index.d.ts.map +0 -1
  238. package/dist/metrics/nlp/index.js +0 -190
  239. package/dist/metrics/nlp/index.js.map +0 -1
  240. package/dist/metrics/nlp/keyword-coverage/index.d.ts +0 -13
  241. package/dist/metrics/nlp/keyword-coverage/index.d.ts.map +0 -1
  242. package/dist/metrics/nlp/textual-difference/index.d.ts +0 -15
  243. package/dist/metrics/nlp/textual-difference/index.d.ts.map +0 -1
  244. package/dist/metrics/nlp/tone/index.d.ts +0 -18
  245. package/dist/metrics/nlp/tone/index.d.ts.map +0 -1
  246. package/dist/scorers/code/index.cjs +0 -329
  247. package/dist/scorers/code/index.cjs.map +0 -1
  248. package/dist/scorers/code/index.js +0 -315
  249. package/dist/scorers/code/index.js.map +0 -1
  250. package/dist/scorers/llm/index.cjs +0 -2588
  251. package/dist/scorers/llm/index.cjs.map +0 -1
  252. package/dist/scorers/llm/index.js +0 -2572
  253. package/dist/scorers/llm/index.js.map +0 -1
@@ -0,0 +1,533 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # Context relevance scorer
4
+
5
+ The `createContextRelevanceScorerLLM()` function creates a scorer that evaluates how relevant and useful provided context was for generating agent responses. It uses weighted relevance levels and applies penalties for unused high-relevance context and missing information.
6
+
7
+ It's especially useful for these use cases:
8
+
9
+ ## Content generation evaluation
10
+
11
+ Best for evaluating context quality in:
12
+
13
+ - Chat systems where context usage matters
14
+ - RAG pipelines needing detailed relevance assessment
15
+ - Systems where missing context affects quality
16
+
17
+ ## Context selection optimization
18
+
19
+ Use when optimizing for:
20
+
21
+ - Comprehensive context coverage
22
+ - Effective context utilization
23
+ - Identifying context gaps
24
+
25
+ ## Parameters
26
+
27
+ **model** (`MastraModelConfig`): The language model to use for evaluating context relevance
28
+
29
+ **options** (`ContextRelevanceOptions`): Configuration options for the scorer
30
+
31
+ Note: Either `context` or `contextExtractor` must be provided. If both are provided, `contextExtractor` takes precedence.
32
+
33
+ ## `.run()` returns
34
+
35
+ **score** (`number`): Weighted relevance score between 0 and scale (default 0-1)
36
+
37
+ **reason** (`string`): Human-readable explanation of the context relevance evaluation
38
+
39
+ ## Scoring details
40
+
41
+ ### Weighted Relevance Scoring
42
+
43
+ Context Relevance uses a advanced scoring algorithm that considers:
44
+
45
+ 1. **Relevance Levels**: Each context piece is classified with weighted values:
46
+
47
+ - `high` = 1.0 (directly addresses the query)
48
+ - `medium` = 0.7 (supporting information)
49
+ - `low` = 0.3 (tangentially related)
50
+ - `none` = 0.0 (completely irrelevant)
51
+
52
+ 2. **Usage Detection**: Tracks whether relevant context was actually used in the response
53
+
54
+ 3. **Penalties Applied** (configurable via `penalties` options):
55
+
56
+ - **Unused High-Relevance**: `unusedHighRelevanceContext` penalty per unused high-relevance context (default: 0.1)
57
+ - **Missing Context**: Up to `maxMissingContextPenalty` for identified missing information (default: 0.5)
58
+
59
+ ### Scoring Formula
60
+
61
+ ```text
62
+ Base Score = Σ(relevance_weights) / (num_contexts × 1.0)
63
+ Usage Penalty = count(unused_high_relevance) × unusedHighRelevanceContext
64
+ Missing Penalty = min(count(missing_context) × missingContextPerItem, maxMissingContextPenalty)
65
+
66
+ Final Score = max(0, Base Score - Usage Penalty - Missing Penalty) × scale
67
+ ```
68
+
69
+ **Default Values**:
70
+
71
+ - `unusedHighRelevanceContext` = 0.1 (10% penalty per unused high-relevance context)
72
+ - `missingContextPerItem` = 0.15 (15% penalty per missing context item)
73
+ - `maxMissingContextPenalty` = 0.5 (maximum 50% penalty for missing context)
74
+ - `scale` = 1
75
+
76
+ ### Score interpretation
77
+
78
+ - **0.9-1.0**: Excellent - all context highly relevant and used
79
+ - **0.7-0.8**: Good - mostly relevant with minor gaps
80
+ - **0.4-0.6**: Mixed - substantial irrelevant or unused context
81
+ - **0.2-0.3**: Poor - mostly irrelevant context
82
+ - **0.0-0.1**: Very poor - no relevant context found
83
+
84
+ ### Reason analysis
85
+
86
+ The reason field provides insights on:
87
+
88
+ - Relevance level of each context piece (high/medium/low/none)
89
+ - Which context was actually used in the response
90
+ - Penalties applied for unused high-relevance context (configurable via `unusedHighRelevanceContext`)
91
+ - Missing context that would have improved the response (penalized via `missingContextPerItem` up to `maxMissingContextPenalty`)
92
+
93
+ ### Optimization strategies
94
+
95
+ Use results to improve your system:
96
+
97
+ - **Filter irrelevant context**: Remove low/none relevance pieces before processing
98
+ - **Ensure context usage**: Make sure high-relevance context is incorporated
99
+ - **Fill context gaps**: Add missing information identified by the scorer
100
+ - **Balance context size**: Find optimal amount of context for best relevance
101
+ - **Tune penalty sensitivity**: Adjust `unusedHighRelevanceContext`, `missingContextPerItem`, and `maxMissingContextPenalty` based on your application's tolerance for unused or missing context
102
+
103
+ ### Difference from Context Precision
104
+
105
+ | Aspect | Context Relevance | Context Precision |
106
+ | ------------- | -------------------------------------- | ---------------------------------- |
107
+ | **Algorithm** | Weighted levels with penalties | Mean Average Precision (MAP) |
108
+ | **Relevance** | Multiple levels (high/medium/low/none) | Binary (yes/no) |
109
+ | **Position** | Not considered | Critical (rewards early placement) |
110
+ | **Usage** | Tracks and penalizes unused context | Not considered |
111
+ | **Missing** | Identifies and penalizes gaps | Not evaluated |
112
+
113
+ ## Scorer configuration
114
+
115
+ ### Custom penalty configuration
116
+
117
+ Control how penalties are applied for unused and missing context:
118
+
119
+ ```typescript
120
+ import { createContextRelevanceScorerLLM } from '@mastra/evals'
121
+
122
+ // Stricter penalty configuration
123
+ const strictScorer = createContextRelevanceScorerLLM({
124
+ model: 'openai/gpt-5.6-sol',
125
+ options: {
126
+ context: [
127
+ 'Einstein won the Nobel Prize for photoelectric effect',
128
+ 'He developed the theory of relativity',
129
+ 'Einstein was born in Germany',
130
+ ],
131
+ penalties: {
132
+ unusedHighRelevanceContext: 0.2, // 20% penalty per unused high-relevance context
133
+ missingContextPerItem: 0.25, // 25% penalty per missing context item
134
+ maxMissingContextPenalty: 0.6, // Maximum 60% penalty for missing context
135
+ },
136
+ scale: 1,
137
+ },
138
+ })
139
+
140
+ // Lenient penalty configuration
141
+ const lenientScorer = createContextRelevanceScorerLLM({
142
+ model: 'openai/gpt-5.6-sol',
143
+ options: {
144
+ context: [
145
+ 'Einstein won the Nobel Prize for photoelectric effect',
146
+ 'He developed the theory of relativity',
147
+ 'Einstein was born in Germany',
148
+ ],
149
+ penalties: {
150
+ unusedHighRelevanceContext: 0.05, // 5% penalty per unused high-relevance context
151
+ missingContextPerItem: 0.1, // 10% penalty per missing context item
152
+ maxMissingContextPenalty: 0.3, // Maximum 30% penalty for missing context
153
+ },
154
+ scale: 1,
155
+ },
156
+ })
157
+
158
+ const testRun = {
159
+ input: {
160
+ inputMessages: [
161
+ {
162
+ id: '1',
163
+ role: 'user',
164
+ content: 'What did Einstein achieve in physics?',
165
+ },
166
+ ],
167
+ },
168
+ output: [
169
+ {
170
+ id: '2',
171
+ role: 'assistant',
172
+ content: 'Einstein won the Nobel Prize for his work on the photoelectric effect.',
173
+ },
174
+ ],
175
+ }
176
+
177
+ const strictResult = await strictScorer.run(testRun)
178
+ const lenientResult = await lenientScorer.run(testRun)
179
+
180
+ console.log('Strict penalties:', strictResult.score) // Lower score due to unused context
181
+ console.log('Lenient penalties:', lenientResult.score) // Higher score, less penalty
182
+ ```
183
+
184
+ ### Dynamic Context Extraction
185
+
186
+ ```typescript
187
+ const scorer = createContextRelevanceScorerLLM({
188
+ model: 'openai/gpt-5.6-sol',
189
+ options: {
190
+ contextExtractor: (input, output) => {
191
+ // Extract context based on the query
192
+ const userQuery = input?.inputMessages?.[0]?.content || ''
193
+ if (userQuery.includes('Einstein')) {
194
+ return [
195
+ 'Einstein won the Nobel Prize for the photoelectric effect',
196
+ 'He developed the theory of relativity',
197
+ ]
198
+ }
199
+ return ['General physics information']
200
+ },
201
+ penalties: {
202
+ unusedHighRelevanceContext: 0.15,
203
+ },
204
+ },
205
+ })
206
+ ```
207
+
208
+ ### Custom scale factor
209
+
210
+ ```typescript
211
+ const scorer = createContextRelevanceScorerLLM({
212
+ model: 'openai/gpt-5.6-sol',
213
+ options: {
214
+ context: ['Relevant information...', 'Supporting details...'],
215
+ scale: 100, // Scale scores from 0-100 instead of 0-1
216
+ },
217
+ })
218
+
219
+ // Result will be scaled: score: 85 instead of 0.85
220
+ ```
221
+
222
+ ### Combining multiple context sources
223
+
224
+ ```typescript
225
+ const scorer = createContextRelevanceScorerLLM({
226
+ model: 'openai/gpt-5.6-sol',
227
+ options: {
228
+ contextExtractor: (input, output) => {
229
+ const query = input?.inputMessages?.[0]?.content || ''
230
+
231
+ // Combine from multiple sources
232
+ const kbContext = knowledgeBase.search(query)
233
+ const docContext = documentStore.retrieve(query)
234
+ const cacheContext = contextCache.get(query)
235
+
236
+ return [...kbContext, ...docContext, ...cacheContext]
237
+ },
238
+ scale: 1,
239
+ },
240
+ })
241
+ ```
242
+
243
+ ## Examples
244
+
245
+ ### High relevance example
246
+
247
+ This example shows excellent context relevance where all context directly supports the response:
248
+
249
+ ```typescript
250
+ import { createContextRelevanceScorerLLM } from '@mastra/evals'
251
+
252
+ const scorer = createContextRelevanceScorerLLM({
253
+ model: 'openai/gpt-5.6-sol',
254
+ options: {
255
+ context: [
256
+ 'Einstein won the Nobel Prize for his discovery of the photoelectric effect in 1921.',
257
+ 'He published his theory of special relativity in 1905.',
258
+ 'His general relativity theory, published in 1915, revolutionized our understanding of gravity.',
259
+ ],
260
+ scale: 1,
261
+ },
262
+ })
263
+
264
+ const result = await scorer.run({
265
+ input: {
266
+ inputMessages: [
267
+ {
268
+ id: '1',
269
+ role: 'user',
270
+ content: "What were Einstein's major scientific achievements?",
271
+ },
272
+ ],
273
+ },
274
+ output: [
275
+ {
276
+ id: '2',
277
+ role: 'assistant',
278
+ content:
279
+ "Einstein's major achievements include the Nobel Prize for the photoelectric effect, special relativity in 1905, and general relativity in 1915.",
280
+ },
281
+ ],
282
+ })
283
+
284
+ console.log(result)
285
+ // Output:
286
+ // {
287
+ // score: 1.0,
288
+ // reason: "The score is 1.0 because all context pieces are highly relevant to Einstein's achievements and were effectively used in generating the comprehensive response."
289
+ // }
290
+ ```
291
+
292
+ ### Mixed relevance example
293
+
294
+ This example shows moderate relevance with some context being irrelevant or unused:
295
+
296
+ ```typescript
297
+ import { createContextRelevanceScorerLLM } from '@mastra/evals'
298
+
299
+ const scorer = createContextRelevanceScorerLLM({
300
+ model: 'openai/gpt-5.6-sol',
301
+ options: {
302
+ context: [
303
+ 'Solar eclipses occur when the Moon blocks the Sun.',
304
+ 'The Moon moves between the Earth and Sun during eclipses.',
305
+ 'The Moon is visible at night.',
306
+ 'Stars twinkle due to atmospheric interference.',
307
+ 'Total eclipses can last up to 7.5 minutes.',
308
+ ],
309
+ scale: 1,
310
+ },
311
+ })
312
+
313
+ const result = await scorer.run({
314
+ input: {
315
+ inputMessages: [
316
+ {
317
+ id: '1',
318
+ role: 'user',
319
+ content: 'What causes solar eclipses?',
320
+ },
321
+ ],
322
+ },
323
+ output: [
324
+ {
325
+ id: '2',
326
+ role: 'assistant',
327
+ content:
328
+ 'Solar eclipses happen when the Moon moves between Earth and the Sun, blocking sunlight.',
329
+ },
330
+ ],
331
+ })
332
+
333
+ console.log(result)
334
+ // Output with default penalties:
335
+ // {
336
+ // score: 0.64,
337
+ // reason: "The score is 0.64 because contexts 1 and 2 are highly relevant and used, context 5 is relevant but unused (10% penalty), while contexts 3 and 4 are irrelevant."
338
+ // }
339
+
340
+ // With custom penalty configuration
341
+ const customScorer = createContextRelevanceScorerLLM({
342
+ model: 'openai/gpt-5.6-sol',
343
+ options: {
344
+ context: [
345
+ 'Solar eclipses occur when the Moon blocks the Sun.',
346
+ 'The Moon moves between the Earth and Sun during eclipses.',
347
+ 'The Moon is visible at night.',
348
+ 'Stars twinkle due to atmospheric interference.',
349
+ 'Total eclipses can last up to 7.5 minutes.',
350
+ ],
351
+ penalties: {
352
+ unusedHighRelevanceContext: 0.05, // Lower penalty for unused context
353
+ missingContextPerItem: 0.1,
354
+ maxMissingContextPenalty: 0.3,
355
+ },
356
+ },
357
+ })
358
+
359
+ const customResult = await customScorer.run({
360
+ input: {
361
+ inputMessages: [{ id: '1', role: 'user', content: 'What causes solar eclipses?' }],
362
+ },
363
+ output: [
364
+ {
365
+ id: '2',
366
+ role: 'assistant',
367
+ content:
368
+ 'Solar eclipses happen when the Moon moves between Earth and the Sun, blocking sunlight.',
369
+ },
370
+ ],
371
+ })
372
+
373
+ console.log(customResult)
374
+ // Output with lenient penalties:
375
+ // {
376
+ // score: 0.69, // Higher score due to reduced penalty for unused context
377
+ // reason: "The score is 0.69 because contexts 1 and 2 are highly relevant and used, context 5 is relevant but unused (5% penalty), while contexts 3 and 4 are irrelevant."
378
+ // }
379
+ ```
380
+
381
+ ### Low relevance example
382
+
383
+ This example shows poor context relevance with mostly irrelevant information:
384
+
385
+ ```typescript
386
+ import { createContextRelevanceScorerLLM } from '@mastra/evals'
387
+
388
+ const scorer = createContextRelevanceScorerLLM({
389
+ model: 'openai/gpt-5.6-sol',
390
+ options: {
391
+ context: [
392
+ 'The Great Barrier Reef is located in Australia.',
393
+ 'Coral reefs need warm water to survive.',
394
+ 'Many fish species live in coral reefs.',
395
+ 'Australia has six states and two territories.',
396
+ 'The capital of Australia is Canberra.',
397
+ ],
398
+ scale: 1,
399
+ },
400
+ })
401
+
402
+ const result = await scorer.run({
403
+ input: {
404
+ inputMessages: [
405
+ {
406
+ id: '1',
407
+ role: 'user',
408
+ content: 'What is the capital of Australia?',
409
+ },
410
+ ],
411
+ },
412
+ output: [
413
+ {
414
+ id: '2',
415
+ role: 'assistant',
416
+ content: 'The capital of Australia is Canberra.',
417
+ },
418
+ ],
419
+ })
420
+
421
+ console.log(result)
422
+ // Output:
423
+ // {
424
+ // score: 0.26,
425
+ // reason: "The score is 0.26 because only context 5 is relevant to the query about Australia's capital, while the other contexts about reefs are completely irrelevant."
426
+ // }
427
+ ```
428
+
429
+ ### Dynamic context extraction
430
+
431
+ Extract context at runtime based on the run input:
432
+
433
+ ```typescript
434
+ import { createContextRelevanceScorerLLM } from '@mastra/evals'
435
+
436
+ const scorer = createContextRelevanceScorerLLM({
437
+ model: 'openai/gpt-5.6-sol',
438
+ options: {
439
+ contextExtractor: (input, output) => {
440
+ // Extract query from input
441
+ const query = input?.inputMessages?.[0]?.content || ''
442
+
443
+ // Dynamically retrieve context based on query
444
+ if (query.toLowerCase().includes('einstein')) {
445
+ return [
446
+ 'Einstein developed E=mc²',
447
+ 'He won the Nobel Prize in 1921',
448
+ 'His theories revolutionized physics',
449
+ ]
450
+ }
451
+
452
+ if (query.toLowerCase().includes('climate')) {
453
+ return [
454
+ 'Global temperatures are rising',
455
+ 'CO2 levels affect climate',
456
+ 'Renewable energy reduces emissions',
457
+ ]
458
+ }
459
+
460
+ return ['General knowledge base entry']
461
+ },
462
+ penalties: {
463
+ unusedHighRelevanceContext: 0.15, // 15% penalty for unused relevant context
464
+ missingContextPerItem: 0.2, // 20% penalty per missing context item
465
+ maxMissingContextPenalty: 0.4, // Cap at 40% total missing context penalty
466
+ },
467
+ scale: 1,
468
+ },
469
+ })
470
+ ```
471
+
472
+ ### RAG system integration
473
+
474
+ Integrate with RAG pipelines to evaluate retrieved context:
475
+
476
+ ```typescript
477
+ import { createContextRelevanceScorerLLM } from '@mastra/evals'
478
+
479
+ const scorer = createContextRelevanceScorerLLM({
480
+ model: 'openai/gpt-5.6-sol',
481
+ options: {
482
+ contextExtractor: (input, output) => {
483
+ // Extract from RAG retrieval results
484
+ const ragResults = inputData.metadata?.ragResults || []
485
+
486
+ // Return the text content of retrieved documents
487
+ return ragResults.filter(doc => doc.relevanceScore > 0.5).map(doc => doc.content)
488
+ },
489
+ penalties: {
490
+ unusedHighRelevanceContext: 0.12, // Moderate penalty for unused RAG context
491
+ missingContextPerItem: 0.18, // Higher penalty for missing information in RAG
492
+ maxMissingContextPenalty: 0.45, // Slightly higher cap for RAG systems
493
+ },
494
+ scale: 1,
495
+ },
496
+ })
497
+
498
+ // Evaluate RAG system performance
499
+ const evaluateRAG = async testCases => {
500
+ const results = []
501
+
502
+ for (const testCase of testCases) {
503
+ const score = await scorer.run(testCase)
504
+ results.push({
505
+ query: testCase.inputData.inputMessages[0].content,
506
+ relevanceScore: score.score,
507
+ feedback: score.reason,
508
+ unusedContext: score.reason.includes('unused'),
509
+ missingContext: score.reason.includes('missing'),
510
+ })
511
+ }
512
+
513
+ return results
514
+ }
515
+ ```
516
+
517
+ ## Comparison with context precision
518
+
519
+ Choose the right scorer for your needs:
520
+
521
+ | Use Case | Context Relevance | Context Precision |
522
+ | ------------------------ | -------------------- | ------------------------- |
523
+ | **RAG evaluation** | When usage matters | When ranking matters |
524
+ | **Context quality** | Nuanced levels | Binary relevance |
525
+ | **Missing detection** | ✓ Identifies gaps | ✗ Not evaluated |
526
+ | **Usage tracking** | ✓ Tracks utilization | ✗ Not considered |
527
+ | **Position sensitivity** | ✗ Position agnostic | ✓ Rewards early placement |
528
+
529
+ ## Related
530
+
531
+ - [Context Precision Scorer](https://mastra.ai/reference/evals/context-precision): Evaluates context ranking using MAP
532
+ - [Faithfulness Scorer](https://mastra.ai/reference/evals/faithfulness): Measures answer groundedness in context
533
+ - [Custom Scorers](https://mastra.ai/docs/evals/custom-scorers): Creating your own evaluation metrics
@@ -0,0 +1,116 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # Faithfulness scorer
4
+
5
+ The `createFaithfulnessScorer()` function evaluates how factually accurate an LLM's output is compared to the provided context. It extracts claims from the output and verifies them against the context, making it essential to measure RAG pipeline responses' reliability.
6
+
7
+ ## Parameters
8
+
9
+ The `createFaithfulnessScorer()` function accepts a single options object with the following properties:
10
+
11
+ **model** (`LanguageModel`): Configuration for the model used to evaluate faithfulness.
12
+
13
+ **context** (`string[]`): Array of context chunks against which the output's claims will be verified.
14
+
15
+ **scale** (`number`): The maximum score value. The final score will be normalized to this scale. (Default: `1`)
16
+
17
+ This function returns an instance of the MastraScorer class. The `.run()` method accepts the same input as other scorers (see the [MastraScorer reference](https://mastra.ai/reference/evals/mastra-scorer)), but the return value includes LLM-specific fields as documented below.
18
+
19
+ ## `.run()` returns
20
+
21
+ **runId** (`string`): The id of the run (optional).
22
+
23
+ **preprocessStepResult** (`string[]`): Array of extracted claims from the output.
24
+
25
+ **preprocessPrompt** (`string`): The prompt sent to the LLM for the preprocess step (optional).
26
+
27
+ **analyzeStepResult** (`object`): Object with verdicts: { verdicts: Array<{ verdict: 'yes' | 'no' | 'unsure', reason: string }> }
28
+
29
+ **analyzePrompt** (`string`): The prompt sent to the LLM for the analyze step (optional).
30
+
31
+ **score** (`number`): A score between 0 and the configured scale, representing the proportion of claims that are supported by the context.
32
+
33
+ **reason** (`string`): A detailed explanation of the score, including which claims were supported, contradicted, or marked as unsure.
34
+
35
+ **generateReasonPrompt** (`string`): The prompt sent to the LLM for the generateReason step (optional).
36
+
37
+ ## Scoring details
38
+
39
+ The scorer evaluates faithfulness through claim verification against provided context.
40
+
41
+ ### Scoring Process
42
+
43
+ 1. Analyzes claims and context:
44
+
45
+ - Extracts all claims (factual and speculative)
46
+
47
+ - Verifies each claim against context
48
+
49
+ - Assigns one of three verdicts:
50
+
51
+ - "yes" - claim supported by context
52
+ - "no" - claim contradicts context
53
+ - "unsure" - claim unverifiable
54
+
55
+ 2. Calculates faithfulness score:
56
+
57
+ - Counts supported claims
58
+ - Divides by total claims
59
+ - Scales to configured range
60
+
61
+ Final score: `(supported_claims / total_claims) * scale`
62
+
63
+ ### Score interpretation
64
+
65
+ A faithfulness score between 0 and 1:
66
+
67
+ - **1.0**: All claims are accurate and directly supported by the context.
68
+ - **0.7 to 0.9**: Most claims are correct, with minor additions or omissions.
69
+ - **0.4 to 0.6**: Some claims are supported, but others are unverifiable.
70
+ - **0.1 to 0.3**: Most of the content is inaccurate or unsupported.
71
+ - **0.0**: All claims are false or contradict the context.
72
+
73
+ ## Example
74
+
75
+ Evaluate agent responses for faithfulness to provided context:
76
+
77
+ ```typescript
78
+ import { runEvals } from '@mastra/core/evals'
79
+ import { createFaithfulnessScorer } from '@mastra/evals/scorers/prebuilt'
80
+ import { myAgent } from './agent'
81
+
82
+ // Context is typically populated from agent tool calls or RAG retrieval
83
+ const scorer = createFaithfulnessScorer({
84
+ model: 'openai/gpt-5.6-sol',
85
+ })
86
+
87
+ const result = await runEvals({
88
+ data: [
89
+ {
90
+ input: 'Tell me about the Tesla Model 3.',
91
+ },
92
+ {
93
+ input: 'What are the key features of this electric vehicle?',
94
+ },
95
+ ],
96
+ scorers: [scorer],
97
+ target: myAgent,
98
+ onItemComplete: ({ scorerResults }) => {
99
+ console.log({
100
+ score: scorerResults[scorer.id].score,
101
+ reason: scorerResults[scorer.id].reason,
102
+ })
103
+ },
104
+ })
105
+
106
+ console.log(result.scores)
107
+ ```
108
+
109
+ For more details on `runEvals`, see the [runEvals reference](https://mastra.ai/reference/evals/run-evals).
110
+
111
+ To add this scorer to an agent, see the [Scorers overview](https://mastra.ai/docs/evals/overview) guide.
112
+
113
+ ## Related
114
+
115
+ - [Answer Relevancy Scorer](https://mastra.ai/reference/evals/answer-relevancy)
116
+ - [Hallucination Scorer](https://mastra.ai/reference/evals/hallucination)