|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import pytest |
| 4 | +from unittest.mock import AsyncMock, patch |
| 5 | +from fastapi import HTTPException |
| 6 | + |
| 7 | +sys.path.insert(0, os.path.abspath("../..")) |
| 8 | + |
| 9 | +from litellm.proxy.guardrails.guardrail_hooks.model_armor.model_armor import ModelArmorGuardrail |
| 10 | +from litellm.proxy._types import UserAPIKeyAuth |
| 11 | +from litellm.caching.caching import DualCache |
| 12 | + |
| 13 | +@pytest.mark.asyncio |
| 14 | +async def test_model_armor_pre_call_hook_inspect_and_deidentify(): |
| 15 | + """ |
| 16 | + Test Model Armor guardrail pre-call hook for both inspectResult and deidentifyResult handling. |
| 17 | + """ |
| 18 | + guardrail = ModelArmorGuardrail( |
| 19 | + template_id="dummy-template", |
| 20 | + project_id="dummy-project", |
| 21 | + location="us-central1", |
| 22 | + credentials=None, |
| 23 | + ) |
| 24 | + armor_response = { |
| 25 | + "sanitizationResult": { |
| 26 | + "filterResults": [ |
| 27 | + { |
| 28 | + "sdpFilterResult": { |
| 29 | + "inspectResult": { |
| 30 | + "executionState": "EXECUTION_SUCCESS", |
| 31 | + "matchState": "NO_MATCH_FOUND", |
| 32 | + "findings": [] |
| 33 | + }, |
| 34 | + "deidentifyResult": { |
| 35 | + "executionState": "EXECUTION_SUCCESS", |
| 36 | + "matchState": "MATCH_FOUND", |
| 37 | + "data": {"text": "sanitized text here"} |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + ] |
| 42 | + } |
| 43 | + } |
| 44 | + with patch.object(guardrail, "make_model_armor_request", AsyncMock(return_value=armor_response)): |
| 45 | + user_api_key_dict = UserAPIKeyAuth(api_key="test_key") |
| 46 | + cache = DualCache() |
| 47 | + data = { |
| 48 | + "messages": [ |
| 49 | + {"role": "system", "content": "You are a helpful assistant."}, |
| 50 | + {"role": "user", "content": "My SSN is 123-45-6789."} |
| 51 | + ], |
| 52 | + "model": "gpt-3.5-turbo", |
| 53 | + "metadata": {} |
| 54 | + } |
| 55 | + guardrail.mask_request_content = True |
| 56 | + with pytest.raises(HTTPException) as exc_info: |
| 57 | + await guardrail.async_pre_call_hook( |
| 58 | + user_api_key_dict=user_api_key_dict, |
| 59 | + cache=cache, |
| 60 | + data=data, |
| 61 | + call_type="completion" |
| 62 | + ) |
| 63 | + assert exc_info.value.status_code == 400 |
| 64 | + assert "Content blocked by Model Armor" in str(exc_info.value.detail) |
| 65 | + |
| 66 | +def test_model_armor_should_block_content(): |
| 67 | + guardrail = ModelArmorGuardrail( |
| 68 | + template_id="dummy-template", |
| 69 | + project_id="dummy-project", |
| 70 | + location="us-central1", |
| 71 | + credentials=None, |
| 72 | + ) |
| 73 | + # Block on inspectResult |
| 74 | + armor_response_inspect = { |
| 75 | + "sanitizationResult": { |
| 76 | + "filterResults": [ |
| 77 | + {"sdpFilterResult": {"inspectResult": {"matchState": "MATCH_FOUND"}}} |
| 78 | + ] |
| 79 | + } |
| 80 | + } |
| 81 | + assert guardrail._should_block_content(armor_response_inspect) |
| 82 | + # Block on deidentifyResult |
| 83 | + armor_response_deidentify = { |
| 84 | + "sanitizationResult": { |
| 85 | + "filterResults": [ |
| 86 | + {"sdpFilterResult": {"deidentifyResult": {"matchState": "MATCH_FOUND"}}} |
| 87 | + ] |
| 88 | + } |
| 89 | + } |
| 90 | + assert guardrail._should_block_content(armor_response_deidentify) |
| 91 | + # No block if neither |
| 92 | + armor_response_none = { |
| 93 | + "sanitizationResult": { |
| 94 | + "filterResults": [ |
| 95 | + {"sdpFilterResult": {"inspectResult": {"matchState": "NO_MATCH_FOUND"}, "deidentifyResult": {"matchState": "NO_MATCH_FOUND"}}} |
| 96 | + ] |
| 97 | + } |
| 98 | + } |
| 99 | + assert not guardrail._should_block_content(armor_response_none) |
0 commit comments