1+ from aws_cdk import (
2+ # Duration,
3+ Stack ,
4+ aws_s3 as s3 ,
5+ aws_lambda as _lambda ,
6+ CfnOutput ,
7+ Duration as Duration
8+ # aws_sqs as sqs,
9+ )
10+ from constructs import Construct
11+ from cdklabs .generative_ai_cdk_constructs import (
12+ bedrock
13+ )
14+
15+ class BedrockGuardrailsStack (Stack ):
16+
17+ def __init__ (self , scope : Construct , construct_id : str , ** kwargs ) -> None :
18+ super ().__init__ (scope , construct_id , ** kwargs )
19+
20+ guardrail = bedrock .Guardrail (self , 'myGuardrails' ,
21+ name = 'my-BedrockGuardrails' ,
22+ description = "Legal ethical guardrails."
23+ )
24+
25+ # Optional - Add Sensitive information filters
26+ guardrail .add_pii_filter (
27+ type = bedrock .pii_type .General .ADDRESS ,
28+ action = bedrock .GuardrailAction .ANONYMIZE ,
29+ )
30+
31+ guardrail .add_regex_filter (
32+ name = "TestRegexFilter" ,
33+ description = "This is a test regex filter" ,
34+ pattern = "/^[A-Z]{2}d{6}$/" ,
35+ action = bedrock .GuardrailAction .ANONYMIZE ,
36+ )
37+
38+ # Optional - Add contextual grounding
39+ guardrail .add_contextual_grounding_filter (
40+ type = bedrock .ContextualGroundingFilterType .GROUNDING ,
41+ threshold = 0.95 ,
42+ )
43+
44+ guardrail .add_contextual_grounding_filter (
45+ type = bedrock .ContextualGroundingFilterType .RELEVANCE ,
46+ threshold = 0.95 ,
47+ )
48+
49+ # Optional - Add Denied topics . You can use default Topic or create your custom Topic with createTopic function. The default Topics can also be overwritten.
50+ guardrail .add_denied_topic_filter (bedrock .Topic .FINANCIAL_ADVICE )
51+
52+ guardrail .add_denied_topic_filter (
53+ bedrock .Topic .custom (
54+ name = "Legal_Advice" ,
55+ definition =
56+ "Offering guidance or suggestions on legal matters, legal actions, interpretation of laws, or legal rights and responsibilities." ,
57+ examples = [
58+ "Can I sue someone for this?" ,
59+ "What are my legal rights in this situation?" ,
60+ "Is this action against the law?" ,
61+ "What should I do to file a legal complaint?" ,
62+ "Can you explain this law to me?" ,
63+ ]
64+ )
65+ )
66+
67+ # Optional - Add Word filters. You can upload words from a file with addWordFilterFromFile function.
68+ guardrail .add_word_filter ("drugs" )
69+ guardrail .add_managed_word_list_filter (bedrock .ManagedWordFilterType .PROFANITY )
70+ #guardrail.add_word_filter_from_file("./scripts/wordsPolicy.csv")
71+
72+ # versioning - if you change any guardrail configuration, a new version will be created
73+ guardrail .create_version ("testversion" )
74+
75+ guardrail .add_denied_topic_filter (bedrock .Topic .FINANCIAL_ADVICE );
76+
77+ # Create a custom topic
78+ guardrail .add_denied_topic_filter (
79+ bedrock .Topic .custom (
80+ name = 'Legal_Advice' ,
81+ definition = 'Offering guidance or suggestions on legal matters, legal actions, interpretation of laws, or legal rights and responsibilities.' ,
82+ examples = [
83+ 'Can I sue someone for this?' ,
84+ 'What are my legal rights in this situation?' ,
85+ 'Is this action against the law?' ,
86+ 'What should I do to file a legal complaint?' ,
87+ 'Can you explain this law to me?' ,
88+ ],
89+ )
90+ );
0 commit comments