Skip to content

Commit 4a86e9a

Browse files
committed
Add VectorKnowledgeBase construct for knowledge base and guardrail
1 parent 27c6490 commit 4a86e9a

File tree

4 files changed

+106
-140
lines changed

4 files changed

+106
-140
lines changed

packages/cdk/constructs/BedrockGuardrail.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

packages/cdk/resources/BedrockResources.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import {Construct} from "constructs"
2+
import {bedrock} from "@cdklabs/generative-ai-cdk-constructs"
3+
import {Role} from "aws-cdk-lib/aws-iam"
4+
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"
12+
13+
export interface VectorKnowledgeBaseProps {
14+
kbName: string
15+
embeddingsModel: bedrock.BedrockFoundationModel
16+
docsBucket: Bucket
17+
bedrockExecutionRole: Role
18+
}
19+
20+
export class VectorKnowledgeBase extends Construct {
21+
public readonly knowledgeBase: bedrock.VectorKnowledgeBase
22+
public readonly guardrail: bedrock.Guardrail
23+
24+
constructor(scope: Construct, id: string, props: VectorKnowledgeBaseProps) {
25+
super(scope, id)
26+
27+
this.guardrail = new bedrock.Guardrail(this, "Guardrail", {
28+
name: "eps-assist-guardrail",
29+
description: "Guardrail for EPS Assist Me Slackbot",
30+
blockedInputMessaging: "Your input was blocked.",
31+
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+
]
73+
})
74+
75+
// Main construct - let it create its own default OpenSearch collection
76+
this.knowledgeBase = new bedrock.VectorKnowledgeBase(this, "VectorKB", {
77+
name: props.kbName,
78+
description: "Knowledge base for EPS Assist Me Slackbot",
79+
embeddingsModel: props.embeddingsModel,
80+
existingRole: props.bedrockExecutionRole
81+
})
82+
83+
// Add S3 data source to knowledge base
84+
this.knowledgeBase.addS3DataSource({
85+
bucket: props.docsBucket
86+
})
87+
}
88+
}

packages/cdk/stacks/EpsAssistMeStack.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import {
44
StackProps,
55
CfnOutput
66
} from "aws-cdk-lib"
7-
87
import {PolicyStatement} from "aws-cdk-lib/aws-iam"
98
import * as cdk from "aws-cdk-lib"
109
import * as iam from "aws-cdk-lib/aws-iam"
1110
import * as ops from "aws-cdk-lib/aws-opensearchserverless"
1211
import * as cr from "aws-cdk-lib/custom-resources"
12+
import {bedrock} from "@cdklabs/generative-ai-cdk-constructs"
1313
import {nagSuppressions} from "../nagSuppressions"
1414
import {Apis} from "../resources/Apis"
1515
import {Functions} from "../resources/Functions"
1616
import {Storage} from "../resources/Storage"
1717
import {Secrets} from "../resources/Secrets"
1818
import {OpenSearchResources} from "../resources/OpenSearchResources"
19-
import {BedrockResources} from "../resources/BedrockResources"
19+
import {VectorKnowledgeBase} from "../resources/VectorKnowledgeBase"
2020

2121
const EMBEDDING_MODEL = "amazon.titan-embed-text-v2:0"
2222
const VECTOR_INDEX_NAME = "eps-assist-os-index"
@@ -83,9 +83,7 @@ export class EpsAssistMeStack extends Stack {
8383
bedrockExecutionRole.addToPolicy(bedrockKBDeleteRolePolicy)
8484

8585
// Create Storage construct
86-
const storage = new Storage(this, "Storage", {
87-
bedrockExecutionRole
88-
})
86+
const storage = new Storage(this, "Storage", {bedrockExecutionRole})
8987

9088
// Create an IAM policy for S3 access
9189
const s3AccessListPolicy = new PolicyStatement({
@@ -143,15 +141,15 @@ export class EpsAssistMeStack extends Stack {
143141

144142
const endpoint = openSearchResources.collection.endpoint
145143

146-
// Create Bedrock Resources
147-
const bedrockResources = new BedrockResources(this, "BedrockResources", {
148-
bedrockExecutionRole,
149-
osCollection: openSearchResources.collection.collection,
150-
kbDocsBucket: storage.kbDocsBucket.bucket,
151-
region
144+
// Create VectorKnowledgeBase construct
145+
const vectorKB = new VectorKnowledgeBase(this, "VectorKB", {
146+
kbName: "eps-assist-kb",
147+
embeddingsModel: bedrock.BedrockFoundationModel.TITAN_EMBED_TEXT_V2_1024,
148+
docsBucket: storage.kbDocsBucket.bucket,
149+
bedrockExecutionRole
152150
})
153151

154-
// Create Functions construct
152+
// Functions construct: use guardrail and kb from the new construct
155153
const functions = new Functions(this, "Functions", {
156154
stackName: props.stackName,
157155
version: props.version,
@@ -161,10 +159,10 @@ export class EpsAssistMeStack extends Stack {
161159
createIndexFunctionRole,
162160
slackBotTokenParameter: secrets.slackBotTokenParameter,
163161
slackSigningSecretParameter: secrets.slackSigningSecretParameter,
164-
guardrailId: bedrockResources.guardrail.guardrailId,
165-
guardrailVersion: bedrockResources.guardrail.guardrailVersionId,
162+
guardrailId: vectorKB.guardrail.guardrailId,
163+
guardrailVersion: vectorKB.guardrail.guardrailVersion,
166164
collectionId: openSearchResources.collection.collection.attrId,
167-
knowledgeBaseId: bedrockResources.knowledgeBase.attrKnowledgeBaseId,
165+
knowledgeBaseId: vectorKB.knowledgeBase.knowledgeBaseId,
168166
region,
169167
account,
170168
slackBotTokenSecret: secrets.slackBotTokenSecret,
@@ -191,6 +189,7 @@ export class EpsAssistMeStack extends Stack {
191189
})
192190
openSearchResources.collection.collection.addDependency(aossAccessPolicy)
193191

192+
// Create a custom resource to create the OpenSearch index
194193
const vectorIndex = new cr.AwsCustomResource(this, "VectorIndex", {
195194
installLatestAwsSdk: true,
196195
onCreate: {
@@ -235,10 +234,10 @@ export class EpsAssistMeStack extends Stack {
235234
vectorIndex.node.addDependency(openSearchResources.collection.collection)
236235

237236
// add a dependency for bedrock kb on the custom resource. Enables vector index to be created before KB
238-
bedrockResources.knowledgeBase.node.addDependency(vectorIndex)
239-
bedrockResources.knowledgeBase.node.addDependency(functions.functions.createIndex)
240-
bedrockResources.knowledgeBase.node.addDependency(openSearchResources.collection.collection)
241-
bedrockResources.knowledgeBase.node.addDependency(bedrockExecutionRole)
237+
vectorKB.knowledgeBase.node.addDependency(vectorIndex)
238+
vectorKB.knowledgeBase.node.addDependency(functions.functions.createIndex)
239+
vectorKB.knowledgeBase.node.addDependency(openSearchResources.collection.collection)
240+
vectorKB.knowledgeBase.node.addDependency(bedrockExecutionRole)
242241

243242
// Create Apis and pass the Lambda function
244243
const apis = new Apis(this, "Apis", {

0 commit comments

Comments
 (0)