Skip to content

Commit e5ce797

Browse files
author
dinsajwa
committed
feat(python_samples): updated python samples with bedrock construct changes
1 parent eae7b28 commit e5ce797

File tree

5 files changed

+90
-74
lines changed

5 files changed

+90
-74
lines changed

samples/python-samples/app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
env = cdk.Environment(account=os.getenv('CDK_DEFAULT_ACCOUNT'),
1414
region=os.getenv('CDK_DEFAULT_REGION'))
1515

16+
1617
#---------------------------------------------------------------------------
1718
# Bedrock knowledge base with OpenSearch
1819
#---------------------------------------------------------------------------
@@ -23,6 +24,7 @@
2324

2425
#---------------------------------------------------------------------------
2526
# Bedrock knowledge base with Amazon RDS Aurora PostgreSQL
27+
# uncomment this if you want to deploy Amazon RDS Aurora PostgreSQL
2628
#---------------------------------------------------------------------------
2729

2830

@@ -31,6 +33,16 @@
3133
# )
3234

3335

36+
#---------------------------------------------------------------------------
37+
# Bedrock knowledge base with Pinecone
38+
# uncomment this if you want to deploy Pinecone
39+
#---------------------------------------------------------------------------
40+
41+
42+
# BedrockPineconeStack(app, "BedrockPineconeStack",
43+
# env=env
44+
# )
45+
3446

3547
app.synth()
3648

samples/python-samples/python_samples/bedrock_aurora_stack.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
2424
#---------------------------------------------------------------------------
2525
# Bedrock knowledge base with Amazon RDS Aurora PostgreSQL
2626
#---------------------------------------------------------------------------
27-
2827

29-
aurora = amazonaurora.AmazonAuroraDefaultVectorStore(self,
30-
'AuroraDefaultVectorStore',
31-
embeddings_model_vector_dimension=bedrock.BedrockFoundationModel.COHERE_EMBED_ENGLISH_V3.vector_dimensions
32-
)
28+
embeddings_model_vector_dimension = 1024
29+
30+
aurora_db = amazonaurora.AmazonAuroraVectorStore(self, 'AuroraDefaultVectorStore',
31+
embeddings_model_vector_dimension=embeddings_model_vector_dimension
32+
)
33+
3334

3435
kb = bedrock.KnowledgeBase(self, 'KnowledgeBase-Aurora',
35-
vector_store= aurora,
36+
vector_store= aurora_db,
3637
embeddings_model= bedrock.BedrockFoundationModel.COHERE_EMBED_ENGLISH_V3,
3738
instruction= 'Use this knowledge base to answer questions about books. ' +
3839
'It contains the full text of novels.'
@@ -44,9 +45,10 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
4445
bucket= docBucket,
4546
knowledge_base=kb,
4647
data_source_name='books',
47-
chunking_strategy= bedrock.ChunkingStrategy.FIXED_SIZE,
48-
max_tokens=500,
49-
overlap_percentage=20
48+
chunking_strategy= bedrock.ChunkingStrategy.fixed_size(
49+
max_tokens=500,
50+
overlap_percentage=20
51+
)
5052
)
5153

5254
## action group
@@ -58,41 +60,44 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
5860
layers= [_lambda.LayerVersion.from_layer_version_arn(self, 'PowerToolsLayer', f'arn:aws:lambda:{self.region}:017000801446:layer:AWSLambdaPowertoolsPythonV2:60')],
5961
timeout= Duration.minutes(2),
6062
)
63+
6164
ag = bedrock.AgentActionGroup(
62-
self,
63-
'ActionGroup',
64-
action_group_name='query-library',
65+
name='query-library',
6566
description= 'Use these functions to get information about the books in the library.',
66-
action_group_executor= action_group_function,
67-
action_group_state= 'ENABLED',
68-
api_schema= bedrock.ApiSchema.from_asset(os.path.join(os.path.dirname(__file__), 'action-group.yaml'))
69-
)
67+
executor=bedrock.ActionGroupExecutor.fromlambda_function(
68+
action_group_function,
69+
),
70+
enabled= True,
71+
api_schema= bedrock.ApiSchema.from_local_asset("./python_samples/action-group.yaml") )
7072

7173
## agent
7274
agent = bedrock.Agent(
7375
self,
7476
"Agent",
75-
foundation_model=bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_V2_1,
76-
instruction=" You are a helpful and friendly agent that answers questions about insurance claims.",
77+
foundation_model=bedrock.BedrockFoundationModel.AMAZON_NOVA_MICRO_V1,
78+
instruction="You are a helpful and friendly agent that answers questions about insurance claims.",
7779
knowledge_bases=[kb],
78-
enable_user_input= True,
80+
user_input_enabled= True,
7981
should_prepare_agent=True
8082
)
8183

