@mastra/evals 0.11.0 → 0.12.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 (187) hide show
  1. package/dist/attachListeners.d.ts +4 -0
  2. package/dist/attachListeners.d.ts.map +1 -0
  3. package/dist/{chunk-2JVD5IX6.cjs → chunk-7QAUEU4L.cjs} +2 -0
  4. package/dist/chunk-7QAUEU4L.cjs.map +1 -0
  5. package/dist/{chunk-IS3BZTWE.cjs → chunk-EMMSS5I5.cjs} +2 -0
  6. package/dist/chunk-EMMSS5I5.cjs.map +1 -0
  7. package/dist/{chunk-U67V476Y.js → chunk-G3PMV62Z.js} +2 -0
  8. package/dist/chunk-G3PMV62Z.js.map +1 -0
  9. package/dist/{chunk-COBCYVZ7.cjs → chunk-IUSAD2BW.cjs} +2 -0
  10. package/dist/chunk-IUSAD2BW.cjs.map +1 -0
  11. package/dist/{chunk-UYXFD4VX.js → chunk-QTWX6TKR.js} +2 -0
  12. package/dist/chunk-QTWX6TKR.js.map +1 -0
  13. package/dist/{chunk-TXXJUIES.js → chunk-YGTIO3J5.js} +2 -0
  14. package/dist/chunk-YGTIO3J5.js.map +1 -0
  15. package/dist/constants.d.ts +2 -0
  16. package/dist/constants.d.ts.map +1 -0
  17. package/dist/{dist-ZXFGMR47.js → dist-66YSVXZH.js} +4 -2
  18. package/dist/dist-66YSVXZH.js.map +1 -0
  19. package/dist/{dist-JD6MNRVB.cjs → dist-6ZEQKKXY.cjs} +14 -12
  20. package/dist/dist-6ZEQKKXY.cjs.map +1 -0
  21. package/dist/evaluation.d.ts +8 -0
  22. package/dist/evaluation.d.ts.map +1 -0
  23. package/dist/index.cjs +3 -1
  24. package/dist/index.cjs.map +1 -0
  25. package/dist/index.d.ts +3 -3
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +3 -1
  28. package/dist/index.js.map +1 -0
  29. package/dist/{magic-string.es-MNZ6ZGOL.js → magic-string.es-6JSI7KY4.js} +2 -0
  30. package/dist/magic-string.es-6JSI7KY4.js.map +1 -0
  31. package/dist/{magic-string.es-T2QO2IBJ.cjs → magic-string.es-NBXOXRCK.cjs} +2 -0
  32. package/dist/magic-string.es-NBXOXRCK.cjs.map +1 -0
  33. package/dist/metrics/index.d.ts +4 -0
  34. package/dist/metrics/index.d.ts.map +1 -0
  35. package/dist/metrics/judge/index.cjs +4 -2
  36. package/dist/metrics/judge/index.cjs.map +1 -0
  37. package/dist/metrics/judge/index.d.ts +7 -1
  38. package/dist/metrics/judge/index.d.ts.map +1 -0
  39. package/dist/metrics/judge/index.js +3 -1
  40. package/dist/metrics/judge/index.js.map +1 -0
  41. package/dist/metrics/llm/answer-relevancy/index.d.ts +16 -0
  42. package/dist/metrics/llm/answer-relevancy/index.d.ts.map +1 -0
  43. package/dist/metrics/llm/answer-relevancy/metricJudge.d.ts +20 -0
  44. package/dist/metrics/llm/answer-relevancy/metricJudge.d.ts.map +1 -0
  45. package/dist/metrics/llm/answer-relevancy/prompts.d.ts +19 -0
  46. package/dist/metrics/llm/answer-relevancy/prompts.d.ts.map +1 -0
  47. package/dist/metrics/llm/bias/index.d.ts +14 -0
  48. package/dist/metrics/llm/bias/index.d.ts.map +1 -0
  49. package/dist/metrics/llm/bias/metricJudge.d.ts +14 -0
  50. package/dist/metrics/llm/bias/metricJudge.d.ts.map +1 -0
  51. package/dist/metrics/llm/bias/prompts.d.ts +14 -0
  52. package/dist/metrics/llm/bias/prompts.d.ts.map +1 -0
  53. package/dist/metrics/llm/context-position/index.d.ts +16 -0
  54. package/dist/metrics/llm/context-position/index.d.ts.map +1 -0
  55. package/dist/metrics/llm/context-position/metricJudge.d.ts +20 -0
  56. package/dist/metrics/llm/context-position/metricJudge.d.ts.map +1 -0
  57. package/dist/metrics/llm/context-position/prompts.d.ts +17 -0
  58. package/dist/metrics/llm/context-position/prompts.d.ts.map +1 -0
  59. package/dist/metrics/llm/context-precision/index.d.ts +16 -0
  60. package/dist/metrics/llm/context-precision/index.d.ts.map +1 -0
  61. package/dist/metrics/llm/context-precision/metricJudge.d.ts +20 -0
  62. package/dist/metrics/llm/context-precision/metricJudge.d.ts.map +1 -0
  63. package/dist/metrics/llm/context-precision/prompts.d.ts +17 -0
  64. package/dist/metrics/llm/context-precision/prompts.d.ts.map +1 -0
  65. package/dist/metrics/llm/context-relevancy/index.d.ts +16 -0
  66. package/dist/metrics/llm/context-relevancy/index.d.ts.map +1 -0
  67. package/dist/metrics/llm/context-relevancy/metricJudge.d.ts +16 -0
  68. package/dist/metrics/llm/context-relevancy/metricJudge.d.ts.map +1 -0
  69. package/dist/metrics/llm/context-relevancy/prompts.d.ts +13 -0
  70. package/dist/metrics/llm/context-relevancy/prompts.d.ts.map +1 -0
  71. package/dist/metrics/llm/contextual-recall/index.d.ts +16 -0
  72. package/dist/metrics/llm/contextual-recall/index.d.ts.map +1 -0
  73. package/dist/metrics/llm/contextual-recall/metricJudge.d.ts +16 -0
  74. package/dist/metrics/llm/contextual-recall/metricJudge.d.ts.map +1 -0
  75. package/dist/metrics/llm/contextual-recall/prompts.d.ts +13 -0
  76. package/dist/metrics/llm/contextual-recall/prompts.d.ts.map +1 -0
  77. package/dist/metrics/llm/faithfulness/index.d.ts +16 -0
  78. package/dist/metrics/llm/faithfulness/index.d.ts.map +1 -0
  79. package/dist/metrics/llm/faithfulness/metricJudge.d.ts +22 -0
  80. package/dist/metrics/llm/faithfulness/metricJudge.d.ts.map +1 -0
  81. package/dist/metrics/llm/faithfulness/prompts.d.ts +20 -0
  82. package/dist/metrics/llm/faithfulness/prompts.d.ts.map +1 -0
  83. package/dist/metrics/llm/hallucination/index.d.ts +16 -0
  84. package/dist/metrics/llm/hallucination/index.d.ts.map +1 -0
  85. package/dist/metrics/llm/hallucination/metricJudge.d.ts +22 -0
  86. package/dist/metrics/llm/hallucination/metricJudge.d.ts.map +1 -0
  87. package/dist/metrics/llm/hallucination/prompts.d.ts +17 -0
  88. package/dist/metrics/llm/hallucination/prompts.d.ts.map +1 -0
  89. package/dist/metrics/llm/index.cjs +26 -24
  90. package/dist/metrics/llm/index.cjs.map +1 -0
  91. package/dist/metrics/llm/index.d.ts +12 -11
  92. package/dist/metrics/llm/index.d.ts.map +1 -0
  93. package/dist/metrics/llm/index.js +4 -2
  94. package/dist/metrics/llm/index.js.map +1 -0
  95. package/dist/metrics/llm/prompt-alignment/index.d.ts +33 -0
  96. package/dist/metrics/llm/prompt-alignment/index.d.ts.map +1 -0
  97. package/dist/metrics/llm/prompt-alignment/metricJudge.d.ts +20 -0
  98. package/dist/metrics/llm/prompt-alignment/metricJudge.d.ts.map +1 -0
  99. package/dist/metrics/llm/prompt-alignment/prompts.d.ts +17 -0
  100. package/dist/metrics/llm/prompt-alignment/prompts.d.ts.map +1 -0
  101. package/dist/metrics/llm/summarization/index.d.ts +19 -0
  102. package/dist/metrics/llm/summarization/index.d.ts.map +1 -0
  103. package/dist/metrics/llm/summarization/metricJudge.d.ts +34 -0
  104. package/dist/metrics/llm/summarization/metricJudge.d.ts.map +1 -0
  105. package/dist/metrics/llm/summarization/prompts.d.ts +30 -0
  106. package/dist/metrics/llm/summarization/prompts.d.ts.map +1 -0
  107. package/dist/metrics/llm/toxicity/index.d.ts +14 -0
  108. package/dist/metrics/llm/toxicity/index.d.ts.map +1 -0
  109. package/dist/metrics/llm/toxicity/metricJudge.d.ts +14 -0
  110. package/dist/metrics/llm/toxicity/metricJudge.d.ts.map +1 -0
  111. package/dist/metrics/llm/toxicity/prompts.d.ts +10 -0
  112. package/dist/metrics/llm/toxicity/prompts.d.ts.map +1 -0
  113. package/dist/metrics/llm/types.d.ts +7 -0
  114. package/dist/metrics/llm/types.d.ts.map +1 -0
  115. package/dist/metrics/llm/utils.d.ts +14 -0
  116. package/dist/metrics/llm/utils.d.ts.map +1 -0
  117. package/dist/metrics/nlp/completeness/index.d.ts +21 -0
  118. package/dist/metrics/nlp/completeness/index.d.ts.map +1 -0
  119. package/dist/metrics/nlp/content-similarity/index.d.ts +18 -0
  120. package/dist/metrics/nlp/content-similarity/index.d.ts.map +1 -0
  121. package/dist/metrics/nlp/index.cjs +2 -0
  122. package/dist/metrics/nlp/index.cjs.map +1 -0
  123. package/dist/metrics/nlp/index.d.ts +6 -5
  124. package/dist/metrics/nlp/index.d.ts.map +1 -0
  125. package/dist/metrics/nlp/index.js +2 -0
  126. package/dist/metrics/nlp/index.js.map +1 -0
  127. package/dist/metrics/nlp/keyword-coverage/index.d.ts +13 -0
  128. package/dist/metrics/nlp/keyword-coverage/index.d.ts.map +1 -0
  129. package/dist/metrics/nlp/textual-difference/index.d.ts +15 -0
  130. package/dist/metrics/nlp/textual-difference/index.d.ts.map +1 -0
  131. package/dist/metrics/nlp/tone/index.d.ts +18 -0
  132. package/dist/metrics/nlp/tone/index.d.ts.map +1 -0
  133. package/dist/scorers/code/completeness/index.d.ts +11 -0
  134. package/dist/scorers/code/completeness/index.d.ts.map +1 -0
  135. package/dist/scorers/code/content-similarity/index.d.ts +11 -0
  136. package/dist/scorers/code/content-similarity/index.d.ts.map +1 -0
  137. package/dist/scorers/code/index.cjs +139 -161
  138. package/dist/scorers/code/index.cjs.map +1 -0
  139. package/dist/scorers/code/index.d.ts +6 -5
  140. package/dist/scorers/code/index.d.ts.map +1 -0
  141. package/dist/scorers/code/index.js +139 -161
  142. package/dist/scorers/code/index.js.map +1 -0
  143. package/dist/scorers/code/keyword-coverage/index.d.ts +17 -0
  144. package/dist/scorers/code/keyword-coverage/index.d.ts.map +1 -0
  145. package/dist/scorers/code/textual-difference/index.d.ts +8 -0
  146. package/dist/scorers/code/textual-difference/index.d.ts.map +1 -0
  147. package/dist/scorers/code/tone/index.d.ts +21 -0
  148. package/dist/scorers/code/tone/index.d.ts.map +1 -0
  149. package/dist/scorers/index.d.ts +3 -0
  150. package/dist/scorers/index.d.ts.map +1 -0
  151. package/dist/scorers/llm/answer-relevancy/index.d.ts +16 -0
  152. package/dist/scorers/llm/answer-relevancy/index.d.ts.map +1 -0
  153. package/dist/scorers/llm/answer-relevancy/prompts.d.ts +13 -0
  154. package/dist/scorers/llm/answer-relevancy/prompts.d.ts.map +1 -0
  155. package/dist/scorers/llm/bias/index.d.ts +17 -0
  156. package/dist/scorers/llm/bias/index.d.ts.map +1 -0
  157. package/dist/scorers/llm/bias/prompts.d.ts +13 -0
  158. package/dist/scorers/llm/bias/prompts.d.ts.map +1 -0
  159. package/dist/scorers/llm/faithfulness/index.d.ts +16 -0
  160. package/dist/scorers/llm/faithfulness/index.d.ts.map +1 -0
  161. package/dist/scorers/llm/faithfulness/prompts.d.ts +20 -0
  162. package/dist/scorers/llm/faithfulness/prompts.d.ts.map +1 -0
  163. package/dist/scorers/llm/hallucination/index.d.ts +19 -0
  164. package/dist/scorers/llm/hallucination/index.d.ts.map +1 -0
  165. package/dist/scorers/llm/hallucination/prompts.d.ts +20 -0
  166. package/dist/scorers/llm/hallucination/prompts.d.ts.map +1 -0
  167. package/dist/scorers/llm/index.cjs +200 -207
  168. package/dist/scorers/llm/index.cjs.map +1 -0
  169. package/dist/scorers/llm/index.d.ts +6 -11
  170. package/dist/scorers/llm/index.d.ts.map +1 -0
  171. package/dist/scorers/llm/index.js +201 -208
  172. package/dist/scorers/llm/index.js.map +1 -0
  173. package/dist/scorers/llm/toxicity/index.d.ts +15 -0
  174. package/dist/scorers/llm/toxicity/index.d.ts.map +1 -0
  175. package/dist/scorers/llm/toxicity/prompts.d.ts +10 -0
  176. package/dist/scorers/llm/toxicity/prompts.d.ts.map +1 -0
  177. package/dist/scorers/utils.d.ts +59 -0
  178. package/dist/scorers/utils.d.ts.map +1 -0
  179. package/package.json +5 -5
  180. package/dist/_tsup-dts-rollup.d.cts +0 -984
  181. package/dist/_tsup-dts-rollup.d.ts +0 -984
  182. package/dist/index.d.cts +0 -3
  183. package/dist/metrics/judge/index.d.cts +0 -1
  184. package/dist/metrics/llm/index.d.cts +0 -11
  185. package/dist/metrics/nlp/index.d.cts +0 -5
  186. package/dist/scorers/code/index.d.cts +0 -5
  187. package/dist/scorers/llm/index.d.cts +0 -11
