|
1 | 1 | import {Construct} from "constructs" |
2 | | -import {bedrock} from "@cdklabs/generative-ai-cdk-constructs" |
3 | 2 | import {Role} from "aws-cdk-lib/aws-iam" |
4 | 3 | 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" |
12 | 5 |
|
13 | 6 | export interface VectorKnowledgeBaseProps { |
14 | 7 | kbName: string |
15 | | - embeddingsModel: bedrock.BedrockFoundationModel |
| 8 | + embeddingsModel: string |
16 | 9 | docsBucket: Bucket |
17 | 10 | bedrockExecutionRole: Role |
| 11 | + collectionArn: string |
| 12 | + vectorIndexName: string |
18 | 13 | } |
19 | 14 |
|
20 | 15 | 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 |
23 | 18 |
|
24 | 19 | constructor(scope: Construct, id: string, props: VectorKnowledgeBaseProps) { |
25 | 20 | super(scope, id) |
26 | 21 |
|
27 | | - this.guardrail = new bedrock.Guardrail(this, "Guardrail", { |
| 22 | + this.guardrail = new bedrock.CfnGuardrail(this, "Guardrail", { |
28 | 23 | name: "eps-assist-guardrail", |
29 | 24 | description: "Guardrail for EPS Assist Me Slackbot", |
30 | 25 | blockedInputMessaging: "Your input was blocked.", |
31 | 26 | 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 | + } |
73 | 48 | }) |
74 | 49 |
|
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", { |
77 | 51 | name: props.kbName, |
78 | 52 | 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 | + } |
81 | 72 | }) |
82 | 73 |
|
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 | + } |
86 | 83 | }) |
87 | 84 | } |
88 | 85 | } |
0 commit comments