@@ -27,7 +27,6 @@ import {
27
27
getGenerativeModel ,
28
28
Part ,
29
29
CountTokensRequest ,
30
- Schema ,
31
30
InlineDataPart ,
32
31
FileDataPart
33
32
} from '../src' ;
@@ -40,11 +39,9 @@ import {
40
39
} from './constants' ;
41
40
import { FIREBASE_CONFIG } from './firebase-config' ;
42
41
43
-
44
42
describe ( 'Count Tokens' , ( ) => {
45
43
testConfigs . forEach ( testConfig => {
46
44
describe ( `${ testConfig . toString ( ) } ` , ( ) => {
47
-
48
45
it ( 'text input' , async ( ) => {
49
46
const generationConfig : GenerationConfig = {
50
47
temperature : 0 ,
@@ -94,12 +91,16 @@ describe('Count Tokens', () => {
94
91
expect ( response . totalBillableCharacters ) . to . equal ( 16 ) ;
95
92
expect ( response . promptTokensDetails ) . to . not . be . null ;
96
93
expect ( response . promptTokensDetails ! . length ) . to . equal ( 1 ) ;
97
- expect ( response . promptTokensDetails ! [ 0 ] . modality ) . to . equal ( Modality . TEXT ) ;
94
+ expect ( response . promptTokensDetails ! [ 0 ] . modality ) . to . equal (
95
+ Modality . TEXT
96
+ ) ;
98
97
expect ( response . promptTokensDetails ! [ 0 ] . tokenCount ) . to . equal ( 6 ) ;
99
98
} ) ;
100
99
101
100
it ( 'image input' , async ( ) => {
102
- const model = getGenerativeModel ( testConfig . ai , { model : testConfig . model } ) ;
101
+ const model = getGenerativeModel ( testConfig . ai , {
102
+ model : testConfig . model
103
+ } ) ;
103
104
const imagePart : Part = {
104
105
inlineData : {
105
106
mimeType : IMAGE_MIME_TYPE ,
@@ -109,15 +110,32 @@ describe('Count Tokens', () => {
109
110
const response = await model . countTokens ( [ imagePart ] ) ;
110
111
111
112
const expectedImageTokens = 258 ;
112
- expect ( response . totalTokens , 'totalTokens should have correct token count' ) . to . equal ( expectedImageTokens ) ;
113
- expect ( response . totalBillableCharacters , 'totalBillableCharacters should be undefined' ) . to . be . undefined ; // Incorrect behavior
114
- expect ( response . promptTokensDetails ! . length , 'promptTokensDetails should have one entry' ) . to . equal ( 1 ) ;
115
- expect ( response . promptTokensDetails ! [ 0 ] . modality , 'modality should be IMAGE' ) . to . equal ( Modality . IMAGE ) ;
116
- expect ( response . promptTokensDetails ! [ 0 ] . tokenCount , 'promptTokenDetails tokenCount should be correct' ) . to . equal ( expectedImageTokens ) ;
113
+ expect (
114
+ response . totalTokens ,
115
+ 'totalTokens should have correct token count'
116
+ ) . to . equal ( expectedImageTokens ) ;
117
+ expect (
118
+ response . totalBillableCharacters ,
119
+ 'totalBillableCharacters should be undefined'
120
+ ) . to . be . undefined ; // Incorrect behavior
121
+ expect (
122
+ response . promptTokensDetails ! . length ,
123
+ 'promptTokensDetails should have one entry'
124
+ ) . to . equal ( 1 ) ;
125
+ expect (
126
+ response . promptTokensDetails ! [ 0 ] . modality ,
127
+ 'modality should be IMAGE'
128
+ ) . to . equal ( Modality . IMAGE ) ;
129
+ expect (
130
+ response . promptTokensDetails ! [ 0 ] . tokenCount ,
131
+ 'promptTokenDetails tokenCount should be correct'
132
+ ) . to . equal ( expectedImageTokens ) ;
117
133
} ) ;
118
134
119
135
it ( 'audio input' , async ( ) => {
120
- const model = getGenerativeModel ( testConfig . ai , { model : testConfig . model } ) ;
136
+ const model = getGenerativeModel ( testConfig . ai , {
137
+ model : testConfig . model
138
+ } ) ;
121
139
const audioPart : InlineDataPart = {
122
140
inlineData : {
123
141
mimeType : AUDIO_MIME_TYPE ,
@@ -127,40 +145,82 @@ describe('Count Tokens', () => {
127
145
128
146
const response = await model . countTokens ( [ audioPart ] ) ;
129
147
// This may be different on Google AI
130
- expect ( response . totalTokens , 'totalTokens is expected to be undefined' ) . to . be . undefined ;
131
- expect ( response . totalBillableCharacters , 'totalBillableCharacters should be undefined' ) . to . be . undefined ; // Incorrect behavior
132
- expect ( response . promptTokensDetails ! . length , 'promptTokensDetails should have one entry' ) . to . equal ( 1 ) ;
133
- expect ( response . promptTokensDetails ! [ 0 ] . modality , 'modality should be AUDIO' ) . to . equal ( Modality . AUDIO ) ;
134
- expect ( response . promptTokensDetails ! [ 0 ] . tokenCount , 'promptTokenDetails tokenCount is expected to be undefined' ) . to . be . undefined ;
148
+ expect ( response . totalTokens , 'totalTokens is expected to be undefined' )
149
+ . to . be . undefined ;
150
+ expect (
151
+ response . totalBillableCharacters ,
152
+ 'totalBillableCharacters should be undefined'
153
+ ) . to . be . undefined ; // Incorrect behavior
154
+ expect (
155
+ response . promptTokensDetails ! . length ,
156
+ 'promptTokensDetails should have one entry'
157
+ ) . to . equal ( 1 ) ;
158
+ expect (
159
+ response . promptTokensDetails ! [ 0 ] . modality ,
160
+ 'modality should be AUDIO'
161
+ ) . to . equal ( Modality . AUDIO ) ;
162
+ expect (
163
+ response . promptTokensDetails ! [ 0 ] . tokenCount ,
164
+ 'promptTokenDetails tokenCount is expected to be undefined'
165
+ ) . to . be . undefined ;
135
166
} ) ;
136
167
137
168
it ( 'text, image, and audio input' , async ( ) => {
138
- const model = getGenerativeModel ( testConfig . ai , { model : testConfig . model } ) ;
169
+ const model = getGenerativeModel ( testConfig . ai , {
170
+ model : testConfig . model
171
+ } ) ;
139
172
const textPart : Part = { text : 'Describe these:' } ;
140
- const imagePart : Part = { inlineData : { mimeType : IMAGE_MIME_TYPE , data : TINY_IMG_BASE64 } } ;
141
- const audioPart : Part = { inlineData : { mimeType : AUDIO_MIME_TYPE , data : TINY_MP3_BASE64 } } ;
173
+ const imagePart : Part = {
174
+ inlineData : { mimeType : IMAGE_MIME_TYPE , data : TINY_IMG_BASE64 }
175
+ } ;
176
+ const audioPart : Part = {
177
+ inlineData : { mimeType : AUDIO_MIME_TYPE , data : TINY_MP3_BASE64 }
178
+ } ;
142
179
143
180
const request : CountTokensRequest = {
144
181
contents : [ { role : 'user' , parts : [ textPart , imagePart , audioPart ] } ]
145
182
} ;
146
183
const response = await model . countTokens ( request ) ;
147
184
148
- expect ( response . totalTokens , 'totalTokens should have correct token count' ) . to . equal ( 261 ) ;
149
- expect ( response . totalBillableCharacters , 'totalBillableCharacters should have correct count' ) . to . equal ( 'Describe these:' . length - 1 ) ; // For some reason it's the length-1
150
-
151
- expect ( response . promptTokensDetails ! . length , 'promptTokensDetails should have three entries' ) . to . equal ( 3 ) ;
152
-
153
- const textDetails = response . promptTokensDetails ! . find ( d => d . modality === Modality . TEXT ) ;
154
- const visionDetails = response . promptTokensDetails ! . find ( d => d . modality === Modality . IMAGE ) ;
155
- const audioDetails = response . promptTokensDetails ! . find ( d => d . modality === Modality . AUDIO ) ;
156
-
157
- expect ( textDetails ) . to . deep . equal ( { modality : Modality . TEXT , tokenCount : 3 } ) ;
158
- expect ( visionDetails ) . to . deep . equal ( { modality : Modality . IMAGE , tokenCount : 258 } ) ;
185
+ expect (
186
+ response . totalTokens ,
187
+ 'totalTokens should have correct token count'
188
+ ) . to . equal ( 261 ) ;
189
+ expect (
190
+ response . totalBillableCharacters ,
191
+ 'totalBillableCharacters should have correct count'
192
+ ) . to . equal ( 'Describe these:' . length - 1 ) ; // For some reason it's the length-1
193
+
194
+ expect (
195
+ response . promptTokensDetails ! . length ,
196
+ 'promptTokensDetails should have three entries'
197
+ ) . to . equal ( 3 ) ;
198
+
199
+ const textDetails = response . promptTokensDetails ! . find (
200
+ d => d . modality === Modality . TEXT
201
+ ) ;
202
+ const visionDetails = response . promptTokensDetails ! . find (
203
+ d => d . modality === Modality . IMAGE
204
+ ) ;
205
+ const audioDetails = response . promptTokensDetails ! . find (
206
+ d => d . modality === Modality . AUDIO
207
+ ) ;
208
+
209
+ expect ( textDetails ) . to . deep . equal ( {
210
+ modality : Modality . TEXT ,
211
+ tokenCount : 3
212
+ } ) ;
213
+ expect ( visionDetails ) . to . deep . equal ( {
214
+ modality : Modality . IMAGE ,
215
+ tokenCount : 258
216
+ } ) ;
159
217
expect ( audioDetails ) . to . deep . equal ( { modality : Modality . AUDIO } ) ; // Incorrect behavior because there's no tokenCount
160
218
} ) ;
161
219
162
220
it ( 'public storage reference' , async ( ) => {
163
- const model = getGenerativeModel ( testConfig . ai , { model : testConfig . model } ) ;
221
+ const model = getGenerativeModel ( testConfig . ai , {
222
+ model : testConfig . model
223
+ } ) ;
164
224
const filePart : FileDataPart = {
165
225
fileData : {
166
226
mimeType : IMAGE_MIME_TYPE ,
@@ -170,13 +230,23 @@ describe('Count Tokens', () => {
170
230
const response = await model . countTokens ( [ filePart ] ) ;
171
231
172
232
const expectedFileTokens = 258 ;
173
- expect ( response . totalTokens , 'totalTokens should have correct token count' ) . to . equal ( expectedFileTokens ) ;
174
- expect ( response . totalBillableCharacters , 'totalBillableCharacters should be undefined' ) . to . be . undefined ;
233
+ expect (
234
+ response . totalTokens ,
235
+ 'totalTokens should have correct token count'
236
+ ) . to . equal ( expectedFileTokens ) ;
237
+ expect (
238
+ response . totalBillableCharacters ,
239
+ 'totalBillableCharacters should be undefined'
240
+ ) . to . be . undefined ;
175
241
expect ( response . promptTokensDetails ) . to . not . be . null ;
176
242
expect ( response . promptTokensDetails ! . length ) . to . equal ( 1 ) ;
177
- expect ( response . promptTokensDetails ! [ 0 ] . modality ) . to . equal ( Modality . IMAGE ) ;
178
- expect ( response . promptTokensDetails ! [ 0 ] . tokenCount ) . to . equal ( expectedFileTokens ) ;
243
+ expect ( response . promptTokensDetails ! [ 0 ] . modality ) . to . equal (
244
+ Modality . IMAGE
245
+ ) ;
246
+ expect ( response . promptTokensDetails ! [ 0 ] . tokenCount ) . to . equal (
247
+ expectedFileTokens
248
+ ) ;
179
249
} ) ;
180
250
} ) ;
181
- } )
251
+ } ) ;
182
252
} ) ;
0 commit comments