Repository: awslabs/generative-ai-cdk-constructs (ts)

Issue:
amazonaurora: ImportError: cannot import name 'AmazonAuroraDefaultVectorStore'
### Describe the bug

`cdklabs.generative-ai-cdk-constructs 0.1.107` doesn't import properly with a vanilla setup. Please check the details provided below.

### Expected Behavior

- To import the library correctly
- To avoid being impacted by issues or errors from other modules when importing a module (I was trying to use `opensearch_vectorindex` but impacted by `amazonaurora`.)
- A build pipeline test to catch these type of breaking errors to avoid broken version releases.

### Current Behavior

```
$ cdk synth                                                      
Traceback (most recent call last):
  File "/Users/***/Experimental/aoss-experiment/app.py", line 6, in <module>
    from aoss_experiment.aoss_experiment_stack import AossExperimentStack
  File "/Users/***/Experimental/aoss-experiment/aoss_experiment/aoss_experiment_stack.py", line 7, in <module>
    from cdklabs.generative_ai_cdk_constructs import (
  File "/Users/***/Experimental/aoss-experiment/.venv/lib/python3.10/site-packages/cdklabs/generative_ai_cdk_constructs/__init__.py", line 21232, in <module>
    from . import amazonaurora
  File "/Users/***/Experimental/aoss-experiment/.venv/lib/python3.10/site-packages/cdklabs/generative_ai_cdk_constructs/amazonaurora/__init__.py", line 79, in <module>
    from ..bedrock import BedrockFoundationModel as _BedrockFoundationModel_91d045a3
  File "/Users/***/Experimental/aoss-experiment/.venv/lib/python3.10/site-packages/cdklabs/generative_ai_cdk_constructs/bedrock/__init__.py", line 348, in <module>
    from ..amazonaurora import (
ImportError: cannot import name 'AmazonAuroraDefaultVectorStore' from partially initialized module 'cdklabs.generative_ai_cdk_constructs.amazonaurora' (most likely due to a circular import) (/Users/***/Experimental/aoss-experiment/.venv/lib/python3.10/site-packages/cdklabs/generative_ai_cdk_constructs/amazonaurora/__init__.py)
```

### Reproduction Steps

Run: 
```
cdk init --language python
source .venv/bin/activate
echo "cdklabs.generative-ai-cdk-constructs" >> requirements.txt
```

In the stack file, import the library:
```python
from cdklabs.generative_ai_cdk_constructs import (
    opensearchserverless,
    opensearch_vectorindex,
)
```

Run `cdk synth` and observe the error.

### Possible Solution

_No response_

### Additional Information/Context

```
$ pip freeze
attrs==23.2.0
aws-cdk-lib==2.126.0
aws-cdk.asset-awscli-v1==2.2.202
aws-cdk.asset-kubectl-v20==2.1.2
aws-cdk.asset-node-proxy-agent-v6==2.0.3
cattrs==23.2.3
cdk-nag==2.28.84
cdklabs.generative-ai-cdk-constructs==0.1.107
constructs==10.3.0
exceptiongroup==1.2.0
importlib_resources==6.4.0
jsii==1.96.0
publication==0.0.3
python-dateutil==2.9.0.post0
six==1.16.0
typeguard==2.13.3
typing_extensions==4.11.0
```

### CDK CLI Version

2.126.0 (build fb74c41)

### Framework Version

_No response_

### Node.js Version

v21.0.0

### OS

macOS 14.3.1

### Language

Python

### Language Version

Python 3.10.13

### Region experiencing the issue

non-related

### Code modification

None

### Other information

_No response_

### Service quota

- [X] I have reviewed the service quotas for this construct

Pull request:
chore(aurora): remove circular dependency preventing python use
Fixes #365 

Package cannot be used in Python due to a circular dependency. Tested with @dineshSajwan to move that dependency however it generates an issue in the produced python package (jsii error ?)
Since this is blocking the use of the library in python, and a next PR will refactor the aurora interface to merge the 2 existing ones, we will work on addressing this issue as well then

In the meantime, we remove the dependency on models in aurora

---
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.


