@mastra/evals 0.0.0-commonjs-20250227130920
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/.turbo/turbo-build.log +37 -0
- package/CHANGELOG.md +777 -0
- package/LICENSE +44 -0
- package/README.md +185 -0
- package/dist/_tsup-dts-rollup.d.cts +764 -0
- package/dist/_tsup-dts-rollup.d.ts +764 -0
- package/dist/chunk-4VNS5WPM.js +37 -0
- package/dist/chunk-TXXJUIES.js +15 -0
- package/dist/dist-MSGH6PAG.js +17947 -0
- package/dist/index.cjs +19675 -0
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +86 -0
- package/dist/magic-string.es-5UDOWOAZ.js +1296 -0
- package/dist/metrics/judge/index.cjs +17 -0
- package/dist/metrics/judge/index.d.cts +1 -0
- package/dist/metrics/judge/index.d.ts +1 -0
- package/dist/metrics/judge/index.js +2 -0
- package/dist/metrics/llm/index.cjs +2482 -0
- package/dist/metrics/llm/index.d.cts +11 -0
- package/dist/metrics/llm/index.d.ts +11 -0
- package/dist/metrics/llm/index.js +2459 -0
- package/dist/metrics/nlp/index.cjs +202 -0
- package/dist/metrics/nlp/index.d.cts +5 -0
- package/dist/metrics/nlp/index.d.ts +5 -0
- package/dist/metrics/nlp/index.js +189 -0
- package/eslint.config.js +6 -0
- package/package.json +89 -0
- package/src/attachListeners.ts +40 -0
- package/src/constants.ts +1 -0
- package/src/evaluation.test.ts +29 -0
- package/src/evaluation.ts +58 -0
- package/src/index.ts +2 -0
- package/src/metrics/index.ts +3 -0
- package/src/metrics/judge/index.ts +14 -0
- package/src/metrics/llm/answer-relevancy/index.test.ts +156 -0
- package/src/metrics/llm/answer-relevancy/index.ts +58 -0
- package/src/metrics/llm/answer-relevancy/metricJudge.ts +56 -0
- package/src/metrics/llm/answer-relevancy/prompts.ts +214 -0
- package/src/metrics/llm/bias/index.test.ts +152 -0
- package/src/metrics/llm/bias/index.ts +52 -0
- package/src/metrics/llm/bias/metricJudge.ts +53 -0
- package/src/metrics/llm/bias/prompts.ts +109 -0
- package/src/metrics/llm/context-position/index.test.ts +275 -0
- package/src/metrics/llm/context-position/index.ts +69 -0
- package/src/metrics/llm/context-position/metricJudge.ts +55 -0
- package/src/metrics/llm/context-position/prompts.ts +135 -0
- package/src/metrics/llm/context-precision/index.test.ts +213 -0
- package/src/metrics/llm/context-precision/index.ts +68 -0
- package/src/metrics/llm/context-precision/metricJudge.ts +55 -0
- package/src/metrics/llm/context-precision/prompts.ts +139 -0
- package/src/metrics/llm/context-relevancy/index.test.ts +162 -0
- package/src/metrics/llm/context-relevancy/index.ts +59 -0
- package/src/metrics/llm/context-relevancy/metricJudge.ts +51 -0
- package/src/metrics/llm/context-relevancy/prompts.ts +111 -0
- package/src/metrics/llm/contextual-recall/index.test.ts +90 -0
- package/src/metrics/llm/contextual-recall/index.ts +56 -0
- package/src/metrics/llm/contextual-recall/metricJudge.ts +52 -0
- package/src/metrics/llm/contextual-recall/prompts.ts +82 -0
- package/src/metrics/llm/faithfulness/index.test.ts +254 -0
- package/src/metrics/llm/faithfulness/index.ts +59 -0
- package/src/metrics/llm/faithfulness/metricJudge.ts +62 -0
- package/src/metrics/llm/faithfulness/prompts.ts +166 -0
- package/src/metrics/llm/hallucination/index.test.ts +214 -0
- package/src/metrics/llm/hallucination/index.ts +59 -0
- package/src/metrics/llm/hallucination/metricJudge.ts +44 -0
- package/src/metrics/llm/hallucination/prompts.ts +143 -0
- package/src/metrics/llm/index.ts +11 -0
- package/src/metrics/llm/prompt-alignment/index.test.ts +335 -0
- package/src/metrics/llm/prompt-alignment/index.ts +116 -0
- package/src/metrics/llm/prompt-alignment/metricJudge.ts +43 -0
- package/src/metrics/llm/prompt-alignment/prompts.ts +224 -0
- package/src/metrics/llm/summarization/index.test.ts +274 -0
- package/src/metrics/llm/summarization/index.ts +72 -0
- package/src/metrics/llm/summarization/metricJudge.ts +101 -0
- package/src/metrics/llm/summarization/prompts.ts +252 -0
- package/src/metrics/llm/toxicity/index.test.ts +84 -0
- package/src/metrics/llm/toxicity/index.ts +54 -0
- package/src/metrics/llm/toxicity/metricJudge.ts +39 -0
- package/src/metrics/llm/toxicity/prompts.ts +87 -0
- package/src/metrics/llm/types.ts +7 -0
- package/src/metrics/llm/utils.ts +20 -0
- package/src/metrics/nlp/completeness/index.test.ts +98 -0
- package/src/metrics/nlp/completeness/index.ts +121 -0
- package/src/metrics/nlp/content-similarity/index.test.ts +76 -0
- package/src/metrics/nlp/content-similarity/index.ts +49 -0
- package/src/metrics/nlp/index.ts +5 -0
- package/src/metrics/nlp/keyword-coverage/index.test.ts +85 -0
- package/src/metrics/nlp/keyword-coverage/index.ts +49 -0
- package/src/metrics/nlp/textual-difference/index.test.ts +88 -0
- package/src/metrics/nlp/textual-difference/index.ts +38 -0
- package/src/metrics/nlp/tone/index.test.ts +100 -0
- package/src/metrics/nlp/tone/index.ts +55 -0
- package/tsconfig.json +5 -0
- package/vitest.config.ts +12 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { openai } from '@ai-sdk/openai';
|
|
2
|
+
import { describe, it, expect } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import type { TestCaseWithContext } from '../utils';
|
|
5
|
+
|
|
6
|
+
import { FaithfulnessMetric } from './index';
|
|
7
|
+
|
|
8
|
+
const testCases: TestCaseWithContext[] = [
|
|
9
|
+
{
|
|
10
|
+
// Perfect faithfulness
|
|
11
|
+
input: 'What can you tell me about the company?',
|
|
12
|
+
context: [
|
|
13
|
+
'The company was founded in 1995 by John Smith.',
|
|
14
|
+
'It has 500 employees and is headquartered in London.',
|
|
15
|
+
'The company specializes in software development.',
|
|
16
|
+
],
|
|
17
|
+
output:
|
|
18
|
+
'The company was founded in 1995 by John Smith and has 500 employees. Their headquarters is in London, and they focus on software development.',
|
|
19
|
+
expectedResult: {
|
|
20
|
+
score: 1.0,
|
|
21
|
+
reason: 'All claims made in the output are directly supported by the context, showing perfect faithfulness',
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
// Mixed faithfulness with contradictions
|
|
26
|
+
input: 'What can you tell me about the company?',
|
|
27
|
+
context: [
|
|
28
|
+
'The company was founded in 1995 by John Smith.',
|
|
29
|
+
'It has 500 employees and is headquartered in London.',
|
|
30
|
+
'The company specializes in software development.',
|
|
31
|
+
],
|
|
32
|
+
output:
|
|
33
|
+
'The company was founded in 1995 by John Smith but has 1000 employees. Their headquarters is in Paris, and they focus on software development.',
|
|
34
|
+
expectedResult: {
|
|
35
|
+
score: 0.5,
|
|
36
|
+
reason:
|
|
37
|
+
'Half of the claims (founding date and specialization) are supported by the context, while two claims (employee count and headquarters location) directly contradict it',
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
// mixed claims with speculative language
|
|
42
|
+
input: 'What can you tell me about the company?',
|
|
43
|
+
context: ['The company was founded in 1995 by John Smith.', 'It has 500 employees and is headquartered in London.'],
|
|
44
|
+
output: 'The company was founded in 1995 and may have international offices. They might be expanding to Asia soon.',
|
|
45
|
+
expectedResult: {
|
|
46
|
+
score: 0.33,
|
|
47
|
+
reason:
|
|
48
|
+
'Only the founding date claim is supported by context. The other claims are marked as unsure due to speculative language',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
// empty output
|
|
53
|
+
input: 'What can you tell me about the company?',
|
|
54
|
+
context: ['The company was founded in 1995 by John Smith.', 'It has 500 employees and is headquartered in London.'],
|
|
55
|
+
output: '',
|
|
56
|
+
expectedResult: {
|
|
57
|
+
score: 0,
|
|
58
|
+
reason: 'The output is empty',
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
// empty context
|
|
63
|
+
input: 'What can you tell me about the company?',
|
|
64
|
+
context: [],
|
|
65
|
+
output: 'The company was founded in 1995.',
|
|
66
|
+
expectedResult: {
|
|
67
|
+
score: 0,
|
|
68
|
+
reason: 'No context provided for verification',
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
// subjective claims
|
|
73
|
+
input: 'What can you tell me about the company?',
|
|
74
|
+
context: ['The company was founded in 1995 by John Smith.', 'It has 500 employees and is headquartered in London.'],
|
|
75
|
+
output: 'The company has a great work culture and amazing benefits. Their employees seem very happy.',
|
|
76
|
+
expectedResult: {
|
|
77
|
+
score: 0,
|
|
78
|
+
reason: 'All claims are marked as unsure as they cannot be verified from the context',
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
// claims with speculative language
|
|
83
|
+
input: 'What can you tell me about the company?',
|
|
84
|
+
context: ['The company was founded in 1995 by John Smith.', 'It has 500 employees and is headquartered in London.'],
|
|
85
|
+
output: 'Founded in 1995, the company might be planning to expand. They possibly have offices in other cities.',
|
|
86
|
+
expectedResult: {
|
|
87
|
+
score: 0.33,
|
|
88
|
+
reason:
|
|
89
|
+
'Only the founding date is supported by the context, while other claims use speculative language (might, possibly) and are marked as unsure',
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
// compound statements
|
|
94
|
+
input: 'What can you tell me about the company?',
|
|
95
|
+
context: ['The company was founded in 1995.', 'John Smith is the CEO.', 'The headquarters has 500 employees.'],
|
|
96
|
+
output:
|
|
97
|
+
'The company was founded in 1995 and has 500 employees at headquarters. John Smith is the CEO and runs global operations.',
|
|
98
|
+
expectedResult: {
|
|
99
|
+
score: 0.75,
|
|
100
|
+
reason:
|
|
101
|
+
'Three claims (founding year, employee count, CEO role) are supported by context, while the global operations claim is not supported',
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
// precise numerical claims
|
|
106
|
+
input: 'How many employees work there?',
|
|
107
|
+
context: ['The company has approximately 500 employees as of 2023.'],
|
|
108
|
+
output: 'The company employs 498 people.',
|
|
109
|
+
expectedResult: {
|
|
110
|
+
score: 0,
|
|
111
|
+
reason:
|
|
112
|
+
'The claim about employee count is marked as unsure rather than contradictory, as the context provides an approximate number',
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
// partially supported claims
|
|
117
|
+
input: 'Describe the company location.',
|
|
118
|
+
context: ['The company headquarters is in London.'],
|
|
119
|
+
output: 'The company headquarters is in London, in the financial district.',
|
|
120
|
+
expectedResult: {
|
|
121
|
+
score: 0.5,
|
|
122
|
+
reason: 'The London location is supported, but the specific district detail is not mentioned in context',
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
// mixed factual and speculative claims
|
|
127
|
+
input: 'Tell me about the company growth.',
|
|
128
|
+
context: ['The company has grown from 100 to 500 employees since 2020.'],
|
|
129
|
+
output: 'The company has grown to 500 employees and will likely continue expanding rapidly.',
|
|
130
|
+
expectedResult: {
|
|
131
|
+
score: 0.5,
|
|
132
|
+
reason:
|
|
133
|
+
'The current employee count is explicitly supported by context. The future expansion claim is marked as unsure due to speculative language',
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
// implicit information
|
|
138
|
+
input: 'Who runs the company?',
|
|
139
|
+
context: ['John Smith is the CEO of the company since 2020.'],
|
|
140
|
+
output: 'John Smith has been leading the company for several years.',
|
|
141
|
+
expectedResult: {
|
|
142
|
+
score: 1.0,
|
|
143
|
+
reason: 'The leadership claim is supported by the context, even though the exact duration is phrased differently',
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
];
|
|
147
|
+
|
|
148
|
+
const SECONDS = 10000;
|
|
149
|
+
|
|
150
|
+
const model = openai('gpt-4o');
|
|
151
|
+
|
|
152
|
+
describe(
|
|
153
|
+
'FaithfulnessMetric',
|
|
154
|
+
() => {
|
|
155
|
+
it('should handle perfect faithfulness', async () => {
|
|
156
|
+
const testCase = testCases[0]!;
|
|
157
|
+
const metric = new FaithfulnessMetric(model, { context: testCase.context });
|
|
158
|
+
const result = await metric.measure(testCase.input, testCase.output);
|
|
159
|
+
|
|
160
|
+
expect(result.score).toBeCloseTo(testCase.expectedResult.score, 1);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('should handle mixed faithfulness with contradictions', async () => {
|
|
164
|
+
const testCase = testCases[1]!;
|
|
165
|
+
const metric = new FaithfulnessMetric(model, { context: testCase.context });
|
|
166
|
+
const result = await metric.measure(testCase.input, testCase.output);
|
|
167
|
+
|
|
168
|
+
expect(result.score).toBeCloseTo(testCase.expectedResult.score, 1);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('should handle claims with speculative language', async () => {
|
|
172
|
+
const testCase = testCases[2]!;
|
|
173
|
+
const metric = new FaithfulnessMetric(model, { context: testCase.context });
|
|
174
|
+
const result = await metric.measure(testCase.input, testCase.output);
|
|
175
|
+
|
|
176
|
+
expect(result.score).toBeCloseTo(testCase.expectedResult.score, 1);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('should handle empty output', async () => {
|
|
180
|
+
const testCase = testCases[3]!;
|
|
181
|
+
const metric = new FaithfulnessMetric(model, { context: testCase.context });
|
|
182
|
+
const result = await metric.measure(testCase.input, testCase.output);
|
|
183
|
+
|
|
184
|
+
expect(result.score).toBe(testCase.expectedResult.score);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('should handle empty context', async () => {
|
|
188
|
+
const testCase = testCases[4]!;
|
|
189
|
+
const metric = new FaithfulnessMetric(model, { context: testCase.context });
|
|
190
|
+
const result = await metric.measure(testCase.input, testCase.output);
|
|
191
|
+
|
|
192
|
+
expect(result.score).toBe(testCase.expectedResult.score);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('should handle subjective claims', async () => {
|
|
196
|
+
const testCase = testCases[5]!;
|
|
197
|
+
const metric = new FaithfulnessMetric(model, { context: testCase.context });
|
|
198
|
+
const result = await metric.measure(testCase.input, testCase.output);
|
|
199
|
+
|
|
200
|
+
expect(result.score).toBe(testCase.expectedResult.score);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('should handle claims with speculative language appropriately', async () => {
|
|
204
|
+
const testCase = testCases[6]!;
|
|
205
|
+
const metric = new FaithfulnessMetric(model, { context: testCase.context });
|
|
206
|
+
const result = await metric.measure(testCase.input, testCase.output);
|
|
207
|
+
|
|
208
|
+
expect(result.score).toBeCloseTo(testCase.expectedResult.score, 1);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('should handle compound statements correctly', async () => {
|
|
212
|
+
const testCase = testCases[7]!;
|
|
213
|
+
const metric = new FaithfulnessMetric(model, { context: testCase.context });
|
|
214
|
+
const result = await metric.measure(testCase.input, testCase.output);
|
|
215
|
+
|
|
216
|
+
expect(result.score).toBeCloseTo(testCase.expectedResult.score, 1);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('should handle precise numerical claims', async () => {
|
|
220
|
+
const testCase = testCases[8]!;
|
|
221
|
+
const metric = new FaithfulnessMetric(model, { context: testCase.context });
|
|
222
|
+
const result = await metric.measure(testCase.input, testCase.output);
|
|
223
|
+
|
|
224
|
+
expect(result.score).toBe(testCase.expectedResult.score);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it('should handle partially supported claims', async () => {
|
|
228
|
+
const testCase = testCases[9]!;
|
|
229
|
+
const metric = new FaithfulnessMetric(model, { context: testCase.context });
|
|
230
|
+
const result = await metric.measure(testCase.input, testCase.output);
|
|
231
|
+
|
|
232
|
+
expect(result.score).toBeCloseTo(testCase.expectedResult.score, 1);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('should handle mixed factual and speculative claims', async () => {
|
|
236
|
+
const testCase = testCases[10]!;
|
|
237
|
+
const metric = new FaithfulnessMetric(model, { context: testCase.context });
|
|
238
|
+
const result = await metric.measure(testCase.input, testCase.output);
|
|
239
|
+
|
|
240
|
+
expect(result.score).toBeCloseTo(testCase.expectedResult.score, 1);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('should handle implicit information appropriately', async () => {
|
|
244
|
+
const testCase = testCases[11]!;
|
|
245
|
+
const metric = new FaithfulnessMetric(model, { context: testCase.context });
|
|
246
|
+
const result = await metric.measure(testCase.input, testCase.output);
|
|
247
|
+
|
|
248
|
+
expect(result.score).toBeCloseTo(testCase.expectedResult.score, 1);
|
|
249
|
+
});
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
timeout: 15 * SECONDS,
|
|
253
|
+
},
|
|
254
|
+
);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Metric } from '@mastra/core/eval';
|
|
2
|
+
import type { LanguageModel } from '@mastra/core/llm';
|
|
3
|
+
|
|
4
|
+
import type { MetricResultWithReason } from '../types';
|
|
5
|
+
import { roundToTwoDecimals } from '../utils';
|
|
6
|
+
|
|
7
|
+
import { FaithfulnessJudge } from './metricJudge';
|
|
8
|
+
|
|
9
|
+
export interface FaithfulnessMetricOptions {
|
|
10
|
+
scale?: number;
|
|
11
|
+
context: string[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class FaithfulnessMetric extends Metric {
|
|
15
|
+
private judge: FaithfulnessJudge;
|
|
16
|
+
private scale: number;
|
|
17
|
+
private context: string[];
|
|
18
|
+
|
|
19
|
+
constructor(model: LanguageModel, { scale = 1, context }: FaithfulnessMetricOptions) {
|
|
20
|
+
super();
|
|
21
|
+
|
|
22
|
+
this.context = context;
|
|
23
|
+
this.judge = new FaithfulnessJudge(model);
|
|
24
|
+
this.scale = scale;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async measure(input: string, output: string): Promise<MetricResultWithReason> {
|
|
28
|
+
const verdicts = await this.judge.evaluate(output, this.context);
|
|
29
|
+
const score = this.calculateScore(verdicts);
|
|
30
|
+
const reason = await this.judge.getReason({
|
|
31
|
+
input,
|
|
32
|
+
output,
|
|
33
|
+
context: this.context,
|
|
34
|
+
score,
|
|
35
|
+
scale: this.scale,
|
|
36
|
+
verdicts,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
score,
|
|
41
|
+
info: {
|
|
42
|
+
reason,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private calculateScore(verdicts: Array<{ verdict: string; reason: string }>): number {
|
|
48
|
+
const totalClaims = verdicts.length;
|
|
49
|
+
const supportedClaims = verdicts.filter(v => v.verdict === 'yes').length;
|
|
50
|
+
|
|
51
|
+
if (totalClaims === 0) {
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const score = (supportedClaims / totalClaims) * this.scale;
|
|
56
|
+
|
|
57
|
+
return roundToTwoDecimals(score);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { LanguageModel } from '@mastra/core/llm';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
import { MastraAgentJudge } from '../../judge';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
generateClaimExtractionPrompt,
|
|
8
|
+
generateEvaluatePrompt,
|
|
9
|
+
FAITHFULNESS_AGENT_INSTRUCTIONS,
|
|
10
|
+
generateReasonPrompt,
|
|
11
|
+
} from './prompts';
|
|
12
|
+
|
|
13
|
+
export class FaithfulnessJudge extends MastraAgentJudge {
|
|
14
|
+
constructor(model: LanguageModel) {
|
|
15
|
+
super('Faithfulness', FAITHFULNESS_AGENT_INSTRUCTIONS, model);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async evaluate(output: string, context: string[]): Promise<{ claim: string; verdict: string; reason: string }[]> {
|
|
19
|
+
const claimsPrompt = generateClaimExtractionPrompt({ output });
|
|
20
|
+
const claims = await this.agent.generate(claimsPrompt, {
|
|
21
|
+
output: z.object({
|
|
22
|
+
claims: z.array(z.string()),
|
|
23
|
+
}),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
if (claims.object.claims.length === 0) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const evaluatePrompt = generateEvaluatePrompt({ claims: claims.object.claims, context });
|
|
31
|
+
const result = await this.agent.generate(evaluatePrompt, {
|
|
32
|
+
output: z.object({
|
|
33
|
+
verdicts: z.array(
|
|
34
|
+
z.object({
|
|
35
|
+
claim: z.string(),
|
|
36
|
+
verdict: z.string(),
|
|
37
|
+
reason: z.string(),
|
|
38
|
+
}),
|
|
39
|
+
),
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return result.object.verdicts;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async getReason(args: {
|
|
47
|
+
input: string;
|
|
48
|
+
output: string;
|
|
49
|
+
context: string[];
|
|
50
|
+
score: number;
|
|
51
|
+
scale: number;
|
|
52
|
+
verdicts: { verdict: string; reason: string }[];
|
|
53
|
+
}): Promise<string> {
|
|
54
|
+
const prompt = generateReasonPrompt(args);
|
|
55
|
+
const result = await this.agent.generate(prompt, {
|
|
56
|
+
output: z.object({
|
|
57
|
+
reason: z.string(),
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
60
|
+
return result.object.reason;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
export const FAITHFULNESS_AGENT_INSTRUCTIONS = `You are a precise and thorough faithfulness evaluator. Your job is to determine if LLM outputs are factually consistent with the provided context, focusing on claim verification.
|
|
2
|
+
|
|
3
|
+
Key Principles:
|
|
4
|
+
1. First extract all claims from the output (both factual and speculative)
|
|
5
|
+
2. Then verify each extracted claim against the provided context
|
|
6
|
+
3. Consider a claim truthful if it is explicitly supported by the context
|
|
7
|
+
4. Consider a claim contradictory if it directly conflicts with the context
|
|
8
|
+
5. Consider a claim unsure if it is not mentioned in the context
|
|
9
|
+
6. Empty outputs should be handled as having no claims
|
|
10
|
+
7. Focus on factual consistency, not relevance or completeness
|
|
11
|
+
8. Never use prior knowledge in judgments
|
|
12
|
+
9. Claims with speculative language (may, might, possibly) should be marked as "unsure"`;
|
|
13
|
+
|
|
14
|
+
export function generateClaimExtractionPrompt({ output }: { output: string }) {
|
|
15
|
+
return `Extract all claims from the given output. A claim is any statement that asserts information, including both factual and speculative assertions.
|
|
16
|
+
|
|
17
|
+
Guidelines for claim extraction:
|
|
18
|
+
- Break down compound statements into individual claims
|
|
19
|
+
- Include all statements that assert information
|
|
20
|
+
- Include both definitive and speculative claims (using words like may, might, could)
|
|
21
|
+
- Extract specific details like numbers, dates, and quantities
|
|
22
|
+
- Keep relationships between entities
|
|
23
|
+
- Include predictions and possibilities
|
|
24
|
+
- Extract claims with their full context
|
|
25
|
+
- Exclude only questions and commands
|
|
26
|
+
|
|
27
|
+
Example:
|
|
28
|
+
Text: "The Tesla Model S was launched in 2012 and has a range of 405 miles. The car can accelerate from 0 to 60 mph in 1.99 seconds. I think it might be the best electric car ever made and could receive major updates next year."
|
|
29
|
+
|
|
30
|
+
{
|
|
31
|
+
"claims": [
|
|
32
|
+
"The Tesla Model S was launched in 2012",
|
|
33
|
+
"The Tesla Model S has a range of 405 miles",
|
|
34
|
+
"The Tesla Model S can accelerate from 0 to 60 mph in 1.99 seconds",
|
|
35
|
+
"The Tesla Model S might be the best electric car ever made",
|
|
36
|
+
"The Tesla Model S could receive major updates next year"
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
Note: All assertions are included, even speculative ones, as they need to be verified against the context.
|
|
40
|
+
|
|
41
|
+
Please return only JSON format with "claims" array.
|
|
42
|
+
Return empty list for empty input.
|
|
43
|
+
|
|
44
|
+
Text:
|
|
45
|
+
${output}
|
|
46
|
+
|
|
47
|
+
JSON:
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function generateEvaluatePrompt({ claims, context }: { claims: string[]; context: string[] }) {
|
|
52
|
+
return `Verify each claim against the provided context. Determine if each claim is supported by, contradicts, or is not mentioned in the context.
|
|
53
|
+
|
|
54
|
+
Context:
|
|
55
|
+
${context.join('\n')}
|
|
56
|
+
|
|
57
|
+
Number of claims: ${claims.length}
|
|
58
|
+
|
|
59
|
+
Claims to verify:
|
|
60
|
+
${claims.join('\n')}
|
|
61
|
+
|
|
62
|
+
For each claim, provide a verdict and reasoning. The verdict must be one of:
|
|
63
|
+
- "yes" if the claim is supported by the context
|
|
64
|
+
- "no" if the claim directly contradicts the context
|
|
65
|
+
- "unsure" if the claim is not mentioned in the context or cannot be verified
|
|
66
|
+
|
|
67
|
+
The number of verdicts MUST MATCH the number of claims exactly.
|
|
68
|
+
|
|
69
|
+
Format:
|
|
70
|
+
{
|
|
71
|
+
"verdicts": [
|
|
72
|
+
{
|
|
73
|
+
"claim": "claim text",
|
|
74
|
+
"verdict": "yes/no/unsure",
|
|
75
|
+
"reason": "explanation of verification"
|
|
76
|
+
}
|
|
77
|
+
]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
Rules:
|
|
81
|
+
- Only use information from the provided context
|
|
82
|
+
- Mark claims as "no" ONLY if they directly contradict the context
|
|
83
|
+
- Mark claims as "yes" if they are explicitly supported by the context
|
|
84
|
+
- Mark claims as "unsure" if they are not mentioned in the context
|
|
85
|
+
- Claims with speculative language (may, might, possibly) should be marked as "unsure"
|
|
86
|
+
- Never use prior knowledge in your judgment
|
|
87
|
+
- Provide clear reasoning for each verdict
|
|
88
|
+
- Be specific about where in the context the claim is supported or contradicted
|
|
89
|
+
|
|
90
|
+
Example:
|
|
91
|
+
Context: "The Tesla Model S was launched in 2012. The car has a maximum range of 375 miles and comes with advanced autopilot features."
|
|
92
|
+
Claims: ["The Tesla Model S was launched in 2012", "The Tesla Model S has a range of 405 miles", "The car might get software updates"]
|
|
93
|
+
{
|
|
94
|
+
"verdicts": [
|
|
95
|
+
{
|
|
96
|
+
"claim": "The Tesla Model S was launched in 2012",
|
|
97
|
+
"verdict": "yes",
|
|
98
|
+
"reason": "This is explicitly stated in the context"
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
"claim": "The Tesla Model S has a range of 405 miles",
|
|
102
|
+
"verdict": "no",
|
|
103
|
+
"reason": "The context states the maximum range is 375 miles, contradicting the claim of 405 miles"
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
"claim": "The car might get software updates",
|
|
107
|
+
"verdict": "unsure",
|
|
108
|
+
"reason": "This is speculative and not mentioned in the context"
|
|
109
|
+
}
|
|
110
|
+
]
|
|
111
|
+
}`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function generateReasonPrompt({
|
|
115
|
+
input,
|
|
116
|
+
output,
|
|
117
|
+
context,
|
|
118
|
+
score,
|
|
119
|
+
scale,
|
|
120
|
+
verdicts,
|
|
121
|
+
}: {
|
|
122
|
+
input: string;
|
|
123
|
+
output: string;
|
|
124
|
+
context: string[];
|
|
125
|
+
score: number;
|
|
126
|
+
scale: number;
|
|
127
|
+
verdicts: { verdict: string; reason: string }[];
|
|
128
|
+
}) {
|
|
129
|
+
return `Explain the faithfulness score 0 is the lowest and ${scale} is the highest for the LLM's response using this context:
|
|
130
|
+
|
|
131
|
+
Context:
|
|
132
|
+
${context.join('\n')}
|
|
133
|
+
|
|
134
|
+
Input:
|
|
135
|
+
${input}
|
|
136
|
+
|
|
137
|
+
Output:
|
|
138
|
+
${output}
|
|
139
|
+
|
|
140
|
+
Score: ${score}
|
|
141
|
+
Verdicts:
|
|
142
|
+
${JSON.stringify(verdicts)}
|
|
143
|
+
|
|
144
|
+
Rules:
|
|
145
|
+
- Explain score based on ratio of supported claims ("yes" verdicts) to total claims
|
|
146
|
+
- Focus on factual consistency with context
|
|
147
|
+
- Keep explanation concise and focused
|
|
148
|
+
- Use given score, don't recalculate
|
|
149
|
+
- Explain both supported and contradicted aspects
|
|
150
|
+
- For mixed cases, explain the balance
|
|
151
|
+
- If no contradictions, use a positive but professional tone
|
|
152
|
+
- Base explanation only on the verified claims, not prior knowledge
|
|
153
|
+
|
|
154
|
+
Format:
|
|
155
|
+
{
|
|
156
|
+
"reason": "The score is {score} because {explanation of faithfulness}"
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
Example Responses:
|
|
160
|
+
{
|
|
161
|
+
"reason": "The score is 1.0 because all claims made in the output are supported by the provided context"
|
|
162
|
+
}
|
|
163
|
+
{
|
|
164
|
+
"reason": "The score is 0.5 because while half of the claims are supported by the context, the remaining claims either contradict the context or cannot be verified"
|
|
165
|
+
}`;
|
|
166
|
+
}
|