Skip to content

Commit e9e8d2d

Browse files
author
awstools
committed
feat(client-bedrock-runtime): Launch CountTokens API to allow token counting
1 parent 384a8db commit e9e8d2d

File tree

8 files changed

+741
-14
lines changed

8 files changed

+741
-14
lines changed

clients/client-bedrock-runtime/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ ConverseStream
226226

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

229+
</details>
230+
<details>
231+
<summary>
232+
CountTokens
233+
</summary>
234+
235+
[Command API Reference](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-runtime/command/CountTokensCommand/) / [Input](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-bedrock-runtime/Interface/CountTokensCommandInput/) / [Output](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-bedrock-runtime/Interface/CountTokensCommandOutput/)
236+
229237
</details>
230238
<details>
231239
<summary>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
ConverseStreamCommandInput,
1515
ConverseStreamCommandOutput,
1616
} from "./commands/ConverseStreamCommand";
17+
import { CountTokensCommand, CountTokensCommandInput, CountTokensCommandOutput } from "./commands/CountTokensCommand";
1718
import {
1819
GetAsyncInvokeCommand,
1920
GetAsyncInvokeCommandInput,
@@ -45,6 +46,7 @@ const commands = {
4546
ApplyGuardrailCommand,
4647
ConverseCommand,
4748
ConverseStreamCommand,
49+
CountTokensCommand,
4850
GetAsyncInvokeCommand,
4951
InvokeModelCommand,
5052
InvokeModelWithBidirectionalStreamCommand,
@@ -93,6 +95,17 @@ export interface BedrockRuntime {
9395
cb: (err: any, data?: ConverseStreamCommandOutput) => void
9496
): void;
9597

98+
/**
99+
* @see {@link CountTokensCommand}
100+
*/
101+
countTokens(args: CountTokensCommandInput, options?: __HttpHandlerOptions): Promise<CountTokensCommandOutput>;
102+
countTokens(args: CountTokensCommandInput, cb: (err: any, data?: CountTokensCommandOutput) => void): void;
103+
countTokens(
104+
args: CountTokensCommandInput,
105+
options: __HttpHandlerOptions,
106+
cb: (err: any, data?: CountTokensCommandOutput) => void
107+
): void;
108+
96109
/**
97110
* @see {@link GetAsyncInvokeCommand}
98111
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import {
6969
import { ApplyGuardrailCommandInput, ApplyGuardrailCommandOutput } from "./commands/ApplyGuardrailCommand";
7070
import { ConverseCommandInput, ConverseCommandOutput } from "./commands/ConverseCommand";
7171
import { ConverseStreamCommandInput, ConverseStreamCommandOutput } from "./commands/ConverseStreamCommand";
72+
import { CountTokensCommandInput, CountTokensCommandOutput } from "./commands/CountTokensCommand";
7273
import { GetAsyncInvokeCommandInput, GetAsyncInvokeCommandOutput } from "./commands/GetAsyncInvokeCommand";
7374
import { InvokeModelCommandInput, InvokeModelCommandOutput } from "./commands/InvokeModelCommand";
7475
import {
@@ -99,6 +100,7 @@ export type ServiceInputTypes =
99100
| ApplyGuardrailCommandInput
100101
| ConverseCommandInput
101102
| ConverseStreamCommandInput
103+
| CountTokensCommandInput
102104
| GetAsyncInvokeCommandInput
103105
| InvokeModelCommandInput
104106
| InvokeModelWithBidirectionalStreamCommandInput
@@ -113,6 +115,7 @@ export type ServiceOutputTypes =
113115
| ApplyGuardrailCommandOutput
114116
| ConverseCommandOutput
115117
| ConverseStreamCommandOutput
118+
| CountTokensCommandOutput
116119
| GetAsyncInvokeCommandOutput
117120
| InvokeModelCommandOutput
118121
| InvokeModelWithBidirectionalStreamCommandOutput

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

Lines changed: 302 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export * from "./ApplyGuardrailCommand";
33
export * from "./ConverseCommand";
44
export * from "./ConverseStreamCommand";
5+
export * from "./CountTokensCommand";
56
export * from "./GetAsyncInvokeCommand";
67
export * from "./InvokeModelCommand";
78
export * from "./InvokeModelWithBidirectionalStreamCommand";

clients/client-bedrock-runtime/src/models/models_0.ts

Lines changed: 155 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ export type GuardrailAutomatedReasoningFinding =
10681068
*/
10691069
export namespace GuardrailAutomatedReasoningFinding {
10701070
/**
1071-
* <p>Indicates that the claims are definitively true and logically implied by the premises, with no possible alternative interpretations.</p>
1071+
* <p>Contains the result when the automated reasoning evaluation determines that the claims in the input are logically valid and definitively true based on the provided premises and policy rules.</p>
10721072
* @public
10731073
*/
10741074
export interface ValidMember {
@@ -1083,7 +1083,7 @@ export namespace GuardrailAutomatedReasoningFinding {
10831083
}
10841084

10851085
/**
1086-
* <p>Indicates that the claims are logically false and contradictory to the established rules or premises.</p>
1086+
* <p>Contains the result when the automated reasoning evaluation determines that the claims in the input are logically invalid and contradict the established premises or policy rules.</p>
10871087
* @public
10881088
*/
10891089
export interface InvalidMember {
@@ -1098,7 +1098,7 @@ export namespace GuardrailAutomatedReasoningFinding {
10981098
}
10991099

11001100
/**
1101-
* <p>Indicates that the claims could be either true or false depending on additional assumptions not provided in the input.</p>
1101+
* <p>Contains the result when the automated reasoning evaluation determines that the claims in the input could be either true or false depending on additional assumptions not provided in the input context.</p>
11021102
* @public
11031103
*/
11041104
export interface SatisfiableMember {
@@ -1113,7 +1113,7 @@ export namespace GuardrailAutomatedReasoningFinding {
11131113
}
11141114

11151115
/**
1116-
* <p>Indicates that no valid claims can be made due to logical contradictions in the premises or rules.</p>
1116+
* <p>Contains the result when the automated reasoning evaluation determines that no valid logical conclusions can be drawn due to contradictions in the premises or policy rules themselves.</p>
11171117
* @public
11181118
*/
11191119
export interface ImpossibleMember {
@@ -1128,7 +1128,7 @@ export namespace GuardrailAutomatedReasoningFinding {
11281128
}
11291129

11301130
/**
1131-
* <p>Indicates that the input has multiple valid logical interpretations, requiring additional context or clarification.</p>
1131+
* <p>Contains the result when the automated reasoning evaluation detects that the input has multiple valid logical interpretations, requiring additional context or clarification to proceed with validation.</p>
11321132
* @public
11331133
*/
11341134
export interface TranslationAmbiguousMember {
@@ -1143,7 +1143,7 @@ export namespace GuardrailAutomatedReasoningFinding {
11431143
}
11441144

11451145
/**
1146-
* <p>Indicates that the input exceeds the processing capacity due to the volume or complexity of the logical information.</p>
1146+
* <p>Contains the result when the automated reasoning evaluation cannot process the input due to its complexity or volume exceeding the system's processing capacity for logical analysis.</p>
11471147
* @public
11481148
*/
11491149
export interface TooComplexMember {
@@ -1158,7 +1158,7 @@ export namespace GuardrailAutomatedReasoningFinding {
11581158
}
11591159

11601160
/**
1161-
* <p>Indicates that no relevant logical information could be extracted from the input for validation.</p>
1161+
* <p>Contains the result when the automated reasoning evaluation cannot extract any relevant logical information from the input that can be validated against the policy rules.</p>
11621162
* @public
11631163
*/
11641164
export interface NoTranslationsMember {
@@ -5568,6 +5568,119 @@ export interface InvokeModelWithResponseStreamResponse {
55685568
performanceConfigLatency?: PerformanceConfigLatency | undefined;
55695569
}
55705570

5571+
/**
5572+
* <p>The inputs from a <code>Converse</code> API request for token counting.</p> <p>This structure mirrors the input format for the <code>Converse</code> operation, allowing you to count tokens for conversation-based inference requests.</p>
5573+
* @public
5574+
*/
5575+
export interface ConverseTokensRequest {
5576+
/**
5577+
* <p>An array of messages to count tokens for.</p>
5578+
* @public
5579+
*/
5580+
messages?: Message[] | undefined;
5581+
5582+
/**
5583+
* <p>The system content blocks to count tokens for. System content provides instructions or context to the model about how it should behave or respond. The token count will include any system content provided.</p>
5584+
* @public
5585+
*/
5586+
system?: SystemContentBlock[] | undefined;
5587+
}
5588+
5589+
/**
5590+
* <p>The body of an <code>InvokeModel</code> API request for token counting. This structure mirrors the input format for the <code>InvokeModel</code> operation, allowing you to count tokens for raw text inference requests.</p>
5591+
* @public
5592+
*/
5593+
export interface InvokeModelTokensRequest {
5594+
/**
5595+
* <p>The request body to count tokens for, formatted according to the model's expected input format. To learn about the input format for different models, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html">Model inference parameters and responses</a>.</p>
5596+
* @public
5597+
*/
5598+
body: Uint8Array | undefined;
5599+
}
5600+
5601+
/**
5602+
* <p>The input value for token counting. The value should be either an <code>InvokeModel</code> or <code>Converse</code> request body. </p>
5603+
* @public
5604+
*/
5605+
export type CountTokensInput =
5606+
| CountTokensInput.ConverseMember
5607+
| CountTokensInput.InvokeModelMember
5608+
| CountTokensInput.$UnknownMember;
5609+
5610+
/**
5611+
* @public
5612+
*/
5613+
export namespace CountTokensInput {
5614+
/**
5615+
* <p>An <code>InvokeModel</code> request for which to count tokens. Use this field when you want to count tokens for a raw text input that would be sent to the <code>InvokeModel</code> operation.</p>
5616+
* @public
5617+
*/
5618+
export interface InvokeModelMember {
5619+
invokeModel: InvokeModelTokensRequest;
5620+
converse?: never;
5621+
$unknown?: never;
5622+
}
5623+
5624+
/**
5625+
* <p>A <code>Converse</code> request for which to count tokens. Use this field when you want to count tokens for a conversation-based input that would be sent to the <code>Converse</code> operation.</p>
5626+
* @public
5627+
*/
5628+
export interface ConverseMember {
5629+
invokeModel?: never;
5630+
converse: ConverseTokensRequest;
5631+
$unknown?: never;
5632+
}
5633+
5634+
/**
5635+
* @public
5636+
*/
5637+
export interface $UnknownMember {
5638+
invokeModel?: never;
5639+
converse?: never;
5640+
$unknown: [string, any];
5641+
}
5642+
5643+
export interface Visitor<T> {
5644+
invokeModel: (value: InvokeModelTokensRequest) => T;
5645+
converse: (value: ConverseTokensRequest) => T;
5646+
_: (name: string, value: any) => T;
5647+
}
5648+
5649+
export const visit = <T>(value: CountTokensInput, visitor: Visitor<T>): T => {
5650+
if (value.invokeModel !== undefined) return visitor.invokeModel(value.invokeModel);
5651+
if (value.converse !== undefined) return visitor.converse(value.converse);
5652+
return visitor._(value.$unknown[0], value.$unknown[1]);
5653+
};
5654+
}
5655+
5656+
/**
5657+
* @public
5658+
*/
5659+
export interface CountTokensRequest {
5660+
/**
5661+
* <p>The unique identifier or ARN of the foundation model to use for token counting. Each model processes tokens differently, so the token count is specific to the model you specify.</p>
5662+
* @public
5663+
*/
5664+
modelId: string | undefined;
5665+
5666+
/**
5667+
* <p>The input for which to count tokens. The structure of this parameter depends on whether you're counting tokens for an <code>InvokeModel</code> or <code>Converse</code> request:</p> <ul> <li> <p>For <code>InvokeModel</code> requests, provide the request body in the <code>invokeModel</code> field</p> </li> <li> <p>For <code>Converse</code> requests, provide the messages and system content in the <code>converse</code> field</p> </li> </ul> <p>The input format must be compatible with the model specified in the <code>modelId</code> parameter.</p>
5668+
* @public
5669+
*/
5670+
input: CountTokensInput | undefined;
5671+
}
5672+
5673+
/**
5674+
* @public
5675+
*/
5676+
export interface CountTokensResponse {
5677+
/**
5678+
* <p>The number of tokens in the provided input according to the specified model's tokenization rules. This count represents the number of input tokens that would be processed if the same input were sent to the model in an inference request. Use this value to estimate costs and ensure your inputs stay within model token limits.</p>
5679+
* @public
5680+
*/
5681+
inputTokens: number | undefined;
5682+
}
5683+
55715684
/**
55725685
* @internal
55735686
*/
@@ -6184,3 +6297,38 @@ export const InvokeModelWithResponseStreamResponseFilterSensitiveLog = (
61846297
...obj,
61856298
...(obj.body && { body: "STREAMING_CONTENT" }),
61866299
});
6300+
6301+
/**
6302+
* @internal
6303+
*/
6304+
export const ConverseTokensRequestFilterSensitiveLog = (obj: ConverseTokensRequest): any => ({
6305+
...obj,
6306+
...(obj.messages && { messages: obj.messages.map((item) => MessageFilterSensitiveLog(item)) }),
6307+
...(obj.system && { system: obj.system.map((item) => SystemContentBlockFilterSensitiveLog(item)) }),
6308+
});
6309+
6310+
/**
6311+
* @internal
6312+
*/
6313+
export const InvokeModelTokensRequestFilterSensitiveLog = (obj: InvokeModelTokensRequest): any => ({
6314+
...obj,
6315+
...(obj.body && { body: SENSITIVE_STRING }),
6316+
});
6317+
6318+
/**
6319+
* @internal
6320+
*/
6321+
export const CountTokensInputFilterSensitiveLog = (obj: CountTokensInput): any => {
6322+
if (obj.invokeModel !== undefined)
6323+
return { invokeModel: InvokeModelTokensRequestFilterSensitiveLog(obj.invokeModel) };
6324+
if (obj.converse !== undefined) return { converse: ConverseTokensRequestFilterSensitiveLog(obj.converse) };
6325+
if (obj.$unknown !== undefined) return { [obj.$unknown[0]]: "UNKNOWN" };
6326+
};
6327+
6328+
/**
6329+
* @internal
6330+
*/
6331+
export const CountTokensRequestFilterSensitiveLog = (obj: CountTokensRequest): any => ({
6332+
...obj,
6333+
...(obj.input && { input: CountTokensInputFilterSensitiveLog(obj.input) }),
6334+
});

0 commit comments

Comments
 (0)