Evaluates whether the response is supported by the provided context.
import { createFaithfulnessScorer } from '@mastra/evals/scorers/prebuilt';const scorer = createFaithfulnessScorer({ model: 'gpt-4o-mini', options: { scale: 1, // Score range (default: 1 = 0-1) context: ['Paris is the capital of France'], // Optional: override context },});const result = await scorer.score({ answer: 'Paris is the capital of France.', context: ['Paris is the capital of France', 'France is in Europe'],});console.log(result);// {// value: 1.0,// reason: 'All claims are supported by the context'// }
Measures how relevant the answer is to the question.
import { createAnswerRelevancyScorer } from '@mastra/evals/scorers/prebuilt';const scorer = createAnswerRelevancyScorer({ model: 'gpt-4o-mini',});const result = await scorer.score({ answer: 'Paris is the capital of France.', question: 'What is the capital of France?',});console.log(result.value); // 0.95
Evaluates if the provided context is relevant to the question.
import { createContextRelevanceScorer } from '@mastra/evals/scorers/prebuilt';const scorer = createContextRelevanceScorer({ model: 'gpt-4o-mini',});const result = await scorer.score({ question: 'What is the capital of France?', context: ['Paris is the capital of France', 'France is in Europe'],});console.log(result.value); // 1.0 (both context items are relevant)
Checks if relevant context appears before irrelevant context.
import { createContextPrecisionScorer } from '@mastra/evals/scorers/prebuilt';const scorer = createContextPrecisionScorer({ model: 'gpt-4o-mini',});const result = await scorer.score({ question: 'What is the capital of France?', context: [ 'Paris is the capital of France', // Relevant - good! 'France is in Europe', // Less relevant ], expectedAnswer: 'Paris',});
import { createBiasScorer } from '@mastra/evals/scorers/prebuilt';const scorer = createBiasScorer({ model: 'gpt-4o-mini',});const result = await scorer.score({ answer: 'Engineers are naturally better at math than others.',});console.log(result.value); // Low score (bias detected)
Checks if the response follows the given instructions.
import { createPromptAlignmentScorer } from '@mastra/evals/scorers/prebuilt';const scorer = createPromptAlignmentScorer({ model: 'gpt-4o-mini',});const result = await scorer.score({ answer: 'Sure! Paris is the capital of France.', instructions: 'Answer in one word only',});console.log(result.value); // Low score (didn't follow instructions)
import { createKeywordCoverageScorer } from '@mastra/evals/scorers/prebuilt';const scorer = createKeywordCoverageScorer({ keywords: ['paris', 'france', 'capital'], caseSensitive: false,});const result = scorer.score({ output: 'Paris is the capital of France',});console.log(result.value); // 1.0 (all keywords present)
import { createCompletenessScorer } from '@mastra/evals/scorers/prebuilt';const scorer = createCompletenessScorer({ requiredElements: ['greeting', 'introduction', 'conclusion'],});const result = scorer.score({ output: 'Hello! I am an AI assistant. Goodbye!',});console.log(result.value); // Score based on element coverage