Example Template Using Constructs

Here's an example of how to use the BedrockAgentBlueprintsConstruct in your CDK app. In this example we first define the agent properties using the AgentDefinitionBuilder. Then we create an action group with a Lambda function and API schema definition. Finally, we instantiate the BedrockAgentBlueprintsConstruct and pass in the agent definition and action group. You can further customize the agent by adding knowledge bases, guardrails, and other configurations:

import * as cdk from 'aws-cdk-lib';
import { readFileSync } from 'fs';
import { resolve } from "path";
import { Construct } from 'constructs';
import { BedrockAgentBlueprintsConstruct } from '../../bin/BedrockAgentBlueprintsConstruct';
import { AgentDefinitionBuilder } from '../../bin/constructs/AgentDefinitionBuilder';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
import { AgentActionGroup } from '../../bin/constructs/AgentActionGroup';
import { AgentKnowledgeBase } from '../../bin/constructs/AgentKnowledgeBase';
import { BedrockGuardrailsBuilder, FilterType, ManagedWordsTypes, PIIAction, PIIType } from '../../bin/constructs/BedrockGuardrailsBuilder';

export class DemoTemplateStack extends cdk.Stack {
    constructor(scope: Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);

        const agentDef = new AgentDefinitionBuilder(this, 'NewAgentDef', {})
            .withAgentName('NewFriendlyAgent')
            .withInstruction('nice new fun agent to do great things in code')
            .withUserInput()
            .build();

        const inlineCode = Buffer.from(
            `
                exports.handler = async (event) => {
                    console.log('Hello from Lambda!');
                    return { message: 'Success!' };
                };
                `);

        const fileBufferSchema: Buffer = readFileSync(resolve(__dirname, 'assets', 'openAPISchema.json'));
        const action = new AgentActionGroup(this, 'NewAction', {
            actionGroupName: 'DummyAction',
            description: 'Dummy action for dummy agent',
            actionGroupExecutor: {
                lambdaDefinition: {
                    lambdaCode: inlineCode,
                    lambdaHandler: 'index.handler',
                    lambdaRuntime: Runtime.NODEJS_18_X
                }
            },
            schemaDefinition: {
                inlineAPISchema: fileBufferSchema.toString('utf8')
            },
        });

        const fileBuffer: Buffer = readFileSync(resolve(__dirname, 'assets', 'dummy.csv'));

        const knowledgeBase = new AgentKnowledgeBase(this, 'NewKB', {
            kbName: 'DummyKB',
            agentInstruction: 'Dummy KB for dummy agent',
            assetFiles: [fileBuffer]
        });

        const guardrail = new BedrockGuardrailsBuilder(this, "AgentGuardrail", {
            name: "RubyOnRails",
            generateKmsKey: true,
        })
            .withFiltersConfig(FilterType.INSULTS)
            .withManagedWordsConfig(ManagedWordsTypes.PROFANITY)
            .withWordsConfig(['government', 'dictator'])
            .withPIIConfig(PIIAction.ANONYMIZE, PIIType.US_SOCIAL_SECURITY_NUMBER)
            .withTopicConfig("Arts", "Anything related to arts and crafts", ['painting', 'ceramics'])
            .build();

        new BedrockAgentBlueprintsConstruct(this, 'AmazonBedrockAgentBlueprintsStack', {
            agentDefinition: agentDef,
            actionGroups: [action],
            knowledgeBases: [knowledgeBase],
            guardrail: guardrail,
        });
    }
}