examples/basics/scorers/faithfulness/README.md
This example demonstrates how to use Mastra's Faithfulness Scorer to evaluate how accurately responses adhere to the provided context without introducing unsupported information.
Clone the repository and navigate to the project directory:
git clone https://github.com/mastra-ai/mastra
cd examples/basics/scorers/faithfulness
Copy the environment variables file and add your OpenAI API key:
cp .env.example .env
Then edit .env and add your OpenAI API key:
OPENAI_API_KEY=sk-your-api-key-here
Install dependencies:
pnpm install --ignore-workspace
Run the example:
pnpm start
The Faithfulness Scorer evaluates how accurately responses adhere to provided context without introducing unsupported claims. It analyzes:
The example includes three scenarios:
Each scenario demonstrates:
The example will output:
createFaithfulnessScorer: Function that creates the faithfulness scorer instancemodel: The language model to use for evaluation (e.g., OpenAI GPT-4)options.context: Array of context strings that serve as the source of truthoptions.scale: Optional scale factor for the final score (default: 1)scorer.run(): Method to evaluate input/output pairs for faithfulness
{ input, output } where:
input: Array of chat messages (e.g., [{ role: 'user', content: 'question' }])output: Response object (e.g., { role: 'assistant', text: 'response' }){ score, reason } with numerical score and detailed explanation