> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # 在 CI 中執行評分器 在 CI pipeline 中執行評分器,可提供量化指標,衡量 Agent 質素隨時間的變化。`runEvals` 函數會透過你的 Agent 或 Workflow 處理多個測試案例,並傳回彙總分數。 ## 基本設定 你可以使用任何支援 ESM 模組的測試框架,例如 [Vitest](https://vitest.dev/)、[Jest](https://jestjs.io/) 或 [Mocha](https://mochajs.org/)。 ## 建立測試案例 使用 `runEvals`,透過多個測試案例評估你的 Agent。此函數接受一個資料項目陣列,每個項目包含一個 `input`,以及供評分器驗證使用的可選 `groundTruth`。 ```typescript 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`:已處理的測試案例總數 ```typescript { scores: { 'location-accuracy': 1.0, // Average score across all items 'another-scorer': 0.85 }, summary: { totalItems: 3 } } ``` ## 多個測試情境 為不同的評估情境建立獨立測試案例: ```typescript 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) }) }) ``` ## 後續步驟 - 了解如何[建立自訂評分器](https://mastra.zisheng.pro/zh-HK/docs/evals/custom-scorers) - 探索[內置評分器](https://mastra.zisheng.pro/zh-HK/docs/evals/built-in-scorers) - 針對[已啟用記憶功能的 Agent](https://mastra.zisheng.pro/zh-HK/docs/evals/evals-with-memory) 執行評分器 - 閱讀 [runEvals API 參考](https://mastra.zisheng.pro/zh-HK/reference/evals/run-evals)