Skip to content

Commit 7d14933

Browse files
authored
disable count tokens api for on-device inference (#8962)
1 parent 43a69d5 commit 7d14933

File tree

2 files changed

+24
-49
lines changed

2 files changed

+24
-49
lines changed

packages/vertexai/src/methods/chrome-adapter.test.ts

+17-32
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { VertexAIError } from '../errors';
1819
import { expect, use } from 'chai';
1920
import sinonChai from 'sinon-chai';
2021
import chaiAsPromised from 'chai-as-promised';
@@ -26,7 +27,7 @@ import {
2627
LanguageModelMessageContent
2728
} from '../types/language-model';
2829
import { match, stub } from 'sinon';
29-
import { GenerateContentRequest } from '../types';
30+
import { GenerateContentRequest, VertexAIErrorCode } from '../types';
3031

3132
use(sinonChai);
3233
use(chaiAsPromised);
@@ -363,17 +364,8 @@ describe('ChromeAdapter', () => {
363364
});
364365
});
365366
describe('countTokens', () => {
366-
it('counts tokens from a singular input', async () => {
367+
it('counts tokens is not yet available', async () => {
367368
const inputText = 'first';
368-
const expectedCount = 10;
369-
const onDeviceParams = {
370-
systemPrompt: 'be yourself'
371-
} as LanguageModelCreateOptions;
372-
const expectedOnDeviceParams = {
373-
systemPrompt: 'be yourself',
374-
expectedInputs: [{ type: 'image' }]
375-
} as LanguageModelCreateOptions;
376-
377369
// setting up stubs
378370
const languageModelProvider = {
379371
create: () => Promise.resolve({})
@@ -385,34 +377,27 @@ describe('ChromeAdapter', () => {
385377
languageModel
386378
);
387379

388-
// overrides impl with stub method
389-
const measureInputUsageStub = stub(
390-
languageModel,
391-
'measureInputUsage'
392-
).resolves(expectedCount);
393-
394380
const adapter = new ChromeAdapter(
395381
languageModelProvider,
396-
'prefer_on_device',
397-
onDeviceParams
382+
'prefer_on_device'
398383
);
399384

400385
const countTokenRequest = {
401386
contents: [{ role: 'user', parts: [{ text: inputText }] }]
402387
} as GenerateContentRequest;
403-
const response = await adapter.countTokens(countTokenRequest);
404-
// Asserts initialization params are proxied.
405-
expect(createStub).to.have.been.calledOnceWith(expectedOnDeviceParams);
406-
// Asserts Vertex input type is mapped to Chrome type.
407-
expect(measureInputUsageStub).to.have.been.calledOnceWith([
408-
{
409-
type: 'text',
410-
content: inputText
411-
}
412-
]);
413-
expect(await response.json()).to.deep.equal({
414-
totalTokens: expectedCount
415-
});
388+
389+
try {
390+
await adapter.countTokens(countTokenRequest);
391+
} catch (e) {
392+
// the call to countToken should be rejected with Error
393+
expect((e as VertexAIError).code).to.equal(
394+
VertexAIErrorCode.REQUEST_ERROR
395+
);
396+
expect((e as VertexAIError).message).includes('not yet available');
397+
}
398+
399+
// Asserts that no language model was initialized
400+
expect(createStub).not.called;
416401
});
417402
});
418403
describe('generateContentStream', () => {

packages/vertexai/src/methods/chrome-adapter.ts

+7-17
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { VertexAIError } from '../errors';
1819
import {
1920
CountTokensRequest,
2021
GenerateContentRequest,
2122
InferenceMode,
22-
Part
23+
Part,
24+
VertexAIErrorCode
2325
} from '../types';
2426
import {
2527
Availability,
@@ -129,23 +131,11 @@ export class ChromeAdapter {
129131
return ChromeAdapter.toStreamResponse(stream);
130132
}
131133

132-
async countTokens(request: CountTokensRequest): Promise<Response> {
133-
// TODO: Check if the request contains an image, and if so, throw.
134-
const session = await this.createSession(
135-
// TODO: normalize on-device params during construction.
136-
this.onDeviceParams || {}
137-
);
138-
// TODO: support multiple content objects when Chrome supports
139-
// sequence<LanguageModelMessage>
140-
const contents = await Promise.all(
141-
request.contents[0].parts.map(ChromeAdapter.toLanguageModelMessageContent)
134+
async countTokens(_request: CountTokensRequest): Promise<Response> {
135+
throw new VertexAIError(
136+
VertexAIErrorCode.REQUEST_ERROR,
137+
'Count Tokens is not yet available for on-device model.'
142138
);
143-
const tokenCount = await session.measureInputUsage(contents);
144-
return {
145-
json: async () => ({
146-
totalTokens: tokenCount
147-
})
148-
} as Response;
149139
}
150140

151141
/**

0 commit comments

Comments
 (0)