-
Notifications
You must be signed in to change notification settings - Fork 143
/
rag.ts
563 lines (508 loc) · 19.2 KB
/
rag.ts
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import * as cdk from 'aws-cdk-lib';
import * as events from 'aws-cdk-lib/aws-events';
import * as targets from 'aws-cdk-lib/aws-events-targets';
import * as kendra from 'aws-cdk-lib/aws-kendra';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as s3Deploy from 'aws-cdk-lib/aws-s3-deployment';
import * as stepfunctions from 'aws-cdk-lib/aws-stepfunctions';
import * as stepfunctionsTasks from 'aws-cdk-lib/aws-stepfunctions-tasks';
import { Construct } from 'constructs';
import { UserPool } from 'aws-cdk-lib/aws-cognito';
import { Duration, Token, Arn, RemovalPolicy } from 'aws-cdk-lib';
import {
AuthorizationType,
CognitoUserPoolsAuthorizer,
LambdaIntegration,
RestApi,
} from 'aws-cdk-lib/aws-apigateway';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
const KENDRA_STATE_CFN_PARAMETER_NAME = 'kendraState';
export interface RagProps {
userPool: UserPool;
api: RestApi;
}
interface IndexScheduleCron {
minute: string;
hour: string;
month: string;
weekDay: string;
}
class KendraIndexWithCfnParameter extends kendra.CfnIndex {
attrId: string;
attrArn: string;
constructor(
scope: Construct,
id: string,
props: kendra.CfnIndexProps,
kendraSwitchCfnCondition: cdk.CfnCondition
) {
super(scope, id, props);
this.attrId = cdk.Fn.conditionIf(
kendraSwitchCfnCondition.logicalId,
this.attrId, // kendraがオンの場合は、attrIdをそのまま返す
`` // kendraがオフの場合は、空文字列を設定しておく
).toString();
this.attrArn = cdk.Fn.conditionIf(
kendraSwitchCfnCondition.logicalId,
this.attrArn, // kendraがオンの場合は、attrArnをそのまま返す
`arn:aws:kendra:${cdk.Stack.of(this).region}:${cdk.Stack.of(this).account}:index/` // kendraがオフの場合は、index/以降を空文字列にする(IAMの許可をさせない)
).toString();
}
}
class KendraDataSourceWithCfnParameter extends kendra.CfnDataSource {
attrId: string;
attrArn: string;
constructor(
scope: Construct,
id: string,
props: kendra.CfnDataSourceProps,
kendraSwitchCfnCondition: cdk.CfnCondition
) {
super(scope, id, props);
this.attrId = cdk.Fn.conditionIf(
kendraSwitchCfnCondition.logicalId,
this.attrId, // kendraがオンの場合は、attrIdをそのまま返す
`` // kendraがオフの場合は、空文字列を設定しておく
).toString();
this.attrArn = cdk.Fn.conditionIf(
kendraSwitchCfnCondition.logicalId,
this.attrArn, // kendraがオンの場合は、attrArnをそのまま返す
`arn:aws:kendra:${cdk.Stack.of(this).region}:${cdk.Stack.of(this).account}:index/*/data-source/` // kendraがオフの場合は、index/以降を空文字列にする(IAMの許可をさせない)
).toString();
}
}
/**
* RAG を実行するためのリソースを作成する
*/
export class Rag extends Construct {
public readonly dataSourceBucketName?: string;
constructor(scope: Construct, id: string, props: RagProps) {
super(scope, id);
const kendraIndexArnInCdkContext =
this.node.tryGetContext('kendraIndexArn');
const kendraDataSourceBucketName = this.node.tryGetContext(
'kendraDataSourceBucketName'
);
const kendraIndexScheduleEnabled: boolean = this.node.tryGetContext(
'kendraIndexScheduleEnabled'
);
const kendraIndexScheduleCreateCron: IndexScheduleCron | null =
this.node.tryGetContext('kendraIndexScheduleCreateCron');
const kendraIndexScheduleDeleteCron: IndexScheduleCron | null =
this.node.tryGetContext('kendraIndexScheduleDeleteCron');
let kendraIndexArn: string;
let kendraIndexId: string;
let dataSourceBucket: s3.IBucket | null = null;
if (kendraIndexArnInCdkContext) {
// 既存の Kendra Index を利用する場合
kendraIndexArn = kendraIndexArnInCdkContext!;
kendraIndexId = Arn.extractResourceName(
kendraIndexArnInCdkContext,
'index'
);
// 既存の S3 データソースを利用する場合は、バケット名からオブジェクトを生成
if (kendraDataSourceBucketName) {
dataSourceBucket = s3.Bucket.fromBucketName(
this,
'DataSourceBucket',
kendraDataSourceBucketName
);
}
} else {
// 新規に Kendra Index を作成する場合
const indexRole = new iam.Role(this, 'KendraIndexRole', {
assumedBy: new iam.ServicePrincipal('kendra.amazonaws.com'),
});
indexRole.addToPolicy(
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: ['*'],
actions: ['s3:GetObject'],
})
);
indexRole.addManagedPolicy(
iam.ManagedPolicy.fromAwsManagedPolicyName('CloudWatchLogsFullAccess')
);
const accessLogsBucket = new s3.Bucket(
this,
'DataSourceAccessLogsBucket',
{
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
encryption: s3.BucketEncryption.S3_MANAGED,
autoDeleteObjects: true,
removalPolicy: RemovalPolicy.DESTROY,
objectOwnership: s3.ObjectOwnership.OBJECT_WRITER,
enforceSSL: true,
}
);
// .pdf や .txt などのドキュメントを格納する S3 Bucket
dataSourceBucket = new s3.Bucket(this, 'DataSourceBucket', {
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
encryption: s3.BucketEncryption.S3_MANAGED,
autoDeleteObjects: true,
removalPolicy: RemovalPolicy.DESTROY,
objectOwnership: s3.ObjectOwnership.OBJECT_WRITER,
serverAccessLogsBucket: accessLogsBucket,
serverAccessLogsPrefix: 'AccessLogs/',
enforceSSL: true,
});
// /kendra/docs ディレクトリを Bucket にアップロードする
new s3Deploy.BucketDeployment(this, 'DeployDocs', {
sources: [s3Deploy.Source.asset('./rag-docs')],
destinationBucket: dataSourceBucket,
// 以前の設定で同 Bucket にアクセスログが残っている可能性があるため、この設定は残す
exclude: ['AccessLogs/*', 'logs*'],
});
let index: kendra.CfnIndex;
const indexProps: kendra.CfnIndexProps = {
name: 'generative-ai-use-cases-index',
edition: 'DEVELOPER_EDITION',
roleArn: indexRole.roleArn,
// トークンベースのアクセス制御を実施
// 参考: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-kendra-index.html#cfn-kendra-index-usercontextpolicy
userContextPolicy: 'USER_TOKEN',
// 認可に利用する Cognito の情報を設定
userTokenConfigurations: [
{
jwtTokenTypeConfiguration: {
keyLocation: 'URL',
userNameAttributeField: 'cognito:username',
groupAttributeField: 'cognito:groups',
url: `${props.userPool.userPoolProviderUrl}/.well-known/jwks.json`,
},
},
],
};
let kendraIsOnCfnCondition;
if (kendraIndexScheduleEnabled) {
// Cloudfomation Parameterの読み込み
const kendraStateCfnParameter = new cdk.CfnParameter(
scope,
KENDRA_STATE_CFN_PARAMETER_NAME,
{
// NOTE contructの名前が付加されないように、thisではなくscopeを指定する
type: 'String',
description:
'parameter to create kendra index. on: create kendra index, off: delete kendra index.',
allowedValues: ['on', 'off'],
default: 'on',
}
);
kendraIsOnCfnCondition = new cdk.CfnCondition(
scope,
'IsKendraOnCondition',
{
expression: cdk.Fn.conditionEquals(
kendraStateCfnParameter.valueAsString,
'on'
),
}
);
index = new KendraIndexWithCfnParameter(
this,
'KendraIndex',
indexProps,
kendraIsOnCfnCondition
);
index.cfnOptions.condition = kendraIsOnCfnCondition; // Cfn Parameterに応じて、リソースをオンオフする
kendraIndexArn = index.attrArn;
kendraIndexId = index.attrId;
} else {
index = new kendra.CfnIndex(this, 'KendraIndex', indexProps);
kendraIndexArn = Token.asString(index.getAtt('Arn'));
kendraIndexId = index.ref;
}
const s3DataSourceRole = new iam.Role(this, 'DataSourceRole', {
assumedBy: new iam.ServicePrincipal('kendra.amazonaws.com'),
});
s3DataSourceRole.addToPolicy(
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: [`arn:aws:s3:::${dataSourceBucket.bucketName}`],
actions: ['s3:ListBucket'],
})
);
s3DataSourceRole.addToPolicy(
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: [`arn:aws:s3:::${dataSourceBucket.bucketName}/*`],
actions: ['s3:GetObject'],
})
);
s3DataSourceRole.addToPolicy(
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: [index.attrArn],
actions: ['kendra:BatchPutDocument', 'kendra:BatchDeleteDocument'],
})
);
let dataSource: kendra.CfnDataSource;
const dataSourceProps: kendra.CfnDataSourceProps = {
indexId: index.attrId,
type: 'S3',
name: 's3-data-source',
roleArn: s3DataSourceRole.roleArn,
languageCode: 'ja',
dataSourceConfiguration: {
s3Configuration: {
bucketName: dataSourceBucket.bucketName,
inclusionPrefixes: ['docs'],
},
},
};
if (kendraIndexScheduleEnabled) {
dataSource = new KendraDataSourceWithCfnParameter(
this,
'S3DataSource',
dataSourceProps,
kendraIsOnCfnCondition as cdk.CfnCondition
);
dataSource.cfnOptions.condition = kendraIsOnCfnCondition; // Cfn Parameterに応じて、リソースをオンオフする
} else {
dataSource = new kendra.CfnDataSource(
this,
'S3DataSource',
dataSourceProps
);
}
dataSource.addDependency(index);
if (kendraIndexScheduleEnabled) {
if (kendraIndexScheduleCreateCron) {
const taskStartDataSourceSyncJob =
new stepfunctionsTasks.CallAwsService(
this,
'TaskStartDataSourceSyncJob',
{
service: 'kendra',
action: 'startDataSourceSyncJob',
parameters: {
IndexId: index.attrId,
Id: dataSource.attrId,
},
iamResources: [
// NOTE インデックス・データソースの両方に対する権限が必要
index.attrArn,
dataSource.attrArn,
],
}
);
const definitionStartDataSourceSyncJob = stepfunctions.Chain.start(
taskStartDataSourceSyncJob
);
const stateMachineStartDataSourceSyncJob =
new stepfunctions.StateMachine(
this,
'StepFunctionsStateMachineStartDataSourceSyncJob',
{
definitionBody: stepfunctions.DefinitionBody.fromChainable(
definitionStartDataSourceSyncJob
),
timeout: cdk.Duration.minutes(180),
}
);
// Kendra On用のStep Functions
const taskUpdateCloudformationStackWithKendraOn =
new stepfunctionsTasks.CallAwsService(
this,
'TaskUpdateCloudformationStackWithKendraOn',
{
service: 'cloudformation',
action: 'updateStack',
parameters: {
StackName: cdk.Stack.of(this).stackName,
UsePreviousTemplate: true,
Parameters: [
{
ParameterKey: KENDRA_STATE_CFN_PARAMETER_NAME,
ParameterValue: 'on',
},
],
Capabilities: ['CAPABILITY_IAM'],
},
iamResources: [cdk.Stack.of(this).stackId], // NOTE stackId (arn:aws:cloudformation:ap-northeast-1:123456789012:stack/myStack/i-01234567890abcdef0) can be used an Resource ARN
}
);
const taskCheckCloudformationState =
new stepfunctionsTasks.CallAwsService(
this,
'TaskCheckCloudformationState',
{
service: 'cloudformation',
action: 'describeStacks',
parameters: {
StackName: cdk.Stack.of(this).stackName,
},
iamResources: [cdk.Stack.of(this).stackId], // NOTE stackId (arn:aws:cloudformation:ap-northeast-1:123456789012:stack/myStack/i-01234567890abcdef0) can be used an Resource ARN
}
);
const taskCallStartDataSourceSyncJob =
new stepfunctionsTasks.StepFunctionsStartExecution(
this,
'TaskCallStateMachineStartDataSourceSyncJob',
{
stateMachine: stateMachineStartDataSourceSyncJob,
integrationPattern: stepfunctions.IntegrationPattern.RUN_JOB,
}
);
const definitionKendraOn = stepfunctions.Chain.start(
taskUpdateCloudformationStackWithKendraOn
)
.next(taskCheckCloudformationState)
.next(
new stepfunctions.Choice(
this,
'TaskChoiceWithCloudformationState'
)
.when(
stepfunctions.Condition.stringEquals(
'$.Stacks[0].StackStatus',
'UPDATE_IN_PROGRESS'
), // 完了するまでループ
new stepfunctions.Wait(this, 'TaskWaitCloudformationChange', {
time: stepfunctions.WaitTime.duration(
cdk.Duration.minutes(5)
),
}).next(taskCheckCloudformationState) // ループ
)
.otherwise(
// 完了したら、次ステップ
// データソースSyncのStateMachineを呼び出す
taskCallStartDataSourceSyncJob
)
);
const stateMachineKendraOn = new stepfunctions.StateMachine(
this,
'StepFunctionsStateMachineKendraOn',
{
definitionBody:
stepfunctions.DefinitionBody.fromChainable(definitionKendraOn),
timeout: cdk.Duration.minutes(180),
}
);
// cronJobKendraOn
new events.Rule(this, 'CronJobKendraOn', {
schedule: events.Schedule.cron({
minute: kendraIndexScheduleCreateCron.minute,
hour: kendraIndexScheduleCreateCron.hour,
month: kendraIndexScheduleCreateCron.month,
weekDay: kendraIndexScheduleCreateCron.weekDay,
}), // NOTE UTC時間で指定
targets: [new targets.SfnStateMachine(stateMachineKendraOn, {})],
});
}
if (kendraIndexScheduleDeleteCron) {
// Kendra Off用のStep Function
const taskUpdateCloudformationStackWithKendraOff =
new stepfunctionsTasks.CallAwsService(
this,
'TaskUpdateCloudformationStackWithKendraOff',
{
service: 'cloudformation',
action: 'updateStack',
parameters: {
StackName: cdk.Stack.of(this).stackName,
UsePreviousTemplate: true,
Parameters: [
{
ParameterKey: KENDRA_STATE_CFN_PARAMETER_NAME,
ParameterValue: 'off',
},
],
Capabilities: ['CAPABILITY_IAM'],
},
iamResources: [cdk.Stack.of(this).stackId],
}
);
const definitionKendraOff = stepfunctions.Chain.start(
taskUpdateCloudformationStackWithKendraOff
);
const stateMachineKendraOff = new stepfunctions.StateMachine(
this,
'StepFunctionsStateMachineKendraOff',
{
definitionBody:
stepfunctions.DefinitionBody.fromChainable(definitionKendraOff),
timeout: cdk.Duration.minutes(180),
}
);
// cronJobKendraOff
new events.Rule(this, 'CronJobKendraOff', {
schedule: events.Schedule.cron({
minute: kendraIndexScheduleDeleteCron.minute,
hour: kendraIndexScheduleDeleteCron.hour,
month: kendraIndexScheduleDeleteCron.month,
weekDay: kendraIndexScheduleDeleteCron.weekDay,
}), // NOTE UTC時間で指定
targets: [new targets.SfnStateMachine(stateMachineKendraOff, {})],
});
}
}
}
// RAG 関連の API を追加する
// Lambda
const queryFunction = new NodejsFunction(this, 'Query', {
runtime: Runtime.NODEJS_18_X,
entry: './lambda/queryKendra.ts',
timeout: Duration.minutes(15),
bundling: {
// 新しい Kendra の機能を使うため、AWS SDK を明示的にバンドルする
externalModules: [],
},
environment: {
INDEX_ID: kendraIndexId,
},
});
queryFunction.role?.addToPrincipalPolicy(
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: [kendraIndexArn],
actions: ['kendra:Query'],
})
);
const retrieveFunction = new NodejsFunction(this, 'Retrieve', {
runtime: Runtime.NODEJS_18_X,
entry: './lambda/retrieveKendra.ts',
timeout: Duration.minutes(15),
bundling: {
// 新しい Kendra の機能を使うため、AWS SDK を明示的にバンドルする
externalModules: [],
},
environment: {
INDEX_ID: kendraIndexId,
},
});
retrieveFunction.role?.addToPrincipalPolicy(
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: [kendraIndexArn],
actions: ['kendra:Retrieve'],
})
);
// API Gateway
const authorizer = new CognitoUserPoolsAuthorizer(this, 'Authorizer', {
cognitoUserPools: [props.userPool],
});
const commonAuthorizerProps = {
authorizationType: AuthorizationType.COGNITO,
authorizer,
};
const ragResource = props.api.root.addResource('rag');
const queryResource = ragResource.addResource('query');
// POST: /rag/query
queryResource.addMethod(
'POST',
new LambdaIntegration(queryFunction),
commonAuthorizerProps
);
const retrieveResource = ragResource.addResource('retrieve');
// POST: /rag/retrieve
retrieveResource.addMethod(
'POST',
new LambdaIntegration(retrieveFunction),
commonAuthorizerProps
);
this.dataSourceBucketName = dataSourceBucket?.bucketName;
}
}