Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6070433
feat(prompt): add prompt management for bedrock agent
Jun 17, 2025
00d04e0
feat(bedrockprompt): add integ test case and update readme
Jun 18, 2025
4ac3f2e
feat(bedrockprompt): updated documentation for rosetta
Jun 18, 2025
8af394a
Merge branch 'main' into bedrock_prompts
dineshSajwan Jun 18, 2025
90d2eb7
feat(bedrockprompt): remove readme files
Jun 18, 2025
3a56a53
feat(bedrockprompt): remove integ test snapshots
Jun 19, 2025
d7f97dc
feat(prompt): updated integ snapshot
Jun 19, 2025
c317949
Merge branch 'main' into bedrock_prompts
dineshSajwan Jun 19, 2025
fff512e
feat(prompt): updated integ snapshot
Jun 19, 2025
c91d58d
feat(prompt): updated integ snapshot
Jun 19, 2025
e7eff9d
Merge branch 'main' into bedrock_prompts
dineshSajwan Jun 24, 2025
670ebb2
Merge branch 'main' into bedrock_prompts
dineshSajwan Jun 30, 2025
23f290c
fix(prompt): code review comments
Jun 30, 2025
c5da21c
fix(prompt): deleting agent snapshot to fix buid issues
Jun 30, 2025
1d18066
fix(prompt): updated yarn deps
Jun 30, 2025
2abbb7e
fix(prompt): updated integ test
Jun 30, 2025
0c7e28e
Merge branch 'main' into bedrock_prompts
dineshSajwan Jul 2, 2025
69e8ffa
fix(prompt): updated readme for rosetta issues
Jul 2, 2025
ce8e827
fix(prompts): removed duplicate file
Jul 10, 2025
228ef7a
Merge branch 'main' into bedrock_prompts
dineshSajwan Jul 10, 2025
e58c45a
fix(prompt): removed l1 refrences
Jul 11, 2025
45ae2e7
chore(prompt): implemented code review comments
Jul 16, 2025
8e749a6
Merge branch 'main' into bedrock_prompts
dineshSajwan Jul 16, 2025
e8ecbb4
Merge branch 'main' into bedrock_prompts
mergify[bot] Jul 17, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions packages/@aws-cdk/aws-bedrock-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ This construct library facilitates the deployment of Bedrock Agents, enabling yo
- [Agent Collaboration](#agent-collaboration)
- [Custom Orchestration](#custom-orchestration)
- [Agent Alias](#agent-alias)
- [Prompts](#prompts)
- [Prompt Variants](#prompt-variants)
- [Basic Text Prompt](#basic-text-prompt)
- [Chat Prompt](#chat-prompt)
- [Agent Prompt](#agent-prompt)
- [Prompt Properties](#prompt-properties)
- [Prompt Version](#prompt-version)
- [Import Methods](#import-methods)

## Agents

Expand Down Expand Up @@ -606,3 +614,201 @@ const agentAlias = new bedrock.AgentAlias(this, 'myAlias', {
description: `Production version of my agent. Created at ${agent.lastUpdated}` // ensure the version update
});
```

## Prompts

Amazon Bedrock provides the ability to create and save prompts using Prompt management so that you can save time by applying the same prompt to different workflows. You can include variables in the prompt so that you can adjust the prompt for different use case.

The `Prompt` resource allows you to create a new prompt.

### Prompt Variants

Prompt variants in the context of Amazon Bedrock refer to alternative configurations of a prompt, including its message or the model and inference configurations used. Prompt variants are the building blocks of prompts - you must create at least one prompt variant to create a prompt. Prompt variants allow you to create different versions of a prompt, test them, and save the variant that works best for your use case.

There are three types of prompt variants:

- **Basic Text Prompt**: Simple text-based prompts for straightforward interactions
- **Chat variant**: Conversational prompts that support system messages, user/assistant message history, and tools
- **Agent variant**: Prompts designed to work with Bedrock Agents

### Basic Text Prompt

Text prompts are the simplest form of prompts, consisting of plain text instructions with optional variables. They are ideal for straightforward tasks like summarization, content generation, or question answering where you need a direct text-based interaction with the model.

```ts fixture=default
const cmk = new kms.Key(this, 'cmk', {});
const claudeModel = bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_SONNET_V1_0;

const variant1 = bedrock.PromptVariant.text({
variantName: 'variant1',
model: claudeModel,
promptVariables: ['topic'],
promptText: 'This is my first text prompt. Please summarize our conversation on: {{topic}}.',
inferenceConfiguration: new bedrock.PromptInferenceConfiguration({
temperature: 1.0,
topP: 0.999,
maxTokens: 2000,
}),
});

const prompt1 = new bedrock.Prompt(this, 'prompt1', {
promptName: 'prompt1',
description: 'my first prompt',
defaultVariant: variant1,
variants: [variant1],
kmsKey: cmk,
});
```

### Chat Prompt

Use this template type when the model supports the Converse API or the Anthropic Claude Messages API. This allows you to include a System prompt and previous User messages and Assistant messages for context.

```ts fixture=default
const cmk = new kms.Key(this, 'cmk', {});

const variantChat = bedrock.PromptVariant.chat({
variantName: 'variant1',
model: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0,
messages: [
bedrock.ChatMessage.user('From now on, you speak Japanese!'),
bedrock.ChatMessage.assistant('Konnichiwa!'),
bedrock.ChatMessage.user('From now on, you speak {{language}}!'),
],
system: 'You are a helpful assistant that only speaks the language you`re told.',
promptVariables: ['language'],
toolConfiguration: {
toolChoice: bedrock.ToolChoice.AUTO,
tools: [
{
toolSpec: {
name: 'top_song',
description: 'Get the most popular song played on a radio station.',
inputSchema: {
json: {
type: 'object',
properties: {
sign: {
type: 'string',
description:
'The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKR.',
},
},
required: ['sign'],
},
},
},
},
],
},
});

new bedrock.Prompt(this, 'prompt1', {
promptName: 'prompt-chat',
description: 'my first chat prompt',
defaultVariant: variantChat,
variants: [variantChat],
kmsKey: cmk,
});
```

### Agent Prompt

Agent prompts are designed to work with Bedrock Agents, allowing you to create prompts that can be used by agents to perform specific tasks. Agent prompts use text prompts as their foundation and can reference agent aliases and include custom instructions for how the agent should behave.

```ts fixture=default
const cmk = new kms.Key(this, 'cmk', {});

// Assuming you have an existing agent and alias
const agent = bedrock.Agent.fromAgentAttributes(this, 'ImportedAgent', {
agentArn: 'arn:aws:bedrock:region:account:agent/agent-id',
roleArn: 'arn:aws:iam::account:role/agent-role',
});

const agentAlias = bedrock.AgentAlias.fromAttributes(this, 'ImportedAlias', {
aliasId: 'alias-id',
aliasName: 'my-alias',
agentVersion: '1',
agent: agent,
});

const agentVariant = bedrock.PromptVariant.agent({
variantName: 'agent-variant',
model: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0,
agentAlias: agentAlias,
promptText: 'Use the agent to help with: {{task}}. Please be thorough and provide detailed explanations.',
promptVariables: ['task'],
});

new bedrock.Prompt(this, 'agentPrompt', {
promptName: 'agent-prompt',
description: 'Prompt for agent interactions',
defaultVariant: agentVariant,
variants: [agentVariant],
kmsKey: cmk,
});
```

### Prompt Properties

| Property | Type | Required | Description |
|----------|------|----------|-------------|
| promptName | string | Yes | The name of the prompt |
| description | string | No | A description of the prompt |
| defaultVariant | PromptVariant | Yes | The default variant to use for the prompt |
| variants | PromptVariant[] | No | Additional variants for the prompt |
| kmsKey | kms.IKey | No | The KMS key to use for encrypting the prompt. Defaults to AWS managed key |
| tags | Record<string, string> | No | Tags to apply to the prompt |

### Prompt Version

A prompt version is a snapshot of a prompt at a specific point in time that you create when you are satisfied with a set of configurations. Versions allow you to deploy your prompt and easily switch between different configurations for your prompt and update your application with the most appropriate version for your use-case.

You can create a Prompt version by using the PromptVersion class or by using the .createVersion(..) on a Prompt object. It is recommended to use the .createVersion(..) method. It uses a hash based mechanism to update the version whenever a certain configuration property changes.

```ts fixture=default
const cmk = new kms.Key(this, 'cmk', {});
const claudeModel = bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_SONNET_V1_0;

const variant1 = bedrock.PromptVariant.text({
variantName: 'variant1',
model: claudeModel,
promptVariables: ['topic'],
promptText: 'This is my first text prompt. Please summarize our conversation on: {{topic}}.',
inferenceConfiguration: new bedrock.PromptInferenceConfiguration({
temperature: 1.0,
topP: 0.999,
maxTokens: 2000,
}),
});

const prompt1 = new bedrock.Prompt(this, 'prompt1', {
promptName: 'prompt1',
description: 'my first prompt',
defaultVariant: variant1,
variants: [variant1],
kmsKey: cmk,
});

const promptVersion = new bedrock.PromptVersion(this, 'MyPromptVersion', {
prompt: prompt1,
description: 'my first version',
});
//or alternatively:
// const promptVersion = prompt1.createVersion('my first version');
const versionString = promptVersion.version;

```

### Import Methods

You can use the `fromPromptAttributes` method to import an existing Bedrock Prompt into your CDK application.

```ts fixture=default
// Import an existing prompt by ARN
const importedPrompt = bedrock.Prompt.fromPromptAttributes(this, 'ImportedPrompt', {
promptArn: 'arn:aws:bedrock:region:account:prompt/prompt-id',
kmsKey: kms.Key.fromKeyArn(this, 'ImportedKey', 'arn:aws:kms:region:account:key/key-id'), // optional
promptVersion: '1', // optional, defaults to 'DRAFT'
});
```
18 changes: 17 additions & 1 deletion packages/@aws-cdk/aws-bedrock-alpha/bedrock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,21 @@ export * from './agents/memory';
export * from './agents/agent-collaborator';
export * from './agents/agent-collaboration';
export * from './agents/orchestration-executor';
export * from './models';
export * from './agents/function-schema';

// ===================================
// Prompts
// ===================================
export * from './prompts/prompt';
export * from './prompts/prompt-variant';
export * from './prompts/prompt-version';
export * from './prompts/tool-choice';
export * from './prompts/text-prompt-variant';
export * from './prompts/chat-prompt-variant';
export * from './prompts/agent-prompt-variant';
export * from './prompts/prompt-inference-configuration';

// ===================================
// Models
// ===================================
export * from './models';
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { CommonPromptVariantProps, PromptTemplateType, IPromptVariant } from './prompt-variant';
import { IAgentAlias } from '../agents/agent-alias';

/**
* Properties for creating an agent prompt variant.
*/
export interface AgentPromptVariantProps extends CommonPromptVariantProps {
/**
* An alias pointing to the agent version to be used.
*/
readonly agentAlias: IAgentAlias;

/**
* The text prompt. Variables are used by enclosing its name with double curly braces
* as in `{{variable_name}}`.
*/
readonly promptText: string;
}

/**
* Creates an agent prompt template variant.
*
* @param props - Properties for the agent prompt variant
* @returns A PromptVariant configured for agent interactions
*/
export function createAgentPromptVariant(props: AgentPromptVariantProps): IPromptVariant {
return {
name: props.variantName,
templateType: PromptTemplateType.TEXT,
genAiResource: {
agent: {
agentIdentifier: props.agentAlias.aliasArn,
},
},
templateConfiguration: {
text: {
inputVariables: props.promptVariables?.map((variable: string) => {
return { name: variable };
}),
text: props.promptText,
},
},
};
}
Loading
Loading