Skip to content

Commit a190935

Browse files
authored
fix(stepfunctions-tasks): fix bedrock input/output path in step-funct… (#31305)
### Issue # (if applicable) Closes #31302. ### Reason for this change PR#30298 introduced a breaking change in the stepfunctions-tasks which was encoding the input/output path from the `TaskStateBaseProps` to be used to input a S3 URI, which is not always the case and customer might be using the JSON string as it is. ### Description of changes To keep the functionality from the original ask of issue #29229 ,added another props to specify S3 URI as an input to the stepfunctions-tasks. Introduced a feature flag to keep the existing behaviour for the customers and not introduce a breaking change. ### Description of how you validated changes Updated integ test and deployed in account Integ Test Results: `aws stepfunctions start-execution --state-machine-arn <deployed state machine arn> `: should return execution arn A5-zEnXPmmPWTJx { "executionArn": "arn:aws:states:us-east-1:XXXX:execution:StateMachine2E01A3A5-zEnXPmmPWTJx:516f2e60-9507-46cb-95e6-4d9453429b08", "startDate": "2024-09-04T15:43:33.200000-07:00" } `aws stepfunctions describe-execution --execution-arn <exection-arn generated before> `: should return status as SUCCEEDED ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 30675f0 commit a190935

File tree

16 files changed

+473
-35
lines changed

16 files changed

+473
-35
lines changed

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/bedrock/integ.invoke-model.js.snapshot/InvokeModelDefaultTestDeployAssert9C0D2DFC.assets.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/bedrock/integ.invoke-model.js.snapshot/aws-stepfunctions-tasks-bedrock-invoke-model-integ.assets.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/bedrock/integ.invoke-model.js.snapshot/aws-stepfunctions-tasks-bedrock-invoke-model-integ.template.json

+14-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
{
104104
"Ref": "AWS::Region"
105105
},
106-
"::foundation-model/amazon.titan-text-express-v1\",\"Body\":{\"inputText.$\":\"States.Format('Alphabetize this list of first names:\\n{}', $.names)\",\"textGenerationConfig\":{\"maxTokenCount\":100,\"temperature\":1}}}},\"Prompt3\":{\"End\":true,\"Type\":\"Task\",\"InputPath\":\"$.names\",\"OutputPath\":\"$.names\",\"Resource\":\"arn:",
106+
"::foundation-model/amazon.titan-text-express-v1\",\"Body\":{\"inputText.$\":\"States.Format('Alphabetize this list of first names:/n{}', $.names)\",\"textGenerationConfig\":{\"maxTokenCount\":100,\"temperature\":1}}}},\"Prompt3\":{\"Next\":\"Prompt4\",\"Type\":\"Task\",\"OutputPath\":\"$.Body.results[0].outputText\",\"Resource\":\"arn:",
107107
{
108108
"Ref": "AWS::Partition"
109109
},
@@ -115,7 +115,19 @@
115115
{
116116
"Ref": "AWS::Region"
117117
},
118-
"::foundation-model/amazon.titan-text-express-v1\",\"Input\":{\"S3Uri.$\":\"$.names\"},\"Output\":{\"S3Uri.$\":\"$.names\"}}}},\"TimeoutSeconds\":30}"
118+
"::foundation-model/amazon.titan-text-express-v1\",\"Body\":{\"inputText.$\":\"States.Format('Echo list of first names: {}', $.names)\",\"textGenerationConfig\":{\"maxTokenCount\":100,\"temperature\":1}}}},\"Prompt4\":{\"End\":true,\"Type\":\"Task\",\"Resource\":\"arn:",
119+
{
120+
"Ref": "AWS::Partition"
121+
},
122+
":states:::bedrock:invokeModel\",\"Parameters\":{\"ModelId\":\"arn:",
123+
{
124+
"Ref": "AWS::Partition"
125+
},
126+
":bedrock:",
127+
{
128+
"Ref": "AWS::Region"
129+
},
130+
"::foundation-model/amazon.titan-text-express-v1\",\"Input\":{\"S3Uri\":\"$.names\"},\"Output\":{\"S3Uri\":\"$.names\"}}}},\"TimeoutSeconds\":30}"
119131
]
120132
]
121133
},

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/bedrock/integ.invoke-model.js.snapshot/cdk.out

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/bedrock/integ.invoke-model.js.snapshot/integ.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/bedrock/integ.invoke-model.js.snapshot/manifest.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/bedrock/integ.invoke-model.js.snapshot/tree.json

