15
15
* limitations under the License.
16
16
*/
17
17
18
+ import { VertexAIError } from '../errors' ;
18
19
import { expect , use } from 'chai' ;
19
20
import sinonChai from 'sinon-chai' ;
20
21
import chaiAsPromised from 'chai-as-promised' ;
@@ -26,7 +27,7 @@ import {
26
27
LanguageModelMessageContent
27
28
} from '../types/language-model' ;
28
29
import { match , stub } from 'sinon' ;
29
- import { GenerateContentRequest } from '../types' ;
30
+ import { GenerateContentRequest , VertexAIErrorCode } from '../types' ;
30
31
31
32
use ( sinonChai ) ;
32
33
use ( chaiAsPromised ) ;
@@ -363,17 +364,8 @@ describe('ChromeAdapter', () => {
363
364
} ) ;
364
365
} ) ;
365
366
describe ( 'countTokens' , ( ) => {
366
- it ( 'counts tokens from a singular input ' , async ( ) => {
367
+ it ( 'counts tokens is not yet available ' , async ( ) => {
367
368
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
-
377
369
// setting up stubs
378
370
const languageModelProvider = {
379
371
create : ( ) => Promise . resolve ( { } )
@@ -385,34 +377,27 @@ describe('ChromeAdapter', () => {
385
377
languageModel
386
378
) ;
387
379
388
- // overrides impl with stub method
389
- const measureInputUsageStub = stub (
390
- languageModel ,
391
- 'measureInputUsage'
392
- ) . resolves ( expectedCount ) ;
393
-
394
380
const adapter = new ChromeAdapter (
395
381
languageModelProvider ,
396
- 'prefer_on_device' ,
397
- onDeviceParams
382
+ 'prefer_on_device'
398
383
) ;
399
384
400
385
const countTokenRequest = {
401
386
contents : [ { role : 'user' , parts : [ { text : inputText } ] } ]
402
387
} 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 ;
416
401
} ) ;
417
402
} ) ;
418
403
describe ( 'generateContentStream' , ( ) => {
0 commit comments