Skip to content

Commit

Permalink
feat(client-bedrock-runtime): This release adds Converse and Converse…
Browse files Browse the repository at this point in the history
…Stream APIs to Bedrock Runtime
  • Loading branch information
awstools committed May 30, 2024
1 parent 8cdaf0a commit 512d901
Show file tree
Hide file tree
Showing 9 changed files with 4,190 additions and 320 deletions.
30 changes: 23 additions & 7 deletions clients/client-bedrock-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ using your favorite package manager:

The AWS SDK is modulized by clients and commands.
To send a request, you only need to import the `BedrockRuntimeClient` and
the commands you need, for example `InvokeModelCommand`:
the commands you need, for example `ConverseCommand`:

```js
// ES5 example
const { BedrockRuntimeClient, InvokeModelCommand } = require("@aws-sdk/client-bedrock-runtime");
const { BedrockRuntimeClient, ConverseCommand } = require("@aws-sdk/client-bedrock-runtime");
```

```ts
// ES6+ example
import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";
import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
```

### Usage
Expand All @@ -51,7 +51,7 @@ const client = new BedrockRuntimeClient({ region: "REGION" });
const params = {
/** input parameters */
};
const command = new InvokeModelCommand(params);
const command = new ConverseCommand(params);
```

#### Async/await
Expand Down Expand Up @@ -130,15 +130,15 @@ const client = new AWS.BedrockRuntime({ region: "REGION" });

// async/await.
try {
const data = await client.invokeModel(params);
const data = await client.converse(params);
// process data.
} catch (error) {
// error handling.
}

// Promises.
client
.invokeModel(params)
.converse(params)
.then((data) => {
// process data.
})
Expand All @@ -147,7 +147,7 @@ client
});

// callbacks.
client.invokeModel(params, (err, data) => {
client.converse(params, (err, data) => {
// process err and data.
});
```
Expand Down Expand Up @@ -203,6 +203,22 @@ see LICENSE for more information.

## Client Commands (Operations List)

<details>
<summary>
Converse
</summary>

[Command API Reference](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-runtime/command/ConverseCommand/) / [Input](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-bedrock-runtime/Interface/ConverseCommandInput/) / [Output](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-bedrock-runtime/Interface/ConverseCommandOutput/)

</details>
<details>
<summary>
ConverseStream
</summary>

[Command API Reference](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-runtime/command/ConverseStreamCommand/) / [Input](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-bedrock-runtime/Interface/ConverseStreamCommandInput/) / [Output](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-bedrock-runtime/Interface/ConverseStreamCommandOutput/)

</details>
<details>
<summary>
InvokeModel
Expand Down
33 changes: 33 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,12 @@ import { createAggregatedClient } from "@smithy/smithy-client";
import { HttpHandlerOptions as __HttpHandlerOptions } from "@smithy/types";

