Skip to content
v2.278.0

Guardrail Class ​

Guradrail settings for BedrockInvokeModel.

Example ​

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

const model = bedrock.FoundationModel.fromFoundationModelId(
  this,
  'Model',
  bedrock.FoundationModelIdentifier.AMAZON_TITAN_TEXT_G1_EXPRESS_V1,
);

const task = new tasks.BedrockInvokeModel(this, 'Prompt Model with guardrail', {
  model,
  body: sfn.TaskInput.fromObject(
    {
      inputText: 'Generate a list of five first names.',
      textGenerationConfig: {
        maxTokenCount: 100,
        temperature: 1,
      },
    },
  ),
  guardrail: tasks.Guardrail.enable('guardrailId', 1),
  resultSelector: {
    names: sfn.JsonPath.stringAt('$.Body.results[0].outputText'),
  },
});
python
import aws_cdk.aws_bedrock as bedrock


model = bedrock.FoundationModel.from_foundation_model_id(self, "Model", bedrock.FoundationModelIdentifier.AMAZON_TITAN_TEXT_G1_EXPRESS_V1)

task = tasks.BedrockInvokeModel(self, "Prompt Model with guardrail",
    model=model,
    body=sfn.TaskInput.from_object({
        "input_text": "Generate a list of five first names.",
        "text_generation_config": {
            "max_token_count": 100,
            "temperature": 1
        }
    }),
    guardrail=tasks.Guardrail.enable("guardrailId", 1),
    result_selector={
        "names": sfn.JsonPath.string_at("$.Body.results[0].outputText")
    }
)
java
import software.amazon.awscdk.services.bedrock.*;


FoundationModel model = FoundationModel.fromFoundationModelId(this, "Model", FoundationModelIdentifier.AMAZON_TITAN_TEXT_G1_EXPRESS_V1);

BedrockInvokeModel task = BedrockInvokeModel.Builder.create(this, "Prompt Model with guardrail")
        .model(model)
        .body(TaskInput.fromObject(Map.of(
                "inputText", "Generate a list of five first names.",
                "textGenerationConfig", Map.of(
                        "maxTokenCount", 100,
                        "temperature", 1))))
        .guardrail(Guardrail.enable("guardrailId", 1))
        .resultSelector(Map.of(
                "names", JsonPath.stringAt("$.Body.results[0].outputText")))
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.Bedrock;


var model = FoundationModel.FromFoundationModelId(this, "Model", FoundationModelIdentifier.AMAZON_TITAN_TEXT_G1_EXPRESS_V1);

var task = new BedrockInvokeModel(this, "Prompt Model with guardrail", new BedrockInvokeModelProps {
    Model = model,
    Body = TaskInput.FromObject(new Dictionary<string, object> {
        { "inputText", "Generate a list of five first names." },
        { "textGenerationConfig", new Dictionary<string, int> {
            { "maxTokenCount", 100 },
            { "temperature", 1 }
        } }
    }),
    Guardrail = Guardrail.Enable("guardrailId", 1),
    ResultSelector = new Dictionary<string, object> {
        { "names", JsonPath.StringAt("$.Body.results[0].outputText") }
    }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import "github.com/aws/aws-cdk-go/awscdk"


model := bedrock.FoundationModel_FromFoundationModelId(this, jsii.String("Model"), bedrock.FoundationModelIdentifier_AMAZON_TITAN_TEXT_G1_EXPRESS_V1())

task := tasks.NewBedrockInvokeModel(this, jsii.String("Prompt Model with guardrail"), &BedrockInvokeModelProps{
	Model: Model,
	Body: sfn.TaskInput_FromObject(map[string]interface{}{
		"inputText": jsii.String("Generate a list of five first names."),
		"textGenerationConfig": map[string]*f64{
			"maxTokenCount": jsii.Number(100),
			"temperature": jsii.Number(1),
		},
	}),
	Guardrail: tasks.Guardrail_Enable(jsii.String("guardrailId"), jsii.Number(1)),
	ResultSelector: map[string]interface{}{
		"names": sfn.JsonPath_stringAt(jsii.String("$.Body.results[0].outputText")),
	},
})

Properties ​

guardrailIdentifierstringguardrail_identifierstrguardrailIdentifierStringGuardrailIdentifierstringGuardrailIdentifier*string #
The identitifier of guardrail.
guardrailVersionstringguardrail_versionstrguardrailVersionStringGuardrailVersionstringGuardrailVersion*string #
The version of guardrail.

Methods ​

enable(identifier: string, version: number): Guardrail // chainable@staticmethod def enable(identifier: str, version: int | float) -> Guardrailpublic static Guardrail enable(String identifier, Number version)public static Guardrail Enable(string identifier, double version)func Enable(Identifier *string, Version *float64) Guardrail static #
Enable guardrail.
2 parameters

Parameters

  • identifieridentifieridentifieridentifierIdentifier — The id or arn of the guardrail.
  • versionversionversionversionVersion — The version of the guardrail.
enableDraft(identifier: string): Guardrail // chainable@staticmethod def enable_draft(identifier: str) -> Guardrailpublic static Guardrail enableDraft(String identifier)public static Guardrail EnableDraft(string identifier)func EnableDraft(Identifier *string) Guardrail static #
Enable guardrail with DRAFT version.
1 parameter

Parameters

  • identifieridentifieridentifieridentifierIdentifier — The identifier of the guardrail.