Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
205 changes: 205 additions & 0 deletions packages/@aws-cdk/aws-bedrock-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ This construct library facilitates the deployment of Bedrock Agents, enabling yo
- [Prompt Properties](#prompt-properties)
- [Prompt Version](#prompt-version)
- [Import Methods](#import-methods)
- [Inference Profiles](#inference-profiles)
- [Using Inference Profiles](#using-inference-profiles)
- [Types of Inference Profiles](#types-of-inference-profiles)
- [Prompt Routers](#prompt-routers)
- [Inference Profile Permissions](#inference-profile-permissions)
- [Inference Profiles Import Methods](#inference-profiles-import-methods)

## Agents

Expand Down Expand Up @@ -807,3 +813,202 @@ const importedPrompt = bedrock.Prompt.fromPromptAttributes(this, 'ImportedPrompt
promptVersion: '1', // optional, defaults to 'DRAFT'
});
```

## Inference Profiles

Amazon Bedrock Inference Profiles provide a way to manage and optimize inference configurations for your foundation models. They allow you to define reusable configurations that can be applied across different prompts and agents.

### Using Inference Profiles

Inference profiles can be used with prompts and agents to maintain consistent inference configurations across your application.

#### With Agents

```ts fixture=default
// Create a cross-region inference profile
const crossRegionProfile = bedrock.CrossRegionInferenceProfile.fromConfig({
geoRegion: bedrock.CrossRegionInferenceProfileRegion.US,
model: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0,
});

// Use the cross-region profile with an agent
const agent = new bedrock.Agent(this, 'Agent', {
foundationModel: crossRegionProfile,
instruction: 'You are a helpful and friendly agent that answers questions about agriculture.',
});
```

#### With Prompts

```ts fixture=default
// Create a prompt router for intelligent model selection
const promptRouter = bedrock.PromptRouter.fromDefaultId(
bedrock.DefaultPromptRouterIdentifier.ANTHROPIC_CLAUDE_V1,
'us-east-1'
);

// Use the prompt router with a prompt variant
const variant = bedrock.PromptVariant.text({
variantName: 'variant1',
promptText: 'What is the capital of France?',
model: promptRouter,
});

new bedrock.Prompt(this, 'Prompt', {
promptName: 'prompt-router-test',
variants: [variant],
});
```

### Types of Inference Profiles

Amazon Bedrock offers two types of inference profiles:

#### Application Inference Profiles

Application inference profiles are user-defined profiles that help you track costs and model usage. They can be created for a single region or for multiple regions using a cross-region inference profile.

##### Single Region Application Profile

```ts fixture=default
// Create an application inference profile for one Region
const appProfile = new bedrock.ApplicationInferenceProfile(this, 'MyApplicationProfile', {
applicationInferenceProfileName: 'claude-3-sonnet-v1',
modelSource: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_SONNET_V1_0,
description: 'Application profile for cost tracking',
tags: {
Environment: 'Production',
},
});
```

##### Multi-Region Application Profile

```ts fixture=default
// Create a cross-region inference profile
const crossRegionProfile = bedrock.CrossRegionInferenceProfile.fromConfig({
geoRegion: bedrock.CrossRegionInferenceProfileRegion.US,
model: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V2_0,
});

// Create an application inference profile across regions
const appProfile = new bedrock.ApplicationInferenceProfile(this, 'MyMultiRegionProfile', {
applicationInferenceProfileName: 'claude-35-sonnet-v2-multi-region',
modelSource: crossRegionProfile,
description: 'Multi-region application profile for cost tracking',
});
```

#### System Defined Inference Profiles

Cross-region inference enables you to seamlessly manage unplanned traffic bursts by utilizing compute across different AWS Regions. With cross-region inference, you can distribute traffic across multiple AWS Regions, enabling higher throughput and enhanced resilience during periods of peak demands.

Before using a CrossRegionInferenceProfile, ensure that you have access to the models and regions defined in the inference profiles. For instance, if you use the system defined inference profile "us.anthropic.claude-3-5-sonnet-20241022-v2:0", inference requests will be routed to US East (Virginia) us-east-1, US East (Ohio) us-east-2 and US West (Oregon) us-west-2. Thus, you need to have model access enabled in those regions for the model anthropic.claude-3-5-sonnet-20241022-v2:0.

##### System Defined Profile Configuration

```ts fixture=default
const crossRegionProfile = bedrock.CrossRegionInferenceProfile.fromConfig({
geoRegion: bedrock.CrossRegionInferenceProfileRegion.US,
model: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V2_0,
});
```

### Prompt Routers

Amazon Bedrock intelligent prompt routing provides a single serverless endpoint for efficiently routing requests between different foundational models within the same model family. It can help you optimize for response quality and cost. They offer a comprehensive solution for managing multiple AI models through a single serverless endpoint, simplifying the process for you. Intelligent prompt routing predicts the performance of each model for each request, and dynamically routes each request to the model that it predicts is most likely to give the desired response at the lowest cost.

#### Default and Custom Prompt Routers

```ts fixture=default
// Use a default prompt router
const variant = bedrock.PromptVariant.text({
variantName: 'variant1',
promptText: 'What is the capital of France?',
model: bedrock.PromptRouter.fromDefaultId(
bedrock.DefaultPromptRouterIdentifier.ANTHROPIC_CLAUDE_V1,
'us-east-1'
),
});

new bedrock.Prompt(this, 'Prompt', {
promptName: 'prompt-router-test',
variants: [variant],
});
```

### Inference Profile Permissions

Use the `grantProfileUsage` method to grant appropriate permissions to resources that need to use the inference profile.

#### Granting Profile Usage Permissions

```ts fixture=default
// Create an application inference profile
const profile = new bedrock.ApplicationInferenceProfile(this, 'MyProfile', {
applicationInferenceProfileName: 'my-profile',
modelSource: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0,
});

// Create a Lambda function
const lambdaFunction = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.PYTHON_3_11,
handler: 'index.handler',
code: lambda.Code.fromInline('def handler(event, context): return "Hello"'),
});

// Grant the Lambda function permission to use the inference profile
profile.grantProfileUsage(lambdaFunction);

// Use a system defined inference profile
const crossRegionProfile = bedrock.CrossRegionInferenceProfile.fromConfig({
geoRegion: bedrock.CrossRegionInferenceProfileRegion.US,
model: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0,
});

// Grant permissions to use the cross-region inference profile
crossRegionProfile.grantProfileUsage(lambdaFunction);
```

The `grantProfileUsage` method adds the necessary IAM permissions to the resource, allowing it to use the inference profile. This includes permissions to call `bedrock:GetInferenceProfile` and `bedrock:ListInferenceProfiles` actions on the inference profile resource.

### Inference Profiles Import Methods

You can import existing application inference profiles using the following methods:

```ts fixture=default
// Import an inference profile through attributes
const importedProfile = bedrock.ApplicationInferenceProfile.fromApplicationInferenceProfileAttributes(
this,
'ImportedProfile',
{
inferenceProfileArn: 'arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/my-profile-id',
inferenceProfileIdentifier: 'my-profile-id',
}
);
```

You can also import an application inference profile from an existing L1 CloudFormation construct:

```ts fixture=default
// Create or reference an existing L1 CfnApplicationInferenceProfile
const cfnProfile = new aws_bedrock_cfn.CfnApplicationInferenceProfile(this, 'CfnProfile', {
inferenceProfileName: 'my-cfn-profile',
modelSource: {
copyFrom: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0.invokableArn,
},
description: 'Profile created via L1 construct',
});

// Import the L1 construct as an L2 ApplicationInferenceProfile
const importedFromCfn = bedrock.ApplicationInferenceProfile.fromCfnApplicationInferenceProfile(cfnProfile);

// Grant permissions to use the imported profile
const lambdaFunction = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.PYTHON_3_11,
handler: 'index.handler',
code: lambda.Code.fromInline('def handler(event, context): return "Hello"'),
});

importedFromCfn.grantProfileUsage(lambdaFunction);
```
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-bedrock-alpha/bedrock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export * from './prompts/prompt-inference-configuration';
export * from './prompts/prompt-template-configuration';
export * from './prompts/prompt-genai-resource';

// ===================================
// Inference Profiles
// ===================================
export * from './inference-profiles/inference-profile';
export * from './inference-profiles/application-inference-profile';
export * from './inference-profiles/cross-region-inference-profile';
export * from './inference-profiles/prompt-router';

// ===================================
// Models
// ===================================
Expand Down
Loading
Loading