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