Skip to content

Commit 8863b77

Browse files
author
awstools
committed
feat(client-bedrock-runtime): Add support for contextual grounding check and ApplyGuardrail API for Guardrails for Amazon Bedrock.
1 parent bda616d commit 8863b77

File tree

10 files changed

+2253
-983
lines changed

10 files changed

+2253
-983
lines changed

clients/client-bedrock-runtime/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ see LICENSE for more information.
203203

204204
## Client Commands (Operations List)
205205

206+
<details>
207+
<summary>
208+
ApplyGuardrail
209+
</summary>
210+
211+
[Command API Reference](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-runtime/command/ApplyGuardrailCommand/) / [Input](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-bedrock-runtime/Interface/ApplyGuardrailCommandInput/) / [Output](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-bedrock-runtime/Interface/ApplyGuardrailCommandOutput/)
212+
213+
</details>
206214
<details>
207215
<summary>
208216
Converse

clients/client-bedrock-runtime/src/BedrockRuntime.ts

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { createAggregatedClient } from "@smithy/smithy-client";
33
import { HttpHandlerOptions as __HttpHandlerOptions } from "@smithy/types";
44

55
import { BedrockRuntimeClient, BedrockRuntimeClientConfig } from "./BedrockRuntimeClient";
6+
import {
7+
ApplyGuardrailCommand,
8+
ApplyGuardrailCommandInput,
9+
ApplyGuardrailCommandOutput,
10+
} from "./commands/ApplyGuardrailCommand";
611
import { ConverseCommand, ConverseCommandInput, ConverseCommandOutput } from "./commands/ConverseCommand";
712
import {
813
ConverseStreamCommand,
@@ -17,13 +22,28 @@ import {
1722
} from "./commands/InvokeModelWithResponseStreamCommand";
1823

1924
const commands = {
25+
ApplyGuardrailCommand,
2026
ConverseCommand,
2127
ConverseStreamCommand,
2228
InvokeModelCommand,
2329
InvokeModelWithResponseStreamCommand,
2430
};
2531

2632
export interface BedrockRuntime {
33+
/**
34+
* @see {@link ApplyGuardrailCommand}
35+
*/
36+
applyGuardrail(
37+
args: ApplyGuardrailCommandInput,
38+
options?: __HttpHandlerOptions
39+
): Promise<ApplyGuardrailCommandOutput>;
40+
applyGuardrail(args: ApplyGuardrailCommandInput, cb: (err: any, data?: ApplyGuardrailCommandOutput) => void): void;
41+
applyGuardrail(
42+
args: ApplyGuardrailCommandInput,
43+
options: __HttpHandlerOptions,
44+
cb: (err: any, data?: ApplyGuardrailCommandOutput) => void
45+
): void;
46+
2747
/**
2848
* @see {@link ConverseCommand}
2949
*/

clients/client-bedrock-runtime/src/BedrockRuntimeClient.ts

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import {
5959
HttpAuthSchemeResolvedConfig,
6060
resolveHttpAuthSchemeConfig,
6161
} from "./auth/httpAuthSchemeProvider";
62+
import { ApplyGuardrailCommandInput, ApplyGuardrailCommandOutput } from "./commands/ApplyGuardrailCommand";
6263
import { ConverseCommandInput, ConverseCommandOutput } from "./commands/ConverseCommand";
6364
import { ConverseStreamCommandInput, ConverseStreamCommandOutput } from "./commands/ConverseStreamCommand";
6465
import { InvokeModelCommandInput, InvokeModelCommandOutput } from "./commands/InvokeModelCommand";
@@ -81,6 +82,7 @@ export { __Client };
8182
* @public
8283
*/
8384
export type ServiceInputTypes =
85+
| ApplyGuardrailCommandInput
8486
| ConverseCommandInput
8587
| ConverseStreamCommandInput
8688
| InvokeModelCommandInput
@@ -90,6 +92,7 @@ export type ServiceInputTypes =
9092
* @public
9193
*/
9294
export type ServiceOutputTypes =
95+
| ApplyGuardrailCommandOutput
9396
| ConverseCommandOutput
9497
| ConverseStreamCommandOutput
9598
| InvokeModelCommandOutput
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
// smithy-typescript generated code
2+
import { getEndpointPlugin } from "@smithy/middleware-endpoint";
3+
import { getSerdePlugin } from "@smithy/middleware-serde";
4+
import { Command as $Command } from "@smithy/smithy-client";
5+
import { MetadataBearer as __MetadataBearer } from "@smithy/types";
6+
7+
import { BedrockRuntimeClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../BedrockRuntimeClient";
8+
import { commonParams } from "../endpoint/EndpointParameters";
9+
import { ApplyGuardrailRequest, ApplyGuardrailResponse } from "../models/models_0";
10+
import { de_ApplyGuardrailCommand, se_ApplyGuardrailCommand } from "../protocols/Aws_restJson1";
11+
12+
/**
13+
* @public
14+
*/
15+
export type { __MetadataBearer };
16+
export { $Command };
17+
/**
18+
* @public
19+
*
20+
* The input for {@link ApplyGuardrailCommand}.
21+
*/
22+
export interface ApplyGuardrailCommandInput extends ApplyGuardrailRequest {}
23+
/**
24+
* @public
25+
*
26+
* The output of {@link ApplyGuardrailCommand}.
27+
*/
28+
export interface ApplyGuardrailCommandOutput extends ApplyGuardrailResponse, __MetadataBearer {}
29+
30+
/**
31+
* <p>The action to apply a guardrail.</p>
32+
* @example
33+
* Use a bare-bones client and the command you need to make an API call.
34+
* ```javascript
35+
* import { BedrockRuntimeClient, ApplyGuardrailCommand } from "@aws-sdk/client-bedrock-runtime"; // ES Modules import
36+
* // const { BedrockRuntimeClient, ApplyGuardrailCommand } = require("@aws-sdk/client-bedrock-runtime"); // CommonJS import
37+
* const client = new BedrockRuntimeClient(config);
38+
* const input = { // ApplyGuardrailRequest
39+
* guardrailIdentifier: "STRING_VALUE", // required
40+
* guardrailVersion: "STRING_VALUE", // required
41+
* source: "INPUT" || "OUTPUT", // required
42+
* content: [ // GuardrailContentBlockList // required
43+
* { // GuardrailContentBlock Union: only one key present
44+
* text: { // GuardrailTextBlock
45+
* text: "STRING_VALUE", // required
46+
* qualifiers: [ // GuardrailContentQualifierList
47+
* "grounding_source" || "query" || "guard_content",
48+
* ],
49+
* },
50+
* },
51+
* ],
52+
* };
53+
* const command = new ApplyGuardrailCommand(input);
54+
* const response = await client.send(command);
55+
* // { // ApplyGuardrailResponse
56+
* // usage: { // GuardrailUsage
57+
* // topicPolicyUnits: Number("int"), // required
58+
* // contentPolicyUnits: Number("int"), // required
59+
* // wordPolicyUnits: Number("int"), // required
60+
* // sensitiveInformationPolicyUnits: Number("int"), // required
61+
* // sensitiveInformationPolicyFreeUnits: Number("int"), // required
62+
* // contextualGroundingPolicyUnits: Number("int"), // required
63+
* // },
64+
* // action: "NONE" || "GUARDRAIL_INTERVENED", // required
65+
* // outputs: [ // GuardrailOutputContentList // required
66+
* // { // GuardrailOutputContent
67+
* // text: "STRING_VALUE",
68+
* // },
69+
* // ],
70+
* // assessments: [ // GuardrailAssessmentList // required
71+
* // { // GuardrailAssessment
72+
* // topicPolicy: { // GuardrailTopicPolicyAssessment
73+
* // topics: [ // GuardrailTopicList // required
74+
* // { // GuardrailTopic
75+
* // name: "STRING_VALUE", // required
76+
* // type: "DENY", // required
77+
* // action: "BLOCKED", // required
78+
* // },
79+
* // ],
80+
* // },
81+
* // contentPolicy: { // GuardrailContentPolicyAssessment
82+
* // filters: [ // GuardrailContentFilterList // required
83+
* // { // GuardrailContentFilter
84+
* // type: "INSULTS" || "HATE" || "SEXUAL" || "VIOLENCE" || "MISCONDUCT" || "PROMPT_ATTACK", // required
85+
* // confidence: "NONE" || "LOW" || "MEDIUM" || "HIGH", // required
86+
* // action: "BLOCKED", // required
87+
* // },
88+
* // ],
89+
* // },
90+
* // wordPolicy: { // GuardrailWordPolicyAssessment
91+
* // customWords: [ // GuardrailCustomWordList // required
92+
* // { // GuardrailCustomWord
93+
* // match: "STRING_VALUE", // required
94+
* // action: "BLOCKED", // required
95+
* // },
96+
* // ],
97+
* // managedWordLists: [ // GuardrailManagedWordList // required
98+
* // { // GuardrailManagedWord
99+
* // match: "STRING_VALUE", // required
100+
* // type: "PROFANITY", // required
101+
* // action: "BLOCKED", // required
102+
* // },
103+
* // ],
104+
* // },
105+
* // sensitiveInformationPolicy: { // GuardrailSensitiveInformationPolicyAssessment
106+
* // piiEntities: [ // GuardrailPiiEntityFilterList // required
107+
* // { // GuardrailPiiEntityFilter
108+
* // match: "STRING_VALUE", // required
109+
* // type: "ADDRESS" || "AGE" || "AWS_ACCESS_KEY" || "AWS_SECRET_KEY" || "CA_HEALTH_NUMBER" || "CA_SOCIAL_INSURANCE_NUMBER" || "CREDIT_DEBIT_CARD_CVV" || "CREDIT_DEBIT_CARD_EXPIRY" || "CREDIT_DEBIT_CARD_NUMBER" || "DRIVER_ID" || "EMAIL" || "INTERNATIONAL_BANK_ACCOUNT_NUMBER" || "IP_ADDRESS" || "LICENSE_PLATE" || "MAC_ADDRESS" || "NAME" || "PASSWORD" || "PHONE" || "PIN" || "SWIFT_CODE" || "UK_NATIONAL_HEALTH_SERVICE_NUMBER" || "UK_NATIONAL_INSURANCE_NUMBER" || "UK_UNIQUE_TAXPAYER_REFERENCE_NUMBER" || "URL" || "USERNAME" || "US_BANK_ACCOUNT_NUMBER" || "US_BANK_ROUTING_NUMBER" || "US_INDIVIDUAL_TAX_IDENTIFICATION_NUMBER" || "US_PASSPORT_NUMBER" || "US_SOCIAL_SECURITY_NUMBER" || "VEHICLE_IDENTIFICATION_NUMBER", // required
110+
* // action: "ANONYMIZED" || "BLOCKED", // required
111+
* // },
112+
* // ],
113+
* // regexes: [ // GuardrailRegexFilterList // required
114+
* // { // GuardrailRegexFilter
115+
* // name: "STRING_VALUE",
116+
* // match: "STRING_VALUE",
117+
* // regex: "STRING_VALUE",
118+
* // action: "ANONYMIZED" || "BLOCKED", // required
119+
* // },
120+
* // ],
121+
* // },
122+
* // contextualGroundingPolicy: { // GuardrailContextualGroundingPolicyAssessment
123+
* // filters: [ // GuardrailContextualGroundingFilters
124+
* // { // GuardrailContextualGroundingFilter
125+
* // type: "GROUNDING" || "RELEVANCE", // required
126+
* // threshold: Number("double"), // required
127+
* // score: Number("double"), // required
128+
* // action: "BLOCKED" || "NONE", // required
129+
* // },
130+
* // ],
131+
* // },
132+
* // },
133+
* // ],
134+
* // };
135+
*
136+
* ```
137+
*
138+
* @param ApplyGuardrailCommandInput - {@link ApplyGuardrailCommandInput}
139+
* @returns {@link ApplyGuardrailCommandOutput}
140+
* @see {@link ApplyGuardrailCommandInput} for command's `input` shape.
141+
* @see {@link ApplyGuardrailCommandOutput} for command's `response` shape.
142+
* @see {@link BedrockRuntimeClientResolvedConfig | config} for BedrockRuntimeClient's `config` shape.
143+
*
144+
* @throws {@link AccessDeniedException} (client fault)
145+
* <p>The request is denied because of missing access permissions.</p>
146+
*
147+
* @throws {@link InternalServerException} (server fault)
148+
* <p>An internal server error occurred. Retry your request.</p>
149+
*
150+
* @throws {@link ResourceNotFoundException} (client fault)
151+
* <p>The specified resource ARN was not found. Check the ARN and try your request again.</p>
152+
*
153+
* @throws {@link ServiceQuotaExceededException} (client fault)
154+
* <p>The number of requests exceeds the service quota. Resubmit your request later.</p>
155+
*
156+
* @throws {@link ThrottlingException} (client fault)
157+
* <p>The number of requests exceeds the limit. Resubmit your request later.</p>
158+
*
159+
* @throws {@link ValidationException} (client fault)
160+
* <p>Input validation failed. Check your request parameters and retry the request.</p>
161+
*
162+
* @throws {@link BedrockRuntimeServiceException}
163+
* <p>Base exception class for all service exceptions from BedrockRuntime service.</p>
164+
*
165+
* @public
166+
*/
167+
export class ApplyGuardrailCommand extends $Command
168+
.classBuilder<
169+
ApplyGuardrailCommandInput,
170+
ApplyGuardrailCommandOutput,
171+
BedrockRuntimeClientResolvedConfig,
172+
ServiceInputTypes,
173+
ServiceOutputTypes
174+
>()
175+
.ep({
176+
...commonParams,
177+
})
178+
.m(function (this: any, Command: any, cs: any, config: BedrockRuntimeClientResolvedConfig, o: any) {
179+
return [
180+
getSerdePlugin(config, this.serialize, this.deserialize),
181+
getEndpointPlugin(config, Command.getEndpointParameterInstructions()),
182+
];
183+
})
184+
.s("AmazonBedrockFrontendService", "ApplyGuardrail", {})
185+
.n("BedrockRuntimeClient", "ApplyGuardrailCommand")
186+
.f(void 0, void 0)
187+
.ser(se_ApplyGuardrailCommand)
188+
.de(de_ApplyGuardrailCommand)
189+
.build() {}

clients/client-bedrock-runtime/src/commands/ConverseCommand.ts

+29
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ export interface ConverseCommandOutput extends ConverseResponse, __MetadataBeare
9898
* guardContent: { // GuardrailConverseContentBlock Union: only one key present
9999
* text: { // GuardrailConverseTextBlock
100100
* text: "STRING_VALUE", // required
101+
* qualifiers: [ // GuardrailConverseContentQualifierList
102+
* "grounding_source" || "query" || "guard_content",
103+
* ],
101104
* },
102105
* },
103106
* },
@@ -110,6 +113,9 @@ export interface ConverseCommandOutput extends ConverseResponse, __MetadataBeare
110113
* guardContent: {// Union: only one key present
111114
* text: {
112115
* text: "STRING_VALUE", // required
116+
* qualifiers: [
117+
* "grounding_source" || "query" || "guard_content",
118+
* ],
113119
* },
114120
* },
115121
* },
@@ -205,6 +211,9 @@ export interface ConverseCommandOutput extends ConverseResponse, __MetadataBeare
205211
* // guardContent: { // GuardrailConverseContentBlock Union: only one key present
206212
* // text: { // GuardrailConverseTextBlock
207213
* // text: "STRING_VALUE", // required
214+
* // qualifiers: [ // GuardrailConverseContentQualifierList
215+
* // "grounding_source" || "query" || "guard_content",
216+
* // ],
208217
* // },
209218
* // },
210219
* // },
@@ -278,6 +287,16 @@ export interface ConverseCommandOutput extends ConverseResponse, __MetadataBeare
278287
* // },
279288
* // ],
280289
* // },
290+
* // contextualGroundingPolicy: { // GuardrailContextualGroundingPolicyAssessment
291+
* // filters: [ // GuardrailContextualGroundingFilters
292+
* // { // GuardrailContextualGroundingFilter
293+
* // type: "GROUNDING" || "RELEVANCE", // required
294+
* // threshold: Number("double"), // required
295+
* // score: Number("double"), // required
296+
* // action: "BLOCKED" || "NONE", // required
297+
* // },
298+
* // ],
299+
* // },
281300
* // },
282301
* // },
283302
* // outputAssessments: { // GuardrailAssessmentListMap
@@ -333,6 +352,16 @@ export interface ConverseCommandOutput extends ConverseResponse, __MetadataBeare
333352
* // },
334353
* // ],
335354
* // },
355+
* // contextualGroundingPolicy: {
356+
* // filters: [
357+
* // {
358+
* // type: "GROUNDING" || "RELEVANCE", // required
359+
* // threshold: Number("double"), // required
360+
* // score: Number("double"), // required
361+
* // action: "BLOCKED" || "NONE", // required
362+
* // },
363+
* // ],
364+
* // },
336365
* // },
337366
* // ],
338367
* // },

clients/client-bedrock-runtime/src/commands/ConverseStreamCommand.ts

+26
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ export interface ConverseStreamCommandOutput extends ConverseStreamResponse, __M
105105
* guardContent: { // GuardrailConverseContentBlock Union: only one key present
106106
* text: { // GuardrailConverseTextBlock
107107
* text: "STRING_VALUE", // required
108+
* qualifiers: [ // GuardrailConverseContentQualifierList
109+
* "grounding_source" || "query" || "guard_content",
110+
* ],
108111
* },
109112
* },
110113
* },
@@ -117,6 +120,9 @@ export interface ConverseStreamCommandOutput extends ConverseStreamResponse, __M
117120
* guardContent: {// Union: only one key present
118121
* text: {
119122
* text: "STRING_VALUE", // required
123+
* qualifiers: [
124+
* "grounding_source" || "query" || "guard_content",
125+
* ],
120126
* },
121127
* },
122128
* },
@@ -258,6 +264,16 @@ export interface ConverseStreamCommandOutput extends ConverseStreamResponse, __M
258264
* // },
259265
* // ],
260266
* // },
267+
* // contextualGroundingPolicy: { // GuardrailContextualGroundingPolicyAssessment
268+
* // filters: [ // GuardrailContextualGroundingFilters
269+
* // { // GuardrailContextualGroundingFilter
270+
* // type: "GROUNDING" || "RELEVANCE", // required
271+
* // threshold: Number("double"), // required
272+
* // score: Number("double"), // required
273+
* // action: "BLOCKED" || "NONE", // required
274+
* // },
275+
* // ],
276+
* // },
261277
* // },
262278
* // },
263279
* // outputAssessments: { // GuardrailAssessmentListMap
@@ -313,6 +329,16 @@ export interface ConverseStreamCommandOutput extends ConverseStreamResponse, __M
313329
* // },
314330
* // ],
315331
* // },
332+
* // contextualGroundingPolicy: {
333+
* // filters: [
334+
* // {
335+
* // type: "GROUNDING" || "RELEVANCE", // required
336+
* // threshold: Number("double"), // required
337+
* // score: Number("double"), // required
338+
* // action: "BLOCKED" || "NONE", // required
339+
* // },
340+
* // ],
341+
* // },
316342
* // },
317343
* // ],
318344
* // },

clients/client-bedrock-runtime/src/commands/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// smithy-typescript generated code
2+
export * from "./ApplyGuardrailCommand";
23
export * from "./ConverseCommand";
34
export * from "./ConverseStreamCommand";
45
export * from "./InvokeModelCommand";

0 commit comments

Comments
 (0)