Skip to content

Commit 49f1dcf

Browse files
fixup: Address PR Comments
Switched to use colang config for endpoint and api key env var Added onboarding steps from Trend's side Expanded on documentation and examples
1 parent c8fd3f6 commit 49f1dcf

File tree

8 files changed

+97
-19
lines changed

8 files changed

+97
-19
lines changed

docs/user-guides/community/trend-micro.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,21 @@ Trend Micro Vision One [AI Application Security's](https://docs.trendmicro.com/e
77
- Sensitive Data
88

99

10-
The following environment variable is required to use the integration:
11-
12-
- `V1_API_KEY`: A Vision One API Token with AI Guard Permissions
13-
14-
You can optionally set:
15-
16-
- `V1_URL`: The URL for which instances of AI Guard should be invoked
17-
Defaults to `https://api.xdr.trendmicro.com/beta/aiSecurity/guard` for Vision One's hosted US SaaS deployment
18-
1910
## Setup
2011

12+
1. Create a new [Vision One API Key](https://docs.trendmicro.com/en-us/documentation/article/trend-vision-one-platform-api-keys) with permissions to Call Detection API
13+
2. See the [AI Guard Integration Guide](https://docs.trendmicro.com/en-us/documentation/article/trend-vision-one-platform-api-keys) for details around creating your policy
14+
2115
[Colang v1](../../../examples/configs/trend_micro/):
2216

2317
```yaml
2418
# config.yml
2519

2620
rails:
21+
config:
22+
trend_micro:
23+
v1_url: "https://api.xdr.trendmicro.com/beta/aiSecurity/guard" # Replace this with your AI Guard URL
24+
api_key_env_var: "V1_API_KEY"
2725
input:
2826
flows:
2927
- trend ai guard input
@@ -36,6 +34,11 @@ rails:
3634
```yaml
3735
# config.yml
3836
colang_version: "2.x"
37+
rails:
38+
config:
39+
trend_micro:
40+
v1_url: "https://api.xdr.trendmicro.com/beta/aiSecurity/guard" # Replace this with your AI Guard URL
41+
api_key_env_var: "V1_API_KEY"
3942
```
4043
```
4144
# rails.co

docs/user-guides/guardrails-library.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,10 @@ For more details, check out the [Pangea AI Guard Integration](./community/pangea
918918

919919
### Trend Micro Vision One AI Application Security
920920

921-
NeMo Guardrails supports using Trend Micro Vision One AI Guard for protecting input and output flows within AI-powered applications.
921+
NeMo Guardrails supports using
922+
[Trend Micro Vision One AI Guard](https://docs.trendmicro.com/en-us/documentation/article/trend-vision-one-ai-scanner-ai-guard) for protecting input and output flows within AI-powered applications.
923+
924+
See [Trend Micro](community/trend-micro.md) for more details.
922925

923926
#### Example usage
924927

examples/configs/trend_micro/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ instructions:
1111
You are a helpful assistant.
1212
1313
rails:
14+
config:
15+
trend_micro:
16+
api_key_env_var: "V1_API_KEY"
1417
input:
1518
flows:
1619
- trend ai guard input

examples/configs/trend_micro_v2/config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ colang_version: "2.x"
22

33
enable_rails_exceptions: True
44

5+
rails:
6+
config:
7+
trend_micro:
8+
api_key_env_var: "V1_API_KEY"
9+
510
models:
611
- type: main
712
engine: openai

examples/configs/trend_micro_v2/rails.co

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import guardrails
22
import nemoguardrails.library.trend_micro
33

44
flow input rails $input_text
5-
trend ai guard $input_text
5+
$result = await TrendAiGuardAction(text=$input_text)
6+
7+
if $result.action == "Block"
8+
send AiGuardException(message="AI Guard detection: " + $result.reason)
9+
abort
610

711
flow output rails $output_text
812
trend ai guard $output_text

nemoguardrails/library/trend_micro/actions.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
# limitations under the License.
1515

1616
import logging
17-
import os
1817
from typing import Optional
1918

2019
import httpx
2120
from pydantic import BaseModel
2221
from pydantic_core import to_json
22+
from typing_extensions import cast
2323

2424
from nemoguardrails.actions import action
25+
from nemoguardrails.rails.llm.config import RailsConfig, TrendMicroRailConfig
2526

2627
log = logging.getLogger(__name__)
2728

@@ -35,17 +36,30 @@ class GuardResult(BaseModel):
3536
reason: str
3637

3738

39+
def get_config(config: RailsConfig) -> TrendMicroRailConfig:
40+
if (
41+
not hasattr(config.rails.config, "trend_micro")
42+
or config.rails.config.trend_micro is None
43+
):
44+
return TrendMicroRailConfig()
45+
46+
return cast(TrendMicroRailConfig, config.rails.config.trend_micro)
47+
48+
3849
@action(is_system_action=True)
39-
async def trend_ai_guard(text: Optional[str] = None):
50+
async def trend_ai_guard(config: RailsConfig, text: Optional[str] = None):
4051
"""
4152
Custom action to invoke the Trend Ai Guard
4253
"""
43-
v1_url = os.environ.get(
44-
"V1_URL", "https://api.xdr.trendmicro.com/beta/aiSecurity/guard"
45-
)
46-
v1_api_key = os.environ.get("V1_API_KEY")
54+
55+
trend_config = get_config(config)
56+
57+
# No checks required since default is set in TrendMicroRailConfig
58+
v1_url = trend_config.v1_url
59+
60+
v1_api_key = trend_config.get_api_key()
4761
if not v1_api_key:
48-
raise ValueError("V1_API_KEY environment variable is not set.")
62+
raise ValueError("Trend Micro Vision One API Key not found")
4963

5064
if text is None:
5165
raise ValueError("No prompt/response found in the last event.")

nemoguardrails/rails/llm/config.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,39 @@ def get_validator_config(self, name: str) -> Optional[GuardrailsAIValidatorConfi
827827
for _validator in self.validators:
828828
if _validator.name == name:
829829
return _validator
830+
return None
831+
832+
833+
class TrendMicroRailConfig(BaseModel):
834+
"""Configuration data for the Trend Micro AI Guard API"""
835+
836+
v1_url: Optional[str] = Field(
837+
default="https://api.xdr.trendmicro.com/beta/aiSecurity/guard",
838+
description="The endpoint for the Trend Micro AI Guard API",
839+
)
840+
841+
api_key_env_var: Optional[str] = Field(
842+
default=None,
843+
description="Environment variable containing API key for Trend Micro AI Guard",
844+
)
845+
846+
def get_api_key(self) -> Optional[str]:
847+
"""Helper to return an API key (if it exists) from a Trend Micro configuration.
848+
The `api_key_env_var` field, a string stored in this environment variable.
849+
850+
If the environment variable is not found None is returned.
851+
"""
852+
853+
if self.api_key_env_var:
854+
v1_api_key = os.getenv(self.api_key_env_var)
855+
if v1_api_key:
856+
return v1_api_key
857+
858+
log.warning(
859+
"Specified a value for Trend Micro config api_key_env var at %s but the environment variable was not set!"
860+
% self.api_key_env_var
861+
)
862+
830863
return None
831864

832865

@@ -887,6 +920,11 @@ class RailsConfigData(BaseModel):
887920
default_factory=GuardrailsAIRailConfig,
888921
description="Configuration for Guardrails AI validators.",
889922
)
923+
924+
trend_micro: Optional[TrendMicroRailConfig] = Field(
925+
default_factory=TrendMicroRailConfig,
926+
description="Configuration for Trend Micro.",
927+
)
890928

891929

892930
class Rails(BaseModel):

tests/test_trend_ai_guard.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
yaml_content="""
2424
models: []
2525
rails:
26+
config:
27+
trend_micro:
28+
v1_url: "https://api.xdr.trendmicro.com/beta/aiSecurity/guard"
29+
api_key_env_var: "V1_API_KEY"
2630
input:
2731
flows:
2832
- trend ai guard input
@@ -32,6 +36,10 @@
3236
yaml_content="""
3337
models: []
3438
rails:
39+
config:
40+
trend_micro:
41+
v1_url: "https://api.xdr.trendmicro.com/beta/aiSecurity/guard"
42+
api_key_env_var: "V1_API_KEY"
3543
output:
3644
flows:
3745
- trend ai guard output
@@ -60,7 +68,7 @@ def test_trend_ai_guard_blocked(httpx_mock: HTTPXMock, monkeypatch: pytest.Monke
6068

6169

6270
@pytest.mark.unit
63-
@pytest.mark.parametrize("status_code", frozenset({429, 500, 502, 503, 504}))
71+
@pytest.mark.parametrize("status_code", frozenset({400, 403, 429, 500}))
6472
def test_trend_ai_guard_error(
6573
httpx_mock: HTTPXMock, monkeypatch: pytest.MonkeyPatch, status_code: int
6674
):

0 commit comments

Comments
 (0)