Skip to content

Commit

Permalink
feat(client-comprehend): This release adds support for toxicity detec…
Browse files Browse the repository at this point in the history
…tion and prompt safety classification.
  • Loading branch information
awstools committed Nov 9, 2023
1 parent 284bd00 commit e05684d
Show file tree
Hide file tree
Showing 15 changed files with 838 additions and 154 deletions.
8 changes: 8 additions & 0 deletions clients/client-comprehend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,14 @@ DetectTargetedSentiment

[Command API Reference](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-comprehend/classes/detecttargetedsentimentcommand.html) / [Input](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-comprehend/interfaces/detecttargetedsentimentcommandinput.html) / [Output](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-comprehend/interfaces/detecttargetedsentimentcommandoutput.html)

</details>
<details>
<summary>
DetectToxicContent
</summary>

[Command API Reference](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-comprehend/classes/detecttoxiccontentcommand.html) / [Input](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-comprehend/interfaces/detecttoxiccontentcommandinput.html) / [Output](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-comprehend/interfaces/detecttoxiccontentcommandoutput.html)

</details>
<details>
<summary>
Expand Down
23 changes: 23 additions & 0 deletions clients/client-comprehend/src/Comprehend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ import {
DetectTargetedSentimentCommandInput,
DetectTargetedSentimentCommandOutput,
} from "./commands/DetectTargetedSentimentCommand";
import {
DetectToxicContentCommand,
DetectToxicContentCommandInput,
DetectToxicContentCommandOutput,
} from "./commands/DetectToxicContentCommand";
import { ImportModelCommand, ImportModelCommandInput, ImportModelCommandOutput } from "./commands/ImportModelCommand";
import {
ListDatasetsCommand,
Expand Down Expand Up @@ -458,6 +463,7 @@ const commands = {
DetectSentimentCommand,
DetectSyntaxCommand,
DetectTargetedSentimentCommand,
DetectToxicContentCommand,
ImportModelCommand,
ListDatasetsCommand,
ListDocumentClassificationJobsCommand,
Expand Down Expand Up @@ -1168,6 +1174,23 @@ export interface Comprehend {
cb: (err: any, data?: DetectTargetedSentimentCommandOutput) => void
): void;

/**
* @see {@link DetectToxicContentCommand}
*/
detectToxicContent(
args: DetectToxicContentCommandInput,
options?: __HttpHandlerOptions
): Promise<DetectToxicContentCommandOutput>;
detectToxicContent(
args: DetectToxicContentCommandInput,
cb: (err: any, data?: DetectToxicContentCommandOutput) => void
): void;
detectToxicContent(
args: DetectToxicContentCommandInput,
options: __HttpHandlerOptions,
cb: (err: any, data?: DetectToxicContentCommandOutput) => void
): void;

/**
* @see {@link ImportModelCommand}
*/
Expand Down
3 changes: 3 additions & 0 deletions clients/client-comprehend/src/ComprehendClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ import {
DetectTargetedSentimentCommandInput,
DetectTargetedSentimentCommandOutput,
} from "./commands/DetectTargetedSentimentCommand";
import { DetectToxicContentCommandInput, DetectToxicContentCommandOutput } from "./commands/DetectToxicContentCommand";
import { ImportModelCommandInput, ImportModelCommandOutput } from "./commands/ImportModelCommand";
import { ListDatasetsCommandInput, ListDatasetsCommandOutput } from "./commands/ListDatasetsCommand";
import {
Expand Down Expand Up @@ -370,6 +371,7 @@ export type ServiceInputTypes =
| DetectSentimentCommandInput
| DetectSyntaxCommandInput
| DetectTargetedSentimentCommandInput
| DetectToxicContentCommandInput
| ImportModelCommandInput
| ListDatasetsCommandInput
| ListDocumentClassificationJobsCommandInput
Expand Down Expand Up @@ -459,6 +461,7 @@ export type ServiceOutputTypes =
| DetectSentimentCommandOutput
| DetectSyntaxCommandOutput
| DetectTargetedSentimentCommandOutput
| DetectToxicContentCommandOutput
| ImportModelCommandOutput
| ListDatasetsCommandOutput
| ListDocumentClassificationJobsCommandOutput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface BatchDetectTargetedSentimentCommandOutput
* @public
* <p>Inspects a batch of documents and returns a sentiment analysis
* for each entity identified in the documents.</p>
* <p>For more information about targeted sentiment, see <a href="https://docs.aws.amazon.com/comprehend/latest/dg/how-targeted-sentiment.html">Targeted sentiment</a>.</p>
* <p>For more information about targeted sentiment, see <a href="https://docs.aws.amazon.com/comprehend/latest/dg/how-targeted-sentiment.html">Targeted sentiment</a> in the <i>Amazon Comprehend Developer Guide</i>.</p>
* @example
* Use a bare-bones client and the command you need to make an API call.
* ```javascript
Expand Down
18 changes: 15 additions & 3 deletions clients/client-comprehend/src/commands/ClassifyDocumentCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,21 @@ export interface ClassifyDocumentCommandOutput extends ClassifyDocumentResponse,

/**
* @public
* <p>Creates a new document classification request to analyze a single document in real-time,
* using a previously created and trained custom model and an endpoint.</p>
* <p>You can input plain text or you can upload a single-page input document (text, PDF, Word, or image). </p>
* <p>Creates a classification request to analyze a single document in real-time. <code>ClassifyDocument</code>
* supports the following model types:</p>
* <ul>
* <li>
* <p>Custom classifier - a custom model that you have created and trained.
* For input, you can provide plain text, a single-page document (PDF, Word, or image), or Textract API output.
* For more information, see <a href="https://docs.aws.amazon.com/comprehend/latest/dg/how-document-classification.html">Custom classification</a> in the <i>Amazon Comprehend Developer Guide</i>.</p>
* </li>
* <li>
* <p>Prompt classifier - Amazon Comprehend provides a model for classifying prompts.
* For input, you provide English plain text input.
* For prompt classification, the response includes only the <code>Classes</code> field.
* For more information about prompt classifiers, see <a href="https://docs.aws.amazon.com/comprehend/latest/dg/prompt-classification.html">Prompt classifiers</a> in the <i>Amazon Comprehend Developer Guide</i>.</p>
* </li>
* </ul>
* <p>If the system detects errors while processing a page in the input document,
* the API response includes an entry in <code>Errors</code> that describes the errors.</p>
* <p>If the system detects a document-level error in your input document, the API returns an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface DetectTargetedSentimentCommandOutput extends DetectTargetedSent
/**
* @public
* <p>Inspects the input text and returns a sentiment analysis for each entity identified in the text.</p>
* <p>For more information about targeted sentiment, see <a href="https://docs.aws.amazon.com/comprehend/latest/dg/how-targeted-sentiment.html">Targeted sentiment</a>.</p>
* <p>For more information about targeted sentiment, see <a href="https://docs.aws.amazon.com/comprehend/latest/dg/how-targeted-sentiment.html">Targeted sentiment</a> in the <i>Amazon Comprehend Developer Guide</i>.</p>
* @example
* Use a bare-bones client and the command you need to make an API call.
* ```javascript
Expand Down
186 changes: 186 additions & 0 deletions clients/client-comprehend/src/commands/DetectToxicContentCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// smithy-typescript generated code
import { EndpointParameterInstructions, getEndpointPlugin } from "@smithy/middleware-endpoint";
import { getSerdePlugin } from "@smithy/middleware-serde";
import { HttpRequest as __HttpRequest, HttpResponse as __HttpResponse } from "@smithy/protocol-http";
import { Command as $Command } from "@smithy/smithy-client";
import {
FinalizeHandlerArguments,
Handler,
HandlerExecutionContext,
HttpHandlerOptions as __HttpHandlerOptions,
MetadataBearer as __MetadataBearer,
MiddlewareStack,
SerdeContext as __SerdeContext,
SMITHY_CONTEXT_KEY,
} from "@smithy/types";

import { ComprehendClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../ComprehendClient";
import {
DetectToxicContentRequest,
DetectToxicContentRequestFilterSensitiveLog,
DetectToxicContentResponse,
} from "../models/models_0";
import { de_DetectToxicContentCommand, se_DetectToxicContentCommand } from "../protocols/Aws_json1_1";

/**
* @public
*/
export { __MetadataBearer, $Command };
/**
* @public
*
* The input for {@link DetectToxicContentCommand}.
*/
export interface DetectToxicContentCommandInput extends DetectToxicContentRequest {}
/**
* @public
*
* The output of {@link DetectToxicContentCommand}.
*/
export interface DetectToxicContentCommandOutput extends DetectToxicContentResponse, __MetadataBearer {}

/**
* @public
* <p>Performs toxicity analysis on the list of text strings that you provide as input.
* The analysis uses the order of strings in the list to determine context when predicting toxicity.
* The API response contains a results list that matches the size of the input list.
* For more information about toxicity detection, see <a href="https://docs.aws.amazon.com/comprehend/latest/dg/toxicity-detection.html">Toxicity detection</a> in the <i>Amazon Comprehend Developer Guide</i>
* </p>
* @example
* Use a bare-bones client and the command you need to make an API call.
* ```javascript
* import { ComprehendClient, DetectToxicContentCommand } from "@aws-sdk/client-comprehend"; // ES Modules import
* // const { ComprehendClient, DetectToxicContentCommand } = require("@aws-sdk/client-comprehend"); // CommonJS import
* const client = new ComprehendClient(config);
* const input = { // DetectToxicContentRequest
* TextSegments: [ // ListOfTextSegments // required
* { // TextSegment
* Text: "STRING_VALUE", // required
* },
* ],
* LanguageCode: "en" || "es" || "fr" || "de" || "it" || "pt" || "ar" || "hi" || "ja" || "ko" || "zh" || "zh-TW", // required
* };
* const command = new DetectToxicContentCommand(input);
* const response = await client.send(command);
* // { // DetectToxicContentResponse
* // ResultList: [ // ListOfToxicLabels
* // { // ToxicLabels
* // Labels: [ // ListOfToxicContent
* // { // ToxicContent
* // Name: "GRAPHIC" || "HARASSMENT_OR_ABUSE" || "HATE_SPEECH" || "INSULT" || "PROFANITY" || "SEXUAL" || "VIOLENCE_OR_THREAT",
* // Score: Number("float"),
* // },
* // ],
* // Toxicity: Number("float"),
* // },
* // ],
* // };
*
* ```
*
* @param DetectToxicContentCommandInput - {@link DetectToxicContentCommandInput}
* @returns {@link DetectToxicContentCommandOutput}
* @see {@link DetectToxicContentCommandInput} for command's `input` shape.
* @see {@link DetectToxicContentCommandOutput} for command's `response` shape.
* @see {@link ComprehendClientResolvedConfig | config} for ComprehendClient's `config` shape.
*
* @throws {@link InternalServerException} (server fault)
* <p>An internal server error occurred. Retry your request.</p>
*
* @throws {@link InvalidRequestException} (client fault)
* <p>The request is invalid.</p>
*
* @throws {@link TextSizeLimitExceededException} (client fault)
* <p>The size of the input text exceeds the limit. Use a smaller document.</p>
*
* @throws {@link UnsupportedLanguageException} (client fault)
* <p>Amazon Comprehend can't process the language of the input text. For custom entity
* recognition APIs, only English, Spanish, French, Italian, German, or Portuguese are accepted.
* For a list of supported languages,
* <a href="https://docs.aws.amazon.com/comprehend/latest/dg/supported-languages.html">Supported languages</a> in the Comprehend Developer Guide.
* </p>
*
* @throws {@link ComprehendServiceException}
* <p>Base exception class for all service exceptions from Comprehend service.</p>
*
*/
export class DetectToxicContentCommand extends $Command<
DetectToxicContentCommandInput,
DetectToxicContentCommandOutput,
ComprehendClientResolvedConfig
> {
// Start section: command_properties
// End section: command_properties

public static getEndpointParameterInstructions(): EndpointParameterInstructions {
return {
UseFIPS: { type: "builtInParams", name: "useFipsEndpoint" },
Endpoint: { type: "builtInParams", name: "endpoint" },
Region: { type: "builtInParams", name: "region" },
UseDualStack: { type: "builtInParams", name: "useDualstackEndpoint" },
};
}

/**
* @public
*/
constructor(readonly input: DetectToxicContentCommandInput) {
// Start section: command_constructor
super();
// End section: command_constructor
}

/**
* @internal
*/
resolveMiddleware(
clientStack: MiddlewareStack<ServiceInputTypes, ServiceOutputTypes>,
configuration: ComprehendClientResolvedConfig,
options?: __HttpHandlerOptions
): Handler<DetectToxicContentCommandInput, DetectToxicContentCommandOutput> {
this.middlewareStack.use(getSerdePlugin(configuration, this.serialize, this.deserialize));
this.middlewareStack.use(
getEndpointPlugin(configuration, DetectToxicContentCommand.getEndpointParameterInstructions())
);

const stack = clientStack.concat(this.middlewareStack);

const { logger } = configuration;
const clientName = "ComprehendClient";
const commandName = "DetectToxicContentCommand";
const handlerExecutionContext: HandlerExecutionContext = {
logger,
clientName,
commandName,
inputFilterSensitiveLog: DetectToxicContentRequestFilterSensitiveLog,
outputFilterSensitiveLog: (_: any) => _,
[SMITHY_CONTEXT_KEY]: {
service: "Comprehend_20171127",
operation: "DetectToxicContent",
},
};
const { requestHandler } = configuration;
return stack.resolve(
(request: FinalizeHandlerArguments<any>) =>
requestHandler.handle(request.request as __HttpRequest, options || {}),
handlerExecutionContext
);
}

/**
* @internal
*/
private serialize(input: DetectToxicContentCommandInput, context: __SerdeContext): Promise<__HttpRequest> {
return se_DetectToxicContentCommand(input, context);
}

/**
* @internal
*/
private deserialize(output: __HttpResponse, context: __SerdeContext): Promise<DetectToxicContentCommandOutput> {
return de_DetectToxicContentCommand(output, context);
}

// Start section: command_body_extra
// End section: command_body_extra
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "@smithy/types";

import { ComprehendClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../ComprehendClient";
import { StopEntitiesDetectionJobRequest, StopEntitiesDetectionJobResponse } from "../models/models_0";
import { StopEntitiesDetectionJobRequest, StopEntitiesDetectionJobResponse } from "../models/models_1";
import { de_StopEntitiesDetectionJobCommand, se_StopEntitiesDetectionJobCommand } from "../protocols/Aws_json1_1";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "@smithy/types";

import { ComprehendClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../ComprehendClient";
import { StopEventsDetectionJobRequest, StopEventsDetectionJobResponse } from "../models/models_0";
import { StopEventsDetectionJobRequest, StopEventsDetectionJobResponse } from "../models/models_1";
import { de_StopEventsDetectionJobCommand, se_StopEventsDetectionJobCommand } from "../protocols/Aws_json1_1";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "@smithy/types";

import { ComprehendClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../ComprehendClient";
import { StopKeyPhrasesDetectionJobRequest, StopKeyPhrasesDetectionJobResponse } from "../models/models_0";
import { StopKeyPhrasesDetectionJobRequest, StopKeyPhrasesDetectionJobResponse } from "../models/models_1";
import { de_StopKeyPhrasesDetectionJobCommand, se_StopKeyPhrasesDetectionJobCommand } from "../protocols/Aws_json1_1";

/**
Expand Down
1 change: 1 addition & 0 deletions clients/client-comprehend/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export * from "./DetectPiiEntitiesCommand";
export * from "./DetectSentimentCommand";
export * from "./DetectSyntaxCommand";
export * from "./DetectTargetedSentimentCommand";
export * from "./DetectToxicContentCommand";
export * from "./ImportModelCommand";
export * from "./ListDatasetsCommand";
export * from "./ListDocumentClassificationJobsCommand";
Expand Down
Loading

0 comments on commit e05684d

Please sign in to comment.