Skip to content

Commit 7c752f0

Browse files
committed
feat(vertex): support token counting
1 parent 69d34f2 commit 7c752f0

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

packages/vertex-sdk/src/client.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class AnthropicVertex extends Core.APIClient {
8383
this._authClientPromise = this._auth.getClient();
8484
}
8585

86-
messages: Resources.Messages = new Resources.Messages(this);
86+
messages: MessagesResource = makeMessagesResource(this);
8787
beta: BetaResource = makeBetaResource(this);
8888

8989
protected override defaultQuery(): Core.DefaultQuery | undefined {
@@ -147,10 +147,41 @@ export class AnthropicVertex extends Core.APIClient {
147147
options.path = `/projects/${this.projectId}/locations/${this.region}/publishers/anthropic/models/${model}:${specifier}`;
148148
}
149149

150+
if (
151+
options.path === '/v1/messages/count_tokens' ||
152+
(options.path == '/v1/messages/count_tokens?beta=true' && options.method === 'post')
153+
) {
154+
if (!this.projectId) {
155+
throw new Error(
156+
'No projectId was given and it could not be resolved from credentials. The client should be instantiated with the `projectId` option or the `ANTHROPIC_VERTEX_PROJECT_ID` environment variable should be set.',
157+
);
158+
}
159+
160+
if (Core.isObj(options.body)) {
161+
options.body['anthropic_version'] = undefined;
162+
}
163+
164+
options.path = `/projects/${this.projectId}/locations/${this.region}/publishers/anthropic/models/count-tokens:rawPredict`;
165+
}
166+
150167
return super.buildRequest(options);
151168
}
152169
}
153170

171+
/**
172+
* The Vertex SDK does not currently support the Batch API.
173+
*/
174+
type MessagesResource = Omit<Resources.Messages, 'batches'>;
175+
176+
function makeMessagesResource(client: AnthropicVertex): MessagesResource {
177+
const resource = new Resources.Messages(client);
178+
179+
// @ts-expect-error we're deleting non-optional properties
180+
delete resource.batches;
181+
182+
return resource;
183+
}
184+
154185
/**
155186
* The Vertex API does not currently support prompt caching, token counting or the Batch API.
156187
*/

0 commit comments

Comments
 (0)