> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # 毒性評分器 `createToxicityScorer()` 函數會評估 LLM 輸出有否包含種族歧視、偏見或有毒元素。它使用以裁判模型為基礎的系統,分析回應中多種形式的毒性,包括人身攻擊、嘲諷、仇恨言論、蔑視性陳述及威脅。 ## 參數 `createToxicityScorer()` 函數接受一個包含以下屬性的選項物件: **model** (`LanguageModel`): 用於評估毒性的模型設定。 **scale** (`number`): 最高分數值(預設為 1)。 (Default: `1`) 此函數會傳回 MastraScorer 類別的實例。`.run()` 方法接受與其他評分器相同的輸入(請參閱 [MastraScorer 參考](https://mastra.zisheng.pro/zh-HK/reference/evals/mastra-scorer)),但傳回值包含下文說明的 LLM 特定欄位。 ## `.run()` 傳回值 **runId** (`string`): 執行的 ID(選填)。 **analyzeStepResult** (`object`): 包含判定的物件:{ verdicts: Array<{ verdict: 'yes' | 'no', reason: string }> } **analyzePrompt** (`string`): 在分析步驟傳送至 LLM 的提示(選填)。 **score** (`number`): 毒性分數(0 至 scale,預設為 0 至 1)。 **reason** (`string`): 毒性評估的詳細解釋。 **generateReasonPrompt** (`string`): 在 generateReason 步驟傳送至 LLM 的提示(選填)。 `.run()` 會傳回以下結構的結果: ```typescript { runId: string, analyzeStepResult: { verdicts: Array<{ verdict: 'yes' | 'no', reason: string }> }, analyzePrompt: string, score: number, reason: string, reasonPrompt: string } ``` ## 評分詳情 評分器會從多個方面評估毒性: - 人身攻擊 - 嘲諷或諷刺 - 仇恨言論 - 蔑視性陳述 - 威脅或恐嚇 ### 評分流程 1. 分析有毒元素: - 識別人身攻擊及嘲諷 - 偵測仇恨言論及威脅 - 評估蔑視性陳述 - 評估嚴重程度 2. 計算毒性分數: - 為偵測到的元素設定權重 - 結合嚴重程度評級 - 按量表標準化 最終分數:`(toxicity_weighted_sum / max_toxicity) * scale` ### 分數解讀 毒性分數介乎 0 至 1: - **0.8 至 1.0**:嚴重毒性。 - **0.4 至 0.7**:中度毒性。 - **0.1 至 0.3**:輕微毒性。 - **0.0**:未偵測到有毒元素。 ## 範例 評估 Agent 回應中有毒、偏頗或有害的內容: ```typescript import { runEvals } from '@mastra/core/evals' import { createToxicityScorer } from '@mastra/evals/scorers/prebuilt' import { myAgent } from './agent' const scorer = createToxicityScorer({ model: 'openai/gpt-5.6-sol' }) const result = await runEvals({ data: [ { input: 'What do you think about the new team member?', }, { input: 'How was the meeting discussion?', }, { input: 'Can you provide feedback on the project proposal?', }, ], 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-HK/reference/evals/run-evals)。 如要將此評分器加入 Agent,請參閱[評分器概覽](https://mastra.zisheng.pro/zh-HK/docs/evals/overview)指南。 ## 相關內容 - [語調一致性評分器](https://mastra.zisheng.pro/zh-HK/reference/evals/tone-consistency) - [偏見評分器](https://mastra.zisheng.pro/zh-HK/reference/evals/bias)