import { BedrockRuntimeClient, BedrockRuntimeClientConfig } from "./BedrockRuntimeClient";
import { ConverseCommand, ConverseCommandInput, ConverseCommandOutput } from "./commands/ConverseCommand";
import {
ConverseStreamCommand,
ConverseStreamCommandInput,
ConverseStreamCommandOutput,
} from "./commands/ConverseStreamCommand";
import { InvokeModelCommand, InvokeModelCommandInput, InvokeModelCommandOutput } from "./commands/InvokeModelCommand";
import {
InvokeModelWithResponseStreamCommand,
Expand All @@ -11,11 +17,38 @@ import {
} from "./commands/InvokeModelWithResponseStreamCommand";

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

export interface BedrockRuntime {
/**
* @see {@link ConverseCommand}
*/
converse(args: ConverseCommandInput, options?: __HttpHandlerOptions): Promise<ConverseCommandOutput>;
converse(args: ConverseCommandInput, cb: (err: any, data?: ConverseCommandOutput) => void): void;
converse(
args: ConverseCommandInput,
options: __HttpHandlerOptions,
cb: (err: any, data?: ConverseCommandOutput) => void
): void;

/**
* @see {@link ConverseStreamCommand}
*/
converseStream(
args: ConverseStreamCommandInput,
options?: __HttpHandlerOptions
): Promise<ConverseStreamCommandOutput>;
converseStream(args: ConverseStreamCommandInput, cb: (err: any, data?: ConverseStreamCommandOutput) => void): void;
converseStream(
args: ConverseStreamCommandInput,
options: __HttpHandlerOptions,
cb: (err: any, data?: ConverseStreamCommandOutput) => void
): void;

/**
* @see {@link InvokeModelCommand}
*/
Expand Down
14 changes: 12 additions & 2 deletions clients/client-bedrock-runtime/src/BedrockRuntimeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import {
HttpAuthSchemeResolvedConfig,
resolveHttpAuthSchemeConfig,
} from "./auth/httpAuthSchemeProvider";
import { ConverseCommandInput, ConverseCommandOutput } from "./commands/ConverseCommand";
import { ConverseStreamCommandInput, ConverseStreamCommandOutput } from "./commands/ConverseStreamCommand";
import { InvokeModelCommandInput, InvokeModelCommandOutput } from "./commands/InvokeModelCommand";
import {
InvokeModelWithResponseStreamCommandInput,
Expand All @@ -78,12 +80,20 @@ export { __Client };
/**
* @public
*/
export type ServiceInputTypes = InvokeModelCommandInput | InvokeModelWithResponseStreamCommandInput;
export type ServiceInputTypes =
| ConverseCommandInput
| ConverseStreamCommandInput
| InvokeModelCommandInput
| InvokeModelWithResponseStreamCommandInput;

/**
* @public
*/
export type ServiceOutputTypes = InvokeModelCommandOutput | InvokeModelWithResponseStreamCommandOutput;
export type ServiceOutputTypes =
| ConverseCommandOutput
| ConverseStreamCommandOutput
| InvokeModelCommandOutput
| InvokeModelWithResponseStreamCommandOutput;

/**
* @public
Expand Down
230 changes: 230 additions & 0 deletions clients/client-bedrock-runtime/src/commands/ConverseCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
// 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 { ConverseRequest, ConverseResponse } from "../models/models_0";
import { de_ConverseCommand, se_ConverseCommand } from "../protocols/Aws_restJson1";

/**
* @public
*/
export { __MetadataBearer, $Command };
/**
* @public
*
* The input for {@link ConverseCommand}.
*/
export interface ConverseCommandInput extends ConverseRequest {}
/**
* @public
*
* The output of {@link ConverseCommand}.
*/
export interface ConverseCommandOutput extends ConverseResponse, __MetadataBearer {}

/**
* <p>Sends messages to the specified Amazon Bedrock model. <code>Converse</code> provides
* a consistent interface that works with all models that
* support messages. This allows you to write code once and use it with different models.
* Should a model have unique inference parameters, you can also pass those unique parameters
* to the model. For more information, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/api-methods-run.html">Run inference</a> in the Bedrock User Guide.</p>
* <p>This operation requires permission for the <code>bedrock:InvokeModel</code> action. </p>
* @example
* Use a bare-bones client and the command you need to make an API call.
* ```javascript
* import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime"; // ES Modules import
* // const { BedrockRuntimeClient, ConverseCommand } = require("@aws-sdk/client-bedrock-runtime"); // CommonJS import
* const client = new BedrockRuntimeClient(config);
* const input = { // ConverseRequest
* modelId: "STRING_VALUE", // required
* messages: [ // Messages // required
* { // Message
* role: "user" || "assistant", // required
* content: [ // ContentBlocks // required
* { // ContentBlock Union: only one key present
* text: "STRING_VALUE",
* image: { // ImageBlock
* format: "png" || "jpeg" || "gif" || "webp", // required
* source: { // ImageSource Union: only one key present
* bytes: new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("")
* },
* },
* toolUse: { // ToolUseBlock
* toolUseId: "STRING_VALUE", // required
* name: "STRING_VALUE", // required
* input: "DOCUMENT_VALUE", // required
* },
* toolResult: { // ToolResultBlock
* toolUseId: "STRING_VALUE", // required
* content: [ // ToolResultContentBlocks // required
* { // ToolResultContentBlock Union: only one key present
* json: "DOCUMENT_VALUE",
* text: "STRING_VALUE",
* image: {
* format: "png" || "jpeg" || "gif" || "webp", // required
* source: {// Union: only one key present
* bytes: new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("")
* },
* },
* },
* ],
* status: "success" || "error",
* },
* },
* ],
* },
* ],
* system: [ // SystemContentBlocks
* { // SystemContentBlock Union: only one key present
* text: "STRING_VALUE",
* },
* ],
* inferenceConfig: { // InferenceConfiguration
* maxTokens: Number("int"),
* temperature: Number("float"),
* topP: Number("float"),
* stopSequences: [ // NonEmptyStringList
* "STRING_VALUE",
* ],
* },
* toolConfig: { // ToolConfiguration
* tools: [ // Tools // required
* { // Tool Union: only one key present
* toolSpec: { // ToolSpecification
* name: "STRING_VALUE", // required
* description: "STRING_VALUE",
* inputSchema: { // ToolInputSchema Union: only one key present
* json: "DOCUMENT_VALUE",
* },
* },
* },
* ],
* toolChoice: { // ToolChoice Union: only one key present
* auto: {},
* any: {},
* tool: { // SpecificToolChoice
* name: "STRING_VALUE", // required
* },
* },
* },
* additionalModelRequestFields: "DOCUMENT_VALUE",
* additionalModelResponseFieldPaths: [ // AdditionalModelResponseFieldPaths
* "STRING_VALUE",
* ],
* };
* const command = new ConverseCommand(input);
* const response = await client.send(command);
* // { // ConverseResponse
* // output: { // ConverseOutput Union: only one key present
* // message: { // Message
* // role: "user" || "assistant", // required
* // content: [ // ContentBlocks // required
* // { // ContentBlock Union: only one key present
* // text: "STRING_VALUE",
* // image: { // ImageBlock
* // format: "png" || "jpeg" || "gif" || "webp", // required
* // source: { // ImageSource Union: only one key present
* // bytes: new Uint8Array(),
* // },
* // },
* // toolUse: { // ToolUseBlock
* // toolUseId: "STRING_VALUE", // required
* // name: "STRING_VALUE", // required
* // input: "DOCUMENT_VALUE", // required
* // },
* // toolResult: { // ToolResultBlock
* // toolUseId: "STRING_VALUE", // required
* // content: [ // ToolResultContentBlocks // required
* // { // ToolResultContentBlock Union: only one key present
* // json: "DOCUMENT_VALUE",
* // text: "STRING_VALUE",
* // image: {
* // format: "png" || "jpeg" || "gif" || "webp", // required
* // source: {// Union: only one key present
* // bytes: new Uint8Array(),
* // },
* // },
* // },
* // ],
* // status: "success" || "error",
* // },
* // },
* // ],
* // },
* // },
* // stopReason: "end_turn" || "tool_use" || "max_tokens" || "stop_sequence" || "content_filtered", // required
* // usage: { // TokenUsage
* // inputTokens: Number("int"), // required
* // outputTokens: Number("int"), // required
* // totalTokens: Number("int"), // required
* // },
* // metrics: { // ConverseMetrics
* // latencyMs: Number("long"), // required
* // },
* // additionalModelResponseFields: "DOCUMENT_VALUE",
* // };
*
* ```
*
* @param ConverseCommandInput - {@link ConverseCommandInput}
* @returns {@link ConverseCommandOutput}
* @see {@link ConverseCommandInput} for command's `input` shape.
* @see {@link ConverseCommandOutput} 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 ModelErrorException} (client fault)
* <p>The request failed due to an error while processing the model.</p>
*
* @throws {@link ModelNotReadyException} (client fault)
* <p>The model specified in the request is not ready to serve inference requests.</p>
*
* @throws {@link ModelTimeoutException} (client fault)
* <p>The request took too long to process. Processing time exceeded the model timeout length.</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 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 ConverseCommand extends $Command
.classBuilder<
ConverseCommandInput,
ConverseCommandOutput,
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", "Converse", {})
.n("BedrockRuntimeClient", "ConverseCommand")
.f(void 0, void 0)
.ser(se_ConverseCommand)
.de(de_ConverseCommand)
.build() {}
Loading

0 comments on commit 512d901

Please sign in to comment.