Patch:
diff --git a/apidocs/classes/amazonaurora.AmazonAuroraDefaultVectorStore.md b/apidocs/classes/amazonaurora.AmazonAuroraDefaultVectorStore.md
index 7f53ad5..9b6a147 100644
--- a/apidocs/classes/amazonaurora.AmazonAuroraDefaultVectorStore.md
+++ b/apidocs/classes/amazonaurora.AmazonAuroraDefaultVectorStore.md
@@ -40,7 +40,7 @@ https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Ve
 - [clusterIdentifier](amazonaurora.AmazonAuroraDefaultVectorStore.md#clusteridentifier)
 - [credentialsSecretArn](amazonaurora.AmazonAuroraDefaultVectorStore.md#credentialssecretarn)
 - [databaseName](amazonaurora.AmazonAuroraDefaultVectorStore.md#databasename)
-- [embeddingsModel](amazonaurora.AmazonAuroraDefaultVectorStore.md#embeddingsmodel)
+- [embeddingsModelVectorDimension](amazonaurora.AmazonAuroraDefaultVectorStore.md#embeddingsmodelvectordimension)
 - [env](amazonaurora.AmazonAuroraDefaultVectorStore.md#env)
 - [node](amazonaurora.AmazonAuroraDefaultVectorStore.md#node)
 - [physicalName](amazonaurora.AmazonAuroraDefaultVectorStore.md#physicalname)
@@ -117,9 +117,9 @@ The name of your Database.
 
 ___
 
-### embeddingsModel
+### embeddingsModelVectorDimension
 
-• `Readonly` **embeddingsModel**: [`BedrockFoundationModel`](bedrock.BedrockFoundationModel.md)
+• `Readonly` **embeddingsModelVectorDimension**: `number`
 
 Model used for embeddings.
 
diff --git a/apidocs/interfaces/amazonaurora.AmazonAuroraDefaultVectorStoreProps.md b/apidocs/interfaces/amazonaurora.AmazonAuroraDefaultVectorStoreProps.md
index 4798c33..b80eba3 100644
--- a/apidocs/interfaces/amazonaurora.AmazonAuroraDefaultVectorStoreProps.md
+++ b/apidocs/interfaces/amazonaurora.AmazonAuroraDefaultVectorStoreProps.md
@@ -8,13 +8,13 @@
 
 ### Properties
 
-- [embeddingsModel](amazonaurora.AmazonAuroraDefaultVectorStoreProps.md#embeddingsmodel)
+- [embeddingsModelVectorDimension](amazonaurora.AmazonAuroraDefaultVectorStoreProps.md#embeddingsmodelvectordimension)
 
 ## Properties
 
-### embeddingsModel
+### embeddingsModelVectorDimension
 
-• `Readonly` **embeddingsModel**: [`BedrockFoundationModel`](../classes/bedrock.BedrockFoundationModel.md)
+• `Readonly` **embeddingsModelVectorDimension**: `number`
 
 The embeddings model for the knowledge base.
 Must be identical as in the KnowledgeBaseV2 construct.
diff --git a/src/cdk-lib/amazonaurora/README.md b/src/cdk-lib/amazonaurora/README.md
index b6a84b8..9882768 100644
--- a/src/cdk-lib/amazonaurora/README.md
+++ b/src/cdk-lib/amazonaurora/README.md
@@ -53,6 +53,6 @@ new amazonaurora.AmazonAuroraVectoStore(
 import { amazonaurora } from '@cdklabs/generative-ai-cdk-constructs';
 
 new amazonaurora.AmazonAuroraDefaultVectorStore(stack, 'AuroraDefaultVectorStore', {
-  embeddingsModel: BedrockFoundationModel.COHERE_EMBED_ENGLISH_V3,
+  embeddingsModel: BedrockFoundationModel.COHERE_EMBED_ENGLISH_V3.vectorDimensions!,
 });
 ```
diff --git a/src/cdk-lib/amazonaurora/aurora-default-vector-store.ts b/src/cdk-lib/amazonaurora/aurora-default-vector-store.ts
index e4c11ce..a9dfaaf 100644
--- a/src/cdk-lib/amazonaurora/aurora-default-vector-store.ts
+++ b/src/cdk-lib/amazonaurora/aurora-default-vector-store.ts
@@ -21,7 +21,6 @@ import { Construct } from 'constructs';
 import { buildCustomResourceProvider } from '../../common/helpers/custom-resource-provider-helper';
 import { generatePhysicalNameV2 } from '../../common/helpers/utils';
 import { buildVpc, DefaultVpcProps } from '../../common/helpers/vpc-helper';
-import { BedrockFoundationModel } from '../bedrock/models';
 
 
 export interface AmazonAuroraDefaultVectorStoreProps {
@@ -33,7 +32,7 @@ export interface AmazonAuroraDefaultVectorStoreProps {
    * needs to know the vector dimensions to create the vector
    * index of appropriate dimensions in the Aurora database.
    */
-  readonly embeddingsModel: BedrockFoundationModel;
+  readonly embeddingsModelVectorDimension: number;
 }
 
 /**
@@ -86,7 +85,7 @@ export class AmazonAuroraDefaultVectorStore extends cdk.Resource {
   /**
    * Model used for embeddings.
    */
-  public readonly embeddingsModel: BedrockFoundationModel;
+  public readonly embeddingsModelVectorDimension: number;
 
   /**
    * An IAM policy that allows Data API access to Aurora.
@@ -105,7 +104,7 @@ export class AmazonAuroraDefaultVectorStore extends cdk.Resource {
     this.tableName = 'bedrock_integration.bedrock_kb';
     this.primaryKeyField = 'id';
     this.clusterIdentifier = 'aurora-serverless-vector-cluster';
-    this.embeddingsModel = props.embeddingsModel;
+    this.embeddingsModelVectorDimension = props.embeddingsModelVectorDimension;
 
     const vpc = buildVpc(this, {
       defaultVpcProps: DefaultVpcProps(),
@@ -249,7 +248,7 @@ export class AmazonAuroraDefaultVectorStore extends cdk.Resource {
       properties: {
         DatabaseName: this.databaseName,
         SecretName: auroraCluster.secret?.secretName || '',
-        VectorDimensions: props.embeddingsModel.vectorDimensions!,
+        VectorDimensions: props.embeddingsModelVectorDimension!,
       },
     });
 
diff --git a/src/cdk-lib/bedrock/README.md b/src/cdk-lib/bedrock/README.md
index 8435a61..94cb604 100644
--- a/src/cdk-lib/bedrock/README.md
+++ b/src/cdk-lib/bedrock/README.md
@@ -104,7 +104,7 @@ import * as s3 from 'aws-cdk-lib/aws-s3';
 import { amazonaurora, bedrock } from '@cdklabs/generative-ai-cdk-constructs';
 
 const auroraDb = new amazonaurora.AmazonAuroraDefaultVectorStore(stack, 'AuroraDefaultVectorStore', {
-  embeddingsModel: BedrockFoundationModel.COHERE_EMBED_ENGLISH_V3,
+  embeddingsModel: BedrockFoundationModel.COHERE_EMBED_ENGLISH_V3.vectorDimensions!,
 });
 
 const kb = new bedrock.KnowledgeBase(this, 'KnowledgeBase', {
diff --git a/src/cdk-lib/bedrock/knowledge-base.ts b/src/cdk-lib/bedrock/knowledge-base.ts
index ec4b5ce..f2c5c78 100644
--- a/src/cdk-lib/bedrock/knowledge-base.ts
+++ b/src/cdk-lib/bedrock/knowledge-base.ts
@@ -229,9 +229,6 @@ export class KnowledgeBase extends Construct implements cdk.ITaggableV2 {
     if (props.vectorIndex) {
       validateIndexParameters(props.vectorIndex, indexName, vectorField);
     }
-    if (props.vectorStore instanceof AmazonAuroraDefaultVectorStore) {
-      validateModelComparability(embeddingsModel, props.vectorStore.embeddingsModel);
-    }
 
     this.name = generatePhysicalNameV2(
       this,
@@ -676,22 +673,6 @@ function validateIndexParameters(
   }
 }
 
-/**
- * Validate that the embedding model provided for `AmazonAuroraDefaultVectorStore`
- * construct is identical to provided for `KnowledgeBase` construct.
- *
- * @internal This is an internal core function and should not be called directly.
- */
-function validateModelComparability(
-  foundationModel: BedrockFoundationModel,
-  foundationModel2: BedrockFoundationModel,
-) {
-  if (foundationModel.modelId !== foundationModel2.modelId) {
-    throw new Error('The embeddings model ' + foundationModel + ' provided in AmazonAuroraDefaultVectorStore' +
-    ' construct has to be identical to provided in KnowledgeBase construct.');
-  }
-}
-
 /**
  * Determine storage configuration based on vector store type.
  *


Test patch:
diff --git a/test/cdk-lib/amazonaurora/aurora-default-vector-store.test.ts b/test/cdk-lib/amazonaurora/aurora-default-vector-store.test.ts
index f329e58..ba4699e 100644
--- a/test/cdk-lib/amazonaurora/aurora-default-vector-store.test.ts
+++ b/test/cdk-lib/amazonaurora/aurora-default-vector-store.test.ts
@@ -39,7 +39,7 @@ describe('Amazon Aurora Default Vector Store', () => {
 
     beforeAll(() => {
       auroraDefaultVectorStore = new AmazonAuroraDefaultVectorStore(stack, 'AuroraVectorStore', {
-        embeddingsModel: model,
+        embeddingsModelVectorDimension: model.vectorDimensions!,
       });
 
       NagSuppressions.addResourceSuppressionsByPath(
@@ -72,7 +72,7 @@ describe('Amazon Aurora Default Vector Store', () => {
       expect(auroraDefaultVectorStore.tableName).toEqual('bedrock_integration.bedrock_kb');
       expect(auroraDefaultVectorStore.primaryKeyField).toEqual('id');
       expect(auroraDefaultVectorStore.clusterIdentifier).toEqual('aurora-serverless-vector-cluster');
-      expect(auroraDefaultVectorStore.embeddingsModel).toEqual(model);
+      expect(auroraDefaultVectorStore.embeddingsModelVectorDimension).toEqual(model.vectorDimensions);
     });
 
     test('No unsuppressed Errors', () => {
diff --git a/test/cdk-lib/bedrock/knowledge-base.test.ts b/test/cdk-lib/bedrock/knowledge-base.test.ts
index d0c0014..65dd274 100644
--- a/test/cdk-lib/bedrock/knowledge-base.test.ts
+++ b/test/cdk-lib/bedrock/knowledge-base.test.ts
@@ -99,7 +99,7 @@ describe('KnowledgeBase', () => {
 
   test('Should correctly initialize with custom props', () => {
     const vectorStore = new AmazonAuroraDefaultVectorStore(stack, 'AuroraDefaultVectorStore6', {
-      embeddingsModel: BedrockFoundationModel.TITAN_EMBED_TEXT_V1,
+      embeddingsModelVectorDimension: BedrockFoundationModel.TITAN_EMBED_TEXT_V1.vectorDimensions!,
     });
     const model = BedrockFoundationModel.TITAN_EMBED_TEXT_V1;
     const knowledgeBase = new KnowledgeBase(stack, 'AuroraDefaultKnowledgeBase', {
@@ -132,7 +132,7 @@ describe('KnowledgeBase', () => {
 
   test('Should throw error when vectorStore is not VectorCollection and indexName is provided', () => {
     const vectorStore = new AmazonAuroraDefaultVectorStore(stack, 'AmazonAuroraDefaultVectorStore6', {
-      embeddingsModel: BedrockFoundationModel.TITAN_EMBED_TEXT_V1,
+      embeddingsModelVectorDimension: BedrockFoundationModel.TITAN_EMBED_TEXT_V1.vectorDimensions!,
     });
     const model = BedrockFoundationModel.TITAN_EMBED_TEXT_V1;
 
@@ -147,7 +147,7 @@ describe('KnowledgeBase', () => {
 
   test('Should throw error when vectorStore is not VectorCollection and vectorField is provided', () => {
     const vectorStore = new AmazonAuroraDefaultVectorStore(stack, 'AmazonAuroraDefaultVectorStore5', {
-      embeddingsModel: BedrockFoundationModel.TITAN_EMBED_TEXT_V1,
+      embeddingsModelVectorDimension: BedrockFoundationModel.TITAN_EMBED_TEXT_V1.vectorDimensions!,
     });
     const model = BedrockFoundationModel.TITAN_EMBED_TEXT_V1;
 
@@ -164,7 +164,7 @@ describe('KnowledgeBase', () => {
     const vectorStores = [
       new VectorCollection(stack, 'VectorCollection3'),
       new AmazonAuroraDefaultVectorStore(stack, 'AmazonAuroraDefaultVectorStore', {
-        embeddingsModel: BedrockFoundationModel.TITAN_EMBED_TEXT_V1,
+        embeddingsModelVectorDimension: BedrockFoundationModel.TITAN_EMBED_TEXT_V1.vectorDimensions!,
       }),
       new PineconeVectorStore({
         connectionString: 'test-connection-string',
@@ -193,7 +193,7 @@ describe('KnowledgeBase', () => {
 
   test('Should correctly initialize with AmazonAuroraDefaultVectorStore and custom embeddingsModel', () => {
     const vectorStore = new AmazonAuroraDefaultVectorStore(stack, 'AuroraDefaultVectorStore2', {
-      embeddingsModel: BedrockFoundationModel.COHERE_EMBED_ENGLISH_V3,
+      embeddingsModelVectorDimension: BedrockFoundationModel.COHERE_EMBED_ENGLISH_V3.vectorDimensions!,
     });
     const model = BedrockFoundationModel.COHERE_EMBED_ENGLISH_V3;