Skip to content

Commit 10bb5ff

Browse files
refactor: L1 to L2 OpenSearch VectorIndex changes
1 parent a2110a9 commit 10bb5ff

File tree

4 files changed

+77
-51
lines changed

4 files changed

+77
-51
lines changed

cdk.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
},
2222
"context": {
2323
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
24-
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
25-
"aws-cdk:enableDiffNoFail": true,
26-
"bundling": {
27-
"dockerless": true
28-
},
2924
"@aws-cdk/core:checkSecretUsage": true,
3025
"@aws-cdk/core:target-partitions": [
3126
"aws",

packages/cdk/resources/OpenSearchResources.ts

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import {
55
VectorCollection,
66
VectorCollectionStandbyReplicas
77
} from "@cdklabs/generative-ai-cdk-constructs/lib/cdk-lib/opensearchserverless"
8-
import {
9-
VectorIndex,
10-
MetadataManagementFieldProps
11-
} from "@cdklabs/generative-ai-cdk-constructs/lib/cdk-lib/opensearch-vectorindex"
128

139
export interface OpenSearchResourcesProps {
1410
readonly stackName: string
@@ -18,7 +14,6 @@ export interface OpenSearchResourcesProps {
1814

1915
export class OpenSearchResources extends Construct {
2016
public readonly collection: VectorCollection
21-
public readonly vectorIndex: VectorIndex
2217

2318
constructor(scope: Construct, id: string, props: OpenSearchResourcesProps) {
2419
super(scope, id)
@@ -33,46 +28,6 @@ export class OpenSearchResources extends Construct {
3328
// Grant access to the Bedrock execution role
3429
this.collection.grantDataAccess(props.bedrockExecutionRole)
3530

36-
// Define the metadata mappings for Bedrock Knowledge Base compatibility
37-
const mappings: Array<MetadataManagementFieldProps> = [
38-
{
39-
mappingField: "AMAZON_BEDROCK_METADATA",
40-
dataType: "text",
41-
filterable: false
42-
},
43-
{
44-
mappingField: "AMAZON_BEDROCK_TEXT_CHUNK",
45-
dataType: "text",
46-
filterable: true
47-
},
48-
{
49-
mappingField: "id",
50-
dataType: "text",
51-
filterable: true
52-
},
53-
{
54-
mappingField: "x-amz-bedrock-kb-data-source-id",
55-
dataType: "text",
56-
filterable: true
57-
},
58-
{
59-
mappingField: "x-amz-bedrock-kb-source-uri",
60-
dataType: "text",
61-
filterable: true
62-
}
63-
]
64-
65-
// Create the vector index with Bedrock-compatible field mappings
66-
this.vectorIndex = new VectorIndex(this, "VectorIndex", {
67-
collection: this.collection,
68-
indexName: "eps-assist-os-index",
69-
vectorField: "bedrock-knowledge-base-default-vector",
70-
vectorDimensions: 1024,
71-
precision: "float",
72-
distanceType: "cosine",
73-
mappings: mappings
74-
})
75-
7631
// Set static values for commit and version tags to prevent recreation
7732
Tags.of(this.collection).add("commit", "static_value")
7833
Tags.of(this.collection).add("version", "static_value")
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {Construct} from "constructs"
2+
import {CfnIndex} from "aws-cdk-lib/aws-opensearchserverless"
3+
import {VectorCollection} from "@cdklabs/generative-ai-cdk-constructs/lib/cdk-lib/opensearchserverless"
4+
5+
export interface VectorIndexProps {
6+
readonly indexName: string
7+
readonly collection: VectorCollection
8+
}
9+
10+
export class VectorIndex extends Construct {
11+
public readonly cfnIndex: CfnIndex
12+
public readonly indexName: string
13+
14+
constructor(scope: Construct, id: string, props: VectorIndexProps) {
15+
super(scope, id)
16+
17+
this.indexName = props.indexName
18+
19+
const indexMapping: CfnIndex.MappingsProperty = {
20+
properties: {
21+
"bedrock-knowledge-base-default-vector": {
22+
type: "knn_vector",
23+
dimension: 1024,
24+
method: {
25+
name: "hnsw",
26+
engine: "faiss",
27+
parameters: {},
28+
spaceType: "l2"
29+
}
30+
},
31+
"AMAZON_BEDROCK_METADATA": {
32+
type: "text",
33+
index: false
34+
},
35+
"AMAZON_BEDROCK_TEXT_CHUNK": {
36+
type: "text",
37+
index: true
38+
},
39+
"id": {
40+
type: "text",
41+
index: true
42+
},
43+
"x-amz-bedrock-kb-data-source-id": {
44+
type: "text",
45+
index: true
46+
},
47+
"x-amz-bedrock-kb-source-uri": {
48+
type: "text",
49+
index: true
50+
}
51+
}
52+
}
53+
54+
const cfnIndex = new CfnIndex(this, "MyCfnIndex", {
55+
collectionEndpoint: props.collection.collectionEndpoint, // Use L2 property
56+
indexName: props.indexName,
57+
mappings: indexMapping,
58+
settings: {
59+
index: {
60+
knn: true,
61+
knnAlgoParamEfSearch: 512
62+
}
63+
}
64+
})
65+
66+
this.cfnIndex = cfnIndex
67+
}
68+
}

packages/cdk/stacks/EpsAssistMeStack.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {RuntimePolicies} from "../resources/RuntimePolicies"
1717
import {DatabaseTables} from "../resources/DatabaseTables"
1818
import {BedrockPromptResources} from "../resources/BedrockPromptResources"
1919
import {S3LambdaNotification} from "../constructs/S3LambdaNotification"
20+
import {VectorIndex} from "../resources/VectorIndex"
2021

2122
export interface EpsAssistMeStackProps extends StackProps {
2223
readonly stackName: string
@@ -82,17 +83,24 @@ export class EpsAssistMeStack extends Stack {
8283
region
8384
})
8485

86+
const vectorIndex = new VectorIndex(this, "VectorIndex", {
87+
indexName: "eps-assist-os-index",
88+
collection: openSearchResources.collection
89+
})
90+
8591
// Create VectorKnowledgeBase construct with Bedrock execution role
8692
const vectorKB = new VectorKnowledgeBaseResources(this, "VectorKB", {
8793
stackName: props.stackName,
8894
docsBucket: storage.kbDocsBucket.bucket,
8995
bedrockExecutionRole: bedrockExecutionRole.role,
9096
collectionArn: openSearchResources.collection.collectionArn,
91-
vectorIndexName: openSearchResources.vectorIndex.indexName,
97+
vectorIndexName: vectorIndex.indexName,
9298
region,
9399
account
94100
})
95101

102+
vectorKB.knowledgeBase.node.addDependency(vectorIndex.cfnIndex)
103+
96104
// Create runtime policies with resource dependencies
97105
const runtimePolicies = new RuntimePolicies(this, "RuntimePolicies", {
98106
region,

0 commit comments

Comments
 (0)