Skip to content

Commit 3dcfbb4

Browse files
committed
chore(python): add additional samples in py
1 parent 5ac41cd commit 3dcfbb4

File tree

4 files changed

+146
-8
lines changed

4 files changed

+146
-8
lines changed

samples/python-samples/app.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from python_samples.bedrock_aurora_stack import BedrockAuroraStack
88
from python_samples.bedrock_pinecone_stack import BedrockPineconeStack
99
from python_samples.opensearch_vectorindex import OpensearchVectorIndex
10-
11-
10+
from python_samples.bedrock_guardrails_stack import BedrockGuardrailsStack
11+
from python_samples.bedrock_data_sources import BedrockDataSourcesStack
1212
app = cdk.App()
1313
env = cdk.Environment(account=os.getenv('CDK_DEFAULT_ACCOUNT'),
1414
region=os.getenv('CDK_DEFAULT_REGION'))
@@ -18,9 +18,9 @@
1818
# Bedrock knowledge base with OpenSearch
1919
#---------------------------------------------------------------------------
2020

21-
BedrockOpensearchStack(app, "BedrockOpensearchStack"+os.getenv('SUFFIX',''),
22-
env=env
23-
)
21+
#BedrockOpensearchStack(app, "BedrockOpensearchStack"+os.getenv('SUFFIX',''),
22+
# env=env
23+
# )
2424

2525
#---------------------------------------------------------------------------
2626
# Bedrock knowledge base with Amazon RDS Aurora PostgreSQL
@@ -43,7 +43,21 @@
4343
# env=env
4444
# )
4545

46+
#---------------------------------------------------------------------------
47+
# Bedrock Guardrails
48+
#---------------------------------------------------------------------------
49+
50+
# BedrockGuardrailsStack(app, "BedrockGuardrailsStack",
51+
# env=env
52+
# )
53+
54+
#---------------------------------------------------------------------------
55+
# Bedrock Data Sources
56+
#---------------------------------------------------------------------------
4657

58+
BedrockDataSourcesStack(app, "BedrockDataSourcesStack",
59+
env=env
60+
)
4761
app.synth()
4862

4963

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 BedrockDataSourcesStack(Stack):
16+
17+
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
18+
super().__init__(scope, construct_id, **kwargs)
19+
20+
# Create a data source
21+
web_crawler_data_source = bedrock.WebCrawlerDataSource(
22+
self,
23+
"MyWebCrawlerDataSource",
24+
data_source_name="my-web-crawler-data-source",
25+
description="Data source for company website",
26+
source_urls=["https://www.example.com"],
27+
crawling_scope=bedrock.CrawlingScope.SUBDOMAINS,
28+
crawling_rate=300,
29+
filters={
30+
"includePatterns": ["/blog/", "/docs/"],
31+
"excludePatterns": ["/private/", "/admin/"],
32+
},
33+
max_pages=1000,
34+
)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
aws-cdk-lib>=2.177.0
1+
aws-cdk-lib>=2.188.0
22
constructs>=10.0.0,<11.0.0
3-
aws-cdk.aws-lambda-python-alpha>=2.141.0a0
4-
cdklabs.generative-ai-cdk-constructs>=0.1.290
3+
aws-cdk.aws-lambda-python-alpha>=2.188.0a0
4+
cdklabs.generative-ai-cdk-constructs>=0.1.301

0 commit comments

Comments
 (0)