Skip to content

Commit 215290a

Browse files
committed
Use L1 constructs from aws-bedrock instead of L2 from generative-ai-cdk-constructs
1 parent bd798d7 commit 215290a

File tree

2 files changed

+65
-66
lines changed

2 files changed

+65
-66
lines changed
Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,85 @@
11
import {Construct} from "constructs"
2-
import {bedrock} from "@cdklabs/generative-ai-cdk-constructs"
32
import {Role} from "aws-cdk-lib/aws-iam"
43
import {Bucket} from "aws-cdk-lib/aws-s3"
5-
import {
6-
ContentFilterType,
7-
ContentFilterStrength,
8-
ManagedWordFilterType,
9-
PIIType,
10-
GuardrailAction
11-
} from "@cdklabs/generative-ai-cdk-constructs/lib/cdk-lib/bedrock/guardrails/guardrail-filters"
4+
import * as bedrock from "aws-cdk-lib/aws-bedrock"
125

136
export interface VectorKnowledgeBaseProps {
147
kbName: string
15-
embeddingsModel: bedrock.BedrockFoundationModel
8+
embeddingsModel: string
169
docsBucket: Bucket
1710
bedrockExecutionRole: Role
11+
collectionArn: string
12+
vectorIndexName: string
1813
}
1914

2015
export class VectorKnowledgeBaseResources extends Construct {
21-
public readonly knowledgeBase: bedrock.VectorKnowledgeBase
22-
public readonly guardrail: bedrock.Guardrail
16+
public readonly knowledgeBase: bedrock.CfnKnowledgeBase
17+
public readonly guardrail: bedrock.CfnGuardrail
2318

2419
constructor(scope: Construct, id: string, props: VectorKnowledgeBaseProps) {
2520
super(scope, id)
2621

27-
this.guardrail = new bedrock.Guardrail(this, "Guardrail", {
22+
this.guardrail = new bedrock.CfnGuardrail(this, "Guardrail", {
2823
name: "eps-assist-guardrail",
2924
description: "Guardrail for EPS Assist Me Slackbot",
3025
blockedInputMessaging: "Your input was blocked.",
3126
blockedOutputsMessaging: "Your output was blocked.",
32-
contentFilters: [
33-
{
34-
type: ContentFilterType.SEXUAL,
35-
inputStrength: ContentFilterStrength.HIGH,
36-
outputStrength: ContentFilterStrength.HIGH
37-
},
38-
{
39-
type: ContentFilterType.VIOLENCE,
40-
inputStrength: ContentFilterStrength.HIGH,
41-
outputStrength: ContentFilterStrength.HIGH
42-
},
43-
{
44-
type: ContentFilterType.HATE,
45-
inputStrength: ContentFilterStrength.HIGH,
46-
outputStrength: ContentFilterStrength.HIGH
47-
},
48-
{
49-
type: ContentFilterType.INSULTS,
50-
inputStrength: ContentFilterStrength.HIGH,
51-
outputStrength: ContentFilterStrength.HIGH
52-
},
53-
{
54-
type: ContentFilterType.MISCONDUCT,
55-
inputStrength: ContentFilterStrength.HIGH,
56-
outputStrength: ContentFilterStrength.HIGH
57-
},
58-
{
59-
type: ContentFilterType.PROMPT_ATTACK,
60-
inputStrength: ContentFilterStrength.HIGH,
61-
outputStrength: ContentFilterStrength.NONE
62-
}
63-
],
64-
piiFilters: [
65-
{type: PIIType.General.EMAIL, action: GuardrailAction.ANONYMIZE},
66-
{type: PIIType.General.PHONE, action: GuardrailAction.ANONYMIZE},
67-
{type: PIIType.General.NAME, action: GuardrailAction.ANONYMIZE},
68-
{type: PIIType.Finance.CREDIT_DEBIT_CARD_NUMBER, action: GuardrailAction.BLOCK}
69-
],
70-
managedWordListFilters: [
71-
{type: ManagedWordFilterType.PROFANITY}
72-
]
27+
contentPolicyConfig: {
28+
filtersConfig: [
29+
{type: "SEXUAL", inputStrength: "HIGH", outputStrength: "HIGH"},
30+
{type: "VIOLENCE", inputStrength: "HIGH", outputStrength: "HIGH"},
31+
{type: "HATE", inputStrength: "HIGH", outputStrength: "HIGH"},
32+
{type: "INSULTS", inputStrength: "HIGH", outputStrength: "HIGH"},
33+
{type: "MISCONDUCT", inputStrength: "HIGH", outputStrength: "HIGH"},
34+
{type: "PROMPT_ATTACK", inputStrength: "HIGH", outputStrength: "NONE"}
35+
]
36+
},
37+
sensitiveInformationPolicyConfig: {
38+
piiEntitiesConfig: [
39+
{type: "EMAIL", action: "ANONYMIZE"},
40+
{type: "PHONE", action: "ANONYMIZE"},
41+
{type: "NAME", action: "ANONYMIZE"},
42+
{type: "CREDIT_DEBIT_CARD_NUMBER", action: "BLOCK"}
43+
]
44+
},
45+
wordPolicyConfig: {
46+
managedWordListsConfig: [{type: "PROFANITY"}]
47+
}
7348
})
7449

75-
// Main construct - let it create its own default OpenSearch collection
76-
this.knowledgeBase = new bedrock.VectorKnowledgeBase(this, "VectorKB", {
50+
this.knowledgeBase = new bedrock.CfnKnowledgeBase(this, "VectorKB", {
7751
name: props.kbName,
7852
description: "Knowledge base for EPS Assist Me Slackbot",
79-
embeddingsModel: props.embeddingsModel,
80-
existingRole: props.bedrockExecutionRole
53+
roleArn: props.bedrockExecutionRole.roleArn,
54+
knowledgeBaseConfiguration: {
55+
type: "VECTOR",
56+
vectorKnowledgeBaseConfiguration: {
57+
embeddingModelArn: `arn:aws:bedrock:eu-west-2::foundation-model/${props.embeddingsModel}`
58+
}
59+
},
60+
storageConfiguration: {
61+
type: "OPENSEARCH_SERVERLESS",
62+
opensearchServerlessConfiguration: {
63+
collectionArn: props.collectionArn,
64+
vectorIndexName: props.vectorIndexName,
65+
fieldMapping: {
66+
vectorField: "bedrock-knowledge-base-default-vector",
67+
textField: "AMAZON_BEDROCK_TEXT_CHUNK",
68+
metadataField: "AMAZON_BEDROCK_METADATA"
69+
}
70+
}
71+
}
8172
})
8273

83-
// Add S3 data source to knowledge base
84-
this.knowledgeBase.addS3DataSource({
85-
bucket: props.docsBucket
74+
new bedrock.CfnDataSource(this, "S3DataSource", {
75+
knowledgeBaseId: this.knowledgeBase.attrKnowledgeBaseId,
76+
name: "eps-assist-s3-datasource",
77+
dataSourceConfiguration: {
78+
type: "S3",
79+
s3Configuration: {
80+
bucketArn: props.docsBucket.bucketArn
81+
}
82+
}
8683
})
8784
}
8885
}

packages/cdk/stacks/EpsAssistMeStack.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as cdk from "aws-cdk-lib"
99
import * as iam from "aws-cdk-lib/aws-iam"
1010
import * as ops from "aws-cdk-lib/aws-opensearchserverless"
1111
import * as cr from "aws-cdk-lib/custom-resources"
12-
import {bedrock} from "@cdklabs/generative-ai-cdk-constructs"
12+
1313
import {nagSuppressions} from "../nagSuppressions"
1414
import {Apis} from "../resources/Apis"
1515
import {Functions} from "../resources/Functions"
@@ -144,9 +144,11 @@ export class EpsAssistMeStack extends Stack {
144144
// Create VectorKnowledgeBase construct
145145
const vectorKB = new VectorKnowledgeBaseResources(this, "VectorKB", {
146146
kbName: "eps-assist-kb",
147-
embeddingsModel: bedrock.BedrockFoundationModel.TITAN_EMBED_TEXT_V2_1024,
147+
embeddingsModel: EMBEDDING_MODEL,
148148
docsBucket: storage.kbDocsBucket.bucket,
149-
bedrockExecutionRole
149+
bedrockExecutionRole,
150+
collectionArn: `arn:aws:aoss:${region}:${account}:collection/${openSearchResources.collection.collection.attrId}`,
151+
vectorIndexName: VECTOR_INDEX_NAME
150152
})
151153

152154
// Create Functions construct
@@ -159,10 +161,10 @@ export class EpsAssistMeStack extends Stack {
159161
createIndexFunctionRole,
160162
slackBotTokenParameter: secrets.slackBotTokenParameter,
161163
slackSigningSecretParameter: secrets.slackSigningSecretParameter,
162-
guardrailId: vectorKB.guardrail.guardrailId,
163-
guardrailVersion: vectorKB.guardrail.guardrailVersion,
164+
guardrailId: vectorKB.guardrail.attrGuardrailId,
165+
guardrailVersion: vectorKB.guardrail.attrVersion,
164166
collectionId: openSearchResources.collection.collection.attrId,
165-
knowledgeBaseId: vectorKB.knowledgeBase.knowledgeBaseId,
167+
knowledgeBaseId: vectorKB.knowledgeBase.attrKnowledgeBaseId,
166168
region,
167169
account,
168170
slackBotTokenSecret: secrets.slackBotTokenSecret,

0 commit comments

Comments
 (0)