Skip to content

Commit

Permalink
feat(client-bedrock-runtime): Add support for contextual grounding ch…
Browse files Browse the repository at this point in the history
…eck and ApplyGuardrail API for Guardrails for Amazon Bedrock.
  • Loading branch information
awstools committed Jul 10, 2024
1 parent bda616d commit 8863b77
Show file tree
Hide file tree
Showing 10 changed files with 2,253 additions and 983 deletions.
8 changes: 8 additions & 0 deletions clients/client-bedrock-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ see LICENSE for more information.

## Client Commands (Operations List)

<details>
<summary>
ApplyGuardrail
</summary>

[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/)

</details>
<details>
<summary>
Converse
Expand Down
20 changes: 20 additions & 0 deletions clients/client-bedrock-runtime/src/BedrockRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { createAggregatedClient } from "@smithy/smithy-client";
import { HttpHandlerOptions as __HttpHandlerOptions } from "@smithy/types";

import { BedrockRuntimeClient, BedrockRuntimeClientConfig } from "./BedrockRuntimeClient";
import {
ApplyGuardrailCommand,
ApplyGuardrailCommandInput,
ApplyGuardrailCommandOutput,
} from "./commands/ApplyGuardrailCommand";
import { ConverseCommand, ConverseCommandInput, ConverseCommandOutput } from "./commands/ConverseCommand";
import {
ConverseStreamCommand,
Expand All @@ -17,13 +22,28 @@ import {
} from "./commands/InvokeModelWithResponseStreamCommand";

const commands = {
ApplyGuardrailCommand,
ConverseCommand,
ConverseStreamCommand,
InvokeModelCommand,
InvokeModelWithResponseStreamCommand,
};

export interface BedrockRuntime {
/**
* @see {@link ApplyGuardrailCommand}
*/
applyGuardrail(
args: ApplyGuardrailCommandInput,
options?: __HttpHandlerOptions
): Promise<ApplyGuardrailCommandOutput>;
applyGuardrail(args: ApplyGuardrailCommandInput, cb: (err: any, data?: ApplyGuardrailCommandOutput) => void): void;
applyGuardrail(
args: ApplyGuardrailCommandInput,
options: __HttpHandlerOptions,
cb: (err: any, data?: ApplyGuardrailCommandOutput) => void
): void;

/**
* @see {@link ConverseCommand}
*/
Expand Down
3 changes: 3 additions & 0 deletions clients/client-bedrock-runtime/src/BedrockRuntimeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {
HttpAuthSchemeResolvedConfig,
resolveHttpAuthSchemeConfig,
} from "./auth/httpAuthSchemeProvider";
import { ApplyGuardrailCommandInput, ApplyGuardrailCommandOutput } from "./commands/ApplyGuardrailCommand";
import { ConverseCommandInput, ConverseCommandOutput } from "./commands/ConverseCommand";
import { ConverseStreamCommandInput, ConverseStreamCommandOutput } from "./commands/ConverseStreamCommand";
import { InvokeModelCommandInput, InvokeModelCommandOutput } from "./commands/InvokeModelCommand";
Expand All @@ -81,6 +82,7 @@ export { __Client };
* @public
*/
export type ServiceInputTypes =
| ApplyGuardrailCommandInput
| ConverseCommandInput
| ConverseStreamCommandInput
| InvokeModelCommandInput
Expand All @@ -90,6 +92,7 @@ export type ServiceInputTypes =
* @public
*/
export type ServiceOutputTypes =
| ApplyGuardrailCommandOutput
| ConverseCommandOutput
| ConverseStreamCommandOutput
| InvokeModelCommandOutput
Expand Down
189 changes: 189 additions & 0 deletions clients/client-bedrock-runtime/src/commands/ApplyGuardrailCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// smithy-typescript generated code
import { getEndpointPlugin } from "@smithy/middleware-endpoint";
import { getSerdePlugin } from "@smithy/middleware-serde";
import { Command as $Command } from "@smithy/smithy-client";
import { MetadataBearer as __MetadataBearer } from "@smithy/types";

import { BedrockRuntimeClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../BedrockRuntimeClient";
import { commonParams } from "../endpoint/EndpointParameters";
import { ApplyGuardrailRequest, ApplyGuardrailResponse } from "../models/models_0";
import { de_ApplyGuardrailCommand, se_ApplyGuardrailCommand } from "../protocols/Aws_restJson1";

/**
* @public
*/
export type { __MetadataBearer };
export { $Command };
/**
* @public
*
* The input for {@link ApplyGuardrailCommand}.
*/
export interface ApplyGuardrailCommandInput extends ApplyGuardrailRequest {}
/**
* @public
*
* The output of {@link ApplyGuardrailCommand}.
*/
export interface ApplyGuardrailCommandOutput extends ApplyGuardrailResponse, __MetadataBearer {}

/**
* <p>The action to apply a guardrail.</p>
* @example
* Use a bare-bones client and the command you need to make an API call.
* ```javascript
* import { BedrockRuntimeClient, ApplyGuardrailCommand } from "@aws-sdk/client-bedrock-runtime"; // ES Modules import
* // const { BedrockRuntimeClient, ApplyGuardrailCommand } = require("@aws-sdk/client-bedrock-runtime"); // CommonJS import
* const client = new BedrockRuntimeClient(config);
* const input = { // ApplyGuardrailRequest
* guardrailIdentifier: "STRING_VALUE", // required
* guardrailVersion: "STRING_VALUE", // required
* source: "INPUT" || "OUTPUT", // required
* content: [ // GuardrailContentBlockList // required
* { // GuardrailContentBlock Union: only one key present
* text: { // GuardrailTextBlock
* text: "STRING_VALUE", // required
* qualifiers: [ // GuardrailContentQualifierList
* "grounding_source" || "query" || "guard_content",
* ],
* },
* },
* ],
* };
* const command = new ApplyGuardrailCommand(input);
* const response = await client.send(command);
* // { // ApplyGuardrailResponse
* // usage: { // GuardrailUsage
* // topicPolicyUnits: Number("int"), // required
* // contentPolicyUnits: Number("int"), // required
* // wordPolicyUnits: Number("int"), // required
* // sensitiveInformationPolicyUnits: Number("int"), // required
* // sensitiveInformationPolicyFreeUnits: Number("int"), // required
* // contextualGroundingPolicyUnits: Number("int"), // required
* // },
* // action: "NONE" || "GUARDRAIL_INTERVENED", // required
* // outputs: [ // GuardrailOutputContentList // required
* // { // GuardrailOutputContent
* // text: "STRING_VALUE",
* // },
* // ],
* // assessments: [ // GuardrailAssessmentList // required
* // { // GuardrailAssessment
* // topicPolicy: { // GuardrailTopicPolicyAssessment
* // topics: [ // GuardrailTopicList // required
* // { // GuardrailTopic
* // name: "STRING_VALUE", // required
* // type: "DENY", // required
* // action: "BLOCKED", // required
* // },
* // ],
* // },
* // contentPolicy: { // GuardrailContentPolicyAssessment
* // filters: [ // GuardrailContentFilterList // required
* // { // GuardrailContentFilter
* // type: "INSULTS" || "HATE" || "SEXUAL" || "VIOLENCE" || "MISCONDUCT" || "PROMPT_ATTACK", // required
* // confidence: "NONE" || "LOW" || "MEDIUM" || "HIGH", // required
* // action: "BLOCKED", // required
* // },
* // ],
* // },
* // wordPolicy: { // GuardrailWordPolicyAssessment
* // customWords: [ // GuardrailCustomWordList // required
* // { // GuardrailCustomWord
* // match: "STRING_VALUE", // required
* // action: "BLOCKED", // required
* // },
* // ],
* // managedWordLists: [ // GuardrailManagedWordList // required
* // { // GuardrailManagedWord
* // match: "STRING_VALUE", // required
* // type: "PROFANITY", // required
* // action: "BLOCKED", // required
* // },
* // ],
* // },
* // sensitiveInformationPolicy: { // GuardrailSensitiveInformationPolicyAssessment
* // piiEntities: [ // GuardrailPiiEntityFilterList // required
* // { // GuardrailPiiEntityFilter
* // match: "STRING_VALUE", // required
* // 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
* // action: "ANONYMIZED" || "BLOCKED", // required
* // },
* // ],
* // regexes: [ // GuardrailRegexFilterList // required
* // { // GuardrailRegexFilter
* // name: "STRING_VALUE",
* // match: "STRING_VALUE",
* // regex: "STRING_VALUE",
* // action: "ANONYMIZED" || "BLOCKED", // required
* // },
* // ],
* // },
* // contextualGroundingPolicy: { // GuardrailContextualGroundingPolicyAssessment
* // filters: [ // GuardrailContextualGroundingFilters
* // { // GuardrailContextualGroundingFilter
* // type: "GROUNDING" || "RELEVANCE", // required
* // threshold: Number("double"), // required
* // score: Number("double"), // required
* // action: "BLOCKED" || "NONE", // required
* // },
* // ],
* // },
* // },
* // ],
* // };
*
* ```
*
* @param ApplyGuardrailCommandInput - {@link ApplyGuardrailCommandInput}
* @returns {@link ApplyGuardrailCommandOutput}
* @see {@link ApplyGuardrailCommandInput} for command's `input` shape.
* @see {@link ApplyGuardrailCommandOutput} for command's `response` shape.
* @see {@link BedrockRuntimeClientResolvedConfig | config} for BedrockRuntimeClient's `config` shape.
*
* @throws {@link AccessDeniedException} (client fault)
* <p>The request is denied because of missing access permissions.</p>
*
* @throws {@link InternalServerException} (server fault)
* <p>An internal server error occurred. Retry your request.</p>
*
* @throws {@link ResourceNotFoundException} (client fault)
* <p>The specified resource ARN was not found. Check the ARN and try your request again.</p>
*
* @throws {@link ServiceQuotaExceededException} (client fault)
* <p>The number of requests exceeds the service quota. Resubmit your request later.</p>
*
* @throws {@link ThrottlingException} (client fault)
* <p>The number of requests exceeds the limit. Resubmit your request later.</p>
*
* @throws {@link ValidationException} (client fault)
* <p>Input validation failed. Check your request parameters and retry the request.</p>
*
* @throws {@link BedrockRuntimeServiceException}
* <p>Base exception class for all service exceptions from BedrockRuntime service.</p>
*
* @public
*/
export class ApplyGuardrailCommand extends $Command
.classBuilder<
ApplyGuardrailCommandInput,
ApplyGuardrailCommandOutput,
BedrockRuntimeClientResolvedConfig,
ServiceInputTypes,
ServiceOutputTypes
>()
.ep({
...commonParams,
})
.m(function (this: any, Command: any, cs: any, config: BedrockRuntimeClientResolvedConfig, o: any) {
return [
getSerdePlugin(config, this.serialize, this.deserialize),
getEndpointPlugin(config, Command.getEndpointParameterInstructions()),
];
})
.s("AmazonBedrockFrontendService", "ApplyGuardrail", {})
.n("BedrockRuntimeClient", "ApplyGuardrailCommand")
.f(void 0, void 0)
.ser(se_ApplyGuardrailCommand)
.de(de_ApplyGuardrailCommand)
.build() {}
29 changes: 29 additions & 0 deletions clients/client-bedrock-runtime/src/commands/ConverseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ export interface ConverseCommandOutput extends ConverseResponse, __MetadataBeare
* guardContent: { // GuardrailConverseContentBlock Union: only one key present
* text: { // GuardrailConverseTextBlock
* text: "STRING_VALUE", // required
* qualifiers: [ // GuardrailConverseContentQualifierList
* "grounding_source" || "query" || "guard_content",
* ],
* },
* },
* },
Expand All @@ -110,6 +113,9 @@ export interface ConverseCommandOutput extends ConverseResponse, __MetadataBeare
* guardContent: {// Union: only one key present
* text: {
* text: "STRING_VALUE", // required
* qualifiers: [
* "grounding_source" || "query" || "guard_content",
* ],
* },
* },
* },
Expand Down Expand Up @@ -205,6 +211,9 @@ export interface ConverseCommandOutput extends ConverseResponse, __MetadataBeare
* // guardContent: { // GuardrailConverseContentBlock Union: only one key present
* // text: { // GuardrailConverseTextBlock
* // text: "STRING_VALUE", // required
* // qualifiers: [ // GuardrailConverseContentQualifierList
* // "grounding_source" || "query" || "guard_content",
* // ],
* // },
* // },
* // },
Expand Down Expand Up @@ -278,6 +287,16 @@ export interface ConverseCommandOutput extends ConverseResponse, __MetadataBeare
* // },
* // ],
* // },
* // contextualGroundingPolicy: { // GuardrailContextualGroundingPolicyAssessment
* // filters: [ // GuardrailContextualGroundingFilters
* // { // GuardrailContextualGroundingFilter
* // type: "GROUNDING" || "RELEVANCE", // required
* // threshold: Number("double"), // required
* // score: Number("double"), // required
* // action: "BLOCKED" || "NONE", // required
* // },
* // ],
* // },
* // },
* // },
* // outputAssessments: { // GuardrailAssessmentListMap
Expand Down Expand Up @@ -333,6 +352,16 @@ export interface ConverseCommandOutput extends ConverseResponse, __MetadataBeare
* // },
* // ],
* // },
* // contextualGroundingPolicy: {
* // filters: [
* // {
* // type: "GROUNDING" || "RELEVANCE", // required
* // threshold: Number("double"), // required
* // score: Number("double"), // required
* // action: "BLOCKED" || "NONE", // required
* // },
* // ],
* // },
* // },
* // ],
* // },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ export interface ConverseStreamCommandOutput extends ConverseStreamResponse, __M
* guardContent: { // GuardrailConverseContentBlock Union: only one key present
* text: { // GuardrailConverseTextBlock
* text: "STRING_VALUE", // required
* qualifiers: [ // GuardrailConverseContentQualifierList
* "grounding_source" || "query" || "guard_content",
* ],
* },
* },
* },
Expand All @@ -117,6 +120,9 @@ export interface ConverseStreamCommandOutput extends ConverseStreamResponse, __M
* guardContent: {// Union: only one key present
* text: {
* text: "STRING_VALUE", // required
* qualifiers: [
* "grounding_source" || "query" || "guard_content",
* ],
* },
* },
* },
Expand Down Expand Up @@ -258,6 +264,16 @@ export interface ConverseStreamCommandOutput extends ConverseStreamResponse, __M
* // },
* // ],
* // },
* // contextualGroundingPolicy: { // GuardrailContextualGroundingPolicyAssessment
* // filters: [ // GuardrailContextualGroundingFilters
* // { // GuardrailContextualGroundingFilter
* // type: "GROUNDING" || "RELEVANCE", // required
* // threshold: Number("double"), // required
* // score: Number("double"), // required
* // action: "BLOCKED" || "NONE", // required
* // },
* // ],
* // },
* // },
* // },
* // outputAssessments: { // GuardrailAssessmentListMap
Expand Down Expand Up @@ -313,6 +329,16 @@ export interface ConverseStreamCommandOutput extends ConverseStreamResponse, __M
* // },
* // ],
* // },
* // contextualGroundingPolicy: {
* // filters: [
* // {
* // type: "GROUNDING" || "RELEVANCE", // required
* // threshold: Number("double"), // required
* // score: Number("double"), // required
* // action: "BLOCKED" || "NONE", // required
* // },
* // ],
* // },
* // },
* // ],
* // },
Expand Down
1 change: 1 addition & 0 deletions clients/client-bedrock-runtime/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// smithy-typescript generated code
export * from "./ApplyGuardrailCommand";
export * from "./ConverseCommand";
export * from "./ConverseStreamCommand";
export * from "./InvokeModelCommand";
Expand Down
Loading

0 comments on commit 8863b77

Please sign in to comment.