@@ -88,17 +88,18 @@ describe('Count Tokens', () => {
88
88
89
89
const response = await model . countTokens ( 'Why is the sky blue?' ) ;
90
90
91
- expect ( response . promptTokensDetails ) . to . not . be . null ;
91
+ expect ( response . promptTokensDetails ) . to . exist ;
92
92
expect ( response . promptTokensDetails ! . length ) . to . equal ( 1 ) ;
93
93
expect ( response . promptTokensDetails ! [ 0 ] . modality ) . to . equal (
94
94
Modality . TEXT
95
95
) ;
96
-
97
96
if ( testConfig . ai . backend . backendType === BackendType . GOOGLE_AI ) {
98
97
expect ( response . totalTokens ) . to . equal ( 7 ) ;
99
98
expect ( response . totalBillableCharacters ) . to . be . undefined ;
100
99
expect ( response . promptTokensDetails ! [ 0 ] . tokenCount ) . to . equal ( 7 ) ;
101
- } else if ( testConfig . ai . backend . backendType === BackendType . VERTEX_AI ) {
100
+ } else if (
101
+ testConfig . ai . backend . backendType === BackendType . VERTEX_AI
102
+ ) {
102
103
expect ( response . totalTokens ) . to . equal ( 6 ) ;
103
104
expect ( response . totalBillableCharacters ) . to . equal ( 16 ) ;
104
105
expect ( response . promptTokensDetails ! [ 0 ] . tokenCount ) . to . equal ( 6 ) ;
@@ -119,7 +120,17 @@ describe('Count Tokens', () => {
119
120
120
121
if ( testConfig . ai . backend . backendType === BackendType . GOOGLE_AI ) {
121
122
const expectedImageTokens = 259 ;
122
-
123
+ expect ( response . totalTokens ) . to . equal ( expectedImageTokens ) ;
124
+ expect ( response . totalBillableCharacters ) . to . be . undefined ; // Incorrect behavior
125
+ expect ( response . promptTokensDetails ! . length ) . to . equal ( 2 ) ;
126
+ expect ( response . promptTokensDetails ! [ 0 ] ) . to . deep . equal ( {
127
+ modality : Modality . TEXT , // Note: 1 unexpected text token observed for Google AI with image-only input.
128
+ tokenCount : 1
129
+ } ) ;
130
+ expect ( response . promptTokensDetails ! [ 1 ] ) . to . deep . equal ( {
131
+ modality : Modality . IMAGE ,
132
+ tokenCount : 258
133
+ } ) ;
123
134
} else if ( testConfig . ai . backend . backendType === BackendType . VERTEX_AI ) {
124
135
const expectedImageTokens = 258 ;
125
136
expect ( response . totalTokens ) . to . equal ( expectedImageTokens ) ;
@@ -129,9 +140,8 @@ describe('Count Tokens', () => {
129
140
expect (
130
141
response . promptTokensDetails ! . length ,
131
142
) . to . equal ( 1 ) ;
132
- expect (
133
- response . promptTokensDetails ! [ 0 ] . modality ,
134
- ) . to . equal ( Modality . IMAGE ) ;
143
+ // Note: No text tokens are present for Vertex AI with image-only input.
144
+ expect ( response . promptTokensDetails ! [ 0 ] ) . to . deep . equal ( { modality : Modality . IMAGE , tokenCount : 258 } )
135
145
expect ( response . promptTokensDetails ! [ 0 ] . tokenCount ) . to . equal ( expectedImageTokens ) ;
136
146
}
137
147
} ) ;
@@ -149,6 +159,7 @@ describe('Count Tokens', () => {
149
159
150
160
const response = await model . countTokens ( [ audioPart ] ) ;
151
161
162
+ expect ( response . promptTokensDetails ) . to . exist ;
152
163
const textDetails = response . promptTokensDetails ! . find (
153
164
d => d . modality === Modality . TEXT
154
165
) ;
@@ -158,20 +169,24 @@ describe('Count Tokens', () => {
158
169
159
170
if ( testConfig . ai . backend . backendType === BackendType . GOOGLE_AI ) {
160
171
expect ( response . totalTokens ) . to . equal ( 6 ) ;
161
- expect (
162
- response . promptTokensDetails ! . length ,
163
- ) . to . equal ( 2 ) ;
164
- expect ( textDetails ) . to . deep . equal ( { modality : Modality . TEXT , tokenCount : 1 } )
165
- expect ( audioDetails ) . to . deep . equal ( { modality : Modality . AUDIO , tokenCount : 5 } )
166
- } else if ( testConfig . ai . backend . backendType === BackendType . VERTEX_AI ) {
172
+ expect ( response . promptTokensDetails ! . length ) . to . equal ( 2 ) ;
173
+ expect ( textDetails ) . to . deep . equal ( {
174
+ modality : Modality . TEXT ,
175
+ tokenCount : 1
176
+ } ) ;
177
+ expect ( audioDetails ) . to . deep . equal ( {
178
+ modality : Modality . AUDIO ,
179
+ tokenCount : 5
180
+ } ) ;
181
+ } else if (
182
+ testConfig . ai . backend . backendType === BackendType . VERTEX_AI
183
+ ) {
167
184
expect ( response . totalTokens ) . to . be . undefined ;
168
- expect ( response . promptTokensDetails ! . length ) . to . equal ( 1 ) ; // For some reason we don't get text
169
- expect ( audioDetails ) . to . deep . equal ( { modality : Modality . AUDIO } ) ; // For some reason there are no tokens
185
+ expect ( response . promptTokensDetails ! . length ) . to . equal ( 1 ) ; // Note: Text modality details absent for Vertex AI with audio-only input.
186
+ expect ( audioDetails ) . to . deep . equal ( { modality : Modality . AUDIO } ) ; // Note: Audio tokenCount is undefined for Vertex AI with audio-only input.
170
187
}
171
188
172
- expect (
173
- response . totalBillableCharacters ,
174
- ) . to . be . undefined ; // Incorrect behavior
189
+ expect ( response . totalBillableCharacters ) . to . be . undefined ; // Incorrect behavior
175
190
} ) ;
176
191
177
192
it ( 'text, image, and audio input' , async ( ) => {
@@ -193,12 +208,19 @@ describe('Count Tokens', () => {
193
208
const textDetails = response . promptTokensDetails ! . find (
194
209
d => d . modality === Modality . TEXT
195
210
) ;
196
- const visionDetails = response . promptTokensDetails ! . find (
211
+ const imageDetails = response . promptTokensDetails ! . find (
197
212
d => d . modality === Modality . IMAGE
198
213
) ;
199
214
const audioDetails = response . promptTokensDetails ! . find (
200
215
d => d . modality === Modality . AUDIO
201
216
) ;
217
+ expect ( response . promptTokensDetails ) . to . exist ;
218
+ expect ( response . promptTokensDetails ! . length ) . to . equal ( 3 ) ;
219
+
220
+ expect ( imageDetails ) . to . deep . equal ( {
221
+ modality : Modality . IMAGE ,
222
+ tokenCount : 258
223
+ } ) ;
202
224
203
225
if ( testConfig . ai . backend . backendType === BackendType . GOOGLE_AI ) {
204
226
expect ( response . totalTokens ) . to . equal ( 267 ) ;
@@ -207,25 +229,22 @@ describe('Count Tokens', () => {
207
229
modality : Modality . TEXT ,
208
230
tokenCount : 4
209
231
} ) ;
210
- expect ( audioDetails ) . to . deep . equal ( { modality : Modality . AUDIO , tokenCount : 5 } ) ; // Incorrect behavior because there's no tokenCount
211
- } else if ( testConfig . ai . backend . backendType === BackendType . VERTEX_AI ) {
232
+ expect ( audioDetails ) . to . deep . equal ( {
233
+ modality : Modality . AUDIO ,
234
+ tokenCount : 5
235
+ } ) ;
236
+ } else if (
237
+ testConfig . ai . backend . backendType === BackendType . VERTEX_AI
238
+ ) {
212
239
expect ( response . totalTokens ) . to . equal ( 261 ) ;
213
240
expect ( textDetails ) . to . deep . equal ( {
214
241
modality : Modality . TEXT ,
215
242
tokenCount : 3
216
243
} ) ;
217
- expect (
218
- response . totalBillableCharacters ,
219
- ) . to . equal ( 'Describe these:' . length - 1 ) ; // For some reason it's the length-1
244
+ const expectedText = 'Describe these:' ;
245
+ expect ( response . totalBillableCharacters ) . to . equal ( expectedText . length - 1 ) ; // Note: BillableCharacters observed as (text length - 1) for Vertex AI.
220
246
expect ( audioDetails ) . to . deep . equal ( { modality : Modality . AUDIO } ) ; // Incorrect behavior because there's no tokenCount
221
247
}
222
-
223
- expect ( response . promptTokensDetails ! . length ) . to . equal ( 3 ) ;
224
-
225
- expect ( visionDetails ) . to . deep . equal ( {
226
- modality : Modality . IMAGE ,
227
- tokenCount : 258
228
- } ) ;
229
248
} ) ;
230
249
231
250
it ( 'public storage reference' , async ( ) => {
@@ -248,7 +267,7 @@ describe('Count Tokens', () => {
248
267
const expectedFileTokens = 258 ;
249
268
expect ( response . totalTokens ) . to . equal ( expectedFileTokens ) ;
250
269
expect ( response . totalBillableCharacters ) . to . be . undefined ;
251
- expect ( response . promptTokensDetails ) . to . not . be . null ;
270
+ expect ( response . promptTokensDetails ) . to . exist ;
252
271
expect ( response . promptTokensDetails ! . length ) . to . equal ( 1 ) ;
253
272
expect ( response . promptTokensDetails ! [ 0 ] . modality ) . to . equal (
254
273
Modality . IMAGE
0 commit comments