@@ -319,23 +319,30 @@ async def chat_completion_full_generator(
319
319
return response
320
320
321
321
def _load_chat_template (self , chat_template ):
322
+ tokenizer = self .tokenizer
323
+
322
324
if chat_template is not None :
323
325
try :
324
326
with open (chat_template , "r" ) as f :
325
- self .tokenizer .chat_template = f .read ()
326
- except OSError :
327
+ tokenizer .chat_template = f .read ()
328
+ except OSError as e :
329
+ JINJA_CHARS = "{}\n "
330
+ if not any (c in chat_template for c in JINJA_CHARS ):
331
+ msg = (f"The supplied chat template ({ chat_template } ) "
332
+ f"looks like a file path, but it failed to be "
333
+ f"opened. Reason: { e } " )
334
+ raise ValueError (msg ) from e
335
+
327
336
# If opening a file fails, set chat template to be args to
328
337
# ensure we decode so our escape are interpreted correctly
329
- self . tokenizer .chat_template = codecs .decode (
338
+ tokenizer .chat_template = codecs .decode (
330
339
chat_template , "unicode_escape" )
331
340
332
341
logger .info (
333
- f"Using supplied chat template:\n { self .tokenizer .chat_template } "
334
- )
335
- elif self .tokenizer .chat_template is not None :
342
+ f"Using supplied chat template:\n { tokenizer .chat_template } " )
343
+ elif tokenizer .chat_template is not None :
336
344
logger .info (
337
- f"Using default chat template:\n { self .tokenizer .chat_template } "
338
- )
345
+ f"Using default chat template:\n { tokenizer .chat_template } " )
339
346
else :
340
347
logger .warning (
341
348
"No chat template provided. Chat API will not work." )
0 commit comments