8284
## associate action group with agent
8385
agent.add_action_group(ag)
8486

8587
## agent alias
86-
agent.add_alias(
87-
alias_name= 'my-agent-alias',
88-
description='alias for my agent'
89-
)
88+
agent_alias= bedrock.AgentAlias(self,
89+
'AgentAlias',
90+
description='alias for my agent',
91+
agent=agent)
92+
9093

9194

9295
CfnOutput(self, "KnowledgeBaseId", value=kb.knowledge_base_id)
9396
CfnOutput(self, 'agentid', value= agent.agent_id)
9497
CfnOutput(self, 'DataSourceId', value= dataSource.data_source_id)
9598
CfnOutput(self, 'DocumentBucket', value= docBucket.bucket_name)
99+
CfnOutput(self, 'agent_alias', value= agent_alias.alias_name)
100+
96101

97102

98103

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import os
22
from aws_cdk import (
3-
# Duration,
43
Stack,
54
aws_s3 as s3,
65
aws_lambda as _lambda,
76
CfnOutput,
87
Duration as Duration
9-
# aws_sqs as sqs,
108
)
119
from constructs import Construct
1210
from cdklabs.generative_ai_cdk_constructs import (
@@ -15,8 +13,6 @@
1513
)
1614
from aws_cdk.aws_lambda_python_alpha import ( BundlingOptions,PythonFunction,PythonLayerVersion)
1715

18-
19-
2016
class BedrockOpensearchStack(Stack):
2117

2218
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
@@ -38,9 +34,11 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
3834
bucket= docBucket,
3935
knowledge_base=kb,
4036
data_source_name='books',
41-
chunking_strategy= bedrock.ChunkingStrategy.FIXED_SIZE,
42-
max_tokens=500,
43-
overlap_percentage=20
37+
chunking_strategy= bedrock.ChunkingStrategy.fixed_size(
38+
max_tokens=500,
39+
overlap_percentage=20
40+
)
41+
4442
)
4543

4644
## action group
@@ -52,41 +50,41 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
5250
layers= [_lambda.LayerVersion.from_layer_version_arn(self, 'PowerToolsLayer', f'arn:aws:lambda:{self.region}:017000801446:layer:AWSLambdaPowertoolsPythonV2:60')],
5351
timeout= Duration.minutes(2),
5452
bundling = BundlingOptions(build_args={"POETRY_VERSION": "1.7.0"})
55-
)
53+
)
54+
5655
ag = bedrock.AgentActionGroup(
57-
self,
58-
'ActionGroup',
59-
action_group_name='query-library',
56+
name='query-library',
6057
description= 'Use these functions to get information about the books in the library.',
61-
action_group_executor=bedrock.ActionGroupExecutor(
62-
lambda_=action_group_function,
58+
executor=bedrock.ActionGroupExecutor.fromlambda_function(
59+
action_group_function,
6360
),
64-
action_group_state= 'ENABLED',
65-
api_schema= bedrock.ApiSchema.from_asset(os.path.join(os.path.dirname(__file__), 'action-group.yaml'))
61+
enabled= True,
62+
api_schema= bedrock.ApiSchema.from_local_asset("./python_samples/action-group.yaml")
6663
)
6764

68-
## agent
65+
## agent
6966
agent = bedrock.Agent(
7067
self,
7168
"Agent",
72-
foundation_model=bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_V2_1,
69+
foundation_model=bedrock.BedrockFoundationModel.AMAZON_NOVA_MICRO_V1,
7370
instruction="You are a helpful and friendly agent that answers questions about insurance claims.",
7471
knowledge_bases=[kb],
75-
enable_user_input= True,
72+
user_input_enabled= True,
7673
should_prepare_agent=True
7774
)
7875

7976
## associate action group with agent
8077
agent.add_action_group(ag)
8178

8279
## agent alias
83-
agent.add_alias(
84-
alias_name= 'my-agent-alias',
85-
description='alias for my agent'
86-
)
87-
80+
agent_alias= bedrock.AgentAlias(self,
81+
'AgentAlias',
82+
description='alias for my agent',
83+
agent=agent)
84+
8885

8986
CfnOutput(self, "KnowledgeBaseId", value=kb.knowledge_base_id)
9087
CfnOutput(self, 'agentid', value= agent.agent_id)
9188
CfnOutput(self, 'DataSourceId', value= dataSource.data_source_id)
9289
CfnOutput(self, 'DocumentBucket', value= docBucket.bucket_name)
90+
CfnOutput(self, 'agent_alias', value= agent_alias.alias_name)

samples/python-samples/python_samples/bedrock_pinecone_stack.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,69 +34,70 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
3434

3535
)
3636

