CI で Scorer を実行する
CI パイプラインで Scorer を実行すると、時間の経過に伴う Agent の品質を測定するための定量的な指標を得られます。runEvals 関数は、Agent または Workflow を通じて複数のテストケースを処理し、集計スコアを返します。
基本設定基本設定への直接リンク
Vitest、Jest、Mocha など、ESM モジュールをサポートする任意のテストフレームワークを使用できます。
テストケースの作成テストケースの作成への直接リンク
runEvals を使用して、複数のテストケースに対して Agent を評価します。この関数はデータ項目の配列を受け取ります。各項目には input と、Scorer の検証に使用する任意の groundTruth が含まれます。
src/mastra/agents/weather-agent.test.ts
import { describe, it, expect } from 'vitest'
import { createScorer, runEvals } from '@mastra/core/evals'
import { weatherAgent } from './weather-agent'
import { locationScorer } from '../scorers/location-scorer'
describe('Weather Agent Tests', () => {
it('should correctly extract locations from queries', async () => {
const result = await runEvals({
data: [
{
input: 'weather in Berlin',
groundTruth: { expectedLocation: 'Berlin', expectedCountry: 'DE' },
},
{
input: 'weather in Berlin, Maryland',
groundTruth: { expectedLocation: 'Berlin', expectedCountry: 'US' },
},
{
input: 'weather in Berlin, Russia',
groundTruth: { expectedLocation: 'Berlin', expectedCountry: 'RU' },
},
],
target: weatherAgent,
scorers: [locationScorer],
})
// Assert aggregate score meets threshold
expect(result.scores['location-accuracy']).toBe(1)
expect(result.summary.totalItems).toBe(3)
})
})
結果を理解する結果を理解するへの直接リンク
runEvals 関数は、次の内容を持つオブジェクトを返します。
scores: すべてのテストケースにおける各 Scorer の平均スコアsummary.totalItems: 処理されたテストケースの総数
{
scores: {
'location-accuracy': 1.0, // Average score across all items
'another-scorer': 0.85
},
summary: {
totalItems: 3
}
}
複数のテストシナリオ複数のテストシナリオへの直接リンク
評価シナリオごとに個別のテストケースを作成します。
src/mastra/agents/weather-agent.test.ts
describe('Weather Agent Tests', () => {
const locationScorer = createScorer({/* ... */})
it('should handle location disambiguation', async () => {
const result = await runEvals({
data: [
{
input: 'weather in Berlin',
groundTruth: {/* ... */},
},
{
input: 'weather in Berlin, Maryland',
groundTruth: {/* ... */},
},
],
target: weatherAgent,
scorers: [locationScorer],
})
expect(result.scores['location-accuracy']).toBe(1)
})
it('should handle typos and misspellings', async () => {
const result = await runEvals({
data: [
{
input: 'weather in Berln',
groundTruth: { expectedLocation: 'Berlin', expectedCountry: 'DE' },
},
{
input: 'weather in Parris',
groundTruth: { expectedLocation: 'Paris', expectedCountry: 'FR' },
},
],
target: weatherAgent,
scorers: [locationScorer],
})
expect(result.scores['location-accuracy']).toBe(1)
})
})
次のステップ次のステップへの直接リンク
- カスタム Scorer の作成について学ぶ
- 組み込み Scorerを確認する
- Memory が有効な Agentに対して Scorer を実行する
- runEvals API リファレンスを読む