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 ) ;
@@ -414,6 +415,54 @@ describe('ChromeAdapter', () => {
414
415
totalTokens : expectedCount
415
416
} ) ;
416
417
} ) ;
418
+ it ( 'count tokens for image based input should throw' , async ( ) => {
419
+ // setting up stubs
420
+ const languageModelProvider = {
421
+ create : ( ) => Promise . resolve ( { } )
422
+ } as LanguageModel ;
423
+ const languageModel = {
424
+ measureInputUsage : _i => Promise . resolve ( 123 )
425
+ } as LanguageModel ;
426
+ const createStub = stub ( languageModelProvider , 'create' ) . resolves (
427
+ languageModel
428
+ ) ;
429
+
430
+ const countTokenRequestWithImagePart = {
431
+ contents : [
432
+ {
433
+ role : 'user' ,
434
+ parts : [
435
+ { text : 'test' } ,
436
+ {
437
+ inlineData : {
438
+ data : sampleBase64EncodedImage ,
439
+ mimeType : 'image/jpeg'
440
+ }
441
+ }
442
+ ]
443
+ }
444
+ ]
445
+ } as GenerateContentRequest ;
446
+
447
+ const adapter = new ChromeAdapter (
448
+ languageModelProvider ,
449
+ 'only_on_device'
450
+ ) ;
451
+
452
+ try {
453
+ await adapter . countTokens ( countTokenRequestWithImagePart ) ;
454
+ } catch ( e ) {
455
+ // the call to countToken should be rejected with Error
456
+ expect ( ( e as VertexAIError ) . code ) . to . equal (
457
+ VertexAIErrorCode . INVALID_CONTENT
458
+ ) ;
459
+ expect ( ( e as VertexAIError ) . message ) . includes ( 'image input' ) ;
460
+ }
461
+
462
+ // Asserts that create stub was not called - error happens before this
463
+ // step is reached
464
+ expect ( createStub ) . not . called ;
465
+ } ) ;
417
466
} ) ;
418
467
describe ( 'generateContentStream' , ( ) => {
419
468
it ( 'generates content stream' , async ( ) => {
0 commit comments