-
Notifications
You must be signed in to change notification settings - Fork 408
/
GenAI-FSI-Agent.yml
499 lines (473 loc) · 15.2 KB
/
GenAI-FSI-Agent.yml
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
AWSTemplateFormatVersion: "2010-09-09"
Description: "GenAI Financial Services Agent powered by Amazon Bedrock, Amazon DynamoDB, AWS Lambda, Amazon Lex, and Amazon Kendra"
Metadata:
LICENSE: >-
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: S3 Bucket, Lambda and Lex Deployment Package Keys, and Lambda Layer ARNs
Parameters:
- S3ArtifactBucket
- DataLoaderS3Key
- LambdaHandlerS3Key
- LexBotS3Key
- BedrockLangChainPDFRWLayerArn
- CfnresponseLayerArn
- Label:
default: GitHub Secrets Manager Configuration
Parameters:
- GitHubTokenSecretName
- Label:
default: Kendra Web Crawler Root Domain
Parameters:
- KendraWebCrawlerUrl
- Label:
default: Amplify Source Repository URL
Parameters:
- AmplifyRepository
ParameterLabels:
S3ArtifactBucket:
default: your-s3-bucket-name
DataLoaderS3Key:
default: /agent/lambda/data-loader/loader_deployment_package.zip
LambdaHandlerS3Key:
default: /agent/lambda/agent-handler/agent_deployment_package.zip
LexBotS3Key:
default: /agent/bot/lex.zip
BedrockLangChainPDFRWLayerArn:
default: bedrock-layer-arn
CfnresponseLayerArn:
default: cfnresponse-layer-arn
GitHubTokenSecretName:
default: your-github-token-secret-name
KendraWebCrawlerUrl:
default: your-kendra-root-domain
AmplifyRepository:
default: your-forked-repo-url
Parameters:
S3ArtifactBucket:
Description: S3 Bucket Containing Lambda Handler, Lambda Data Loader, and Lex Deployment Packages, along with Customer FAQ and Mortgage Application example documents.
Type: String
Default: your-s3-bucket-name
DataLoaderS3Key:
Description: S3 Key for DynamoDB data loader.
Type: String
Default: /agent/lambda/data-loader/loader_deployment_package.zip
LambdaHandlerS3Key:
Description: S3 Key for Lambda handler.
Type: String
Default: /agent/lambda/agent-handler/agent_deployment_package.zip
LexBotS3Key:
Description: S3 key for Lex bot deployment package.
Type: String
Default: /agent/bot/lex.zip
BedrockLangChainPDFRWLayerArn:
Description: Bedrock LangChain PDFRW Lambda layer ARN.
Type: String
Default: bedrock-layer-arn
CfnresponseLayerArn:
Description: cfnresponse Lambda layer ARN.
Type: String
Default: cfnresponse-layer-arn
GitHubTokenSecretName:
Description: GitHub PAT secret name.
Type: String
NoEcho: true
Default: your-github-token-secret-name
KendraWebCrawlerUrl:
Description: Kendra Web Crawler root domain URL.
Type: String
Default: your-kendra-root-domain
AmplifyRepository:
Description: Source repository for AWS Amplify frontend.
Type: String
Default: your-forked-repo-url
Resources:
UserPendingAccountsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub ${AWS::StackName}-UserPendingAccounts
AttributeDefinitions:
- AttributeName: userName
AttributeType: S
- AttributeName: planName
AttributeType: S
KeySchema:
- AttributeName: userName
KeyType: HASH
- AttributeName: planName
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: '3'
WriteCapacityUnits: '3'
SSESpecification:
SSEEnabled: True
UserExistingAccountsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub ${AWS::StackName}-UserExistingAccounts
AttributeDefinitions:
- AttributeName: userName
AttributeType: S
- AttributeName: planName
AttributeType: S
KeySchema:
- AttributeName: userName
KeyType: HASH
- AttributeName: planName
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: '3'
WriteCapacityUnits: '3'
SSESpecification:
SSEEnabled: True
ConversationIndexTable:
Type: 'AWS::DynamoDB::Table'
Properties:
TableName: !Sub ${AWS::StackName}-ConversationIndexTable
KeySchema:
- AttributeName: id
KeyType: HASH
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PAY_PER_REQUEST
SSESpecification:
SSEEnabled: True
ConversationTable:
Type: 'AWS::DynamoDB::Table'
Properties:
TableName: !Sub ${AWS::StackName}-ConversationTable
KeySchema:
- AttributeName: SessionId
KeyType: HASH
AttributeDefinitions:
- AttributeName: SessionId
AttributeType: S
BillingMode: PAY_PER_REQUEST
SSESpecification:
SSEEnabled: True
AgentHandlerServiceRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: !Sub ${AWS::StackName}-AgentHandlerServiceRole
AssumeRolePolicyDocument:
Statement:
- Action: 'sts:AssumeRole'
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Version: 2012-10-17
ManagedPolicyArns:
- !Join
- ''
- - 'arn:'
- !Ref 'AWS::Partition'
- ':iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
AgentHandlerServiceRoleDefaultPolicy:
Type: 'AWS::IAM::Policy'
Properties:
PolicyName: !Sub ${AWS::StackName}-AgentHandlerServiceRoleDefaultPolicy
PolicyDocument:
Statement:
- Action:
- dynamodb:BatchGetItem
- dynamodb:BatchWriteItem
- dynamodb:ConditionCheckItem
- dynamodb:DeleteItem
- dynamodb:DescribeTable
- dynamodb:GetItem
- dynamodb:GetRecords
- dynamodb:GetShardIterator
- dynamodb:PutItem
- dynamodb:Query
- dynamodb:Scan
- dynamodb:UpdateItem
- lambda:InvokeFunction
- bedrock:InvokeModel
- kendra:Query
- kendra:Retrieve
- kendra:BatchGetDocumentStatus
- s3:GetObject
- s3:PutObject
Effect: Allow
Resource: '*'
Version: 2012-10-17
Roles:
- !Ref AgentHandlerServiceRole
AgentHandlerFunction:
Type: AWS::Lambda::Function
Properties:
Description: Lambda handler for GenAI FSI Agent.
FunctionName: !Sub ${AWS::StackName}-GenAILexHandler
Code:
S3Bucket: !Ref S3ArtifactBucket
S3Key: !Ref LambdaHandlerS3Key
Runtime: python3.11
MemorySize: 512
Timeout: 30
Handler: lambda_function.handler
Layers:
- !Ref BedrockLangChainPDFRWLayerArn
Role: !GetAtt AgentHandlerServiceRole.Arn
Architectures:
- x86_64
Environment:
Variables:
USER_PENDING_ACCOUNTS_TABLE: !Ref UserPendingAccountsTable
USER_EXISTING_ACCOUNTS_TABLE: !Ref UserExistingAccountsTable
CONVERSATION_INDEX_TABLE: !Ref ConversationIndexTable
CONVERSATION_TABLE: !Ref ConversationTable
KENDRA_INDEX_ID: !GetAtt KendraIndex.Id
S3_ARTIFACT_BUCKET_NAME: !Ref S3ArtifactBucket
LexLambdaPermissions:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref AgentHandlerFunction
Principal: 'lexv2.amazonaws.com'
DataLoaderFunction:
Type: AWS::Lambda::Function
Properties:
Description: Lambda function to load the plan catalog.
FunctionName: !Sub ${AWS::StackName}-DDBDataLoader
Code:
S3Bucket: !Ref S3ArtifactBucket
S3Key: !Ref DataLoaderS3Key
Runtime: python3.11
MemorySize: 256
Timeout: 20
Handler: index.handler
Layers:
- !Ref CfnresponseLayerArn
Role: !GetAtt AgentHandlerServiceRole.Arn
Environment:
Variables:
USER_EXISTING_ACCOUNTS_TABLE: !Ref UserExistingAccountsTable
LoadPlanData:
Type: Custom::LoadDynamoDB
Properties:
ServiceToken: !GetAtt DataLoaderFunction.Arn
AmplifyRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-AmplifyRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- amplify.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: !Sub ${AWS::StackName}-Amplify
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'amplify:Create*'
- 'amplify:Get*'
- 'amplify:List*'
- 'amplify:Start*'
- 'amplify:Stop*'
- 'amplify:Update*'
Resource: '*'
AmplifyApp:
Type: AWS::Amplify::App
Properties:
Name: !Sub ${AWS::StackName}-AnyCompany-Website
Repository: !Ref AmplifyRepository
BuildSpec: |
frontend:
phases:
# IMPORTANT - Please verify your build commands
build:
commands: []
artifacts:
# IMPORTANT - Please verify your build output directory
baseDirectory: ./frontend/
files:
- '**/*'
cache:
paths: []
AccessToken: !Sub "{{resolve:secretsmanager:${GitHubTokenSecretName}}}"
Description: AnyCompany website
IAMServiceRole: !GetAtt AmplifyRole.Arn
AmplifyBranch:
Type: AWS::Amplify::Branch
Properties:
AppId: !GetAtt AmplifyApp.AppId
BranchName: main
KendraLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub ${AWS::StackName}-KendraLogGroup
RetentionInDays: 7
KendraLogStream:
Type: AWS::Logs::LogStream
Properties:
LogGroupName: !Ref KendraLogGroup
LogStreamName: !Sub ${AWS::StackName}-KendraLogStream
DependsOn: KendraLogGroup
KendraIndexRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-KendraIndexRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- kendra.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: !Sub ${AWS::StackName}-KendraIndexPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: cloudwatch:PutMetricData
Resource: '*'
- Effect: Allow
Action: logs:DescribeLogGroups
Resource: '*'
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:DescribeLogStreams'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: '*'
KendraIndex:
Type: AWS::Kendra::Index
Properties:
Edition: DEVELOPER_EDITION
Name: !Sub ${AWS::StackName}-KendraIndex
RoleArn: !GetAtt KendraIndexRole.Arn
KendraDataSourceRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-KendraDataSourceRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- kendra.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: !Sub ${AWS::StackName}-KendraDataSourcePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 's3:GetObject'
- 's3:GetBucketLocation'
- 's3:ListBucket'
- 's3:GetBucketAcl'
- 's3:GetObjectAcl'
Resource:
- !Sub arn:aws:s3:::${S3ArtifactBucket}
- !Sub arn:aws:s3:::${S3ArtifactBucket}/*
- Effect: Allow
Action:
- 'kendra:PutPrincipalMapping'
- 'kendra:DeletePrincipalMapping'
- 'kendra:ListGroupsOlderThanOrderingId'
- 'kendra:DescribePrincipalMapping'
- 'kendra:BatchPutDocument'
- 'kendra:BatchDeleteDocument'
Resource: '*'
KendraWebCrawler:
DependsOn: KendraIndex
Type: AWS::Kendra::DataSource
Properties:
Name: !Sub ${AWS::StackName}-WebCrawler
Type: WEBCRAWLER
IndexId: !GetAtt KendraIndex.Id
RoleArn: !GetAtt KendraDataSourceRole.Arn
DataSourceConfiguration:
WebCrawlerConfiguration:
CrawlDepth: 3
MaxContentSizePerPageInMegaBytes: 50
MaxLinksPerPage: 20
MaxUrlsPerMinuteCrawlRate: 100
Urls:
SeedUrlConfiguration:
SeedUrls:
- !Ref KendraWebCrawlerUrl
WebCrawlerMode: SUBDOMAINS
LexBotRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-LexBotRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lexv2.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: "/"
Policies:
- PolicyName: !Sub ${AWS::StackName}-LexBotRolePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'polly:SynthesizeSpeech'
- 'comprehend:DetectSentiment'
Resource: "*"
LexBot:
DependsOn: LexBotRole
Type: AWS::Lex::Bot
Properties:
Name: !Sub ${AWS::StackName}-FSI-Agent
BotFileS3Location:
S3Bucket: !Ref S3ArtifactBucket
S3ObjectKey: !Ref LexBotS3Key
DataPrivacy:
ChildDirected: false
Description: 'Financial Services Agent'
IdleSessionTTLInSeconds: 900
RoleArn: !GetAtt LexBotRole.Arn
Outputs:
AmplifyDemoWebsite:
Value: !Join ['', ['main.', !GetAtt AmplifyApp.DefaultDomain]]
AmplifyAppID:
Value: !GetAtt AmplifyApp.AppId
AmplifyBranch:
Value: !GetAtt AmplifyBranch.BranchName
KendraIndexID:
Value: !GetAtt KendraIndex.Id
KendraWebCrawlerDataSourceID:
Value: !GetAtt KendraWebCrawler.Id
KendraDataSourceRoleARN:
Value: !GetAtt KendraDataSourceRole.Arn
LambdaARN:
Value: !GetAtt AgentHandlerFunction.Arn
LexBotID:
Value: !GetAtt LexBot.Id