Skip to content

Commit 23f290c

Browse files
author
dinsajwa
committed
fix(prompt): code review comments
1 parent 670ebb2 commit 23f290c

17 files changed

+388
-93
lines changed

packages/@aws-cdk/aws-bedrock-alpha/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,12 +790,13 @@ const prompt1 = new bedrock.Prompt(this, 'prompt1', {
790790
kmsKey: cmk,
791791
});
792792

793-
new bedrock.PromptVersion(this, 'MyPromptVersion', {
793+
const promptVersion = new bedrock.PromptVersion(this, 'MyPromptVersion', {
794794
prompt: prompt1,
795795
description: 'my first version',
796796
});
797797
//or alternatively:
798-
// prompt1.createVersion('my first version');
798+
// const promptVersion = prompt1.createVersion('my first version');
799+
const versionString = promptVersion.version;
799800

800801
```
801802

packages/@aws-cdk/aws-bedrock-alpha/bedrock/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export * from './prompts/tool-choice';
2323
export * from './prompts/text-prompt-variant';
2424
export * from './prompts/chat-prompt-variant';
2525
export * from './prompts/agent-prompt-variant';
26+
export * from './prompts/prompt-inference-configuration';
2627

2728
// ===================================
2829
// Models

packages/@aws-cdk/aws-bedrock-alpha/bedrock/prompts/agent-prompt-variant.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function createAgentPromptVariant(props: AgentPromptVariantProps): IPromp
3434
},
3535
templateConfiguration: {
3636
text: {
37-
inputVariables: props.promptVariables?.flatMap((variable: string) => {
37+
inputVariables: props.promptVariables?.map((variable: string) => {
3838
return { name: variable };
3939
}),
4040
text: props.promptText,

packages/@aws-cdk/aws-bedrock-alpha/bedrock/prompts/chat-prompt-variant.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as bedrock from 'aws-cdk-lib/aws-bedrock';
21
import { CfnPrompt } from 'aws-cdk-lib/aws-bedrock';
32
import { CommonPromptVariantProps, PromptTemplateType, IPromptVariant } from './prompt-variant';
43
import { ToolConfiguration } from './tool-choice';
4+
import { PromptInferenceConfiguration } from './prompt-inference-configuration';
55

66
/**
77
* Properties for creating a chat prompt variant.
@@ -33,7 +33,7 @@ export interface ChatPromptVariantProps extends CommonPromptVariantProps {
3333
*
3434
* @default - No inference configuration provided.
3535
*/
36-
readonly inferenceConfiguration?: bedrock.CfnPrompt.PromptModelInferenceConfigurationProperty;
36+
readonly inferenceConfiguration?: PromptInferenceConfiguration;
3737
}
3838

3939
/**
@@ -123,11 +123,11 @@ export function createChatPromptVariant(props: ChatPromptVariantProps): IPromptV
123123
templateType: PromptTemplateType.CHAT,
124124
modelId: props.model.invokableArn,
125125
inferenceConfiguration: {
126-
text: { ...props.inferenceConfiguration },
126+
text: props.inferenceConfiguration ? props.inferenceConfiguration._render() : {},
127127
},
128128
templateConfiguration: {
129129
chat: {
130-
inputVariables: props.promptVariables?.flatMap((variable: string) => {
130+
inputVariables: props.promptVariables?.map((variable: string) => {
131131
return { name: variable };
132132
}),
133133
messages: props.messages?.flatMap(m => m._render()),
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import * as bedrock from 'aws-cdk-lib/aws-bedrock';
2+
3+
/**
4+
* Properties for creating a prompt inference configuration.
5+
*/
6+
export interface PromptInferenceConfigurationProps {
7+
/**
8+
* The maximum number of tokens to return in the response.
9+
*
10+
* @default - No limit specified
11+
*/
12+
readonly maxTokens?: number;
13+
14+
/**
15+
* A list of strings that define sequences after which the model will stop generating.
16+
*
17+
* @default - No stop sequences
18+
*/
19+
readonly stopSequences?: string[];
20+
21+
/**
22+
* Controls the randomness of the response.
23+
* Higher values make output more random, lower values more deterministic.
24+
* Valid range is 0.0 to 1.0.
25+
*
26+
* @default - Model default temperature
27+
*/
28+
readonly temperature?: number;
29+
30+
/**
31+
* The percentage of most-likely candidates that the model considers for the next token.
32+
* Valid range is 0.0 to 1.0.
33+
*
34+
* @default - Model default topP
35+
*/
36+
readonly topP?: number;
37+
}
38+
39+
/**
40+
* Configuration for model inference parameters used in prompts.
41+
*
42+
* This class provides a high-level abstraction over the underlying CloudFormation
43+
* inference configuration properties, offering a more developer-friendly interface
44+
* while maintaining full compatibility with the underlying AWS Bedrock service.
45+
*/
46+
export class PromptInferenceConfiguration {
47+
/**
48+
* The maximum number of tokens to return in the response.
49+
*/
50+
public readonly maxTokens?: number;
51+
52+
/**
53+
* A list of strings that define sequences after which the model will stop generating.
54+
*/
55+
public readonly stopSequences?: string[];
56+
57+
/**
58+
* Controls the randomness of the response.
59+
*/
60+
public readonly temperature?: number;
61+
62+
/**
63+
* The percentage of most-likely candidates that the model considers for the next token.
64+
*/
65+
public readonly topP?: number;
66+
67+
constructor(props: PromptInferenceConfigurationProps) {
68+
this.maxTokens = props.maxTokens;
69+
this.stopSequences = props.stopSequences;
70+
this.temperature = props.temperature;
71+
this.topP = props.topP;
72+
}
73+
74+
/**
75+
* Renders the inference configuration as a CloudFormation property.
76+
* @internal This is an internal core function and should not be called directly.
77+
*/
78+
public _render(): bedrock.CfnPrompt.PromptModelInferenceConfigurationProperty {
79+
return {
80+
maxTokens: this.maxTokens,
81+
stopSequences: this.stopSequences,
82+
temperature: this.temperature,
83+
topP: this.topP,
84+
};
85+
}
86+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import * as bedrock from 'aws-cdk-lib/aws-bedrock';
2+
3+
/**
4+
* Properties for creating a prompt inference configuration.
5+
*/
6+
export interface PromptInferenceConfigurationProps {
7+
/**
8+
* The maximum number of tokens to return in the response.
9+
*
10+
* @default - No limit specified
11+
*/
12+
readonly maxTokens?: number;
13+
14+
/**
15+
* A list of strings that define sequences after which the model will stop generating.
16+
*
17+
* @default - No stop sequences
18+
*/
19+
readonly stopSequences?: string[];
20+
21+
/**
22+
* Controls the randomness of the response.
23+
* Higher values make output more random, lower values more deterministic.
24+
* Valid range is 0.0 to 1.0.
25+
*
26+
* @default - Model default temperature
27+
*/
28+
readonly temperature?: number;
29+
30+
/**
31+
* The percentage of most-likely candidates that the model considers for the next token.
32+
* Valid range is 0.0 to 1.0.
33+
*
34+
* @default - Model default topP
35+
*/
36+
readonly topP?: number;
37+
}
38+
39+
/**
40+
* Configuration for model inference parameters used in prompts.
41+
*
42+
* This class provides a high-level abstraction over the underlying CloudFormation
43+
* inference configuration properties, offering a more developer-friendly interface
44+
* while maintaining full compatibility with the underlying AWS Bedrock service.
45+
*/
46+
export class PromptInferenceConfiguration {
47+
/**
48+
* The maximum number of tokens to return in the response.
49+
*/
50+
public readonly maxTokens?: number;
51+
52+
/**
53+
* A list of strings that define sequences after which the model will stop generating.
54+
*/
55+
public readonly stopSequences?: string[];
56+
57+
/**
58+
* Controls the randomness of the response.
59+
*/
60+
public readonly temperature?: number;
61+
62+
/**
63+
* The percentage of most-likely candidates that the model considers for the next token.
64+
*/
65+
public readonly topP?: number;
66+
67+
constructor(props: PromptInferenceConfigurationProps) {
68+
this.maxTokens = props.maxTokens;
69+
this.stopSequences = props.stopSequences;
70+
this.temperature = props.temperature;
71+
this.topP = props.topP;
72+
}
73+
74+
/**
75+
* Renders the inference configuration as a CloudFormation property.
76+
* @internal This is an internal core function and should not be called directly.
77+
*/
78+
public _render(): bedrock.CfnPrompt.PromptModelInferenceConfigurationProperty {
79+
return {
80+
maxTokens: this.maxTokens,
81+
stopSequences: this.stopSequences,
82+
temperature: this.temperature,
83+
topP: this.topP,
84+
};
85+
}
86+
}

packages/@aws-cdk/aws-bedrock-alpha/bedrock/prompts/prompt-version.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as bedrock from 'aws-cdk-lib/aws-bedrock';
22
import { Construct } from 'constructs';
33
// Internal Libs
44
import { IPrompt } from './prompt';
5+
import * as validation from '../agents/validation-helpers';
56

67
/******************************************************************************
78
* PROPS FOR NEW CONSTRUCT
@@ -19,6 +20,7 @@ export interface PromptVersionProps {
1920
* The description of the prompt version.
2021
*
2122
* @default - No description provided.
23+
* Maximum length: 200
2224
*/
2325
readonly description?: string;
2426
}
@@ -59,6 +61,11 @@ export class PromptVersion extends Construct {
5961
*/
6062
public readonly version: string;
6163

64+
/**
65+
* The description of the prompt version.
66+
*/
67+
public readonly description?: string;
68+
6269
/******************************************************************************
6370
* INTERNAL ONLY
6471
*****************************************************************************/
@@ -76,6 +83,21 @@ export class PromptVersion extends Construct {
7683

7784
this.prompt = props.prompt;
7885

86+
// Store description for validation
87+
this.description = props.description;
88+
89+
// ------------------------------------------------------
90+
// Immediate Validation (throws error at construction time)
91+
// ------------------------------------------------------
92+
if (props.description) {
93+
validation.throwIfInvalid(this.validateDescriptionImmediate, props.description);
94+
}
95+
96+
// ------------------------------------------------------
97+
// Synthesis-time Validation
98+
// ------------------------------------------------------
99+
this.node.addValidation({ validate: () => this.validateDescription() });
100+
79101
// ------------------------------------------------------
80102
// L1 Instantiation
81103
// ------------------------------------------------------
@@ -87,4 +109,40 @@ export class PromptVersion extends Construct {
87109
this.versionArn = this.__resource.attrArn;
88110
this.version = this.__resource.attrVersion;
89111
}
112+
113+
/******************************************************************************
114+
* VALIDATION METHODS
115+
*****************************************************************************/
116+
/**
117+
* Validates whether the description length is within the allowed limit (immediate validation).
118+
* @param description - The description to validate
119+
* @returns Array of validation error messages, empty if valid
120+
*/
121+
private validateDescriptionImmediate = (description: string): string[] => {
122+
const errors: string[] = [];
123+
124+
if (description && description.length > 200) {
125+
errors.push(
126+
`Description must be 200 characters or less, got ${description.length} characters.`,
127+
);
128+
}
129+
130+
return errors;
131+
};
132+
133+
/**
134+
* Validates whether the description length is within the allowed limit.
135+
* @returns Array of validation error messages, empty if valid
136+
*/
137+
private validateDescription(): string[] {
138+
const errors: string[] = [];
139+
140+
if (this.description && this.description.length > 200) {
141+
errors.push(
142+
`Description must be 200 characters or less, got ${this.description.length} characters.`,
143+
);
144+
}
145+
146+
return errors;
147+
}
90148
}

0 commit comments

Comments
 (0)