37-
kb = bedrock.KnowledgeBase(self, 'KnowledgeBase-pinecone',
37+
kb = bedrock.KnowledgeBase(self, 'KnowledgeBase-Aurora',
3838
vector_store= pineconevs,
3939
embeddings_model= bedrock.BedrockFoundationModel.COHERE_EMBED_ENGLISH_V3,
40-
instruction= 'Use this knowledge base to answer questions about books.It contains the full text of novels.',
41-
description= 'Pinecone knowledge base.'
40+
instruction= 'Use this knowledge base to answer questions about books. ' +
41+
'It contains the full text of novels.'
4242
)
4343

44-
docBucket = s3.Bucket(self, 'DockBucket-pinecone')
44+
docBucket = s3.Bucket(self, 'DockBucket-Aurora')
4545

46-
dataSource = bedrock.S3DataSource(self, 'DataSource-pinecone',
46+
dataSource= bedrock.S3DataSource(self, 'DataSource-Aurora',
4747
bucket= docBucket,
4848
knowledge_base=kb,
4949
data_source_name='books',
50-
chunking_strategy= bedrock.ChunkingStrategy.FIXED_SIZE,
51-
max_tokens=500,
52-
overlap_percentage=20
50+
chunking_strategy= bedrock.ChunkingStrategy.fixed_size(
51+
max_tokens=500,
52+
overlap_percentage=20
53+
)
5354
)
5455

55-
## action group
56+
## action group
5657
action_group_function = PythonFunction(
5758
self,
5859
"LambdaFunction",
5960
runtime=_lambda.Runtime.PYTHON_3_12,
6061
entry= os.path.join(os.path.dirname(__file__), '../lambda/action-group/'),
6162
layers= [_lambda.LayerVersion.from_layer_version_arn(self, 'PowerToolsLayer', f'arn:aws:lambda:{self.region}:017000801446:layer:AWSLambdaPowertoolsPythonV2:60')],
6263
timeout= Duration.minutes(2),
63-
bundling = BundlingOptions(build_args={"POETRY_VERSION": "1.7.0"}),
6464
)
65-
65+
6666
ag = bedrock.AgentActionGroup(
67-
self,
68-
'ActionGroup',
69-
action_group_name='query-library',
67+
name='query-library',
7068
description= 'Use these functions to get information about the books in the library.',
71-
action_group_executor=bedrock.ActionGroupExecutor(
72-
lambda_=action_group_function,
69+
executor=bedrock.ActionGroupExecutor.fromlambda_function(
70+
action_group_function,
7371
),
74-
action_group_state= 'ENABLED',
75-
api_schema= bedrock.ApiSchema.from_asset(os.path.join(os.path.dirname(__file__), 'action-group.yaml'))
76-
)
72+
enabled= True,
73+
api_schema= bedrock.ApiSchema.from_local_asset("./python_samples/action-group.yaml") )
7774

7875
## agent
7976
agent = bedrock.Agent(
8077
self,
8178
"Agent",
82-
foundation_model=bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_V2_1,
79+
foundation_model=bedrock.BedrockFoundationModel.AMAZON_NOVA_MICRO_V1,
8380
instruction="You are a helpful and friendly agent that answers questions about insurance claims.",
8481
knowledge_bases=[kb],
85-
enable_user_input= True,
82+
user_input_enabled= True,
8683
should_prepare_agent=True
8784
)
8885

8986
## associate action group with agent
9087
agent.add_action_group(ag)
9188

9289
## agent alias
93-
agent.add_alias(
94-
alias_name= 'pinecone-agent-alias',
95-
description='alias for my agent'
96-
)
97-
90+
agent_alias= bedrock.AgentAlias(self,
91+
'AgentAlias',
92+
description='alias for my agent',
93+
agent=agent)
94+
95+
9896

9997
CfnOutput(self, "KnowledgeBaseId", value=kb.knowledge_base_id)
10098
CfnOutput(self, 'agentid', value= agent.agent_id)
10199
CfnOutput(self, 'DataSourceId', value= dataSource.data_source_id)
102100
CfnOutput(self, 'DocumentBucket', value= docBucket.bucket_name)
101+
CfnOutput(self, 'agent_alias', value= agent_alias.alias_name)
102+
103+
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
aws-cdk-lib>=2.141.0
1+
aws-cdk-lib>=2.177.0
22
constructs>=10.0.0,<11.0.0
33
aws-cdk.aws-lambda-python-alpha>=2.141.0a0
4-
cdklabs.generative-ai-cdk-constructs>=0.1.158
4+
cdklabs.generative-ai-cdk-constructs>=0.1.290

0 commit comments

Comments
 (0)