Spaces:
Running
Running
File size: 4,493 Bytes
d202ada |
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 |
import * as kendra from 'aws-cdk-lib/aws-kendra';
import * as iam from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';
import { Duration, Token, Arn } from 'aws-cdk-lib';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
export interface RagProps {
}
/**
* RAG を実行するためのリソースを作成する
*/
export class Rag extends Construct {
constructor(scope: Construct, id: string, props: RagProps) {
super(scope, id);
const kendraIndexArnInCdkContext =
this.node.tryGetContext('kendraIndexArn');
let kendraIndexArn: string;
let kendraIndexId: string;
if (kendraIndexArnInCdkContext) {
// 既存の Kendra Index を利用する場合
kendraIndexArn = kendraIndexArnInCdkContext!;
kendraIndexId = Arn.extractResourceName(
kendraIndexArnInCdkContext,
'index'
);
} 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 index = new kendra.CfnIndex(this, 'KendraIndex', {
name: 'langflow-index',
edition: 'DEVELOPER_EDITION',
roleArn: indexRole.roleArn,
});
kendraIndexArn = Token.asString(index.getAtt('Arn'));
kendraIndexId = index.ref;
// WebCrawler を作成
const webCrawlerRole = new iam.Role(this, 'KendraWebCrawlerRole', {
assumedBy: new iam.ServicePrincipal('kendra.amazonaws.com'),
});
webCrawlerRole.addToPolicy(
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: [kendraIndexArn],
actions: ['kendra:BatchPutDocument', 'kendra:BatchDeleteDocument'],
})
);
new kendra.CfnDataSource(this, 'WebCrawler', {
indexId: kendraIndexId,
name: 'WebCrawler',
type: 'WEBCRAWLER',
roleArn: webCrawlerRole.roleArn,
languageCode: 'ja',
dataSourceConfiguration: {
webCrawlerConfiguration: {
urls: {
seedUrlConfiguration: {
webCrawlerMode: 'HOST_ONLY',
// デモ用に AWS の GenAI 関連のページを取り込む
seedUrls: [
'https://aws.amazon.com/jp/what-is/generative-ai/',
'https://aws.amazon.com/jp/generative-ai/',
'https://aws.amazon.com/jp/generative-ai/use-cases/',
'https://aws.amazon.com/jp/bedrock/',
'https://aws.amazon.com/jp/bedrock/features/',
'https://aws.amazon.com/jp/bedrock/testimonials/',
],
},
},
crawlDepth: 1,
urlInclusionPatterns: ['https://aws.amazon.com/jp/.*'],
},
},
});
}
// 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'],
})
);
}
} |