跳至主要內容

在 CI 中執行評分器

在 CI pipeline 中執行評分器,可提供量化指標,衡量 Agent 質素隨時間的變化。runEvals 函數會透過你的 Agent 或 Workflow 處理多個測試案例,並傳回彙總分數。

基本設定
基本設定 的直接連結

你可以使用任何支援 ESM 模組的測試框架,例如 VitestJestMocha

建立測試案例
建立測試案例 的直接連結

使用 runEvals,透過多個測試案例評估你的 Agent。此函數接受一個資料項目陣列,每個項目包含一個 input,以及供評分器驗證使用的可選 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:每個評分器在所有測試案例中的平均分數
  • 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)
})
})

後續步驟
後續步驟 的直接連結