@@ -58,198 +58,174 @@ function calculateCoverage({ original, simplified }) {
58
58
  function createCompletenessScorer() {
59
59
  return scores.createScorer({
60
60
  name: "Completeness",
61
- description: 'Leverage the nlp method from "compromise" to extract elements from the input and output and calculate the coverage.',
62
- extract: async (run) => {
63
- const isInputInvalid = !run.input || run.input.some((i) => i.content === null || i.content === void 0);
64
- const isOutputInvalid = !run.output || run.output.text === null || run.output.text === void 0;
65
- if (isInputInvalid || isOutputInvalid) {
66
- throw new Error("Inputs cannot be null or undefined");
67
- }
68
- const input = run.input?.map((i) => i.content).join(", ") || "";
69
- const output = run.output.text;
70
- const inputToProcess = input;
71
- const outputToProcess = output;
72
- const inputDoc = nlp__default.default(inputToProcess.trim());
73
- const outputDoc = nlp__default.default(outputToProcess.trim());
74
- const inputElements = extractElements(inputDoc);
75
- const outputElements = extractElements(outputDoc);
76
- return {
77
- result: {
78
- inputElements,
79
- outputElements,
80
- missingElements: inputElements.filter((e) => !outputElements.includes(e)),
81
- elementCounts: {
82
- input: inputElements.length,
83
- output: outputElements.length
84
- }
85
- }
86
- };
87
- },
88
- analyze: async (run) => {
89
- const inputElements = run.extractStepResult?.inputElements;
90
- const outputElements = run.extractStepResult?.outputElements;
91
- return {
92
- score: calculateCoverage({
93
- original: inputElements,
94
- simplified: outputElements
95
- })
96
- };
61
+ description: 'Leverage the nlp method from "compromise" to extract elements from the input and output and calculate the coverage.'
62
+ }).preprocess(async ({ run }) => {
63
+ const isInputInvalid = !run.input || run.input.inputMessages.some((i) => i.content === null || i.content === void 0);
64
+ const isOutputInvalid = !run.output || run.output.some((i) => i.content === null || i.content === void 0);
65
+ if (isInputInvalid || isOutputInvalid) {
66
+ throw new Error("Inputs cannot be null or undefined");
97
67
  }
68
+ const input = run.input?.inputMessages.map((i) => i.content).join(", ") || "";
69
+ const output = run.output?.map(({ content }) => content).join(", ") || "";
70
+ const inputToProcess = input;
71
+ const outputToProcess = output;
72
+ const inputDoc = nlp__default.default(inputToProcess.trim());
73
+ const outputDoc = nlp__default.default(outputToProcess.trim());
74
+ const inputElements = extractElements(inputDoc);
75
+ const outputElements = extractElements(outputDoc);
76
+ return {
77
+ inputElements,
78
+ outputElements,
79
+ missingElements: inputElements.filter((e) => !outputElements.includes(e)),
80
+ elementCounts: {
81
+ input: inputElements.length,
82
+ output: outputElements.length
83
+ }
84
+ };
85
+ }).generateScore(({ results }) => {
86
+ const inputElements = results.preprocessStepResult?.inputElements;
87
+ const outputElements = results.preprocessStepResult?.outputElements;
88
+ return calculateCoverage({
89
+ original: inputElements,
90
+ simplified: outputElements
91
+ });
98
92
  });
99
93
  }
100
94
  function createTextualDifferenceScorer() {
101
95
  return scores.createScorer({
102
96
  name: "Completeness",
103
- description: 'Leverage the nlp method from "compromise" to extract elements from the input and output and calculate the coverage.',
104
- analyze: async (run) => {
105
- const input = run.input?.map((i) => i.content).join(", ") || "";
106
- const output = run.output.text;
107
- const matcher = new difflib.SequenceMatcher(null, input, output);
108
- const ratio = matcher.ratio();
109
- const ops = matcher.getOpcodes();
110
- const changes = ops.filter(([op]) => op !== "equal").length;
111
- const maxLength = Math.max(input.length, output.length);
112
- const lengthDiff = maxLength > 0 ? Math.abs(input.length - output.length) / maxLength : 0;
113
- const confidence = 1 - lengthDiff;
114
- return {
115
- score: ratio,
116
- result: {
117
- confidence,
118
- changes,
119
- lengthDiff
120
- }
121
- };
122
- }
97
+ description: 'Leverage the nlp method from "compromise" to extract elements from the input and output and calculate the coverage.'
98
+ }).preprocess(async ({ run }) => {
99
+ const input = run.input?.inputMessages?.map((i) => i.content).join(", ") || "";
100
+ const output = run.output?.map((i) => i.content).join(", ") || "";
101
+ const matcher = new difflib.SequenceMatcher(null, input, output);
102
+ const ratio = matcher.ratio();
103
+ const ops = matcher.getOpcodes();
104
+ const changes = ops.filter(([op]) => op !== "equal").length;
105
+ const maxLength = Math.max(input.length, output.length);
106
+ const lengthDiff = maxLength > 0 ? Math.abs(input.length - output.length) / maxLength : 0;
107
+ const confidence = 1 - lengthDiff;
108
+ return {
109
+ ratio,
110
+ confidence,
111
+ changes,
112
+ lengthDiff
113
+ };
114
+ }).generateScore(({ results }) => {
115
+ return results.preprocessStepResult?.ratio;
123
116
  });
124
117
  }
125
118
  function createKeywordCoverageScorer() {
126
119
  return scores.createScorer({
127
120
  name: "Completeness",
128
- description: 'Leverage the nlp method from "compromise" to extract elements from the input and output and calculate the coverage.',
129
- extract: async (run) => {
130
- const input = run.input?.map((i) => i.content).join(", ") || "";
131
- const output = run.output.text;
132
- if (!input && !output) {
133
- return {
134
- result: {
135
- referenceKeywords: /* @__PURE__ */ new Set(),
136
- responseKeywords: /* @__PURE__ */ new Set()
137
- }
138
- };
139
- }
140
- const extractKeywords = (text) => {
141
- return keyword_extractor__default.default.extract(text, {
142
- language: "english",
143
- remove_digits: true,
144
- return_changed_case: true,
145
- remove_duplicates: true
146
- });
147
- };
148
- const referenceKeywords = new Set(extractKeywords(input));
149
- const responseKeywords = new Set(extractKeywords(output));
121
+ description: 'Leverage the nlp method from "compromise" to extract elements from the input and output and calculate the coverage.'
122
+ }).preprocess(async ({ run }) => {
123
+ const input = run.input?.inputMessages?.map((i) => i.content).join(", ") || "";
124
+ const output = run.output?.map((i) => i.content).join(", ") || "";
125
+ if (!input && !output) {
150
126
  return {
151
127
  result: {
152
- referenceKeywords,
153
- responseKeywords
128
+ referenceKeywords: /* @__PURE__ */ new Set(),
129
+ responseKeywords: /* @__PURE__ */ new Set()
154
130
  }
155
131
  };
156
- },
157
- analyze: async (run) => {
158
- if (!run.extractStepResult?.referenceKeywords.size && !run.extractStepResult?.responseKeywords.size) {
159
- return {
160
- score: 1,
161
- result: {
162
- totalKeywords: 0,
163
- matchedKeywords: 0
164
- }
165
- };
166
- }
167
- const matchedKeywords = [...run.extractStepResult?.referenceKeywords].filter(
168
- (k) => run.extractStepResult?.responseKeywords.has(k)
169
- );
170
- const totalKeywords = run.extractStepResult?.referenceKeywords.size;
171
- const coverage = totalKeywords > 0 ? matchedKeywords.length / totalKeywords : 0;
132
+ }
133
+ const extractKeywords = (text) => {
134
+ return keyword_extractor__default.default.extract(text, {
135
+ language: "english",
136
+ remove_digits: true,
137
+ return_changed_case: true,
138
+ remove_duplicates: true
139
+ });
140
+ };
141
+ const referenceKeywords = new Set(extractKeywords(input));
142
+ const responseKeywords = new Set(extractKeywords(output));
143
+ return {
144
+ referenceKeywords,
145
+ responseKeywords
146
+ };
147
+ }).analyze(async ({ results }) => {
148
+ if (!results.preprocessStepResult?.referenceKeywords?.size && !results.preprocessStepResult?.responseKeywords?.size) {
172
149
  return {
173
- score: coverage,
174
- result: {
175
- totalKeywords: run.extractStepResult?.referenceKeywords.size,
176
- matchedKeywords: matchedKeywords.length
177
- }
150
+ totalKeywordsLength: 0,
151
+ matchedKeywordsLength: 0
178
152
  };
179
153
  }
154
+ const matchedKeywords = [...results.preprocessStepResult?.referenceKeywords].filter(
155
+ (k) => results.preprocessStepResult?.responseKeywords?.has(k)
156
+ );
157
+ return {
158
+ totalKeywordsLength: Array.from(results.preprocessStepResult?.referenceKeywords).length ?? 0,
159
+ matchedKeywordsLength: matchedKeywords.length ?? 0
160
+ };
161
+ }).generateScore(({ results }) => {
162
+ if (!results.analyzeStepResult?.totalKeywordsLength) {
163
+ return 1;
164
+ }
165
+ const totalKeywords = results.analyzeStepResult?.totalKeywordsLength;
166
+ const matchedKeywords = results.analyzeStepResult?.matchedKeywordsLength;
167
+ return totalKeywords > 0 ? matchedKeywords / totalKeywords : 0;
180
168
  });
181
169
  }
182
170
  function createContentSimilarityScorer({ ignoreCase, ignoreWhitespace } = { ignoreCase: true, ignoreWhitespace: true }) {
183
171
  return scores.createScorer({
184
172
  name: "Completeness",
185
- description: 'Leverage the nlp method from "compromise" to extract elements from the input and output and calculate the coverage.',
186
- extract: async (run) => {
187
- let processedInput = run.input?.map((i) => i.content).join(", ") || "";
188
- let processedOutput = run.output.text;
189
- if (ignoreCase) {
190
- processedInput = processedInput.toLowerCase();
191
- processedOutput = processedOutput.toLowerCase();
192
- }
193
- if (ignoreWhitespace) {
194
- processedInput = processedInput.replace(/\s+/g, " ").trim();
195
- processedOutput = processedOutput.replace(/\s+/g, " ").trim();
196
- }
197
- return {
198
- result: {
199
- processedInput,
200
- processedOutput
201
- }
202
- };
203
- },
204
- analyze: async (run) => {
205
- const similarity = stringSimilarity__default.default.compareTwoStrings(
206
- run.extractStepResult?.processedInput,
207
- run.extractStepResult?.processedOutput
208
- );
209
- return {
210
- score: similarity,
211
- result: {
212
- similarity
213
- }
214
- };
173
+ description: 'Leverage the nlp method from "compromise" to extract elements from the input and output and calculate the coverage.'
174
+ }).preprocess(async ({ run }) => {
175
+ let processedInput = run.input?.inputMessages.map((i) => i.content).join(", ") || "";
176
+ let processedOutput = run.output.map((i) => i.content).join(", ") || "";
177
+ if (ignoreCase) {
178
+ processedInput = processedInput.toLowerCase();
179
+ processedOutput = processedOutput.toLowerCase();
215
180
  }
181
+ if (ignoreWhitespace) {
182
+ processedInput = processedInput.replace(/\s+/g, " ").trim();
183
+ processedOutput = processedOutput.replace(/\s+/g, " ").trim();
184
+ }
185
+ return {
186
+ processedInput,
187
+ processedOutput
188
+ };
189
+ }).generateScore(({ results }) => {
190
+ const similarity = stringSimilarity__default.default.compareTwoStrings(
191
+ results.preprocessStepResult?.processedInput,
192
+ results.preprocessStepResult?.processedOutput
193
+ );
194
+ return similarity;
216
195
  });
217
196
  }
218
- function createToneScorer() {
197
+ function createToneScorer(config = {}) {
198
+ const { referenceTone } = config;
219
199
  return scores.createScorer({
220
200
  name: "Completeness",
221
- description: 'Leverage the nlp method from "compromise" to extract elements from the input and output and calculate the coverage.',
222
- analyze: async (run) => {
223
- const sentiment = new Sentiment__default.default();
224
- const input = run.input?.map((i) => i.content).join(", ") || "";
225
- const output = run.output.text;
226
- const responseSentiment = sentiment.analyze(input);
227
- if (output) {
228
- const referenceSentiment = sentiment.analyze(output);
229
- const sentimentDiff = Math.abs(responseSentiment.comparative - referenceSentiment.comparative);
230
- const normalizedScore = Math.max(0, 1 - sentimentDiff);
231
- return {
232
- score: normalizedScore,
233
- result: {
234
- responseSentiment: responseSentiment.comparative,
235
- referenceSentiment: referenceSentiment.comparative,
236
- difference: sentimentDiff
237
- }
238
- };
239
- }
240
- const sentences = input.match(/[^.!?]+[.!?]+/g) || [input];
241
- const sentiments = sentences.map((s) => sentiment.analyze(s).comparative);
242
- const avgSentiment = sentiments.reduce((a, b) => a + b, 0) / sentiments.length;
243
- const variance = sentiments.reduce((sum, s) => sum + Math.pow(s - avgSentiment, 2), 0) / sentiments.length;
244
- const stability = Math.max(0, 1 - variance);
201
+ description: 'Leverage the nlp method from "compromise" to extract elements from the input and output and calculate the coverage.'
202
+ }).preprocess(async ({ run }) => {
203
+ const sentiment = new Sentiment__default.default();
204
+ const agentMessage = run.output?.map((i) => i.content).join(", ") || "";
205
+ const responseSentiment = sentiment.analyze(agentMessage);
206
+ if (referenceTone) {
207
+ const referenceSentiment = sentiment.analyze(referenceTone);
208
+ const sentimentDiff = Math.abs(responseSentiment.comparative - referenceSentiment.comparative);
209
+ const normalizedScore = Math.max(0, 1 - sentimentDiff);
245
210
  return {
246
- score: stability,
247
- result: {
248
- avgSentiment,
249
- sentimentVariance: variance
250
- }
211
+ score: normalizedScore,
212
+ responseSentiment: responseSentiment.comparative,
213
+ referenceSentiment: referenceSentiment.comparative,
214
+ difference: sentimentDiff
251
215
  };
252
216
  }
217
+ const sentences = agentMessage.match(/[^.!?]+[.!?]+/g) || [agentMessage];
218
+ const sentiments = sentences.map((s) => sentiment.analyze(s).comparative);
219
+ const avgSentiment = sentiments.reduce((a, b) => a + b, 0) / sentiments.length;
220
+ const variance = sentiments.reduce((sum, s) => sum + Math.pow(s - avgSentiment, 2), 0) / sentiments.length;
221
+ const stability = Math.max(0, 1 - variance);
222
+ return {
223
+ score: stability,
224
+ avgSentiment,
225
+ sentimentVariance: variance
226
+ };
227
+ }).generateScore(({ results }) => {
228
+ return results.preprocessStepResult?.score;
253
229
  });
254
230
  }
255
231
 
@@ -258,3 +234,5 @@ exports.createContentSimilarityScorer = createContentSimilarityScorer;
258
234
  exports.createKeywordCoverageScorer = createKeywordCoverageScorer;
259
235
  exports.createTextualDifferenceScorer = createTextualDifferenceScorer;
260
236
  exports.createToneScorer = createToneScorer;
237
+ //# sourceMappingURL=index.cjs.map
238
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/scorers/code/completeness/index.ts","../../../src/scorers/code/textual-difference/index.ts","../../../src/scorers/code/keyword-coverage/index.ts","../../../src/scorers/code/content-similarity/index.ts","../../../src/scorers/code/tone/index.ts"],"names":["createScorer","nlp","SequenceMatcher","keyword_extractor","stringSimilarity","Sentiment"],"mappings":";;;;;;;;;;;;;;;;AAIA,SAAS,gBAAgB,GAAA,EAAqB;AAE5C,EAAA,OAAO,GAAA,CACJ,UAAU,KAAK,CAAA,CACf,QAAQ,kBAAA,EAAoB,EAAE,EAC9B,WAAA,EAAY;AACjB;AAEA,SAAS,gBAAgB,GAAA,EAAoB;AAE3C,EAAA,MAAM,QAAQ,GAAA,CAAI,KAAA,GAAQ,GAAA,CAAI,OAAO,KAAK,EAAC;AAC3C,EAAA,MAAM,KAAA,GAAQ,IAAI,KAAA,EAAM,CAAE,cAAa,CAAE,GAAA,CAAI,OAAO,CAAA,IAAK,EAAC;AAC1D,EAAA,MAAM,SAAS,GAAA,CAAI,MAAA,GAAS,GAAA,CAAI,OAAO,KAAK,EAAC;AAC7C,EAAA,MAAM,QAAQ,GAAA,CAAI,KAAA,GAAQ,GAAA,CAAI,OAAO,KAAK,EAAC;AAG3C,EAAA,MAAM,iBAAA,GAAoB,CAAC,IAAA,KAA2B;AAEpD,IAAA,MAAM,UAAA,GAAa,gBAAgB,IAAI,CAAA;AAGvC,IAAA,OAAO,WACJ,OAAA,CAAQ,iBAAA,EAAmB,OAAO,CAAA,CAClC,OAAA,CAAQ,eAAe,GAAG,CAAA,CAC1B,IAAA,EAAK,CACL,MA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Content":["import { createScorer } from '@mastra/core/scores';\nimport type { ScorerRunInputForAgent, ScorerRunOutputForAgent } from '@mastra/core/scores';\nimport nlp from 'compromise';\n\nfunction normalizeString(str: string): string {\n // Remove diacritics and convert to lowercase\n return str\n .normalize('NFD')\n .replace(/[\\u0300-\\u036f]/g, '')\n .toLowerCase();\n}\n\nfunction extractElements(doc: any): string[] {\n // Get more specific elements and ensure they're arrays\n const nouns = doc.nouns().out('array') || [];\n const verbs = doc.verbs().toInfinitive().out('array') || [];\n const topics = doc.topics().out('array') || [];\n const terms = doc.terms().out('array') || [];\n\n // Helper function to clean and split terms\n const cleanAndSplitTerm = (term: string): string[] => {\n // First normalize the string\n const normalized = normalizeString(term);\n\n // Split on word boundaries and filter out empty strings\n return normalized\n .replace(/([a-z])([A-Z])/g, '$1 $2') // Split camelCase\n .replace(/[^a-z0-9]+/g, ' ') // Replace non-alphanumeric with spaces\n .trim()\n .split(/\\s+/)\n .filter(word => word.length > 0);\n };\n\n // Process all elements\n const processedTerms = [\n ...nouns.flatMap(cleanAndSplitTerm),\n ...verbs.flatMap(cleanAndSplitTerm),\n ...topics.flatMap(cleanAndSplitTerm),\n ...terms.flatMap(cleanAndSplitTerm),\n ];\n\n // Remove duplicates\n return [...new Set(processedTerms)];\n}\n\nfunction calculateCoverage({ original, simplified }: { original: string[]; simplified: string[] }): number {\n if (original.length === 0) {\n return simplified.length === 0 ? 1 : 0;\n }\n\n // Exact matching for short words (3 chars or less), substring matching for longer words\n const covered = original.filter(element =>\n simplified.some(s => {\n const elem = normalizeString(element);\n const simp = normalizeString(s);\n\n // For short words (3 chars or less), require exact match\n if (elem.length <= 3) {\n return elem === simp;\n }\n\n // For longer words, require substantial overlap (more than 60% of the longer word)\n const longer = elem.length > simp.length ? elem : simp;\n const shorter = elem.length > simp.length ? simp : elem;\n\n if (longer.includes(shorter)) {\n return shorter.length / longer.length > 0.6;\n }\n\n return false;\n }),\n );\n return covered.length / original.length;\n}\n\nexport function createCompletenessScorer() {\n return createScorer<ScorerRunInputForAgent, ScorerRunOutputForAgent>({\n name: 'Completeness',\n description:\n 'Leverage the nlp method from \"compromise\" to extract elements from the input and output and calculate the coverage.',\n })\n .preprocess(async ({ run }) => {\n const isInputInvalid =\n !run.input ||\n run.input.inputMessages.some((i: { content: string }) => i.content === null || i.content === undefined);\n\n const isOutputInvalid =\n !run.output || run.output.some((i: { content: string }) => i.content === null || i.content === undefined);\n\n if (isInputInvalid || isOutputInvalid) {\n throw new Error('Inputs cannot be null or undefined');\n }\n\n const input = run.input?.inputMessages.map((i: { content: string }) => i.content).join(', ') || '';\n const output = run.output?.map(({ content }: { content: string }) => content).join(', ') || '';\n\n const inputToProcess = input;\n const outputToProcess = output;\n\n const inputDoc = nlp(inputToProcess.trim());\n const outputDoc = nlp(outputToProcess.trim());\n\n // Extract and log elements\n const inputElements = extractElements(inputDoc);\n const outputElements = extractElements(outputDoc);\n\n return {\n inputElements,\n outputElements,\n missingElements: inputElements.filter(e => !outputElements.includes(e)),\n elementCounts: {\n input: inputElements.length,\n output: outputElements.length,\n },\n };\n })\n .generateScore(({ results }) => {\n const inputElements = results.preprocessStepResult?.inputElements;\n const outputElements = results.preprocessStepResult?.outputElements;\n\n return calculateCoverage({\n original: inputElements,\n simplified: outputElements,\n });\n });\n}\n","import { createScorer } from '@mastra/core/scores';\nimport type { ScorerRunInputForAgent, ScorerRunOutputForAgent } from '@mastra/core/scores';\nimport { SequenceMatcher } from 'difflib';\n\nexport function createTextualDifferenceScorer() {\n return createScorer<ScorerRunInputForAgent, ScorerRunOutputForAgent>({\n name: 'Completeness',\n description:\n 'Leverage the nlp method from \"compromise\" to extract elements from the input and output and calculate the coverage.',\n })\n .preprocess(async ({ run }) => {\n const input = run.input?.inputMessages?.map((i: { content: string }) => i.content).join(', ') || '';\n const output = run.output?.map((i: { content: string }) => i.content).join(', ') || '';\n const matcher = new SequenceMatcher(null, input, output);\n const ratio = matcher.ratio();\n\n // Get detailed operations\n const ops = matcher.getOpcodes();\n const changes = ops.filter(([op]) => op !== 'equal').length;\n\n // Calculate confidence based on text length difference\n const maxLength = Math.max(input.length, output.length);\n const lengthDiff = maxLength > 0 ? Math.abs(input.length - output.length) / maxLength : 0;\n const confidence = 1 - lengthDiff;\n\n return {\n ratio,\n confidence,\n changes,\n lengthDiff,\n };\n })\n .generateScore(({ results }) => {\n return results.preprocessStepResult?.ratio;\n });\n}\n","import { createScorer } from '@mastra/core/scores';\nimport type { ScorerRunInputForAgent, ScorerRunOutputForAgent } from '@mastra/core/scores';\nimport keyword_extractor from 'keyword-extractor';\n\nexport function createKeywordCoverageScorer() {\n return createScorer<ScorerRunInputForAgent, ScorerRunOutputForAgent>({\n name: 'Completeness',\n description:\n 'Leverage the nlp method from \"compromise\" to extract elements from the input and output and calculate the coverage.',\n })\n .preprocess(async ({ run }) => {\n const input = run.input?.inputMessages?.map((i: { content: string }) => i.content).join(', ') || '';\n const output = run.output?.map((i: { content: string }) => i.content).join(', ') || '';\n\n if (!input && !output) {\n return {\n result: {\n referenceKeywords: new Set<string>(),\n responseKeywords: new Set<string>(),\n },\n };\n }\n\n const extractKeywords = (text: string) => {\n return keyword_extractor.extract(text, {\n language: 'english',\n remove_digits: true,\n return_changed_case: true,\n remove_duplicates: true,\n });\n };\n\n const referenceKeywords = new Set(extractKeywords(input));\n const responseKeywords = new Set(extractKeywords(output));\n return {\n referenceKeywords,\n responseKeywords,\n };\n })\n .analyze(async ({ results }) => {\n if (\n !results.preprocessStepResult?.referenceKeywords?.size &&\n !results.preprocessStepResult?.responseKeywords?.size\n ) {\n return {\n totalKeywordsLength: 0,\n matchedKeywordsLength: 0,\n };\n }\n\n const matchedKeywords = [...results.preprocessStepResult?.referenceKeywords].filter(k =>\n results.preprocessStepResult?.responseKeywords?.has(k),\n );\n\n return {\n totalKeywordsLength: Array.from(results.preprocessStepResult?.referenceKeywords).length ?? 0,\n matchedKeywordsLength: matchedKeywords.length ?? 0,\n };\n })\n .generateScore(({ results }) => {\n if (!results.analyzeStepResult?.totalKeywordsLength) {\n return 1;\n }\n\n const totalKeywords = results.analyzeStepResult?.totalKeywordsLength!;\n const matchedKeywords = results.analyzeStepResult?.matchedKeywordsLength!;\n return totalKeywords > 0 ? matchedKeywords / totalKeywords : 0;\n });\n}\n","import { createScorer } from '@mastra/core/scores';\nimport type { ScorerRunInputForAgent, ScorerRunOutputForAgent } from '@mastra/core/scores';\nimport stringSimilarity from 'string-similarity';\n\ninterface ContentSimilarityOptions {\n ignoreCase?: boolean;\n ignoreWhitespace?: boolean;\n}\n\nexport function createContentSimilarityScorer(\n { ignoreCase, ignoreWhitespace }: ContentSimilarityOptions = { ignoreCase: true, ignoreWhitespace: true },\n) {\n return createScorer<ScorerRunInputForAgent, ScorerRunOutputForAgent>({\n name: 'Completeness',\n description:\n 'Leverage the nlp method from \"compromise\" to extract elements from the input and output and calculate the coverage.',\n })\n .preprocess(async ({ run }) => {\n let processedInput = run.input?.inputMessages.map((i: { content: string }) => i.content).join(', ') || '';\n let processedOutput = run.output.map((i: { content: string }) => i.content).join(', ') || '';\n\n if (ignoreCase) {\n processedInput = processedInput.toLowerCase();\n processedOutput = processedOutput.toLowerCase();\n }\n\n if (ignoreWhitespace) {\n processedInput = processedInput.replace(/\\s+/g, ' ').trim();\n processedOutput = processedOutput.replace(/\\s+/g, ' ').trim();\n }\n\n return {\n processedInput,\n processedOutput,\n };\n })\n .generateScore(({ results }) => {\n const similarity = stringSimilarity.compareTwoStrings(\n results.preprocessStepResult?.processedInput,\n results.preprocessStepResult?.processedOutput,\n );\n\n return similarity;\n });\n}\n","import { createScorer } from '@mastra/core/scores';\nimport type { ScorerRunInputForAgent, ScorerRunOutputForAgent } from '@mastra/core/scores';\nimport Sentiment from 'sentiment';\n\ninterface ToneScorerConfig {\n referenceTone?: string;\n}\n\nexport function createToneScorer(config: ToneScorerConfig = {}) {\n const { referenceTone } = config;\n\n return createScorer<ScorerRunInputForAgent, ScorerRunOutputForAgent>({\n name: 'Completeness',\n description:\n 'Leverage the nlp method from \"compromise\" to extract elements from the input and output and calculate the coverage.',\n })\n .preprocess(async ({ run }) => {\n const sentiment = new Sentiment();\n const agentMessage: string = run.output?.map((i: { content: string }) => i.content).join(', ') || '';\n const responseSentiment = sentiment.analyze(agentMessage);\n\n if (referenceTone) {\n // Compare sentiment with reference\n const referenceSentiment = sentiment.analyze(referenceTone);\n const sentimentDiff = Math.abs(responseSentiment.comparative - referenceSentiment.comparative);\n const normalizedScore = Math.max(0, 1 - sentimentDiff);\n\n return {\n score: normalizedScore,\n responseSentiment: responseSentiment.comparative,\n referenceSentiment: referenceSentiment.comparative,\n difference: sentimentDiff,\n };\n }\n\n // Evaluate sentiment stability across response\n const sentences = agentMessage.match(/[^.!?]+[.!?]+/g) || [agentMessage];\n const sentiments = sentences.map(s => sentiment.analyze(s).comparative);\n const avgSentiment = sentiments.reduce((a, b) => a + b, 0) / sentiments.length;\n const variance = sentiments.reduce((sum, s) => sum + Math.pow(s - avgSentiment, 2), 0) / sentiments.length;\n const stability = Math.max(0, 1 - variance);\n\n return {\n score: stability,\n avgSentiment,\n sentimentVariance: variance,\n };\n })\n .generateScore(({ results }) => {\n return results.preprocessStepResult?.score;\n });\n}\n"]}
@@ -1,5 +1,6 @@
1
- export { createCompletenessScorer_alias_1 as createCompletenessScorer } from '../../_tsup-dts-rollup.js';
2
- export { createTextualDifferenceScorer_alias_1 as createTextualDifferenceScorer } from '../../_tsup-dts-rollup.js';
3
- export { createKeywordCoverageScorer_alias_1 as createKeywordCoverageScorer } from '../../_tsup-dts-rollup.js';
4
- export { createContentSimilarityScorer_alias_1 as createContentSimilarityScorer } from '../../_tsup-dts-rollup.js';
5
- export { createToneScorer_alias_1 as createToneScorer } from '../../_tsup-dts-rollup.js';
1
+ export * from './completeness';
2
+ export * from './textual-difference';
3
+ export * from './keyword-coverage';
4
+ export * from './content-similarity';
5
+ export * from './tone';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/scorers/code/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC;AACrC,cAAc,QAAQ,CAAC"}