> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # 答案相似度評分器 `createAnswerSimilarityScorer()` 函式會建立一個評分器,用來評估 Agent 輸出與標準答案的相似程度。此評分器專為 CI/CD 測試情境設計,適合已有預期答案,並希望確保結果長期一致的情況。 ## 參數 **model** (`LanguageModel`): 用於評估輸出與標準答案之間語意相似度的語言模型。 **options** (`AnswerSimilarityOptions`): 評分器的設定選項。 **options.requireGroundTruth** (`boolean`): 是否要求提供標準答案以進行評估。若為 false,缺少標準答案時會傳回 0 分。 **options.semanticThreshold** (`number`): 語意相符相對於完全相符的權重(0-1)。 **options.exactMatchBonus** (`number`): 完全相符時額外增加的分數(0-1)。 **options.missingPenalty** (`number`): 標準答案中每個缺漏關鍵概念的扣分。 **options.contradictionPenalty** (`number`): 資訊矛盾時的扣分。較高的值可確保錯誤答案的分數接近 0。 **options.extraInfoPenalty** (`number`): 輸出包含標準答案中沒有的額外資訊時,給予輕微扣分(上限為 0.2)。 **options.scale** (`number`): 分數縮放係數。 此函式會傳回 MastraScorer 類別的執行個體。`.run()` 方法接受與其他評分器相同的輸入(請參閱 [MastraScorer 參考文件](https://mastra.zisheng.pro/zh-TW/reference/evals/mastra-scorer)),但執行物件中**必須提供標準答案**。 ## `.run()` 傳回值 **runId** (`string`): 執行 ID(選填)。 **score** (`number`): 介於 0-1 的相似度分數(若使用自訂 scale,則為 0-scale)。分數越高,表示與標準答案越相似。 **reason** (`string`): 便於閱讀的分數說明,並提供可採取行動的意見。 **preprocessStepResult** (`object`): 從輸出與標準答案中擷取的語意單元。 **analyzeStepResult** (`object`): 相符項目、矛盾與額外資訊的詳細分析。 **preprocessPrompt** (`string`): 用於擷取語意單元的提示詞。 **analyzePrompt** (`string`): 用於相似度分析的提示詞。 **generateReasonPrompt** (`string`): 用於產生說明的提示詞。 ## 評分詳情 此評分器採用多步驟流程: 1. **擷取**:將輸出與標準答案拆分為語意單元 2. **分析**:比較各單元,找出相符項目、矛盾與缺漏 3. **評分**:計算加權相似度,並對矛盾內容扣分 4. **說明**:產生便於閱讀的說明 分數計算:`max(0, base_score - contradiction_penalty - missing_penalty - extra_info_penalty) × scale` ## 範例 評估 Agent 在不同情境下的回應與標準答案的相似度: ```typescript import { runEvals } from '@mastra/core/evals' import { createAnswerSimilarityScorer } from '@mastra/evals/scorers/prebuilt' import { myAgent } from './agent' const scorer = createAnswerSimilarityScorer({ model: 'openai/gpt-5.6-sol' }) const result = await runEvals({ data: [ { input: 'What is 2+2?', groundTruth: '4', }, { input: 'What is the capital of France?', groundTruth: 'The capital of France is Paris', }, { input: 'What are the primary colors?', groundTruth: 'The primary colors are red, blue, and yellow', }, ], scorers: [scorer], target: myAgent, onItemComplete: ({ scorerResults }) => { console.log({ score: scorerResults[scorer.id].score, reason: scorerResults[scorer.id].reason, }) }, }) console.log(result.scores) ``` 如需 `runEvals` 的更多詳細資訊,請參閱 [runEvals 參考文件](https://mastra.zisheng.pro/zh-TW/reference/evals/run-evals)。 若要將此評分器加入 Agent,請參閱[評分器概觀](https://mastra.zisheng.pro/zh-TW/docs/evals/overview)指南。