> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # 答案相似度 Scorer `createAnswerSimilarityScorer()` 函数会创建一个 Scorer,用于评估 Agent 输出与 ground truth 答案的相似程度。该 Scorer 专为 CI/CD 测试场景而设计,适用于已有预期答案并希望确保结果长期保持一致的情况。 ## 参数 **model** (`LanguageModel`): 用于评估输出与 ground truth 之间语义相似度的语言模型。 **options** (`AnswerSimilarityOptions`): Scorer 的配置选项。 **options.requireGroundTruth** (`boolean`): 评估时是否必须提供 ground truth。若为 false,缺少 ground truth 时返回 0 分。 **options.semanticThreshold** (`number`): 语义匹配相对于完全匹配的权重(0-1)。 **options.exactMatchBonus** (`number`): 完全匹配时的额外加分(0-1)。 **options.missingPenalty** (`number`): ground truth 中每缺少一个关键概念所扣的分数。 **options.contradictionPenalty** (`number`): 信息矛盾时的扣分。较高的值可确保错误答案的分数接近 0。 **options.extraInfoPenalty** (`number`): 包含 ground truth 中没有的额外信息时的小幅扣分(上限为 0.2)。 **options.scale** (`number`): 分数缩放系数。 此函数返回 MastraScorer 类的实例。`.run()` 方法接受与其他 Scorer 相同的输入(参见 [MastraScorer 参考](https://mastra.zisheng.pro/reference/evals/mastra-scorer)),但 run 对象中**必须提供 ground truth**。 ## `.run()` 返回值 **runId** (`string`): run ID(可选)。 **score** (`number`): 0-1 之间的相似度分数(若使用自定义 scale,则为 0-scale)。分数越高,表示与 ground truth 越相似。 **reason** (`string`): 对分数的易读说明,并包含可操作的反馈。 **preprocessStepResult** (`object`): 从输出和 ground truth 中提取的语义单元。 **analyzeStepResult** (`object`): 对匹配项、矛盾和额外信息的详细分析。 **preprocessPrompt** (`string`): 用于提取语义单元的 prompt。 **analyzePrompt** (`string`): 用于相似度分析的 prompt。 **generateReasonPrompt** (`string`): 用于生成说明的 prompt。 ## 评分详情 该 Scorer 使用多步骤流程: 1. **提取**:将输出和 ground truth 拆分为语义单元 2. **分析**:比较语义单元,并识别匹配项、矛盾和缺漏 3. **评分**:计算加权相似度,并对矛盾进行扣分 4. **说明**:生成易读的说明 分数计算:`max(0, base_score - contradiction_penalty - missing_penalty - extra_info_penalty) × scale` ## 示例 在不同场景下评估 Agent 响应与 ground truth 的相似度: ```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/reference/evals/run-evals)。 要将此 Scorer 添加到 Agent,请参阅 [Scorer 概述](https://mastra.zisheng.pro/docs/evals/overview)指南。