-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
195 lines (174 loc) · 7.49 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import aws_cdk as cdk
from aws_cdk import (
Duration,
Stack,
aws_lex as lex,
aws_iam as iam,
aws_lambda as lambda_,
)
from constructs import Construct
class LexGenAIServerless(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# Iam role for bot to invoke lambda
lex_cfn_role = iam.Role(
self,
"CfnLexGenAIDemoRole",
assumed_by=iam.ServicePrincipal("lexv2.amazonaws.com"),
)
lex_cfn_role.add_managed_policy(
iam.ManagedPolicy.from_aws_managed_policy_name("AWSLambdaExecute")
)
# lambda layer containing boto3, Pillow for image processing, and Pyshortener for shortening the pre-signed
# s3 url.
layer = lambda_.LayerVersion(
self,
"Boto3Layer",
code=lambda_.Code.from_asset("./Boto3PillowPyshorteners.zip"),
compatible_runtimes=[lambda_.Runtime.PYTHON_3_9],
)
# lambda function for processing the incoming request from Lex Chatbot
lambda_function = lambda_.Function(
self,
"LexGenAIBotLambda",
runtime=lambda_.Runtime.PYTHON_3_9,
handler="TextGeneration.lambda_handler",
code=lambda_.Code.from_asset("TextGeneration.zip"),
layers=[layer],
timeout=Duration.minutes(10),
memory_size=2048,
environment={"environment": "dev"},
)
# lambda policy
lambda_function.add_to_role_policy(
iam.PolicyStatement(
actions=[
"s3:GetObject",
"s3:PutObject",
"s3:ListBucket",
"s3:DeleteObject",
],
resources=["*"],
)
)
lambda_function.add_to_role_policy(
iam.PolicyStatement(
actions=["lex:*", "logs:*"],
resources=["*"],
)
)
lambda_function.add_to_role_policy(
iam.PolicyStatement(
actions=["bedrock:InvokeModel"],
resources=[
f"arn:aws:bedrock:{self.region}::foundation-model/anthropic.claude-v2"
],
)
)
lambda_function.grant_invoke(iam.ServicePrincipal("lexv2.amazonaws.com"))
### BOT SETUP
# alias settings, where we define the lambda function with the ECR container with our LLM dialog code (defined in the lex-gen-ai-demo-docker-image directory)
# test bot alias for demo, create a dedicated alias for serving traffic
bot_alias_settings = lex.CfnBot.TestBotAliasSettingsProperty(
bot_alias_locale_settings=[
lex.CfnBot.BotAliasLocaleSettingsItemProperty(
bot_alias_locale_setting=lex.CfnBot.BotAliasLocaleSettingsProperty(
enabled=True,
code_hook_specification=lex.CfnBot.CodeHookSpecificationProperty(
lambda_code_hook=lex.CfnBot.LambdaCodeHookProperty(
code_hook_interface_version="1.0",
lambda_arn=lambda_function.function_arn,
)
),
),
locale_id="en_US",
)
]
)
# lambda itself is tied to alias but codehook settings are intent specific
initial_response_codehook_settings = lex.CfnBot.InitialResponseSettingProperty(
code_hook=lex.CfnBot.DialogCodeHookInvocationSettingProperty(
enable_code_hook_invocation=True,
is_active=True,
post_code_hook_specification=lex.CfnBot.PostDialogCodeHookInvocationSpecificationProperty(),
)
)
# attaching a closing setting for Welcome intent when the intent is fulfilled.
intent_closing_setting_property = lex.CfnBot.IntentClosingSettingProperty(
closing_response=lex.CfnBot.ResponseSpecificationProperty(
message_groups_list=[
lex.CfnBot.MessageGroupProperty(
message=lex.CfnBot.MessageProperty(
plain_text_message=lex.CfnBot.PlainTextMessageProperty(
value="Hi there, I'm GenAI Bot. How can I help you?"
)
)
)
]
)
)
# Welcome Intent
welcome_intent = lex.CfnBot.IntentProperty(
name="WelcomeIntent",
initial_response_setting=initial_response_codehook_settings,
sample_utterances=[
lex.CfnBot.SampleUtteranceProperty(utterance="Hi"),
lex.CfnBot.SampleUtteranceProperty(utterance="Hey there"),
lex.CfnBot.SampleUtteranceProperty(utterance="Hello"),
lex.CfnBot.SampleUtteranceProperty(utterance="I need some help"),
lex.CfnBot.SampleUtteranceProperty(utterance="Help needed"),
lex.CfnBot.SampleUtteranceProperty(utterance="Can I get some help?"),
],
intent_closing_setting=intent_closing_setting_property,
)
# Text generation Intent
text_gen_intent = lex.CfnBot.IntentProperty(
name="GenerateTextIntent",
initial_response_setting=initial_response_codehook_settings,
fulfillment_code_hook=lex.CfnBot.FulfillmentCodeHookSettingProperty(
enabled=True,
is_active=True,
post_fulfillment_status_specification=lex.CfnBot.PostFulfillmentStatusSpecificationProperty(),
),
sample_utterances=[
lex.CfnBot.SampleUtteranceProperty(utterance="Generate content for"),
lex.CfnBot.SampleUtteranceProperty(utterance="Create text "),
lex.CfnBot.SampleUtteranceProperty(utterance="Create a response for "),
lex.CfnBot.SampleUtteranceProperty(
utterance="Text to be generated for"
),
],
)
# Fallback Intent
fallback_intent = lex.CfnBot.IntentProperty(
name="FallbackIntent",
parent_intent_signature="AMAZON.FallbackIntent",
initial_response_setting=initial_response_codehook_settings,
fulfillment_code_hook=lex.CfnBot.FulfillmentCodeHookSettingProperty(
enabled=True,
is_active=True,
post_fulfillment_status_specification=lex.CfnBot.PostFulfillmentStatusSpecificationProperty(),
),
)
# Create actual Lex Bot
cfn_bot = lex.CfnBot(
self,
"LexGenAIBot",
data_privacy={"ChildDirected": "false"},
idle_session_ttl_in_seconds=300,
name="LexGenAIBot",
description="Bot created demonstration of GenAI capabilities.",
role_arn=lex_cfn_role.role_arn,
bot_locales=[
lex.CfnBot.BotLocaleProperty(
locale_id="en_US",
nlu_confidence_threshold=0.4,
intents=[welcome_intent, text_gen_intent, fallback_intent],
)
],
test_bot_alias_settings=bot_alias_settings,
auto_build_bot_locales=True,
)
app = cdk.App()
filestack = LexGenAIServerless(app, "LexGenAIServerlessStack")
app.synth()