一個篩選每個請求是否企圖覆寫模型的 AI 閘道
支援助理的使用者可以輸入任何東西,其中有些企圖讓模型忽略它被給予的指示。模型前方的閘道可以先將每個請求送過一個防護:一個服務,向受過訓練的分類器詢問該請求是否為這類企圖,若是就拒絕,並將其餘的轉送到模型。這個工作區將這樣的安排編譯為 Kubernetes 資訊清單:閘道、防護,以及防護的分類器所執行的偵測器。
要求它的 Feature 是 core/prompt-injection-shield。它的作用比名稱所暗示的更窄,而本頁花在說明它不做什麼的篇幅,與說明它做什麼一樣多:判定出自機率式分類器,它會漏掉一些攻擊、拒絕一些合法請求,已知可被規避,而且它既不判斷模型的回覆,也不判斷工具呼叫的引數,也不判斷圖片。它是眾多控制措施之一。
這個工作區不撰寫任何自己的目錄實體。這裡的三個實體檔案——一個 Aggregate、一個 Manifest 與一個 Release——就是任何人需要撰寫的全部內容。
它建置什麼
| 實例 | 目錄 Component | 它在這裡是什麼 |
|---|---|---|
router | core/gateway/litellm | AI 閘道:一個別名 assistant,輸入到它的模型對應表中,並指向防護後方的模型 |
guard | core/security/nemo-guardrails | NVIDIA NeMo Guardrails 的伺服器位於模型的路徑上,檢查每個聊天請求,並將它接受的轉送到模型 |
detector | core/security/trustyai-huggingface-detector | 防護所詢問的分類器服務,執行它的預設提示注入分類器 |
兩條 uses 邊:閘道的 guard 插槽連到防護,以及防護的 detector 插槽連到偵測器。兩個 Feature 附加項目:閘道上的 shield,也就是防護盾,位於 sensitivity: standard;以及防護上的 upstream_key,防護向模型 API 出示的認證。
草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example ai-gateway-prompt-injection-shield它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd ai-gateway-prompt-injection-shield建置
aggrega build --release release.yaml --auto-pullRelease 在建置前需要鎖定檔,而鎖定檔是產生出來的、不隨附發布,所以全新複製的專案中沒有。先解析一個,或讓建置來填入:
aggrega pull --release release.yaml # resolve once, then build with no flagaggrega build --release release.yaml --auto-pull # or resolve during the build這個工作區從 .aggrega-config.yaml 所指名的預設登錄庫解析目錄實體。防護盾的實現執行一個既不隨附於編譯器也不隨附於目錄的外掛程式,也就是閘道設定步驟 pipeline-step/litellm-gateway-config,解析時會從 default_registry 所指名的登錄庫 hub.aggrega.ai 擷取它。
建置會印出以下內容,其中 <workspace> 代表此範例的資料夾:
[… omitted: a line reporting on the gateway configuration step's plugin, a warning that the upstream_key credential's declared-sensitive origin reaches the target with no per-key sensitivity marking, because the realization maps it through a valuesMapping, the resolution's summary, a notice that core/referenced-secret declares origin in both of its pillars, and a total build time, which differs per run]Build completed: 10 files written to <workspace>/distRelease 指名防護與偵測器的映像檔,因為目錄兩者都不指名。 每個 Component 的 infra.image 是沒有預設值的必要設定;Component 中它旁邊的註解說明原因,並指名其行為經證明所依據的建置。release.yaml 中的兩個值是你所執行建置的副本的預留位置,鏡像到你控制的登錄庫中。建置會傳遞那裡的任何字串;部署前請取代兩者。
搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,且不要自己建立資料夾。若工作區資料夾中還沒有 `ai-gateway-prompt-injection-shield` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `ai-gateway-prompt-injection-shield` 且不指定 `dir`:它會將範例寫入新的 `ai-gateway-prompt-injection-shield` 資料夾。然後呼叫 `build` 工具,將 `workspace_root` 設為 `ai-gateway-prompt-injection-shield`、`release` 設為 `release.yaml`、`auto_pull` 設為 true、`write` 設為 true。告訴我建置寫入了哪些檔案;它們位於 `ai-gateway-prompt-injection-shield/dist/` 之下。渲染樹
dist/helm/├── detector-default-service.yaml├── detector-deployment.yaml├── guard--upstream-zukey--guard-secret-guard-upstream-key.yaml├── guard-default-service.yaml├── guard-deployment.yaml├── guard/│ └── nemo-guardrails-config.configmap.yaml├── router-default-service.yaml├── router-deployment.yaml└── router/ ├── litellm-gateway-config/ │ └── configmap.yaml └── request-guard/ └── request-guard.yaml防護盾不帶來任何自己的檔案。它的作用寫入閘道的設定中。
閘道讀取的路由
閘道設定本文,即 dist/helm/router/litellm-gateway-config/configmap.yaml 的 config.yaml 鍵:
model_list: - model_name: assistant litellm_params: model: openai/gpt-4o-mini api_base: "http://guard-default:8000/v1" api_key: "the-guard-holds-the-upstream-credential"litellm_settings: callbacks: - /etc/litellm-request-guard/request_field_filter.filtercallback_settings: request_field_filter.router-shield: policies: - reason: "sends every model request through a prompt-injection guard, which checks chat completions only" served_request_types: - acompletion從中可以讀出三件值得注意的事。
每個別名都指向防護。 api_base 是防護所發布的端點,從 guard 邊讀出,所以對 assistant 的請求會透過 OpenAI API 送到防護,要求 gpt-4o-mini;防護將它接受的轉送到 release.yaml 在其 upstream_endpoint 中指名的模型 API。移除防護盾,該項目就寫成沒有 api_base,與之前完全相同。這就是別名輸入到閘道的 values.models,而不是由連到它的 models 插槽的模型提供的原因:連接的模型的項目是以該模型自己的端點與它的連線自己的金鑰寫成,所以它的請求會繞過防護直接到模型,而防護盾會拒絕這樣的連線,如下所示。
閘道不持有模型的任何認證。 項目以固定的詞取代金鑰,因為閘道連到的是防護而不是提供者,而防護出示它自己的認證,也就是附加到它的 upstream_key 槽位的那一個。呼叫端自己的 Authorization 標頭絕不會到達模型。
只提供聊天補全。 防護檢查聊天補全,而不提供補全、嵌入、responses 或 messages 路由,所以原則讓閘道的請求防護在轉送任何東西之前,以 HTTP 400 拒絕所有其他種類的請求,並說明原因:
This gateway sends every model request through a prompt-injection guard, which checks chat completions only, and it does not serve embeddings requests while it does, so the request was not forwarded. It serves chat completions requests.防護做什麼
防護的設定,即 dist/helm/guard/nemo-guardrails-config.configmap.yaml 的 config.yaml 鍵,從它的 rails 開始:
rails: config: hf_classifier: prompt_injection: engine: fms model: "protectai/deberta-v3-base-prompt-injection-v2" base_url: "http://detector-default:8000" threshold: 0.5 blocked_labels: ["INJECTION"] input: flows: - hf classifier check input $classifier=prompt_injection對於每個聊天請求,無論一般或串流,防護會將最新的使用者訊息送到偵測器,偵測器以它載入的分類器評分,並在偵測器回報 INJECTION 達到或超過 0.5 時拒絕該請求。被拒絕的請求由防護自己以一個普通補全回應,其訊息是 I’m sorry, I can’t respond to that.(我很抱歉,我無法回應。)——當請求要求串流時就以串流回應——而它的任何內容都不會到達模型。它接受的請求會以呼叫端送出的樣子到達模型,包括它的系統訊息與先前的輪次。
呼叫端無法從請求中關閉檢查。防護的設定帶有一個流程 rails.co,無論請求本文要求什麼都會執行檢查,而防護只執行這一份設定,並拒絕指名另一份設定的請求。在這個閘道上,guardrails 欄位屬於閘道自己,不會被轉送,而閘道未被設定要轉送的欄位,例如 extra_body,會被拒絕。
它不做什麼
- 它會漏掉一些攻擊,也會拒絕一些合法請求。 判定是分類器的分數與門檻的比較。經改寫、以另一種語言撰寫、編碼或換拼的企圖可能得分低而被放行;一般請求可能得分高而被拒絕。
- 它只讀取最新的使用者訊息。 放在系統訊息、先前輪次或工具結果中的企圖,會未被讀取就到達模型。
- 它不判斷模型的回覆、工具呼叫的引數或圖片。
- 它只涵蓋穿過閘道的流量。 直接呼叫模型位址的工作負載會繞過它。防護與偵測器不驗證任何呼叫端,所以它們的 Service 是供呼叫它們的元件使用的位址,而不是要發布的位址。
敏感度
standard 與 strict 會作為防護的門檻執行:0.5 與 0.001。這兩個數字是針對偵測器的預設分類器測得的,其分數接近 0 或接近 1。在 0.5,防護拒絕覆寫模型指示的一般企圖。在 0.001,它也會拒絕以 leetspeak 換拼的這類企圖,分類器對它的評分約為 0.008,並連帶拒絕一些分類器評分低但不接近零的一般請求。
門檻是防護自己的值,所以改為 strict 是同時進行的兩項編輯:附加項目上的 sensitivity: strict,以及防護的 values 中的 threshold: 0.001。實現會檢查兩者一致。它看得到防護而看不到其後的偵測器,所以無法判斷偵測器載入了哪個模型;執行其他模型的偵測器評分不同,它的門檻要重新測量。
什麼會中止建置
以下每一項都是建置印出的一個句子,指出要變更什麼:
-
敏感度與防護的門檻不一致。 設定
sensitivity: strict並讓防護維持預設:[… omitted: a line reporting on the gateway configuration step's plugin, what resolving the lockfile prints, the same halt among it, and a notice that core/referenced-secret declares origin in both of its pillars][CATALOG_ERROR E0010] instance routernode "router": pipeline step failed[… omitted: the step, the template position and the template's own source]error calling fail: core/prompt-injection-shield attachment "shield" on "router" asks for sensitivity strict, which this realization runs as the guard's threshold 0.001, but the guard "guard" runs at 0.5; set values.threshold of "guard" to 0.001 -
沒有連接防護。 移除閘道的
uses區塊(其中只有guard邊),建置會在渲染任何東西之前中止,並指名插槽:[… omitted: a line reporting on the gateway configuration step's plugin, what resolving the lockfile prints, the same halt among it, and a notice that core/referenced-secret declares origin in both of its pillars][CATALOG_ERROR E0033]Recipe/FeatureRecipe assertionscaused by: FeatureRecipe "prompt-injection-shield-litellm" instance "router" assertion "guard_is_wired": [… omitted: the template position and the assertion's own source]: error calling fail: assertion failed: core/prompt-injection-shield attachment "shield" on "router" needs a guard to send the gateway's model requests through, but nothing is wired to its guard socket; wire a core/security/nemo-guardrails to the guard socket of "router" -
連到閘道
models插槽的模型。 它的項目會以該模型自己的端點與它的連線自己的金鑰送出,繞過防護。將core/ai-model/openai以fast連到該插槽,並在連線上帶認證,與輸入的別名並列——因為閘道此時提供兩個別名而只發布一個,所以要設定values.model: assistant,並在 Release 中指名fast、在 Manifest 中指名它的 Recipe——建置就會在渲染任何東西之前中止,並指名該連線:[… omitted: a line reporting on the gateway configuration step's plugin, what resolving the lockfile prints, the same halt among it, and a notice that core/referenced-secret declares origin in both of its pillars][CATALOG_ERROR E0033]Recipe/FeatureRecipe assertionscaused by: FeatureRecipe "prompt-injection-shield-litellm" instance "router" assertion "no_wired_models": [… omitted: the template position and the assertion's own source]: error calling fail: assertion failed: core/prompt-injection-shield attachment "shield" on "router" sends every model request through the guard, which forwards it to its own upstream with its own credential, but the models socket of "router" holds fast, each sent to its own model's endpoint with its own connection's credential, past the guard; remove those connections and reach that model through the guard's upstream, or remove the shield attachment -
閘道
provider_key槽位上的認證。 它會被出示給防護而不是任何模型;建置改為指名防護的upstream_key槽位。 -
另一個防護。 插槽接受任何其提示防護是受過訓練的分類器的元件,而實現對除了
core/security/nemo-guardrails之外的任何一個都會中止——它的兩個敏感度就是在它上面測得的——對同時連到插槽的第二個防護也是如此。 -
目標未寫成
openai/<model>,閘道會將它透過另一種 API 送到防護。 -
同一個閘道上的
core/semantic-caching。 快取透過對應表的一個別名嵌入每個提示,而它會被送到防護,防護不回應嵌入請求;快取什麼也快取不到。 -
閘道上的第二個防護盾。
無法連到防護或偵測器時
請求被拒絕,沒有任何東西到達模型。偵測器停止運作時,防護對一般請求以回報內部錯誤的補全回應,對串流請求以不帶文字的串流回應。防護停止運作時,閘道以錯誤回應。這份設定中沒有任何東西會在防護失敗時,讓請求繞過它。
編譯器不解讀的地方
編譯器將防護的 Service 名稱與連接埠傳遞到閘道的設定中,並將門檻與偵測器的位址傳遞到防護的設定中,全都以字串形式。它不評估提示,不知道分類器會給什麼分數,也不連絡防護、偵測器或模型。上面每一次中止都是由目錄自己的實體所宣告,作為防護盾的實現中的斷言或其管線步驟中的檢查,且沒有任何一項依賴於對流量的任何理解。
調整它
- 更嚴格。 在附加項目上設定
sensitivity: strict,並同時在防護上設定threshold: 0.001。 - 自己執行的模型。 把防護的
upstream_endpoint指向模型伺服器的端點——core/ai-model/vllm會發布一個——並把別名的目標寫成openai/<the name that server serves>。 - 改用 NGINX 閘道。
core/gateway/nginx宣告了相同的guard插槽,而prompt-injection-shield-nginx在 helm 與 compose 上,會讓它唯一的 location 指向防護,取代其上游。把防護的upstream_endpoint設為閘道的upstream插槽所指名的模型。該 location 上的每個請求都會送到防護,它提供聊天補全,並對其他模型路由回應 404。