-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathvector-knowledge-base.ts
More file actions
1126 lines (1050 loc) · 40.3 KB
/
Copy pathvector-knowledge-base.ts
File metadata and controls
1126 lines (1050 loc) · 40.3 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import { ArnFormat, aws_bedrock as bedrock, Stack } from 'aws-cdk-lib';
import * as iam from 'aws-cdk-lib/aws-iam';
import { NagSuppressions } from 'cdk-nag/lib/nag-suppressions';
import { Construct } from 'constructs';
import {
CommonKnowledgeBaseAttributes,
CommonKnowledgeBaseProps,
createKnowledgeBaseServiceRole,
IKnowledgeBase,
KnowledgeBaseBase,
KnowledgeBaseType,
} from './knowledge-base';
import { SupplementalDataStorageLocation } from './supplemental-data-storage';
import { generatePhysicalNameV2 } from '../../../common/helpers/utils';
import { ExistingAmazonAuroraVectorStore, AmazonAuroraVectorStore } from '../../amazonaurora';
import { MongoDBAtlasVectorStore } from '../../mongodb-atlas';
import { VectorIndex } from '../../opensearch-vectorindex';
import { OpenSearchManagedClusterVectorStore } from '../../opensearchmanagedcluster';
import { VectorCollection } from '../../opensearchserverless';
import { PineconeVectorStore } from '../../pinecone';
import { VectorIndex as S3VectorIndex } from '../../s3vectors';
import { Agent } from '../agents/agent';
import {
ConfluenceDataSource,
ConfluenceDataSourceAssociationProps,
} from '../data-sources/confluence-data-source';
import { ContextEnrichment } from '../data-sources/context-enrichment';
import {
CustomDataSource,
CustomDataSourceAssociationProps,
} from '../data-sources/custom-data-source';
import { S3DataSource, S3DataSourceAssociationProps } from '../data-sources/s3-data-source';
import {
SalesforceDataSource,
SalesforceDataSourceAssociationProps,
} from '../data-sources/salesforce-data-source';
import {
SharePointDataSource,
SharePointDataSourceAssociationProps,
} from '../data-sources/sharepoint-data-source';
import {
WebCrawlerDataSource,
WebCrawlerDataSourceAssociationProps,
} from '../data-sources/web-crawler-data-source';
import { BedrockFoundationModel, VectorType } from '../models';
/******************************************************************************
* ENUMS
*****************************************************************************/
/**
* Knowledge base can be backed by different vector databases.
* This enum represents the different vector databases that can be used.
*
* `OPENSEARCH_SERVERLESS` is the default vector database.
* `PINECONE` is the vector database for Pinecone.
* `AMAZON_AURORA` is the vector database for Amazon Aurora PostgreSQL.
*/
export enum VectorStoreType {
/**
* `OPENSEARCH_SERVERLESS` is the vector store for OpenSearch Serverless.
*/
OPENSEARCH_SERVERLESS = 'OPENSEARCH_SERVERLESS',
/**
* `OPENSEARCH_MANAGED_CLUSTER` is the vector store for OpenSearch Managed Cluster.
*/
OPENSEARCH_MANAGED_CLUSTER = 'OPENSEARCH_MANAGED_CLUSTER',
/**
* `PINECONE` is the vector store for Pinecone.
*/
PINECONE = 'PINECONE',
/**
* `RDS` is the vector store for Amazon Aurora.
*/
AMAZON_AURORA = 'RDS',
/**
* `MONGO_DB_ATLAS` is the vector store for MongoDB Atlas.
*/
MONGO_DB_ATLAS = 'MONGO_DB_ATLAS',
/**
* `NEPTUNE_ANALYTICS` is the vector store for Amazon Neptune Analytics.
*/
NEPTUNE_ANALYTICS = 'NEPTUNE_ANALYTICS',
/**
* `S3_VECTORS` is the vector store for Amazon S3 Vectors.
*/
S3_VECTORS = 'S3_VECTORS',
}
/******************************************************************************
* COMMON INTERFACES
*****************************************************************************/
/**
* Interface for the configuration of the storage for knowledge base.
*/
interface StorageConfiguration {
/**
* The vector store, which can be of `VectorCollection`, `PineconeVectorStore`,
* `AmazonAuroraVectorStore`, `MongoDBAtlasVectorStore`, `OpenSearchManagedClusterVectorStore`,
* or `S3VectorIndex` (from s3vectors) types.
*/
vectorStore:
| VectorCollection
| PineconeVectorStore
| AmazonAuroraVectorStore
| ExistingAmazonAuroraVectorStore
| MongoDBAtlasVectorStore
| OpenSearchManagedClusterVectorStore
| S3VectorIndex;
/**
* The type of the vector store.
*/
vectorStoreType: VectorStoreType;
/**
* The name of the index.
*/
indexName: string;
/**
* The field of the vector field for vector mapping.
*/
vectorField: string;
/**
* The field of the text field for vector mapping.
*/
textField: string;
/**
* The field of the metadata.
*/
metadataField: string;
}
/**
* Represents a Knowledge Base, either created with CDK or imported.
*/
export interface IVectorKnowledgeBase extends IKnowledgeBase {
/**
* The storage type for the Vector Embeddings.
*/
readonly vectorStoreType: VectorStoreType;
/**
* Add an S3 data source to the knowledge base.
*/
addS3DataSource(props: S3DataSourceAssociationProps): S3DataSource;
/**
* Add a web crawler data source to the knowledge base.
*/
addWebCrawlerDataSource(props: WebCrawlerDataSourceAssociationProps): WebCrawlerDataSource;
/**
* Add a SharePoint data source to the knowledge base.
*/
addSharePointDataSource(props: SharePointDataSourceAssociationProps): SharePointDataSource;
/**
* Add a Confluence data source to the knowledge base.
*/
addConfluenceDataSource(props: ConfluenceDataSourceAssociationProps): ConfluenceDataSource;
/**
* Add a Salesforce data source to the knowledge base.
*/
addSalesforceDataSource(props: SalesforceDataSourceAssociationProps): SalesforceDataSource;
/**
* Add a Custom data source to the knowledge base.
*/
addCustomDataSource(props: CustomDataSourceAssociationProps): CustomDataSource;
/**
* Grant the given identity permissions to retrieve content from the knowledge base.
*/
grantRetrieve(grantee: iam.IGrantable): iam.Grant;
/**
* Grant the given identity permissions to retrieve content from the knowledge base.
*/
grantRetrieveAndGenerate(grantee: iam.IGrantable): iam.Grant;
}
/******************************************************************************
* ABSTRACT CLASS
*****************************************************************************/
/**
* Abstract base class for Vector Knowledge Base.
* Contains methods valid for KBs either created with CDK or imported.
*/
export abstract class VectorKnowledgeBaseBase
extends KnowledgeBaseBase
implements IVectorKnowledgeBase {
public abstract readonly knowledgeBaseArn: string;
public abstract readonly knowledgeBaseId: string;
public abstract readonly role: iam.IRole;
public abstract readonly vectorStoreType: VectorStoreType;
public abstract readonly description?: string;
public abstract readonly instruction?: string;
public readonly type: KnowledgeBaseType = KnowledgeBaseType.VECTOR;
constructor(scope: Construct, id: string) {
super(scope, id);
}
// ------------------------------------------------------
// Helper methods to add Data Sources
// ------------------------------------------------------
/**
* Adds an S3 data source to the knowledge base.
*/
public addS3DataSource(props: S3DataSourceAssociationProps): S3DataSource {
// Validate context enrichment is only used with Neptune Analytics
const isNeptuneKB = this.vectorStoreType === VectorStoreType.NEPTUNE_ANALYTICS;
if (props.contextEnrichment && !isNeptuneKB) {
throw new Error('Context enrichment is only supported for Neptune/GraphRAG KnowledgeBases');
}
// Set context enrichment - use provided value or default for Neptune
let contextEnrichment = props.contextEnrichment;
if (isNeptuneKB) {
contextEnrichment =
props.contextEnrichment ??
ContextEnrichment.foundationModel({
enrichmentModel: BedrockFoundationModel.ANTHROPIC_CLAUDE_HAIKU_V1_0,
});
}
// Create and return the S3 data source
return new S3DataSource(this, `s3-${props.bucket.node.addr}`, {
knowledgeBase: this,
...props,
contextEnrichment: contextEnrichment,
});
}
public addWebCrawlerDataSource(
props: WebCrawlerDataSourceAssociationProps,
): WebCrawlerDataSource {
const url = new URL(props.sourceUrls[0]);
return new WebCrawlerDataSource(this, `web-${url.hostname.replace('.', '-')}`, {
knowledgeBase: this,
...props,
});
}
public addSharePointDataSource(
props: SharePointDataSourceAssociationProps,
): SharePointDataSource {
const url = new URL(props.siteUrls[0]);
return new SharePointDataSource(this, `sp-${url.hostname.replace('.', '-')}`, {
knowledgeBase: this,
...props,
});
}
public addConfluenceDataSource(
props: ConfluenceDataSourceAssociationProps,
): ConfluenceDataSource {
const url = new URL(props.confluenceUrl);
return new ConfluenceDataSource(this, `cf-${url.hostname.replace('.', '-')}`, {
knowledgeBase: this,
...props,
});
}
public addSalesforceDataSource(
props: SalesforceDataSourceAssociationProps,
): SalesforceDataSource {
const url = new URL(props.endpoint);
return new SalesforceDataSource(this, `sf-${url.hostname.replace('.', '-')}`, {
knowledgeBase: this,
...props,
});
}
public addCustomDataSource(props: CustomDataSourceAssociationProps): CustomDataSource {
return new CustomDataSource(this, `custom-${props.dataSourceName}`, {
knowledgeBase: this,
...props,
});
}
}
/******************************************************************************
* PROPS FOR NEW CONSTRUCT
*****************************************************************************/
/**
* Properties for a knowledge base
*/
export interface VectorKnowledgeBaseProps extends CommonKnowledgeBaseProps {
/**
* The embeddings model for the knowledge base
*/
readonly embeddingsModel: BedrockFoundationModel;
/**
* The supplemental data storage locations for the knowledge base
*/
readonly supplementalDataStorageLocations?: SupplementalDataStorageLocation[];
/**
* The vector type to store vector embeddings.
*
* @default - VectorType.FLOATING_POINT
*/
readonly vectorType?: VectorType;
/**
* The name of the vector index.
* If vectorStore is not of type `VectorCollection`,
* do not include this property as it will throw error.
*
* @default - 'bedrock-knowledge-base-default-index'
*/
readonly indexName?: string;
/**
* The name of the field in the vector index.
* If vectorStore is not of type `VectorCollection`,
* do not include this property as it will throw error.
*
* @default - 'bedrock-knowledge-base-default-vector'
*/
readonly vectorField?: string;
/**
* The vector store for the knowledge base. Must be either of
* type `VectorCollection`, `PineconeVectorStore`, `AmazonAuroraVectorStore`,
* `MongoDBAtlasVectorStore`, `OpenSearchManagedClusterVectorStore`, or
* `VectorIndex` from s3vectors (for S3 Vectors).
*
* @default - A new OpenSearch Serverless vector collection is created.
*/
readonly vectorStore?:
| VectorCollection
| PineconeVectorStore
| AmazonAuroraVectorStore
| ExistingAmazonAuroraVectorStore
| MongoDBAtlasVectorStore
| OpenSearchManagedClusterVectorStore
| S3VectorIndex;
/**
* The vector index for the OpenSearch Serverless backed knowledge base.
* If vectorStore is not of type `VectorCollection`, do not include
* this property as it will throw error.
*
* @default - A new vector index is created on the Vector Collection
* if vector store is of `VectorCollection` type.
*/
readonly vectorIndex?: VectorIndex;
}
/******************************************************************************
* ATTRS FOR IMPORTED CONSTRUCT
*****************************************************************************/
/**
* Properties for importing a knowledge base outside of this stack
*/
export interface VectorKnowledgeBaseAttributes extends CommonKnowledgeBaseAttributes {
/**
* The vector store type for the knowledge base.
*/
readonly vectorStoreType: VectorStoreType;
}
/**
* Deploys a Bedrock Knowledge Base and configures a backend by OpenSearch Serverless,
* Pinecone, Redis Enterprise Cloud or Amazon Aurora PostgreSQL.
*
*/
export class VectorKnowledgeBase extends VectorKnowledgeBaseBase {
// ------------------------------------------------------
// Import Methods
// ------------------------------------------------------
public static fromKnowledgeBaseAttributes(
scope: Construct,
id: string,
attrs: VectorKnowledgeBaseAttributes,
): IVectorKnowledgeBase {
const stack = Stack.of(scope);
class Import extends VectorKnowledgeBaseBase {
public readonly role = iam.Role.fromRoleArn(
this,
`kb-${attrs.knowledgeBaseId}-role`,
attrs.executionRoleArn,
);
public readonly description = attrs.description;
public readonly instruction = attrs.instruction;
public readonly knowledgeBaseId = attrs.knowledgeBaseId;
public readonly vectorStoreType = attrs.vectorStoreType;
public readonly knowledgeBaseArn = stack.formatArn({
service: 'bedrock',
resource: 'knowledge-base',
resourceName: attrs.knowledgeBaseId,
arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
});
}
return new Import(scope, id);
}
// ------------------------------------------------------
// Attributes
// ------------------------------------------------------
/**
* The name of the knowledge base.
*/
public readonly name: string;
/**
* Instance of knowledge base.
*/
public readonly knowledgeBaseInstance: bedrock.CfnKnowledgeBase;
/**
* The role the Knowledge Base uses to access the vector store and data source.
*/
public readonly role: iam.IRole;
/**
* The vector store for the knowledge base.
*/
public readonly vectorStore:
| VectorCollection
| PineconeVectorStore
| AmazonAuroraVectorStore
| ExistingAmazonAuroraVectorStore
| MongoDBAtlasVectorStore
| OpenSearchManagedClusterVectorStore
| S3VectorIndex;
/**
* A description of the knowledge base.
*/
readonly description?: string;
/**
* Instructions for agents based on the design and type of information of the
* Knowledge Base. This will impact how Agents interact with the Knowledge Base.
*/
readonly instruction?: string;
/**
* The ARN of the knowledge base.
*/
public readonly knowledgeBaseArn: string;
/**
* The ID of the knowledge base.
*/
public readonly knowledgeBaseId: string;
/**
* The type of the knowledge base.
*/
public readonly vectorStoreType: VectorStoreType;
/**
* The OpenSearch vector index for the knowledge base.
* @private
*/
private vectorIndex?: VectorIndex;
constructor(scope: Construct, id: string, props: VectorKnowledgeBaseProps) {
super(scope, id);
// ------------------------------------------------------
// Set properties or defaults
// ------------------------------------------------------
const embeddingsModel = props.embeddingsModel;
const vectorType = props.vectorType ?? VectorType.FLOATING_POINT;
const indexName = props.indexName ?? 'bedrock-knowledge-base-default-index';
const vectorField = props.vectorField ?? 'bedrock-knowledge-base-default-vector';
const textField = 'AMAZON_BEDROCK_TEXT_CHUNK';
const metadataField = 'AMAZON_BEDROCK_METADATA';
this.description = props.description ?? 'CDK deployed Knowledge base'; // even though this prop is optional, if no value is provided it will fail to deploy
// this.knowledgeBaseState = props.knowledgeBaseState ?? 'ENABLED';
this.instruction = props.instruction;
this.name = props.name ?? generatePhysicalNameV2(this, 'KB', { maxLength: 32 });
// ------------------------------------------------------
// Validations
// ------------------------------------------------------
validateModel(embeddingsModel, vectorType);
validateVectorIndex(props.vectorStore, props.vectorIndex, props.vectorField, props.indexName);
if (props.vectorIndex) {
validateIndexParameters(props.vectorIndex, indexName, vectorField);
}
// ------------------------------------------------------
// Role
// ------------------------------------------------------
// Use existing role if provided, otherwise create a new one
this.role = props.existingRole ?? createKnowledgeBaseServiceRole(this);
if (!props.existingRole) {
embeddingsModel.grantInvoke(this.role);
// Add CDK Nag suppression for bedrock:InvokeModel* wildcard permission
NagSuppressions.addResourceSuppressions(
this.role,
[
{
id: 'AwsSolutions-IAM5',
reason: 'Bedrock Knowledge Base requires wildcard permissions to invoke embedding models',
},
],
true,
);
}
// ------------------------------------------------------
// Vector Store
// ------------------------------------------------------
/**
* Create the vector store if the vector store was provided by the user.
* Otherwise check againts all possible vector datastores.
* If none was provided create default OpenSearch Serverless Collection.
*/
if (props.vectorStore instanceof VectorCollection) {
this.vectorStoreType = VectorStoreType.OPENSEARCH_SERVERLESS;
({ vectorStore: this.vectorStore, vectorStoreType: this.vectorStoreType } =
this.handleOpenSearchCollection(props));
} else if (props.vectorStore instanceof PineconeVectorStore) {
this.vectorStoreType = VectorStoreType.PINECONE;
({ vectorStore: this.vectorStore, vectorStoreType: this.vectorStoreType } =
this.handlePineconeVectorStore(props));
} else if (
props.vectorStore instanceof AmazonAuroraVectorStore ||
props.vectorStore instanceof ExistingAmazonAuroraVectorStore
) {
this.vectorStoreType = VectorStoreType.AMAZON_AURORA;
({ vectorStore: this.vectorStore, vectorStoreType: this.vectorStoreType } =
this.handleAmazonAuroraVectorStore(props));
} else if (props.vectorStore instanceof MongoDBAtlasVectorStore) {
this.vectorStoreType = VectorStoreType.MONGO_DB_ATLAS;
({ vectorStore: this.vectorStore, vectorStoreType: this.vectorStoreType } =
this.handleMongoDBAtlasVectorStore(props));
} else if (props.vectorStore instanceof OpenSearchManagedClusterVectorStore) {
this.vectorStoreType = VectorStoreType.OPENSEARCH_MANAGED_CLUSTER;
({ vectorStore: this.vectorStore, vectorStoreType: this.vectorStoreType } =
this.handleOpenSearchManagedClusterVectorStore(props));
} else if (props.vectorStore instanceof S3VectorIndex) {
this.vectorStoreType = VectorStoreType.S3_VECTORS;
({ vectorStore: this.vectorStore, vectorStoreType: this.vectorStoreType } =
this.handleS3VectorsVectorStore(props));
} else {
this.vectorStoreType = VectorStoreType.OPENSEARCH_SERVERLESS;
({ vectorStore: this.vectorStore, vectorStoreType: this.vectorStoreType } =
this.handleOpenSearchDefaultVectorCollection());
}
// perform this validation after the vector store is handled since if the user
// doesn't provide one, the method above will create it
validateVectorType(this.vectorStore, vectorType);
/**
* We need to add `secretsmanager:GetSecretValue` to the role
* of the knowledge base if we use vector stores
* other than OpenSearch Serverless, OpenSearch Managed Cluster, or S3 Vectors.
*/
if (
!(this.vectorStore instanceof VectorCollection) &&
!(this.vectorStore instanceof OpenSearchManagedClusterVectorStore) &&
!(this.vectorStore instanceof S3VectorIndex)
) {
this.role.addToPrincipalPolicy(
new iam.PolicyStatement({
actions: ['secretsmanager:GetSecretValue'],
resources: [this.vectorStore.credentialsSecretArn],
}),
);
}
/**
* We need to add `rds-data:ExecuteStatement`,
* `rds-data:BatchExecuteStatement` and
* `rds:DescribeDBClusters` to the role
* of the knowledge base if we use Amazon Aurora as
* a data source.
*/
if (
this.vectorStore instanceof AmazonAuroraVectorStore ||
this.vectorStore instanceof ExistingAmazonAuroraVectorStore
) {
this.role.addToPrincipalPolicy(
new iam.PolicyStatement({
actions: [
'rds-data:ExecuteStatement',
'rds-data:BatchExecuteStatement',
'rds:DescribeDBClusters',
],
resources: [this.vectorStore.resourceArn],
}),
);
}
/**
* Create the vector index if the vector store is OpenSearch Serverless
* and it was not provided. Otherwise use the provided vector index.
*/
if (this.vectorStoreType === VectorStoreType.OPENSEARCH_SERVERLESS) {
if (!props.vectorIndex) {
this.vectorIndex = new VectorIndex(this, 'KBIndex', {
collection: this.vectorStore as VectorCollection,
indexName,
vectorField,
vectorDimensions: embeddingsModel.vectorDimensions!,
precision: props.vectorType === VectorType.BINARY ? 'Binary' : 'float',
distanceType: props.vectorType === VectorType.BINARY ? 'hamming' : 'l2',
mappings: [
{
mappingField: 'AMAZON_BEDROCK_TEXT_CHUNK',
dataType: 'text',
filterable: true,
},
{
mappingField: 'AMAZON_BEDROCK_METADATA',
dataType: 'text',
filterable: false,
},
],
});
this.vectorIndex.node.addDependency(this.vectorStore);
} else {
this.vectorIndex = props.vectorIndex;
}
}
/**
* Create storage configuraion. If it is of type of
* `AmazonAuroraVectorStore` or `ExistingAmazonAuroraVectorStore`,
* then get textField, metadataField and vectorField from
* the arguments. Otherwise use default values.
*/
const storageConfiguration: StorageConfiguration = {
indexName: indexName,
vectorStore: this.vectorStore,
vectorStoreType: this.vectorStoreType,
vectorField:
this.vectorStore instanceof AmazonAuroraVectorStore ||
this.vectorStore instanceof ExistingAmazonAuroraVectorStore
? this.vectorStore.vectorField
: vectorField,
textField:
this.vectorStore instanceof AmazonAuroraVectorStore ||
this.vectorStore instanceof ExistingAmazonAuroraVectorStore ||
this.vectorStore instanceof PineconeVectorStore
? this.vectorStore.textField
: textField,
metadataField:
this.vectorStore instanceof AmazonAuroraVectorStore ||
this.vectorStore instanceof ExistingAmazonAuroraVectorStore ||
this.vectorStore instanceof PineconeVectorStore
? this.vectorStore.metadataField
: metadataField,
};
// ------------------------------------------------------
// L1 Instantiation
// ------------------------------------------------------
const knowledgeBase = new bedrock.CfnKnowledgeBase(this, 'MyCfnKnowledgeBase', {
knowledgeBaseConfiguration: {
type: KnowledgeBaseType.VECTOR,
vectorKnowledgeBaseConfiguration: {
embeddingModelArn: embeddingsModel.asArn(this),
// Used this approach as if property is specified on models that do not
// support configurable dimensions CloudFormation throws an error at runtime
embeddingModelConfiguration: {
bedrockEmbeddingModelConfiguration:
embeddingsModel.modelId === 'amazon.titan-embed-text-v2:0'
? {
dimensions: embeddingsModel.vectorDimensions,
embeddingDataType: vectorType,
}
: { embeddingDataType: vectorType },
},
...(props.supplementalDataStorageLocations && props.supplementalDataStorageLocations.length > 0
? {
supplementalDataStorageConfiguration: {
supplementalDataStorageLocations: props.supplementalDataStorageLocations.map(location => location.__render()),
},
}
: {}),
},
},
name: this.name,
roleArn: this.role.roleArn,
storageConfiguration: getStorageConfiguration(storageConfiguration),
description: props.description,
});
this.knowledgeBaseInstance = knowledgeBase;
const kbCRPolicy = new iam.Policy(this, 'KBCRPolicy', {
// roles: [crProvider.role],
roles: [this.role],
statements: [
new iam.PolicyStatement({
actions: [
'bedrock:CreateKnowledgeBase',
/**
* We need to add `bedrock:AssociateThirdPartyKnowledgeBase` if
* we are deploying Redis or Pinecone data sources
*/
// ...(this.vectorStoreType === VectorStoreType.REDIS_ENTERPRISE_CLOUD ||
...(this.vectorStoreType === VectorStoreType.PINECONE
? ['bedrock:AssociateThirdPartyKnowledgeBase']
: []),
],
resources: ['*'],
}),
new iam.PolicyStatement({
actions: [
'bedrock:UpdateKnowledgeBase',
'bedrock:DeleteKnowledgeBase',
'bedrock:TagResource',
],
resources: [
Stack.of(this).formatArn({
service: 'bedrock',
resource: 'knowledge-base',
resourceName: '*',
arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
}),
],
}),
new iam.PolicyStatement({
actions: ['iam:PassRole'],
resources: [this.role.roleArn],
}),
],
});
knowledgeBase.node.addDependency(this.role);
knowledgeBase.node.addDependency(kbCRPolicy);
if (this.vectorStoreType === VectorStoreType.OPENSEARCH_SERVERLESS && this.vectorIndex) {
knowledgeBase.node.addDependency(this.vectorIndex);
}
if (this.vectorStoreType === VectorStoreType.AMAZON_AURORA) {
knowledgeBase.node.addDependency(this.vectorStore);
}
if (this.vectorStoreType === VectorStoreType.S3_VECTORS) {
knowledgeBase.node.addDependency(this.vectorStore);
}
NagSuppressions.addResourceSuppressions(
kbCRPolicy,
[
{
id: 'AwsSolutions-IAM5',
reason: "Bedrock CreateKnowledgeBase can't be restricted by resource.",
},
],
true,
);
this.knowledgeBaseArn = knowledgeBase.attrKnowledgeBaseArn;
this.knowledgeBaseId = knowledgeBase.attrKnowledgeBaseId;
}
/**
* Handle VectorCollection type of VectorStore.
*
* @param props - The properties of the KnowledgeBase.
* @returns The instance of VectorCollection, VectorStoreType.
* @internal This is an internal core function and should not be called directly.
*/
private handleOpenSearchCollection(props: VectorKnowledgeBaseProps): {
vectorStore: VectorCollection;
vectorStoreType: VectorStoreType;
} {
const vectorStore = props.vectorStore as VectorCollection;
vectorStore.grantDataAccess(this.role);
return {
vectorStore: vectorStore,
vectorStoreType: VectorStoreType.OPENSEARCH_SERVERLESS,
};
}
/**
* Handle PineconeVectorStore type of VectorStore.
*
* @param props - The properties of the KnowledgeBase.
* @returns The instance of PineconeVectorStore, VectorStoreType.
* @internal This is an internal core function and should not be called directly.
*/
private handlePineconeVectorStore(props: VectorKnowledgeBaseProps): {
vectorStore: PineconeVectorStore;
vectorStoreType: VectorStoreType;
} {
const vectorStore = props.vectorStore as PineconeVectorStore;
return {
vectorStore: vectorStore,
vectorStoreType: VectorStoreType.PINECONE,
};
}
/**
* Handle AmazonAuroraVectorStore type of VectorStore.
*
* @param props - The properties of the KnowledgeBase.
* @returns The instance of AmazonAuroraVectorStore, VectorStoreType.
* @internal This is an internal core function and should not be called directly.
*/
private handleAmazonAuroraVectorStore(props: VectorKnowledgeBaseProps): {
vectorStore: AmazonAuroraVectorStore | ExistingAmazonAuroraVectorStore;
vectorStoreType: VectorStoreType;
} {
const vectorStore =
props.vectorStore instanceof ExistingAmazonAuroraVectorStore
? props.vectorStore
: (props.vectorStore as AmazonAuroraVectorStore);
return {
vectorStore: vectorStore,
vectorStoreType: VectorStoreType.AMAZON_AURORA,
};
}
/**
* Handle OpenSearchManagedClusterVectorStore type of VectorStore.
*
* @param props - The properties of the KnowledgeBase.
* @returns The instance of OpenSearchManagedClusterVectorStore, VectorStoreType.
* @internal This is an internal core function and should not be called directly.
*/
private handleOpenSearchManagedClusterVectorStore(props: VectorKnowledgeBaseProps): {
vectorStore: OpenSearchManagedClusterVectorStore;
vectorStoreType: VectorStoreType;
} {
const vectorStore = props.vectorStore as OpenSearchManagedClusterVectorStore;
return {
vectorStore: vectorStore,
vectorStoreType: VectorStoreType.OPENSEARCH_MANAGED_CLUSTER,
};
}
/**
* Handle MongoDBAtlasVectorStore type of VectorStore.
*
* @param props - The properties of the KnowledgeBase.
* @returns The instance of MongoDBAtlasVectorStore, VectorStoreType.
* @internal This is an internal core function and should not be called directly.
*/
private handleMongoDBAtlasVectorStore(props: VectorKnowledgeBaseProps): {
vectorStore: MongoDBAtlasVectorStore;
vectorStoreType: VectorStoreType;
} {
const vectorStore = props.vectorStore as MongoDBAtlasVectorStore;
return {
vectorStore: vectorStore,
vectorStoreType: VectorStoreType.MONGO_DB_ATLAS,
};
}
/**
* Handle S3VectorIndex type of VectorStore.
*
* @param props - The properties of the KnowledgeBase.
* @returns The instance of S3VectorIndex, VectorStoreType.
* @internal This is an internal core function and should not be called directly.
*/
private handleS3VectorsVectorStore(props: VectorKnowledgeBaseProps): {
vectorStore: S3VectorIndex;
vectorStoreType: VectorStoreType;
} {
const vectorStore = props.vectorStore as S3VectorIndex;
// Validate that the S3 vector index dimension matches the embeddings model
const expectedDimension = props.embeddingsModel.vectorDimensions;
if (expectedDimension != null && vectorStore.dimension !== expectedDimension) {
throw new Error(
`S3 vector index dimension (${vectorStore.dimension}) must match the embeddings model dimension (${expectedDimension}).`,
);
}
// Grant the KB role read and write access to the vector index
vectorStore.vectorBucket.grantRead(this.role, [vectorStore.vectorIndexName]);
vectorStore.vectorBucket.grantWrite(this.role, [vectorStore.vectorIndexName]);
return {
vectorStore: vectorStore,
vectorStoreType: VectorStoreType.S3_VECTORS,
};
}
/**
* Handle the default VectorStore type.
*
* @returns The instance of VectorCollection, VectorStoreType.
* @internal This is an internal core function and should not be called directly.
*/
private handleOpenSearchDefaultVectorCollection(): {
vectorStore: VectorCollection;
vectorStoreType: VectorStoreType;
} {
const vectorStore = new VectorCollection(this, 'KBVectors');
vectorStore.grantDataAccess(this.role);
return {
vectorStore: vectorStore,
vectorStoreType: VectorStoreType.OPENSEARCH_SERVERLESS,
};
}
/**
* Associate knowledge base with an agent
*/
public associateToAgent(agent: Agent) {
agent.addKnowledgeBase(this);
}
}
/**
* Validate that Bedrock Knowledge Base can use the selected model.
*
* @internal This is an internal core function and should not be called directly.
*/
function validateModel(foundationModel: BedrockFoundationModel, vectorType: VectorType) {
if (!foundationModel.supportsKnowledgeBase) {
throw new Error(`The model ${foundationModel} is not supported by Bedrock Knowledge Base.`);
}
if (
foundationModel.supportedVectorType &&
!foundationModel.supportedVectorType.includes(vectorType)
) {
throw new Error(
`The vector type ${vectorType} is not supported by the model ${foundationModel}.`,
);
}
}
/**
* Validate that the storage configuration can use the selected vector type.
* It prevents the use of vector types with vector stores that do not support them,
* thereby avoiding potential runtime errors.
*
* @internal This is an internal core function and should not be called directly.
*/
function validateVectorType(vectorStore: any, vectorType: VectorType) {
if (!(vectorStore instanceof VectorCollection) && vectorType == VectorType.BINARY) {
throw new Error(
'Amazon OpenSearch Serverless is currently the only vector store that supports storing binary vectors.',
);
}
}
/**
* Validate if VectorIndex was provided for a VectorStore of type
* other than `VectorCollection`.
*
* @internal This is an internal core function and should not be called directly.
*/
function validateVectorIndex(vectorStore: any, vectorIndex: any, vectorField: any, indexName: any) {
if (!(vectorStore instanceof VectorCollection) && vectorIndex) {
throw new Error(
'If vectorStore is not of type VectorCollection, vectorIndex should not be provided ' +
'in KnowledgeBase construct.',
);
}
if (!(vectorStore instanceof VectorCollection) && indexName) {
throw new Error(
'If vectorStore is not of type VectorCollection, indexName should not be provided ' +
'in KnowledgeBase construct.',
);
}
if (!(vectorStore instanceof VectorCollection) && vectorField) {
throw new Error(
'If vectorStore is not of type VectorCollection, vectorField should not be provided ' +
'in KnowledgeBase construct.',
);
}
}
/**
* Validate that indexName and vectorField parameters are identical
* in KnowledgeBase construct if VectorIndex was created manually.
*
* By default we assign `vectorIndex` to `bedrock-knowledge-base-default-index`
* value and if user provides `vectorIndex` manually, we need to make sure
* they also provide it in KnowledgeBase construct if the value is not
* `bedrock-knowledge-base-default-index`. Same for vectorField.
*
* @internal This is an internal core function and should not be called directly.
*/
function validateIndexParameters(vectorIndex: VectorIndex, indexName: string, vectorField: string) {
if (vectorIndex.indexName !== 'bedrock-knowledge-base-default-index') {
if (vectorIndex.indexName !== indexName) {
throw new Error(