Skip to content

Commit 15cd2f6

Browse files
author
wowzoo
committed
bedrock demo
1 parent 1310da7 commit 15cd2f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2590
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ doc/labguide/Korean/~$S Korea AI ML Immersion Day Hands-on PilHoKim 23rd Novembe
3434
genai/aws-gen-ai-kr/30_fine_tune/reranker-kr/dataset/msmarco/
3535
genai/aws-gen-ai-kr/30_fine_tune/reranker-kr/dataset/translated/
3636
genai/aws-gen-ai-kr/30_fine_tune/reranker-kr/model/
37+
38+
genai/aws-gen-ai-kr/11_bedrock-with-opensearch/data/hanwhalife/
39+
genai/aws-gen-ai-kr/11_bedrock-with-opensearch/data/kt/
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
# Bedrock Demo
3+
This repository is a Gen AI demo using claude 3. It contains 5 pre-built examples which help customers getting started with the Amazon Bedrock.
4+
5+
![Pic 1.](architecture.png)
6+
7+
## Prerequisites
8+
- Make sure you have [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured with an aws account you want to use.
9+
- To build container image, [Docker Engine](https://docs.docker.com/engine/install/) must be installed.
10+
11+
### install cdk
12+
```shell
13+
npm install -g aws-cdk
14+
cdk --version
15+
```
16+
17+
### setting AWS_PROFILE
18+
```shell
19+
export AWS_PROFILE=[The configuration profile for aws-cli]
20+
```
21+
22+
## How to deploy
23+
24+
### Step 1. Create virtualenv
25+
After cloning this git repository. You need to create virtualenv.
26+
```shell
27+
cd bedrock-demo
28+
python3 -m venv .venv
29+
source .venv/bin/activate
30+
```
31+
32+
### Step 2. Install requirements
33+
```shell
34+
pip install -r requirements.txt
35+
```
36+
37+
### Step 3. Deploy CDK
38+
Before running the cdk deploy command, make sure the Docker Engine is running in the background.
39+
```shell
40+
cdk deploy BedrockDemo
41+
```
42+
43+
### Step 4. Set Environment Variables
44+
After successfully deploying the cdk, there are two variables in the output. One is the DNS name of the ALB and the other is the name of the S3 bucket. Copy these and make them environment variables.
45+
46+
![Pic 2.](cdk_output.png)
47+
48+
49+
```shell
50+
export ALB_URL=[The DNS name of the ALB]
51+
export BUCKET_NAME=[The name of the S3 bucket]
52+
```
53+
54+
### Step 5. Run streamlit
55+
```shell
56+
cd frontUI
57+
streamlit run app/Home.py
58+
```
59+
60+
![Pic 3.](run_streamlit.png)
61+
62+
The browser will open a demo page if streamlit is running successfully.
63+
64+
## Clean up resources
65+
If you've created anything by yourself, you'll need to delete it.
66+
67+
### Destroy Stack
68+
```shell
69+
cdk destroy BedrockDemo
70+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import aws_cdk as cdk
4+
5+
from bedrock_demo.config import app_config
6+
from bedrock_demo.deployment import BedrockDemo
7+
from cdk_nag import AwsSolutionsChecks, NagSuppressions
8+
9+
10+
app = cdk.App()
11+
conf = app_config['config']
12+
stack = BedrockDemo(
13+
app,
14+
'BedrockDemo',
15+
conf=conf,
16+
env=cdk.Environment(account=os.getenv('CDK_DEFAULT_ACCOUNT'), region=os.getenv('CDK_DEFAULT_REGION')),
17+
tags={'Project': 'bedrock-demo'}
18+
)
19+
cdk.Aspects.of(app).add(AwsSolutionsChecks())
20+
NagSuppressions.add_stack_suppressions(stack, [
21+
{'id': 'AwsSolutions-CB3', 'reason': 'allowed in this stack'},
22+
{'id': 'AwsSolutions-CB4', 'reason': 'allowed in this stack'},
23+
{'id': 'AwsSolutions-IAM4', 'reason': 'allowed in this stack'},
24+
{'id': 'AwsSolutions-IAM5', 'reason': 'allowed in this stack'},
25+
{'id': 'AwsSolutions-VPC7', 'reason': 'allowed in this stack'},
26+
{'id': 'AwsSolutions-ELB2', 'reason': 'allowed in this stack'},
27+
{'id': 'AwsSolutions-EC23', 'reason': 'allowed in this stack'},
28+
{'id': 'AwsSolutions-S1', 'reason': 'allowed in this stack'},
29+
{'id': 'AwsSolutions-S10', 'reason': 'allowed in this stack'},
30+
])
31+
app.synth()
94.3 KB
Loading

genai/genai-app-demo/01-prompt-engineering-demo/bedrock_demo/__init__.py

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from aws_cdk import (
2+
aws_iam as iam
3+
)
4+
5+
"""
6+
"containers/prompt": "prompt-func",
7+
"containers/summarization": "summary-func",
8+
"containers/identification": "identify-func",
9+
"containers/comment": "comment-func",
10+
"containers/shortform": "shortform-func"
11+
"""
12+
13+
ECR_LAMBDA_POLICY = iam.PolicyStatement(
14+
actions=[
15+
'ecr:BatchGetImage',
16+
'ecr:DeleteRepositoryPolicy',
17+
'ecr:GetDownloadUrlForLayer',
18+
'ecr:GetRepositoryPolicy',
19+
'ecr:SetRepositoryPolicy'
20+
],
21+
principals=[
22+
iam.ServicePrincipal("lambda.amazonaws.com")
23+
],
24+
effect=iam.Effect.ALLOW
25+
)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from aws_cdk import (
2+
RemovalPolicy, Duration,
3+
aws_ec2 as ec2,
4+
aws_ecr as ecr,
5+
aws_lambda as lambda_,
6+
aws_ecr_assets as ecr_assets,
7+
aws_iam as iam,
8+
aws_elasticloadbalancingv2 as elbv2,
9+
aws_elasticloadbalancingv2_targets as targets,
10+
)
11+
from constructs import Construct
12+
from . import ECR_LAMBDA_POLICY
13+
from cdk_ecr_deployment import DockerImageName, ECRDeployment
14+
15+
16+
class CommentFunc(Construct):
17+
18+
def __init__(self, scope: Construct, id_: str, vpc: ec2.Vpc, sg: ec2.SecurityGroup,
19+
role: iam.Role, listener: elbv2.ApplicationListener) -> None:
20+
super().__init__(scope, id_)
21+
22+
# Comment Repo
23+
self.ecr_repo = ecr.Repository(
24+
self,
25+
'CommentRepo',
26+
repository_name='comment-func',
27+
removal_policy=RemovalPolicy.DESTROY,
28+
empty_on_delete=True
29+
)
30+
31+
self.ecr_repo.add_to_resource_policy(
32+
statement=ECR_LAMBDA_POLICY
33+
)
34+
35+
image_asset = ecr_assets.DockerImageAsset(
36+
self,
37+
'CommentFuncDockerImage',
38+
directory='sample_source/containers/comment/',
39+
platform=ecr_assets.Platform.LINUX_AMD64
40+
)
41+
image_asset.node.add_dependency(self.ecr_repo)
42+
43+
ecr_deployment = ECRDeployment(
44+
self,
45+
'DeployCommentFuncImage',
46+
src=DockerImageName(image_asset.image_uri),
47+
dest=DockerImageName(
48+
f'{self.ecr_repo.repository_uri}:{image_asset.asset_hash}'
49+
)
50+
)
51+
ecr_deployment.node.add_dependency(image_asset)
52+
53+
lambda_func = lambda_.DockerImageFunction(
54+
self,
55+
'CommentFunc',
56+
function_name='CommentFunc',
57+
code=lambda_.DockerImageCode.from_ecr(
58+
repository=self.ecr_repo,
59+
tag_or_digest=image_asset.asset_hash
60+
),
61+
memory_size=512,
62+
timeout=Duration.seconds(300),
63+
role=role,
64+
vpc=vpc,
65+
vpc_subnets=ec2.SubnetSelection(
66+
subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS
67+
),
68+
security_groups=[sg],
69+
environment={
70+
'BEDROCK_REGION_NAME': 'us-west-2',
71+
}
72+
)
73+
lambda_func.node.add_dependency(ecr_deployment)
74+
75+
target_group = listener.add_targets(
76+
'CommentFuncTarget',
77+
target_group_name='comment-func-tg',
78+
targets=[targets.LambdaTarget(lambda_func)],
79+
conditions=[
80+
elbv2.ListenerCondition.path_patterns(['/comment'])
81+
],
82+
priority=1
83+
)
84+
target_group.node.add_dependency(lambda_func)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from aws_cdk import (
2+
RemovalPolicy, Duration,
3+
aws_ec2 as ec2,
4+
aws_ecr as ecr,
5+
aws_lambda as lambda_,
6+
aws_ecr_assets as ecr_assets,
7+
aws_iam as iam,
8+
aws_elasticloadbalancingv2 as elbv2,
9+
aws_elasticloadbalancingv2_targets as targets,
10+
)
11+
from constructs import Construct
12+
from . import ECR_LAMBDA_POLICY
13+
from cdk_ecr_deployment import DockerImageName, ECRDeployment
14+
15+
16+
class IdentifyFunc(Construct):
17+
18+
def __init__(self, scope: Construct, id_: str, vpc: ec2.Vpc, sg: ec2.SecurityGroup,
19+
role: iam.Role, listener: elbv2.ApplicationListener, bucket_name: str) -> None:
20+
super().__init__(scope, id_)
21+
22+
# Identify Repo
23+
self.ecr_repo = ecr.Repository(
24+
self,
25+
'IdentifyRepo',
26+
repository_name='identify-func',
27+
removal_policy=RemovalPolicy.DESTROY,
28+
empty_on_delete=True
29+
)
30+
31+
self.ecr_repo.add_to_resource_policy(
32+
statement=ECR_LAMBDA_POLICY
33+
)
34+
35+
image_asset = ecr_assets.DockerImageAsset(
36+
self,
37+
'IdentifyFuncDockerImage',
38+
directory='sample_source/containers/identification/',
39+
platform=ecr_assets.Platform.LINUX_AMD64
40+
)
41+
image_asset.node.add_dependency(self.ecr_repo)
42+
43+
ecr_deployment = ECRDeployment(
44+
self,
45+
'DeployIdentifyFuncImage',
46+
src=DockerImageName(image_asset.image_uri),
47+
dest=DockerImageName(
48+
f'{self.ecr_repo.repository_uri}:{image_asset.asset_hash}'
49+
)
50+
)
51+
ecr_deployment.node.add_dependency(image_asset)
52+
53+
lambda_func = lambda_.DockerImageFunction(
54+
self,
55+
'IdentifyFunc',
56+
function_name='IdentifyFunc',
57+
code=lambda_.DockerImageCode.from_ecr(
58+
repository=self.ecr_repo,
59+
tag_or_digest=image_asset.asset_hash
60+
),
61+
memory_size=512,
62+
timeout=Duration.seconds(300),
63+
role=role,
64+
vpc=vpc,
65+
vpc_subnets=ec2.SubnetSelection(
66+
subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS
67+
),
68+
security_groups=[sg],
69+
environment={
70+
'BEDROCK_REGION_NAME': 'us-west-2',
71+
'BUCKET_NAME': bucket_name
72+
}
73+
)
74+
lambda_func.node.add_dependency(ecr_deployment)
75+
76+
target_group = listener.add_targets(
77+
'IdentifyFuncTarget',
78+
target_group_name='identify-func-tg',
79+
targets=[targets.LambdaTarget(lambda_func)],
80+
conditions=[
81+
elbv2.ListenerCondition.path_patterns(['/identify'])
82+
],
83+
priority=2
84+
)
85+
target_group.node.add_dependency(lambda_func)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from aws_cdk import (
2+
RemovalPolicy, Duration,
3+
aws_ec2 as ec2,
4+
aws_ecr as ecr,
5+
aws_lambda as lambda_,
6+
aws_ecr_assets as ecr_assets,
7+
aws_iam as iam,
8+
aws_elasticloadbalancingv2 as elbv2,
9+
aws_elasticloadbalancingv2_targets as targets,
10+
)
11+
from constructs import Construct
12+
from . import ECR_LAMBDA_POLICY
13+
from cdk_ecr_deployment import DockerImageName, ECRDeployment
14+
15+
16+
class PromptFunc(Construct):
17+
18+
def __init__(self, scope: Construct, id_: str, vpc: ec2.Vpc, sg: ec2.SecurityGroup,
19+
role: iam.Role, listener: elbv2.ApplicationListener) -> None:
20+
super().__init__(scope, id_)
21+
22+
# Prompt Repo
23+
self.ecr_repo = ecr.Repository(
24+
self,
25+
'PromptRepo',
26+
repository_name='prompt-func',
27+
removal_policy=RemovalPolicy.DESTROY,
28+
empty_on_delete=True
29+
)
30+
31+
self.ecr_repo.add_to_resource_policy(
32+
statement=ECR_LAMBDA_POLICY
33+
)
34+
35+
image_asset = ecr_assets.DockerImageAsset(
36+
self,
37+
'PromptFuncDockerImage',
38+
directory='sample_source/containers/prompt/',
39+
platform=ecr_assets.Platform.LINUX_AMD64
40+
)
41+
image_asset.node.add_dependency(self.ecr_repo)
42+
43+
ecr_deployment = ECRDeployment(
44+
self,
45+
'DeployPromptFuncImage',
46+
src=DockerImageName(image_asset.image_uri),
47+
dest=DockerImageName(
48+
f'{self.ecr_repo.repository_uri}:{image_asset.asset_hash}'
49+
)
50+
)
51+
ecr_deployment.node.add_dependency(image_asset)
52+
53+
lambda_func = lambda_.DockerImageFunction(
54+
self,
55+
'PromptFunc',
56+
function_name='PromptFunc',
57+
code=lambda_.DockerImageCode.from_ecr(
58+
repository=self.ecr_repo,
59+
tag_or_digest=image_asset.asset_hash
60+
),
61+
memory_size=512,
62+
timeout=Duration.seconds(300),
63+
role=role,
64+
vpc=vpc,
65+
vpc_subnets=ec2.SubnetSelection(
66+
subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS
67+
),
68+
security_groups=[sg],
69+
environment={
70+
'BEDROCK_REGION_NAME': 'us-west-2',
71+
}
72+
)
73+
lambda_func.node.add_dependency(ecr_deployment)
74+
75+
target_group = listener.add_targets(
76+
'PromptFuncTarget',
77+
target_group_name='prompt-func-tg',
78+
targets=[targets.LambdaTarget(lambda_func)],
79+
conditions=[
80+
elbv2.ListenerCondition.path_patterns(['/prompt'])
81+
],
82+
priority=3
83+
)
84+
target_group.node.add_dependency(lambda_func)

0 commit comments

Comments
 (0)