> For the complete documentation index, see [llms.txt](https://docs.maiagent.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.maiagent.ai/tech/advanced-genai-tech/guardrails.md).

# AI 安全防護機制

MaiAgent 的 AI 安全防護機制：客戶可自行設定的 Hook 訊息攔截（個資遮罩、內容安全分類等），以及由 MaiAgent 代為設定的 ICAP 內容驗證與 AWS Bedrock Guardrails，各自的作用位置與限制。

MaiAgent 在內容進出大型語言模型的路徑上提供幾種防護機制。它們由系統強制執行，不依賴模型「自律」。本頁說明每種機制由誰設定、作用在哪裡、適合什麼情境；後台的操作步驟以使用者手冊為準。

## 機制總覽 <a href="#overview" id="overview"></a>

| 機制                                                                | 誰可以設定                   | 作用位置                 | 適合情境                                             |
| ----------------------------------------------------------------- | ----------------------- | -------------------- | ------------------------------------------------ |
| [Hook 訊息攔截](#hook)                                                | 具「Hook 權限」的成員在後台自行設定    | 內容送進模型前、AI 回覆送出前     | 個資遮罩、擋下提示注入與不當內容、敏感詞、把關回覆                        |
| [ICAP 內容驗證](/tech/advanced-genai-tech/icap-content-validation.md) | MaiAgent 依組織需求設定        | 對話訊息與附件、知識庫檔案（範圍見該頁） | 已有 ICAP 防毒／DLP 服務、需要納入既有資安控管的企業                  |
| [AWS Bedrock Guardrails](#aws-bedrock-guardrails)                 | 只能由 MaiAgent 替 Agent 設定 | 使用者輸入、AI 回覆          | 需要 Bedrock Guardrails 政策（內容篩選、拒絕主題、個資遮罩、依據檢查）的情境 |

三種機制各自設定、各自執行，沒有共同的政策層或統一的「封鎖／告警／放行」規則表：Hook 的處置是改寫或攔截、Bedrock Guardrails 依其政策擋下或遮罩、ICAP 依掃描結果放行或拒絕檔案。有啟用多種機制時，內容會分別經過每一種檢查，任一機制攔下就不會送進模型或送給使用者。平台也不整合第三方的執行期提示注入防護產品（例如 WAF 或 AI 防火牆）；需要這類控制時，可把它放在使用者與 MaiAgent 之間的網路層，或改用 Hook 的提示注入偵測。

{% hint style="warning" %}
**角色指令不是安全控制。** 角色指令可以引導 Agent 的語氣與回答範圍，但它只是送給模型的文字，模型可能被誘導而不遵守，也擋不住已經被檢索出來的段落。要確保某些內容「一定不會」進入模型或出現在回覆中，請使用本頁的機制；要限制使用者能檢索哪些文件，請用 [Query Metadata](/tech/identity-and-access/zhi-shi-guan-li-quan-xian-query-metadata-cha-xun-yuan-zi-liao-zong-lan.md)。角色指令的寫法見使用者手冊的 [角色指令設計指南](https://docs.maiagent.ai/build/agent/system-prompt)。
{% endhint %}

## Hook 訊息攔截 <a href="#hook" id="hook"></a>

Hook 是客戶可以自行設定的防護層：從範本建立 Hook、調整參數，再套用到 Agent。一個 Agent 可以套用多個 Hook，依序執行。

**開通條件**：平台已啟用 Hook 功能，且組織具備 Hook 使用權（企業級方案自動具備，或由 MaiAgent 個別開通）。後台看不到 Hook 選單時，請洽 MaiAgent 窗口。

### 作用位置與處置 <a href="#hook-trigger-points" id="hook-trigger-points"></a>

| 時點       | 檢查的內容                                 |
| -------- | ------------------------------------- |
| 內容送進模型前  | 使用者訊息、附件內容、知識庫檢索到的段落、Agent 模式下的工具回傳結果 |
| AI 回覆送出前 | Agent 產生的回覆                           |

處置方式有兩種：**改寫**（例如把個資遮罩後再送出）與 **攔截**（擋下這則內容，回覆自訂的訊息）。附件、知識庫段落或工具結果被攔截時，只有該段內容不送進模型，對話不會中斷。

### 偵測方式 <a href="#hook-detection" id="hook-detection"></a>

| 方式       | 說明                                                      | 資料流向             |
| -------- | ------------------------------------------------------- | ---------------- |
| 規則比對     | 以格式或關鍵字比對，例如 Email、手機、身分證、信用卡號，或指定的禁用字                  | 在 MaiAgent 平台內處理 |
| 大型語言模型判斷 | 以選定的模型判斷內容，例如是否為提示注入攻擊、回覆是否缺乏依據                         | 送往選定的模型          |
| 個人資料偵測服務 | 偵測姓名、地址等沒有固定格式的個資；目前提供 Google Cloud DLP                 | 送往該偵測服務          |
| 內容安全分類服務 | 依仇恨、騷擾、色情內容、暴力、自殘、違法行為、越獄攻擊等類別判定；目前提供 OpenAI Moderation | 送往該分類服務          |

個人資料偵測與內容安全分類依掃描的字元數扣點（每千字元）。可用的範本、各範本的參數，以及每次攔截的紀錄，見使用者手冊的 [Hook 訊息攔截](https://docs.maiagent.ai/build/hook)、[新增與設定 Hook](https://docs.maiagent.ai/build/hook/setup) 與 [Hook 執行紀錄](https://docs.maiagent.ai/build/hook/execution-logs)。

## AWS Bedrock Guardrails <a href="#aws-bedrock-guardrails" id="aws-bedrock-guardrails"></a>

AWS Bedrock Guardrails 是 AWS 提供的防護服務。MaiAgent 可以替個別 Agent 指定一組 Guardrail（ID 與版本），之後該 Agent 的對話會經過這組 Guardrail 檢查：

* **使用者輸入**：送進模型前檢查，命中封鎖規則時直接回覆 Guardrail 設定的封鎖訊息。
* **AI 回覆**：送出前連同問題與檢索到的段落一起檢查；命中封鎖規則時改回封鎖訊息，命中遮罩規則時改回遮罩後的內容。

使用限制：

* **只能由 MaiAgent 設定**：後台沒有設定入口。Guardrail 必須建立在 MaiAgent 平台呼叫的 AWS 環境中，客戶在自己 AWS 帳號建立的 Guardrail 無法直接套用。需要時請洽 MaiAgent 窗口。
* **回覆不會逐字串流**：套用 Guardrail 的 Agent，回覆會在檢查完成後一次送出。
* 需要能連線到 AWS Bedrock，離線的地端環境無法使用。

不需要 AWS 的情境，建議優先使用可自行設定的 [Hook](#hook)。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.maiagent.ai/tech/advanced-genai-tech/guardrails.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
