@@ -145,8 +145,12 @@ class BPETokenizer extends Disposable implements ITokenizer {
145
145
case Raw . ChatCompletionContentPartKind . Opaque :
146
146
return text . tokenUsage || 0 ;
147
147
case Raw . ChatCompletionContentPartKind . Image :
148
- if ( text . imageUrl . url . startsWith ( 'data' ) ) {
149
- return calculateImageTokenCost ( text . imageUrl . url , text . imageUrl . detail ) ;
148
+ if ( text . imageUrl . url . startsWith ( 'data:image/' ) ) {
149
+ try {
150
+ return calculateImageTokenCost ( text . imageUrl . url , text . imageUrl . detail ) ;
151
+ } catch {
152
+ return this . _textTokenLength ( text . imageUrl . url ) ;
153
+ }
150
154
}
151
155
return this . _textTokenLength ( text . imageUrl . url ) ;
152
156
case Raw . ChatCompletionContentPartKind . CacheBreakpoint :
@@ -210,8 +214,12 @@ class BPETokenizer extends Disposable implements ITokenizer {
210
214
if ( casted . type === 'text' ) {
211
215
numTokens += await this . tokenLength ( casted . text ) ;
212
216
} else if ( casted . type === 'image_url' && casted . image_url ) {
213
- if ( casted . image_url . url . startsWith ( 'data' ) ) {
214
- numTokens += calculateImageTokenCost ( casted . image_url . url , casted . image_url . detail ) ;
217
+ if ( casted . image_url . url . startsWith ( 'data:image/' ) ) {
218
+ try {
219
+ numTokens += calculateImageTokenCost ( casted . image_url . url , casted . image_url . detail ) ;
220
+ } catch {
221
+ numTokens += await this . tokenLength ( casted . image_url . url ) ;
222
+ }
215
223
} else {
216
224
numTokens += await this . tokenLength ( casted . image_url . url ) ;
217
225
}
0 commit comments