@@ -12,7 +12,7 @@ import Jinja
12
12
public typealias Message = [ String : Any ]
13
13
public typealias ToolSpec = [ String : Any ]
14
14
15
- enum TokenizerError : Error {
15
+ public enum TokenizerError : Error {
16
16
case missingConfig
17
17
case missingTokenizerClassInConfig
18
18
case unsupportedTokenizer( String )
@@ -144,6 +144,8 @@ public protocol Tokenizer {
144
144
var unknownToken : String ? { get }
145
145
var unknownTokenId : Int ? { get }
146
146
147
+ var hasChatTemplate : Bool { get }
148
+
147
149
/// The appropriate chat template is selected from the tokenizer config
148
150
func applyChatTemplate( messages: [ Message ] ) throws -> [ Int ]
149
151
@@ -182,6 +184,8 @@ public protocol Tokenizer {
182
184
}
183
185
184
186
extension Tokenizer {
187
+ public var hasChatTemplate : Bool { false }
188
+
185
189
/// Call previous signature for backwards compatibility
186
190
func applyChatTemplate(
187
191
messages: [ Message ] ,
@@ -399,6 +403,10 @@ public class PreTrainedTokenizer: Tokenizer {
399
403
model. convertIdToToken ( id)
400
404
}
401
405
406
+ public var hasChatTemplate : Bool {
407
+ return tokenizerConfig. chatTemplate != nil
408
+ }
409
+
402
410
public func applyChatTemplate( messages: [ Message ] ) throws -> [ Int ] {
403
411
try applyChatTemplate ( messages: messages, addGenerationPrompt: true )
404
412
}
@@ -485,7 +493,7 @@ public class PreTrainedTokenizer: Tokenizer {
485
493
}
486
494
487
495
guard let selectedChatTemplate else {
488
- throw TokenizerError . chatTemplate ( " No chat template was specified " )
496
+ throw TokenizerError . chatTemplate ( " This tokenizer does not have a chat template, and no template was passed. " )
489
497
}
490
498
491
499
let template = try Template ( selectedChatTemplate)
0 commit comments