# Guardrail

Guradrail settings for BedrockInvokeModel.

Obtainable from: Guardrail.enable, Guardrail.enableDraft

- Source: [`aws-stepfunctions-tasks/lib/bedrock/guardrail.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-stepfunctions-tasks/lib/bedrock/guardrail.ts#L7)
- Type last changed: v2.149.0 · 12 Jul 2024

## Example

TypeScript:

```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:

```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:

```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();
```

C# / .NET:

```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") }
    }
});
```

Golang:

```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

- `guardrailIdentifier`: string — The identitifier of guardrail.

- `guardrailVersion`: string — The version of guardrail.

## Methods

- `enable(identifier: string, version: number): Guardrail // chainable` (static) — Enable guardrail.

  **Parameters**

  - `identifier` — The id or arn of the guardrail.
  - `version` — The version of the guardrail.

- `enableDraft(identifier: string): Guardrail // chainable` (static) — Enable guardrail with DRAFT version.

  **Parameters**

  - `identifier` — The identifier of the guardrail.
