@@ -85,7 +85,7 @@ def copy_with_new_metadata(reader: gguf.GGUFReader, writer: gguf.GGUFWriter, new
85
85
continue
86
86
87
87
# Skip old chat templates if we have new ones
88
- if field .name .startswith (gguf .Keys .Tokenizer .CHAT_TEMPLATE ) and gguf .Keys .Tokenizer .CHAT_TEMPLATE in new_metadata :
88
+ if ( field .name .startswith (gguf .Keys .Tokenizer .CHAT_TEMPLATE ) and gguf .Keys .Tokenizer .CHAT_TEMPLATE in new_metadata ) or ( field . name . startswith ( gguf . Keys . Tokenizer . INVERSE_TEMPLATE ) and gguf . Keys . Tokenizer . INVERSE_TEMPLATE in new_metadata ) :
89
89
logger .debug (f'Skipping { field .name } ' )
90
90
continue
91
91
@@ -110,6 +110,11 @@ def copy_with_new_metadata(reader: gguf.GGUFReader, writer: gguf.GGUFWriter, new
110
110
writer .add_chat_template (new_metadata [gguf .Keys .Tokenizer .CHAT_TEMPLATE ].value )
111
111
del new_metadata [gguf .Keys .Tokenizer .CHAT_TEMPLATE ]
112
112
113
+ if gguf .Keys .Tokenizer .INVERSE_TEMPLATE in new_metadata :
114
+ logger .debug ('Adding inverse template(s)' )
115
+ writer .add_inverse_template (new_metadata [gguf .Keys .Tokenizer .INVERSE_TEMPLATE ].value )
116
+ del new_metadata [gguf .Keys .Tokenizer .INVERSE_TEMPLATE ]
117
+
113
118
for key , val in new_metadata .items ():
114
119
logger .debug (f'Adding { key } : "{ val .value } " { val .description } ' )
115
120
writer .add_key_value (key , val .value , val .type )
@@ -143,7 +148,8 @@ def main() -> None:
143
148
parser .add_argument ("--general-name" , type = str , help = "The models general.name" , metavar = '"name"' )
144
149
parser .add_argument ("--general-description" , type = str , help = "The models general.description" , metavar = '"Description ..."' )
145
150
parser .add_argument ("--chat-template" , type = str , help = "Chat template string (or JSON string containing templates)" , metavar = '"{% ... %} ..."' )
146
- parser .add_argument ("--chat-template-config" , type = Path , help = "Config file containing chat template(s)" , metavar = 'tokenizer_config.json' )
151
+ parser .add_argument ("--inverse-template" , type = str , help = "Inverse template string" , metavar = '"{% ... %} ..."' )
152
+ parser .add_argument ("--chat-template-config" , type = Path , help = "Config file containing chat and/or inverse template(s)" , metavar = 'tokenizer_config.json' )
147
153
parser .add_argument ("--pre-tokenizer" , type = str , help = "The models tokenizer.ggml.pre" , metavar = '"pre tokenizer"' )
148
154
parser .add_argument ("--remove-metadata" , action = "append" , type = str , help = "Remove metadata (by key name) from output model" , metavar = 'general.url' )
149
155
parser .add_argument ("--special-token" , action = "append" , type = str , help = "Special token by value" , nargs = 2 , metavar = (' | ' .join (token_names .keys ()), '"<token>"' ))
@@ -166,12 +172,18 @@ def main() -> None:
166
172
if args .chat_template :
167
173
new_metadata [gguf .Keys .Tokenizer .CHAT_TEMPLATE ] = MetadataDetails (gguf .GGUFValueType .STRING , json .loads (args .chat_template ) if args .chat_template .startswith ('[' ) else args .chat_template )
168
174
175
+ if args .inverse_template :
176
+ new_metadata [gguf .Keys .Tokenizer .INVERSE_TEMPLATE ] = MetadataDetails (gguf .GGUFValueType .STRING , args .inverse_template )
177
+
169
178
if args .chat_template_config :
170
179
with open (args .chat_template_config , 'r' ) as fp :
171
180
config = json .load (fp )
172
- template = config .get ('chat_template' )
173
- if template :
174
- new_metadata [gguf .Keys .Tokenizer .CHAT_TEMPLATE ] = MetadataDetails (gguf .GGUFValueType .STRING , template )
181
+ chat_template = config .get ('chat_template' )
182
+ inverse_template = config .get ('inverse_template' )
183
+ if chat_template :
184
+ new_metadata [gguf .Keys .Tokenizer .CHAT_TEMPLATE ] = MetadataDetails (gguf .GGUFValueType .STRING , chat_template )
185
+ if inverse_template :
186
+ new_metadata [gguf .Keys .Tokenizer .INVERSE_TEMPLATE ] = MetadataDetails (gguf .GGUFValueType .STRING , inverse_template )
175
187
176
188
if args .pre_tokenizer :
177
189
new_metadata [gguf .Keys .Tokenizer .PRE ] = MetadataDetails (gguf .GGUFValueType .STRING , args .pre_tokenizer )
0 commit comments