@mastra/evals 1.7.0 → 1.8.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.
- package/CHANGELOG.md +38 -0
- package/dist/docs/SKILL.md +3 -2
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-evals-overview.md +20 -8
- package/dist/docs/references/docs-evals-quick-checks.md +1 -1
- package/dist/docs/references/reference-evals-completeness.md +1 -1
- package/dist/docs/references/reference-evals-noise-sensitivity.md +1 -1
- package/dist/docs/references/reference-evals-prompt-alignment.md +18 -0
- package/dist/docs/references/reference-evals-rubric.md +1 -1
- package/dist/docs/references/reference-evals-summarization.md +1 -1
- package/dist/docs/references/reference-migrations-upgrade-to-v1-evals.md +241 -0
- package/dist/scorers/llm/prompt-alignment/index.d.ts +10 -0
- package/dist/scorers/llm/prompt-alignment/index.d.ts.map +1 -1
- package/dist/scorers/llm/prompt-alignment/prompts.d.ts +4 -2
- package/dist/scorers/llm/prompt-alignment/prompts.d.ts.map +1 -1
- package/dist/scorers/prebuilt/index.cjs +28 -9
- package/dist/scorers/prebuilt/index.cjs.map +1 -1
- package/dist/scorers/prebuilt/index.js +29 -10
- package/dist/scorers/prebuilt/index.js.map +1 -1
- package/dist/scorers/utils.cjs +46 -1
- package/dist/scorers/utils.cjs.map +1 -1
- package/dist/scorers/utils.d.ts +26 -0
- package/dist/scorers/utils.d.ts.map +1 -1
- package/dist/scorers/utils.js +46 -2
- package/dist/scorers/utils.js.map +1 -1
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
# @mastra/evals
|
|
2
2
|
|
|
3
|
+
## 1.8.0-alpha.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Added an `includeConversationHistory` option to the Prompt Alignment scorer so multi-turn agent runs are scored in context. ([#21683](https://github.com/mastra-ai/mastra/pull/21683))
|
|
8
|
+
|
|
9
|
+
Previously the scorer only saw the current turn. In a conversation a short reply like `"A"` has no meaning on its own, so the judge could not tell what the user asked for and scored a perfectly good response as misaligned. The scorer now optionally includes the prior turns from the agent's memory, uses them to interpret the current prompt, and still scores only the current response.
|
|
10
|
+
|
|
11
|
+
**Before**
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
const scorer = createPromptAlignmentScorerLLM({
|
|
15
|
+
model: 'openai/gpt-5-mini',
|
|
16
|
+
options: { evaluationMode: 'user' },
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**After**
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
const scorer = createPromptAlignmentScorerLLM({
|
|
24
|
+
model: 'openai/gpt-5-mini',
|
|
25
|
+
options: {
|
|
26
|
+
evaluationMode: 'user',
|
|
27
|
+
includeConversationHistory: true, // or { maxMessages: 6 }
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The option is off by default, so existing scores do not change. It only applies to agent runs, which are the runs that carry remembered messages. Fixes https://github.com/mastra-ai/mastra/issues/21638
|
|
33
|
+
|
|
34
|
+
### Patch Changes
|
|
35
|
+
|
|
36
|
+
- Fixed LLM-judge scorers scoring an intermediate reply instead of the agent's final answer. When agent output contains multiple assistant messages (multi-step runs), scorers such as Prompt Alignment now evaluate the last assistant response that contains text. Fixes #21645 ([#21686](https://github.com/mastra-ai/mastra/pull/21686))
|
|
37
|
+
|
|
38
|
+
- Updated dependencies [[`4e7a421`](https://github.com/mastra-ai/mastra/commit/4e7a421dce8a48742f785d1e93ad2f43a572b282), [`242e324`](https://github.com/mastra-ai/mastra/commit/242e3241e73cbd5c9bb86a31ebb49ca0256488d4), [`217e967`](https://github.com/mastra-ai/mastra/commit/217e9672d8b3160eb729d8e9f0044949e88da239), [`d774e89`](https://github.com/mastra-ai/mastra/commit/d774e8930c781df8c9effe3763e6b501c099b6cc), [`9c27a53`](https://github.com/mastra-ai/mastra/commit/9c27a53cd9d3de4f3f025bc387d94ce371c33f95), [`dff25a1`](https://github.com/mastra-ai/mastra/commit/dff25a1103fa72ee082a9b6f805ebeb5ce400753), [`217e967`](https://github.com/mastra-ai/mastra/commit/217e9672d8b3160eb729d8e9f0044949e88da239), [`7f78585`](https://github.com/mastra-ai/mastra/commit/7f785857e401570e2ffb316911f126ed363aa537), [`f2a4afd`](https://github.com/mastra-ai/mastra/commit/f2a4afd7e37e809669001ed17724b341a5c1f45e), [`d438148`](https://github.com/mastra-ai/mastra/commit/d438148e222c1e2fb3c652725ce75680962ebec4), [`ba05fe0`](https://github.com/mastra-ai/mastra/commit/ba05fe0738f70cb686777546e968237d09269142), [`d26a8d4`](https://github.com/mastra-ai/mastra/commit/d26a8d4281f28414715b333c85bedaf70d0b2890), [`677cdc6`](https://github.com/mastra-ai/mastra/commit/677cdc6af564dec29a13464d12b7ab2a4efc22e9), [`a318490`](https://github.com/mastra-ai/mastra/commit/a318490e17da32f338d50929c770d901a9b3dd72), [`763e0c6`](https://github.com/mastra-ai/mastra/commit/763e0c61e04d76ad9a9efd301aa57525ca0cbea9), [`23e0be2`](https://github.com/mastra-ai/mastra/commit/23e0be261381e49534b4ff3101c60ee64a946cbf), [`7fc8806`](https://github.com/mastra-ai/mastra/commit/7fc880627d3cbf995d31ea0e8b807bf15417e651), [`0e02eac`](https://github.com/mastra-ai/mastra/commit/0e02eacdb2e30e1697a41910b41163742a181dc1), [`4df174c`](https://github.com/mastra-ai/mastra/commit/4df174c32bddf093a82f273070b8380aef7c9e90), [`f7c25b5`](https://github.com/mastra-ai/mastra/commit/f7c25b5106ddfb48e591f98df7a51e0f2dd01dba), [`dc09cc1`](https://github.com/mastra-ai/mastra/commit/dc09cc1083d861cde192c1cd235324dc75b8c731), [`36b4649`](https://github.com/mastra-ai/mastra/commit/36b4649045a3a380cbab8ceca866db4086223aff), [`377eb81`](https://github.com/mastra-ai/mastra/commit/377eb81ce43b964e3a6b541df172da74a8ff3716)]:
|
|
39
|
+
- @mastra/core@1.60.0-alpha.8
|
|
40
|
+
|
|
3
41
|
## 1.7.0
|
|
4
42
|
|
|
5
43
|
### Minor Changes
|
package/dist/docs/SKILL.md
CHANGED
|
@@ -3,7 +3,7 @@ name: mastra-evals
|
|
|
3
3
|
description: Documentation for @mastra/evals. Use when working with @mastra/evals APIs, configuration, or implementation.
|
|
4
4
|
metadata:
|
|
5
5
|
package: "@mastra/evals"
|
|
6
|
-
version: "1.
|
|
6
|
+
version: "1.8.0-alpha.0"
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
## When to use
|
|
@@ -17,7 +17,7 @@ Read the individual reference documents for detailed explanations and code examp
|
|
|
17
17
|
### Docs
|
|
18
18
|
|
|
19
19
|
- [Built-in scorers](references/docs-evals-built-in-scorers.md) - Overview of Mastra's ready-to-use scorers for evaluating AI outputs across quality, safety, and performance dimensions.
|
|
20
|
-
- [
|
|
20
|
+
- [Evals](references/docs-evals-overview.md) - Overview of evaluations and scorers in Mastra, including Quick Checks, live evaluations, and trace evaluations.
|
|
21
21
|
- [Quick Checks](references/docs-evals-quick-checks.md) - Zero-LLM micro-scorers for fast, deterministic assertions on agent output text and tool usage.
|
|
22
22
|
|
|
23
23
|
### Reference
|
|
@@ -44,6 +44,7 @@ Read the individual reference documents for detailed explanations and code examp
|
|
|
44
44
|
- [Reference: Tool call accuracy scorers](references/reference-evals-tool-call-accuracy.md) - Documentation for the Tool Call Accuracy Scorers in Mastra, which evaluate whether LLM outputs call the correct tools from available options.
|
|
45
45
|
- [Reference: Toxicity scorer](references/reference-evals-toxicity.md) - Documentation for the Toxicity Scorer in Mastra, which evaluates LLM outputs for racist, biased, or toxic elements.
|
|
46
46
|
- [Reference: Trajectory accuracy scorers](references/reference-evals-trajectory-accuracy.md) - Documentation for the Trajectory Accuracy Scorers in Mastra, which evaluate whether an agent or workflow follows the expected sequence of actions.
|
|
47
|
+
- [Evals and scorers](references/reference-migrations-upgrade-to-v1-evals.md) - Learn how to migrate evals and scorers changes when upgrading to v1.
|
|
47
48
|
|
|
48
49
|
|
|
49
50
|
Read [assets/SOURCE_MAP.json](assets/SOURCE_MAP.json) for source code references.
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
2
|
|
|
3
|
-
#
|
|
3
|
+
# Evals
|
|
4
4
|
|
|
5
5
|
While traditional software tests have clear pass/fail conditions, AI outputs are non-deterministic, they can vary with the same input. **Scorers** help bridge this gap by providing quantifiable metrics for measuring agent quality.
|
|
6
6
|
|
|
7
|
-
Scorers are automated tests that evaluate Agents outputs using model-graded, rule-based, and statistical methods. Scorers return **scores**: numerical values (typically between 0 and 1) that quantify how well an output meets your evaluation criteria. These scores enable you to objectively track performance
|
|
7
|
+
Scorers are automated tests that evaluate Agents outputs using model-graded, rule-based, and statistical methods. Scorers return **scores**: numerical values (typically between 0 and 1) that quantify how well an output meets your evaluation criteria. These scores enable you to objectively track performance and compare different approaches while identifying areas for improvement in your AI systems. Scorers can be customized with your own prompts and scoring functions.
|
|
8
8
|
|
|
9
|
-
Scorers can
|
|
9
|
+
Scorers can run in the cloud to capture real-time results. Scorers can also be part of your CI/CD pipeline for testing and monitoring agents over time.
|
|
10
10
|
|
|
11
11
|
> **📹 Watch:** Watch [Mastra evals overview](https://www.youtube.com/watch?v=12WN6u2DrBk) for an introduction to evals and how to reason about agent quality.
|
|
12
12
|
|
|
@@ -75,7 +75,7 @@ export const evaluatedAgent = new Agent({
|
|
|
75
75
|
|
|
76
76
|
### Adding scorers to workflow steps
|
|
77
77
|
|
|
78
|
-
You can also add scorers to individual workflow steps to evaluate outputs at specific points in your process:
|
|
78
|
+
You can also add scorers to individual workflow steps to evaluate outputs at specific points in your process. Each scorer receives that step's own input and output, so you can measure quality at each step instead of only scoring the final answer:
|
|
79
79
|
|
|
80
80
|
```typescript
|
|
81
81
|
import { createWorkflow, createStep } from "@mastra/core/workflows";
|
|
@@ -83,25 +83,37 @@ import { z } from "zod";
|
|
|
83
83
|
import { customStepScorer } from "../scorers/custom-step-scorer";
|
|
84
84
|
|
|
85
85
|
const contentStep = createStep({
|
|
86
|
+
id: "content-step",
|
|
87
|
+
inputSchema: z.object({ topic: z.string() }),
|
|
88
|
+
outputSchema: z.object({ content: z.string() }),
|
|
86
89
|
scorers: {
|
|
87
90
|
customStepScorer: {
|
|
88
91
|
scorer: customStepScorer(),
|
|
89
92
|
sampling: {
|
|
90
93
|
type: "ratio",
|
|
91
94
|
rate: 1, // Score every step execution
|
|
92
|
-
}
|
|
93
|
-
}
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
execute: async ({ inputData }) => {
|
|
99
|
+
return { content: await generateContent(inputData.topic) };
|
|
94
100
|
},
|
|
95
101
|
});
|
|
96
102
|
|
|
97
|
-
export const contentWorkflow = createWorkflow({
|
|
103
|
+
export const contentWorkflow = createWorkflow({
|
|
104
|
+
id: "content-workflow",
|
|
105
|
+
inputSchema: z.object({ topic: z.string() }),
|
|
106
|
+
outputSchema: z.object({ content: z.string() }),
|
|
107
|
+
})
|
|
98
108
|
.then(contentStep)
|
|
99
109
|
.commit();
|
|
100
110
|
```
|
|
101
111
|
|
|
112
|
+
For the step-level `scorers` API, see the [Step class reference](https://mastra.ai/reference/workflows/step).
|
|
113
|
+
|
|
102
114
|
### How live evaluations work
|
|
103
115
|
|
|
104
|
-
**Asynchronous execution**: Live evaluations run in the background without blocking your agent responses or workflow execution.
|
|
116
|
+
**Asynchronous execution**: Live evaluations run in the background without blocking your agent responses or workflow execution. Your AI systems remain responsive while live evaluations monitor them.
|
|
105
117
|
|
|
106
118
|
**Sampling control**: The `sampling.rate` parameter (0-1) controls what percentage of outputs get scored:
|
|
107
119
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
2
|
|
|
3
|
-
# Quick
|
|
3
|
+
# Quick checks
|
|
4
4
|
|
|
5
5
|
Quick Checks are composable micro-scorers for common assertions like "output contains X" or "agent called tool Y." They require no LLM, run instantly, and plug into the same `scorers: [...]` array as any other scorer.
|
|
6
6
|
|
|
@@ -177,7 +177,7 @@ Include in your test harness to:
|
|
|
177
177
|
|
|
178
178
|
- Benchmark different models' noise resistance before deployment
|
|
179
179
|
- Identify agents vulnerable to manipulation during development
|
|
180
|
-
- Create detailed test coverage for
|
|
180
|
+
- Create detailed test coverage for multiple noise types
|
|
181
181
|
- Ensure consistent behavior across updates
|
|
182
182
|
|
|
183
183
|
### Security Testing
|
|
@@ -70,6 +70,24 @@ const scorer = createPromptAlignmentScorerLLM({
|
|
|
70
70
|
})
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
+
### Multi-turn conversations
|
|
74
|
+
|
|
75
|
+
By default the scorer only sees the current turn. In a conversation, a reply like `"A"` is meaningless on its own, so the judge can't tell what the user asked for and scores the response as misaligned.
|
|
76
|
+
|
|
77
|
+
Set `includeConversationHistory` to give the judge the prior turns from the agent's memory. The judge uses them to interpret the current prompt, but still scores only the current response.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
const scorer = createPromptAlignmentScorerLLM({
|
|
81
|
+
model: 'openai/gpt-5.6-sol',
|
|
82
|
+
options: {
|
|
83
|
+
evaluationMode: 'user',
|
|
84
|
+
includeConversationHistory: { maxMessages: 6 }, // or `true` for the last 10 messages
|
|
85
|
+
},
|
|
86
|
+
})
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
This only affects agent runs, where the scorer receives the remembered messages. Runs scored from a plain prompt string are unchanged.
|
|
90
|
+
|
|
73
91
|
### Multi-Dimensional Analysis
|
|
74
92
|
|
|
75
93
|
Prompt Alignment evaluates responses across four key dimensions with weighted scoring that adapts based on the evaluation mode:
|
|
@@ -111,5 +111,5 @@ The `reason` summarizes the result and lists each criterion with its verdict, so
|
|
|
111
111
|
## Related
|
|
112
112
|
|
|
113
113
|
- [isTaskComplete on stream()](https://mastra.ai/reference/streaming/agents/stream)
|
|
114
|
-
- [Supervisor agents](https://mastra.ai/docs/
|
|
114
|
+
- [Supervisor agents](https://mastra.ai/docs/subagents)
|
|
115
115
|
- [createScorer](https://mastra.ai/reference/evals/create-scorer)
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# Summarization scorer
|
|
4
4
|
|
|
5
|
-
The `createSummarizationScorer()` function creates a scorer that evaluates a summary on two axes: whether every claim
|
|
5
|
+
The `createSummarizationScorer()` function creates a scorer that evaluates a summary on two axes: whether every claim has source-text support and whether the summary preserves the source information. The final score is the lower of the two. A summary can't pass by being faithful but empty or by being thorough but wrong.
|
|
6
6
|
|
|
7
7
|
The summary is the agent's last message that carries text, and the source text defaults to the first user message of the run input. Pass `source` or `sourceExtractor` when the text being summarized lives somewhere else, such as a tool result.
|
|
8
8
|
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Evals and scorers
|
|
4
|
+
|
|
5
|
+
The evaluation API has been consolidated on the new scorers system with updated naming conventions and configuration requirements.
|
|
6
|
+
|
|
7
|
+
## Changed
|
|
8
|
+
|
|
9
|
+
### `getScorers` to `listScorers`
|
|
10
|
+
|
|
11
|
+
The `getScorers()` method has been renamed to `listScorers()`. This change aligns with the naming convention used across the API where plural getter methods use the `list` prefix.
|
|
12
|
+
|
|
13
|
+
To migrate, replace all calls to `getScorers()` with `listScorers()`.
|
|
14
|
+
|
|
15
|
+
```diff
|
|
16
|
+
- const scorers = mastra.getScorers();
|
|
17
|
+
+ const scorers = mastra.listScorers();
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
> **Codemod:** You can use Mastra's codemod CLI to update your code automatically:
|
|
21
|
+
>
|
|
22
|
+
> ```bash
|
|
23
|
+
> npx @mastra/codemod@latest v1/mastra-plural-apis .
|
|
24
|
+
> ```
|
|
25
|
+
|
|
26
|
+
### `runExperiment` to `runEvals`
|
|
27
|
+
|
|
28
|
+
The `runExperiment()` function has been renamed to `runEvals()` to identify that it runs evaluations.
|
|
29
|
+
|
|
30
|
+
To migrate, update function calls from `runExperiment` to `runEvals`.
|
|
31
|
+
|
|
32
|
+
```diff
|
|
33
|
+
- import { createScorer, runExperiment } from '@mastra/core/evals';
|
|
34
|
+
+ import { createScorer, runEvals } from '@mastra/core/evals';
|
|
35
|
+
import { myAgent } from './agents/my-agent';
|
|
36
|
+
|
|
37
|
+
const scorer = createScorer({
|
|
38
|
+
id: 'helpfulness-scorer',
|
|
39
|
+
// ...
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
- const result = await runExperiment({ target: myAgent, scorers: [scorer], data: inputs });
|
|
43
|
+
+ const result = await runEvals({ target: myAgent, scorers: [scorer], data: inputs });
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
> **Codemod:** You can use Mastra's codemod CLI to update your code automatically:
|
|
47
|
+
>
|
|
48
|
+
> ```bash
|
|
49
|
+
> npx @mastra/codemod@latest v1/evals-run-experiment .
|
|
50
|
+
> ```
|
|
51
|
+
|
|
52
|
+
### `getScorerByName` to `getScorerById`
|
|
53
|
+
|
|
54
|
+
The `getScorerByName()` method has been renamed to `getScorerById()`. Scorers now require an `id` field instead of `name`. This change aligns with the broader API pattern of using `id` for entity identification.
|
|
55
|
+
|
|
56
|
+
To migrate, update method calls and scorer configuration to use `id` instead of `name`.
|
|
57
|
+
|
|
58
|
+
```diff
|
|
59
|
+
const scorer = createScorer({
|
|
60
|
+
- name: 'helpfulness-scorer',
|
|
61
|
+
+ id: 'helpfulness-scorer',
|
|
62
|
+
// ...
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
- const scorer = mastra.getScorerByName('helpfulness-scorer');
|
|
66
|
+
+ const scorer = mastra.getScorerById('helpfulness-scorer');
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
> **Codemod:** You can use Mastra's codemod CLI to update your code automatically:
|
|
70
|
+
>
|
|
71
|
+
> ```bash
|
|
72
|
+
> npx @mastra/codemod@latest v1/evals-scorer-by-name .
|
|
73
|
+
> ```
|
|
74
|
+
|
|
75
|
+
### Scorer configuration from `name` to `id`
|
|
76
|
+
|
|
77
|
+
Scorers now require an `id` field instead of `name`. The `name` field is now optional. The new field aligns scorers with other Mastra entities.
|
|
78
|
+
|
|
79
|
+
To migrate, update scorer definitions to use `id` as the required field.
|
|
80
|
+
|
|
81
|
+
```diff
|
|
82
|
+
const scorer = createScorer({
|
|
83
|
+
- name: 'helpfulness-scorer',
|
|
84
|
+
+ id: 'helpfulness-scorer',
|
|
85
|
+
+ name: 'Helpfulness Scorer', // optional
|
|
86
|
+
// ...
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Storage score APIs to `listScoresBy*` pattern
|
|
91
|
+
|
|
92
|
+
Score storage APIs have been renamed to follow the `listScoresBy*` pattern. This change aligns with the broader storage API naming conventions.
|
|
93
|
+
|
|
94
|
+
To migrate, update score query methods to use the new naming pattern.
|
|
95
|
+
|
|
96
|
+
```diff
|
|
97
|
+
- const scores = await storage.getScores({ scorerName: 'helpfulness-scorer' });
|
|
98
|
+
+ const scores = await storage.listScoresByScorerId({
|
|
99
|
+
+ scorerId: 'helpfulness-scorer',
|
|
100
|
+
+ });
|
|
101
|
+
|
|
102
|
+
// Also available:
|
|
103
|
+
// - listScoresByRunId
|
|
104
|
+
// - listScoresByEntityId
|
|
105
|
+
// - listScoresBySpan
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Prebuilt scorer imports to `scorers/prebuilt` path
|
|
109
|
+
|
|
110
|
+
Prebuilt scorer imports have been consolidated under a single `@mastra/evals/scorers/prebuilt` path instead of separate `scorers/llm` and `scorers/code` paths. This change simplifies imports and provides a clearer organization of prebuilt scorers.
|
|
111
|
+
|
|
112
|
+
To migrate, update import statements to use the new `scorers/prebuilt` path.
|
|
113
|
+
|
|
114
|
+
```diff
|
|
115
|
+
// LLM-based scorers
|
|
116
|
+
- import { createHallucinationScorer } from '@mastra/evals/scorers/llm';
|
|
117
|
+
- import { createFaithfulnessScorer } from '@mastra/evals/scorers/llm';
|
|
118
|
+
+ import { createHallucinationScorer } from '@mastra/evals/scorers/prebuilt';
|
|
119
|
+
+ import { createFaithfulnessScorer } from '@mastra/evals/scorers/prebuilt';
|
|
120
|
+
|
|
121
|
+
// Code-based scorers
|
|
122
|
+
- import { createContentSimilarityScorer } from '@mastra/evals/scorers/code';
|
|
123
|
+
- import { createCompletenessScorer } from '@mastra/evals/scorers/code';
|
|
124
|
+
+ import { createContentSimilarityScorer } from '@mastra/evals/scorers/prebuilt';
|
|
125
|
+
+ import { createCompletenessScorer } from '@mastra/evals/scorers/prebuilt';
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
> **Codemod:** You can use Mastra's codemod CLI to update your code automatically:
|
|
129
|
+
>
|
|
130
|
+
> ```bash
|
|
131
|
+
> npx @mastra/codemod@latest v1/evals-prebuilt-imports .
|
|
132
|
+
> ```
|
|
133
|
+
|
|
134
|
+
### Scorer message types from `UIMessage` to `MastraDBMessage`
|
|
135
|
+
|
|
136
|
+
Scorer input and output types now use `MastraDBMessage[]` instead of `UIMessage`. This change aligns scorers with the database-persisted message format for consistency across the framework.
|
|
137
|
+
|
|
138
|
+
To migrate, update scorer implementations to use `MastraDBMessage` types and access message content through the nested `content` structure.
|
|
139
|
+
|
|
140
|
+
```diff
|
|
141
|
+
import type {
|
|
142
|
+
ScorerRunInputForAgent,
|
|
143
|
+
ScorerRunOutputForAgent
|
|
144
|
+
} from '@mastra/core/evals';
|
|
145
|
+
|
|
146
|
+
- // ScorerRunInputForAgent uses UIMessage[]
|
|
147
|
+
- const inputMessages: UIMessage[] = run.input.inputMessages;
|
|
148
|
+
+ // ScorerRunInputForAgent now uses MastraDBMessage[]
|
|
149
|
+
+ import type { MastraDBMessage } from '@mastra/core/agent';
|
|
150
|
+
+ const inputMessages: MastraDBMessage[] = run.input.inputMessages;
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Message content structure to nested format
|
|
154
|
+
|
|
155
|
+
Tool invocations and text content are now accessed through a nested `content` object instead of flat message properties. The nested structure matches the database message format and its types.
|
|
156
|
+
|
|
157
|
+
To migrate, access tool invocations via `message.content.toolInvocations` and text via `message.content.content` or use the `getTextContentFromMastraDBMessage()` helper.
|
|
158
|
+
|
|
159
|
+
```diff
|
|
160
|
+
+ import { getTextContentFromMastraDBMessage } from '@mastra/evals';
|
|
161
|
+
+
|
|
162
|
+
const run = await scorer.run(testRun);
|
|
163
|
+
|
|
164
|
+
// Accessing text content
|
|
165
|
+
- const text = message.content;
|
|
166
|
+
+ const text = getTextContentFromMastraDBMessage(message);
|
|
167
|
+
+ // or directly: message.content.content
|
|
168
|
+
|
|
169
|
+
// Accessing tool invocations
|
|
170
|
+
- const toolCalls = message.toolInvocations;
|
|
171
|
+
+ const toolCalls = message.content.toolInvocations;
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Removed
|
|
175
|
+
|
|
176
|
+
### Legacy evals code
|
|
177
|
+
|
|
178
|
+
Legacy evals code has been removed from `@mastra/core`. This includes legacy evaluation metrics, scorer/judge modules, and hook-based automatic evaluation code. This change simplifies the codebase by removing outdated evaluation approaches.
|
|
179
|
+
|
|
180
|
+
To migrate, use the new evals/scorers API in `@mastra/core/evals` or `@mastra/evals`.
|
|
181
|
+
|
|
182
|
+
```diff
|
|
183
|
+
- // Legacy evals APIs
|
|
184
|
+
+ import { createScorer, runEvals } from '@mastra/core/evals';
|
|
185
|
+
+
|
|
186
|
+
+ const scorer = createScorer({
|
|
187
|
+
+ id: 'my-scorer',
|
|
188
|
+
+ // Use new scorer API
|
|
189
|
+
+ });
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Agent `TMetrics` generic parameter
|
|
193
|
+
|
|
194
|
+
The `TMetrics` generic parameter has been removed from `AgentConfig` and the `Agent` constructor. Metrics/scorers are now configured using the scorers API instead of being part of the Agent type system. This change simplifies the Agent type signature.
|
|
195
|
+
|
|
196
|
+
To migrate, remove the `TMetrics` generic parameter and configure scorers using the scorers API.
|
|
197
|
+
|
|
198
|
+
```diff
|
|
199
|
+
- const agent = new Agent<AgentId, Tools, Metrics>({
|
|
200
|
+
+ const agent = new Agent<AgentId, Tools>({
|
|
201
|
+
// ...
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Evals-related type exports
|
|
206
|
+
|
|
207
|
+
Several evals-related type exports have been removed including `DeprecatedOutputOptions`, `Metric`, and processor option types. These types are now internal or have been replaced by the new scorers API. This change reduces API surface area.
|
|
208
|
+
|
|
209
|
+
To migrate, remove references to these removed types and use the new scorers API.
|
|
210
|
+
|
|
211
|
+
```diff
|
|
212
|
+
- import type {
|
|
213
|
+
- DeprecatedOutputOptions,
|
|
214
|
+
- Metric,
|
|
215
|
+
- LanguageDetectorOptions,
|
|
216
|
+
- ModerationOptions,
|
|
217
|
+
- } from '@mastra/core';
|
|
218
|
+
+ // Use new scorers API types
|
|
219
|
+
+ import type { Scorer } from '@mastra/core/evals';
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### `createUIMessage` test helper
|
|
223
|
+
|
|
224
|
+
The `createUIMessage()` test helper has been removed and replaced with `createTestMessage()`. The new helper creates `MastraDBMessage` objects with the nested content structure and supports optional tool invocations. This change aligns test utilities with the new message format.
|
|
225
|
+
|
|
226
|
+
To migrate, replace `createUIMessage()` calls with `createTestMessage()` and update to use `MastraDBMessage` types.
|
|
227
|
+
|
|
228
|
+
```diff
|
|
229
|
+
- import { createUIMessage } from '@mastra/evals';
|
|
230
|
+
+ import { createTestMessage } from '@mastra/evals';
|
|
231
|
+
|
|
232
|
+
// Creating test messages
|
|
233
|
+
- const message = createUIMessage({
|
|
234
|
+
- id: 'test-1',
|
|
235
|
+
+ const message = createTestMessage({
|
|
236
|
+
+ id: 'test-1', // optional, defaults to 'test-message'
|
|
237
|
+
role: 'user',
|
|
238
|
+
content: 'Hello',
|
|
239
|
+
toolInvocations: [], // optional
|
|
240
|
+
});
|
|
241
|
+
```
|
|
@@ -3,6 +3,16 @@ import type { ScorerRunInputForLLMJudge, ScorerRunOutputForLLMJudge } from '../.
|
|
|
3
3
|
export interface PromptAlignmentOptions {
|
|
4
4
|
scale?: number;
|
|
5
5
|
evaluationMode?: 'user' | 'system' | 'both';
|
|
6
|
+
/**
|
|
7
|
+
* Include prior conversation turns from the agent's remembered messages in the judge prompt.
|
|
8
|
+
*
|
|
9
|
+
* Off by default. Enable it for multi-turn agents so short, referential replies (e.g. "A",
|
|
10
|
+
* "yes") are judged against what was previously asked instead of in isolation. Only applies
|
|
11
|
+
* to agent runs that carry `rememberedMessages`.
|
|
12
|
+
*/
|
|
13
|
+
includeConversationHistory?: boolean | {
|
|
14
|
+
maxMessages?: number;
|
|
15
|
+
};
|
|
6
16
|
}
|
|
7
17
|
export declare function createPromptAlignmentScorerLLM({ model, options, }: {
|
|
8
18
|
model: MastraModelConfig;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/scorers/llm/prompt-alignment/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/scorers/llm/prompt-alignment/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAS1D,OAAO,KAAK,EAAE,yBAAyB,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAGzF,MAAM,WAAW,sBAAsB;IACrC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,cAAc,CAAC,EAAE,MAAM,GAAG,QAAQ,GAAG,MAAM,CAAC;IAC5C;;;;;;OAMG;IACH,0BAA0B,CAAC,EAAE,OAAO,GAAG;QAAE,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACjE;AAgED,wBAAgB,8BAA8B,CAAC,EAC7C,KAAK,EACL,OAAO,GACR,EAAE;IACD,KAAK,EAAE,iBAAiB,CAAC;IACzB,OAAO,CAAC,EAAE,sBAAsB,CAAC;CAClC;;;;;;;;;;;;;;;;;;;;;;;;;;;6FAwHA"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export declare const PROMPT_ALIGNMENT_INSTRUCTIONS = "You are an expert prompt-response alignment evaluator. Your job is to analyze how well an agent's response aligns with the user's prompt in terms of intent, requirements, completeness, and appropriateness.\n\nKey Evaluation Dimensions:\n1. **Intent Alignment**: Does the response address the core purpose of the prompt?\n2. **Requirements Fulfillment**: Are all explicit and implicit requirements met?\n3. **Completeness**: Is the response comprehensive and thorough?\n4. **Response Appropriateness**: Does the format, tone, and style match expectations?\n\nEvaluation Guidelines:\n- Identify the primary intent and any secondary intents in the prompt\n- Extract all explicit requirements (specific tasks, constraints, formats)\n- Consider implicit requirements based on context and standard expectations\n- Assess whether the response fully addresses the prompt or leaves gaps\n- Evaluate if the response format and tone are appropriate for the request\n- Be objective and focus on alignment rather than response quality\n\nScore each dimension from 0.0 (completely misaligned) to 1.0 (perfectly aligned).";
|
|
2
|
-
export declare function createAnalyzePrompt({ userPrompt, systemPrompt, agentResponse, evaluationMode, }: {
|
|
2
|
+
export declare function createAnalyzePrompt({ userPrompt, systemPrompt, agentResponse, evaluationMode, conversationHistory, }: {
|
|
3
3
|
userPrompt: string;
|
|
4
4
|
systemPrompt?: string;
|
|
5
5
|
agentResponse: string;
|
|
6
6
|
evaluationMode: 'user' | 'system' | 'both';
|
|
7
|
+
conversationHistory?: string;
|
|
7
8
|
}): string;
|
|
8
9
|
export type AnalysisResult = {
|
|
9
10
|
intentAlignment: {
|
|
@@ -33,12 +34,13 @@ export type AnalysisResult = {
|
|
|
33
34
|
};
|
|
34
35
|
overallAssessment: string;
|
|
35
36
|
};
|
|
36
|
-
export declare function createReasonPrompt({ userPrompt, systemPrompt, score, scale, analysis, evaluationMode, }: {
|
|
37
|
+
export declare function createReasonPrompt({ userPrompt, systemPrompt, score, scale, analysis, evaluationMode, conversationHistory, }: {
|
|
37
38
|
userPrompt: string;
|
|
38
39
|
systemPrompt?: string;
|
|
39
40
|
score: number;
|
|
40
41
|
scale: number;
|
|
41
42
|
analysis: AnalysisResult;
|
|
42
43
|
evaluationMode: 'user' | 'system' | 'both';
|
|
44
|
+
conversationHistory?: string;
|
|
43
45
|
}): string;
|
|
44
46
|
//# sourceMappingURL=prompts.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompts.d.ts","sourceRoot":"","sources":["../../../../src/scorers/llm/prompt-alignment/prompts.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,6BAA6B,ulCAgBwC,CAAC;
|
|
1
|
+
{"version":3,"file":"prompts.d.ts","sourceRoot":"","sources":["../../../../src/scorers/llm/prompt-alignment/prompts.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,6BAA6B,ulCAgBwC,CAAC;AAgBnF,wBAAgB,mBAAmB,CAAC,EAClC,UAAU,EACV,YAAY,EACZ,aAAa,EACb,cAAc,EACd,mBAAmB,GACpB,EAAE;IACD,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,GAAG,QAAQ,GAAG,MAAM,CAAC;IAC3C,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B,UA8KA;AAED,MAAM,MAAM,cAAc,GAAG;IAC3B,eAAe,EAAE;QACf,KAAK,EAAE,MAAM,CAAC;QACd,aAAa,EAAE,MAAM,CAAC;QACtB,WAAW,EAAE,OAAO,CAAC;QACrB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;IACF,uBAAuB,EAAE;QACvB,YAAY,EAAE,KAAK,CAAC;YAClB,WAAW,EAAE,MAAM,CAAC;YACpB,WAAW,EAAE,OAAO,CAAC;YACrB,SAAS,EAAE,MAAM,CAAC;SACnB,CAAC,CAAC;QACH,YAAY,EAAE,MAAM,CAAC;KACtB,CAAC;IACF,YAAY,EAAE;QACZ,KAAK,EAAE,MAAM,CAAC;QACd,eAAe,EAAE,MAAM,EAAE,CAAC;QAC1B,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;IACF,uBAAuB,EAAE;QACvB,KAAK,EAAE,MAAM,CAAC;QACd,eAAe,EAAE,OAAO,CAAC;QACzB,aAAa,EAAE,OAAO,CAAC;QACvB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;IACF,iBAAiB,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF,wBAAgB,kBAAkB,CAAC,EACjC,UAAU,EACV,YAAY,EACZ,KAAK,EACL,KAAK,EACL,QAAQ,EACR,cAAc,EACd,mBAAmB,GACpB,EAAE;IACD,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,cAAc,CAAC;IACzB,cAAc,EAAE,MAAM,GAAG,QAAQ,GAAG,MAAM,CAAC;IAC3C,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B,UAmEA"}
|
|
@@ -2543,19 +2543,30 @@ Evaluation Guidelines:
|
|
|
2543
2543
|
- Be objective and focus on alignment rather than response quality
|
|
2544
2544
|
|
|
2545
2545
|
Score each dimension from 0.0 (completely misaligned) to 1.0 (perfectly aligned).`;
|
|
2546
|
-
function
|
|
2547
|
-
|
|
2546
|
+
function createConversationHistoryContext(conversationHistory) {
|
|
2547
|
+
if (!conversationHistory) return "";
|
|
2548
|
+
return `Conversation History (prior turns, for context only):
|
|
2549
|
+
${conversationHistory}
|
|
2550
|
+
|
|
2551
|
+
`;
|
|
2552
|
+
}
|
|
2553
|
+
const CONVERSATION_HISTORY_GUIDANCE = `
|
|
2554
|
+
Note on conversation history:
|
|
2555
|
+
- The conversation history is provided only to resolve the meaning of the current prompt. Use it to interpret short or referential prompts (e.g. "A", "yes", "the second one") against what was previously asked or offered.
|
|
2556
|
+
- Evaluate only the agent response to the current prompt. Do not score earlier turns, and do not credit or penalise the agent for anything it said in them.`;
|
|
2557
|
+
function createAnalyzePrompt$2({ userPrompt, systemPrompt, agentResponse, evaluationMode, conversationHistory }) {
|
|
2558
|
+
let promptContext = createConversationHistoryContext(conversationHistory);
|
|
2548
2559
|
let evaluationTarget = "";
|
|
2549
2560
|
if (evaluationMode === "user") {
|
|
2550
|
-
promptContext
|
|
2561
|
+
promptContext += `User Prompt:
|
|
2551
2562
|
${userPrompt}`;
|
|
2552
2563
|
evaluationTarget = "the user's prompt";
|
|
2553
2564
|
} else if (evaluationMode === "system") {
|
|
2554
|
-
promptContext
|
|
2565
|
+
promptContext += `System Prompt:
|
|
2555
2566
|
${systemPrompt}`;
|
|
2556
2567
|
evaluationTarget = "the system's behavioral guidelines and constraints";
|
|
2557
2568
|
} else {
|
|
2558
|
-
promptContext
|
|
2569
|
+
promptContext += `User Prompt:
|
|
2559
2570
|
${userPrompt}
|
|
2560
2571
|
|
|
2561
2572
|
System Prompt:
|
|
@@ -2568,6 +2579,7 @@ ${promptContext}
|
|
|
2568
2579
|
|
|
2569
2580
|
Agent Response:
|
|
2570
2581
|
${agentResponse}
|
|
2582
|
+
${conversationHistory ? CONVERSATION_HISTORY_GUIDANCE : ""}
|
|
2571
2583
|
|
|
2572
2584
|
Evaluate the following aspects:
|
|
2573
2585
|
|
|
@@ -2690,10 +2702,10 @@ Agent Response: "def factorial(n):
|
|
|
2690
2702
|
"overallAssessment": "The response perfectly aligns with the prompt, providing a correct Python factorial function with the requested error handling for negative numbers"
|
|
2691
2703
|
}`;
|
|
2692
2704
|
}
|
|
2693
|
-
function createReasonPrompt$1({ userPrompt, systemPrompt, score, scale, analysis, evaluationMode }) {
|
|
2705
|
+
function createReasonPrompt$1({ userPrompt, systemPrompt, score, scale, analysis, evaluationMode, conversationHistory }) {
|
|
2694
2706
|
const fulfilledCount = analysis.requirementsFulfillment.requirements.filter((r) => r.isFulfilled).length;
|
|
2695
2707
|
const totalRequirements = analysis.requirementsFulfillment.requirements.length;
|
|
2696
|
-
const promptContext = evaluationMode === "system" ? `System Prompt:\n${systemPrompt}` : evaluationMode === "user" ? `User Prompt:\n${userPrompt}` : `User Prompt:\n${userPrompt}\n\nSystem Prompt:\n${systemPrompt}
|
|
2708
|
+
const promptContext = createConversationHistoryContext(conversationHistory) + (evaluationMode === "system" ? `System Prompt:\n${systemPrompt}` : evaluationMode === "user" ? `User Prompt:\n${userPrompt}` : `User Prompt:\n${userPrompt}\n\nSystem Prompt:\n${systemPrompt}`);
|
|
2697
2709
|
return `Explain the prompt alignment score based on how well the agent's response addresses the ${evaluationMode === "system" ? "system behavioral guidelines and constraints" : evaluationMode === "user" ? "user's prompt" : "both user's prompt and system guidelines"}.
|
|
2698
2710
|
|
|
2699
2711
|
${promptContext}
|
|
@@ -2840,9 +2852,14 @@ const SCORING_WEIGHTS = {
|
|
|
2840
2852
|
SYSTEM_WEIGHT: .3
|
|
2841
2853
|
}
|
|
2842
2854
|
};
|
|
2855
|
+
function normalizeConversationHistoryOptions(includeConversationHistory) {
|
|
2856
|
+
if (!includeConversationHistory) return void 0;
|
|
2857
|
+
return includeConversationHistory === true ? {} : includeConversationHistory;
|
|
2858
|
+
}
|
|
2843
2859
|
function createPromptAlignmentScorerLLM({ model, options }) {
|
|
2844
2860
|
const scale = options?.scale || 1;
|
|
2845
2861
|
const evaluationMode = options?.evaluationMode || "both";
|
|
2862
|
+
const historyOptions = normalizeConversationHistoryOptions(options?.includeConversationHistory);
|
|
2846
2863
|
return (0, _mastra_core_evals.createScorer)({
|
|
2847
2864
|
id: "prompt-alignment-scorer",
|
|
2848
2865
|
name: "Prompt Alignment (LLM)",
|
|
@@ -2866,7 +2883,8 @@ function createPromptAlignmentScorerLLM({ model, options }) {
|
|
|
2866
2883
|
userPrompt,
|
|
2867
2884
|
systemPrompt,
|
|
2868
2885
|
agentResponse,
|
|
2869
|
-
evaluationMode
|
|
2886
|
+
evaluationMode,
|
|
2887
|
+
conversationHistory: historyOptions && require_scorers_utils.getConversationHistoryFromRunInput(run.input, historyOptions)
|
|
2870
2888
|
});
|
|
2871
2889
|
}
|
|
2872
2890
|
}).generateScore(({ results }) => {
|
|
@@ -2902,7 +2920,8 @@ function createPromptAlignmentScorerLLM({ model, options }) {
|
|
|
2902
2920
|
score,
|
|
2903
2921
|
scale,
|
|
2904
2922
|
analysis,
|
|
2905
|
-
evaluationMode
|
|
2923
|
+
evaluationMode,
|
|
2924
|
+
conversationHistory: historyOptions && require_scorers_utils.getConversationHistoryFromRunInput(run.input, historyOptions)
|
|
2906
2925
|
});
|
|
2907
2926
|
}
|
|
2908
2927
|
});
|