1515 * limitations under the License.
1616 */
1717
18+ import { VertexAIError } from '../errors' ;
1819import { expect , use } from 'chai' ;
1920import sinonChai from 'sinon-chai' ;
2021import chaiAsPromised from 'chai-as-promised' ;
@@ -26,7 +27,7 @@ import {
2627 LanguageModelMessageContent
2728} from '../types/language-model' ;
2829import { match , stub } from 'sinon' ;
29- import { GenerateContentRequest } from '../types' ;
30+ import { GenerateContentRequest , VertexAIErrorCode } from '../types' ;
3031
3132use ( sinonChai ) ;
3233use ( chaiAsPromised ) ;
@@ -414,6 +415,54 @@ describe('ChromeAdapter', () => {
414415 totalTokens : expectedCount
415416 } ) ;
416417 } ) ;
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+ } ) ;
417466 } ) ;
418467 describe ( 'generateContentStream' , ( ) => {
419468 it ( 'generates content stream' , async ( ) => {
0 commit comments