import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as s3 from 'aws-cdk-lib/aws-s3'; import { bedrock } from '@cdklabs/generative-ai-cdk-constructs'; export interface KnowledgeBaseProps { accountId: string; region: string; } export class KnowledgeBaseConstruct extends Construct { public readonly knowledgeBase: bedrock.VectorKnowledgeBase; public readonly dataSource: bedrock.S3DataSource; public readonly docsBucket: s3.Bucket; constructor(scope: Construct, id: string, props: KnowledgeBaseProps) { super(scope, id); // S3 bucket for SA8000 docs, SOPs, employee handbook, work orders, and purchase orders this.docsBucket = new s3.Bucket(this, 'DocsBucket', { bucketName: `seahaven-kb-docs-${props.accountId}`, encryption: s3.BucketEncryption.S3_MANAGED, blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL, versioned: true, removalPolicy: cdk.RemovalPolicy.RETAIN, }); // VectorKnowledgeBase from @cdklabs/generative-ai-cdk-constructs handles: // - AOSS collection with correct encryption/network/access policies // - IAM execution role // - kNN vector index creation via built-in custom resource // - Bedrock Knowledge Base creation once the index is ready this.knowledgeBase = new bedrock.VectorKnowledgeBase(this, 'KnowledgeBase', { embeddingsModel: bedrock.BedrockFoundationModel.TITAN_EMBED_TEXT_V2_1024, name: 'seahaven-kb', description: 'SA8000 compliance docs, SOPs, employee handbook, work orders, and purchase orders', instruction: 'Use this knowledge base to answer questions about Sea Haven Industries company policies, SOPs, SA8000 social accountability compliance requirements, approved vendor lists, the employee handbook, work order status and history, and purchase order details.', }); // S3 data source — chunking configured via ChunkingStrategy.fixedSize() this.dataSource = new bedrock.S3DataSource(this, 'S3DataSource', { bucket: this.docsBucket, knowledgeBase: this.knowledgeBase, dataSourceName: 'seahaven-s3-docs', chunkingStrategy: bedrock.ChunkingStrategy.fixedSize({ maxTokens: 512, overlapPercentage: 20, }), }); } }