> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # PIIDetector `PIIDetector` 是一種**混合式處理器**,可同時用於輸入與輸出處理。它會偵測並遮蔽個人識別資訊(PII),以符合隱私規範。此處理器能識別各種類型的 PII,並提供多種處理策略,包括多種遮蔽方法,以協助符合 GDPR、CCPA、HIPAA 及其他隱私法規,進而維護隱私。 ## 使用範例 ```typescript import { PIIDetector } from '@mastra/core/processors' const processor = new PIIDetector({ model: 'openrouter/openai/gpt-oss-safeguard-20b', threshold: 0.6, strategy: 'redact', detectionTypes: ['email', 'phone', 'credit-card', 'ssn'], lastMessageOnly: true, }) ``` ## 建構函式參數 **options** (`Options`): PII 偵測與遮蔽的設定選項 **options.model** (`MastraModelConfig`): 偵測 Agent 的模型設定 **options.detectionTypes** (`string[]`): 要偵測的 PII 類型。若未指定,則使用預設類型 **options.threshold** (`number`): 標記內容的信賴度門檻(0-1)。若任一類別分數超過此門檻,就會標記該 PII **options.strategy** (`'block' | 'warn' | 'filter' | 'redact'`): 偵測到 PII 時採取的策略:'block' 以錯誤拒絕內容,'warn' 記錄警告但允許內容通過,'filter' 移除遭標記的訊息,'redact' 以遮蔽後的版本取代 PII **options.redactionMethod** (`'mask' | 'hash' | 'remove' | 'placeholder'`): PII 的遮蔽方法:'mask' 以星號取代,'hash' 以 SHA256 雜湊值取代,'remove' 完全移除,'placeholder' 以類型預留位置取代 **options.instructions** (`string`): 偵測 Agent 的自訂指示。若未提供,則使用依偵測類型產生的預設指示 **options.includeDetections** (`boolean`): 是否在記錄中包含偵測詳細資訊。適合用於合規稽核與偵錯 **options.lastMessageOnly** (`boolean`): 是否只檢查批次中最新的訊息,而不檢查每則訊息。使用此選項,可避免在長討論串中為每則較早的訊息呼叫一次 LLM。 **options.preserveFormat** (`boolean`): 遮蔽時是否保留 PII 格式。設為 true 時,會保留電話號碼的 \*\*\*-\*\*-1234 等結構 **options.providerOptions** (`ProviderOptions`): 傳遞給內部偵測 Agent 的 Provider 專用選項。可用於控制模型行為,例如思考型模型的推理強度(如 { openai: { reasoningEffort: 'low' } }) ## 回傳值 **id** (`string`): 設為 'pii-detector' 的處理器識別碼 **name** (`string`): 選用的處理器顯示名稱 **processInput** (`(args: { messages: MastraDBMessage[]; abort: (reason?: string) => never; tracingContext?: TracingContext }) => Promise`): 處理輸入訊息,以便在傳送至 LLM 前偵測並遮蔽 PII **processOutputStream** (`(args: { part: ChunkType; streamParts: ChunkType[]; state: Record; abort: (reason?: string) => never; tracingContext?: TracingContext }) => Promise`): 處理串流輸出部分,以便在串流期間偵測並遮蔽 PII ## 進階使用範例 ### 輸入處理 ```typescript import { Agent } from '@mastra/core/agent' import { PIIDetector } from '@mastra/core/processors' export const agent = new Agent({ id: 'private-agent', name: 'private-agent', instructions: 'You are a helpful assistant', model: 'openai/gpt-5.6-sol', inputProcessors: [ new PIIDetector({ model: 'openrouter/openai/gpt-oss-safeguard-20b', detectionTypes: ['email', 'phone', 'credit-card', 'ssn'], threshold: 0.6, strategy: 'redact', redactionMethod: 'mask', instructions: 'Detect and redact personally identifiable information while preserving message intent', includeDetections: true, preserveFormat: true, }), ], }) ``` ### 搭配批次處理的輸出處理 建議將 `PIIDetector` 作為輸出處理器時,搭配 `BatchPartsProcessor` 使用以最佳化效能。`BatchPartsProcessor` 會先將串流區塊批次合併,再傳遞給 PII 偵測器,減少偵測所需的 LLM 呼叫次數。 ```typescript import { Agent } from '@mastra/core/agent' import { BatchPartsProcessor, PIIDetector } from '@mastra/core/processors' export const agent = new Agent({ id: 'output-pii-agent', name: 'output-pii-agent', instructions: 'You are a helpful assistant', model: 'openai/gpt-5.6-sol', outputProcessors: [ // Batch stream parts first to reduce LLM calls new BatchPartsProcessor({ batchSize: 10, }), // Then apply PII detection on batched content new PIIDetector({ model: 'openrouter/openai/gpt-oss-safeguard-20b', strategy: 'redact', }), ], }) ``` ## 相關資源 - [防護機制](https://mastra.zisheng.pro/zh-TW/docs/agents/guardrails)