+22-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/bedrock/integ.invoke-model.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const prompt2 = new BedrockInvokeModel(stack, 'Prompt2', {
3737
body: sfn.TaskInput.fromObject(
3838
{
3939
inputText: sfn.JsonPath.format(
40-
'Alphabetize this list of first names:\n{}',
40+
'Alphabetize this list of first names:/n{}',
4141
sfn.JsonPath.stringAt('$.names'),
4242
),
4343
textGenerationConfig: {
@@ -52,13 +52,33 @@ const prompt2 = new BedrockInvokeModel(stack, 'Prompt2', {
5252
resultPath: '$',
5353
});
5454

55+
/** Test for Bedrock Output Path */
5556
const prompt3 = new BedrockInvokeModel(stack, 'Prompt3', {
5657
model,
57-
inputPath: sfn.JsonPath.stringAt('$.names'),
58-
outputPath: sfn.JsonPath.stringAt('$.names'),
58+
body: sfn.TaskInput.fromObject(
59+
{
60+
inputText: sfn.JsonPath.format(
61+
'Echo list of first names: {}',
62+
sfn.JsonPath.stringAt('$.names'),
63+
),
64+
textGenerationConfig: {
65+
maxTokenCount: 100,
66+
temperature: 1,
67+
},
68+
},
69+
),
70+
outputPath: '$.Body.results[0].outputText',
71+
});
72+
73+
/** Test for Bedrock s3 URI Path */
74+
//State Machine Execution will fail for the following input as it expects a valid s3 URI from previous prompt
75+
const prompt4 = new BedrockInvokeModel(stack, 'Prompt4', {
76+
model,
77+
input: { s3InputUri: '$.names' },
78+
output: { s3OutputUri: '$.names' },
5979
});
6080

61-
const chain = sfn.Chain.start(prompt1).next(prompt2).next(prompt3);
81+
const chain = sfn.Chain.start(prompt1).next(prompt2).next(prompt3).next(prompt4);
6282

6383
new sfn.StateMachine(stack, 'StateMachine', {
6484
definitionBody: sfn.DefinitionBody.fromChainable(chain),

Diff for: packages/@aws-cdk/cx-api/FEATURE_FLAGS.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Flags come in three types:
7171
| [@aws-cdk/pipelines:reduceAssetRoleTrustScope](#aws-cdkpipelinesreduceassetroletrustscope) | Remove the root account principal from PipelineAssetsFileRole trust policy | 2.141.0 | (default) |
7272
| [@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm](#aws-cdkaws-ecsremovedefaultdeploymentalarm) | When enabled, remove default deployment alarm settings | 2.143.0 | (default) |
7373
| [@aws-cdk/custom-resources:logApiResponseDataPropertyTrueDefault](#aws-cdkcustom-resourceslogapiresponsedatapropertytruedefault) | When enabled, the custom resource used for `AwsCustomResource` will configure the `logApiResponseData` property as true by default | 2.145.0 | (fix) |
74+
| [@aws-cdk/aws-s3:keepNotificationInImportedBucket](#aws-cdkaws-s3keepnotificationinimportedbucket) | When enabled, Adding notifications to a bucket in the current stack will not remove notification from imported stack. | 2.155.0 | (fix) |
75+
| [@aws-cdk/aws-stepfunctions-tasks:useNewS3UriParametersForBedrockInvokeModelTask](#aws-cdkaws-stepfunctions-tasksusenews3uriparametersforbedrockinvokemodeltask) | When enabled, use new props for S3 URI field in task definition of state machine for bedrock invoke model. | V2NEXT | (fix) |
7476

7577
<!-- END table -->
7678

@@ -131,7 +133,9 @@ The following json shows the current recommended set of flags, as `cdk init` wou
131133
"@aws-cdk/aws-eks:nodegroupNameAttribute": true,
132134
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true,
133135
"@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm": true,
134-
"@aws-cdk/custom-resources:logApiResponseDataPropertyTrueDefault": false
136+
"@aws-cdk/custom-resources:logApiResponseDataPropertyTrueDefault": false,
137+
"@aws-cdk/aws-s3:keepNotificationInImportedBucket": false,
138+
"@aws-cdk/aws-stepfunctions-tasks:useNewS3UriParametersForBedrockInvokeModelTask": true
135139
}
136140
}
137141
```
@@ -1338,4 +1342,38 @@ property from the event object.
13381342
| 2.145.0 | `false` | `false` |
13391343

13401344

1345+
### @aws-cdk/aws-s3:keepNotificationInImportedBucket
1346+
1347+
*When enabled, Adding notifications to a bucket in the current stack will not remove notification from imported stack.* (fix)
1348+
1349+
Currently, adding notifications to a bucket where it was created by ourselves will override notification added where it is imported.
1350+
1351+
When this feature flag is enabled, adding notifications to a bucket in the current stack will only update notification defined in this stack.
1352+
Other notifications that are not managed by this stack will be kept.
1353+
1354+
1355+
| Since | Default | Recommended |
1356+
| ----- | ----- | ----- |
1357+
| (not in v1) | | |
1358+
| 2.155.0 | `false` | `false` |
1359+
1360+
1361+
### @aws-cdk/aws-stepfunctions-tasks:useNewS3UriParametersForBedrockInvokeModelTask
1362+
1363+
*When enabled, use new props for S3 URI field in task definition of state machine for bedrock invoke model.* (fix)
1364+
1365+
Currently, 'inputPath' and 'outputPath' from the TaskStateBase Props is being used under BedrockInvokeModelProps to define S3URI under 'input' and 'output' fields
1366+
of State Machine Task definition.
1367+
1368+
When this feature flag is enabled, specify newly introduced props 's3InputUri' and
1369+
's3OutputUri' to populate S3 uri under input and output fields in state machine task definition for Bedrock invoke model.
1370+
1371+
1372+
| Since | Default | Recommended |
1373+
| ----- | ----- | ----- |
1374+
| (not in v1) | | |
1375+
| V2NEXT | `true` | `true` |
1376+
1377+
**Compatibility with old behavior:** Disable the feature flag to use input and output path fields for s3 URI
1378+
13411379
<!-- END details -->

Diff for: packages/aws-cdk-lib/aws-stepfunctions-tasks/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,48 @@ const task = new tasks.BedrockInvokeModel(this, 'Prompt Model', {
398398
names: sfn.JsonPath.stringAt('$.Body.results[0].outputText'),
399399
},
400400
});
401+
402+
```
403+
### Using Input Path for S3 URI
404+
405+
Provide S3 URI as an input or output path to invoke a model
406+
407+
To specify the S3 URI as JSON path to your input or output fields, use props `s3InputUri` and `s3OutputUri` under BedrockInvokeModelProps and set
408+
feature flag `@aws-cdk/aws-stepfunctions-tasks:useNewS3UriParametersForBedrockInvokeModelTask` to true.
409+
410+
411+
If this flag is not enabled, the code will populate the S3Uri using `InputPath` and `OutputPath` fields, which is not recommended.
412+
413+
```ts
414+
415+
import * as bedrock from 'aws-cdk-lib/aws-bedrock';
416+
417+
const model = bedrock.FoundationModel.fromFoundationModelId(
418+
this,
419+
'Model',
420+
bedrock.FoundationModelIdentifier.AMAZON_TITAN_TEXT_G1_EXPRESS_V1,
421+
);
422+
423+
const task = new tasks.BedrockInvokeModel(this, 'Prompt Model', {
424+
model,
425+
input : { s3InputUri: sfn.JsonPath.stringAt('$.prompt') },
426+
output: { s3OutputUri: sfn.JsonPath.stringAt('$.prompt') },
427+
});
428+
401429
```
430+
402431
### Using Input Path
403432

404433
Provide S3 URI as an input or output path to invoke a model
405434

435+
Currently, input and output Path provided in the BedrockInvokeModelProps input is defined as S3URI field under task definition of state machine.
436+
To modify the existing behaviour, set `@aws-cdk/aws-stepfunctions-tasks:useNewS3UriParametersForBedrockInvokeModelTask` to true.
437+
438+
If this feature flag is enabled, S3URI fields will be generated from other Props(`s3InputUri` and `s3OutputUri`), and the given inputPath, OutputPath will be rendered as
439+
it is in the JSON task definition.
440+
441+
If the feature flag is set to `false`, the behavior will be to populate the S3Uri using the `InputPath` and `OutputPath` fields, which is not recommended.
442+
406443
```ts
407444

408445
import * as bedrock from 'aws-cdk-lib/aws-bedrock';

0 commit comments

Comments
 (0)