@@ -450,6 +450,7 @@ def build_all_requests(
450
450
ctx = fewshot_ctx ,
451
451
metadata = (self .config ["task" ], doc_id , self .config .repeats ),
452
452
apply_chat_template = apply_chat_template ,
453
+ chat_template = chat_template ,
453
454
)
454
455
455
456
if not isinstance (inst , list ):
@@ -1087,6 +1088,8 @@ def fewshot_context(
1087
1088
example = self .doc_to_text (doc )
1088
1089
if apply_chat_template :
1089
1090
if self .multiple_input :
1091
+ if not labeled_examples :
1092
+ return ""
1090
1093
return chat_template (labeled_examples )
1091
1094
if isinstance (example , str ):
1092
1095
self .append_target_question (
@@ -1303,6 +1306,7 @@ def construct_requests(
1303
1306
self , doc : dict , ctx : str , ** kwargs
1304
1307
) -> Union [List [Instance ], Instance ]:
1305
1308
apply_chat_template = kwargs .pop ("apply_chat_template" , False )
1309
+ chat_template : Callable | None = kwargs .pop ("chat_template" , None )
1306
1310
1307
1311
aux_arguments = None
1308
1312
@@ -1317,9 +1321,20 @@ def construct_requests(
1317
1321
target_delimiter = ""
1318
1322
if self .multiple_input :
1319
1323
# If there are multiple inputs, choices are placed in the ctx
1324
+ # apply chat_template to choices if apply_chat_template
1320
1325
cont = self .doc_to_target (doc )
1326
+
1321
1327
arguments = [
1322
- (ctx + choice , f"{ target_delimiter } { cont } " ) for choice in choices
1328
+ (
1329
+ ctx
1330
+ + (
1331
+ chat_template ([{"role" : "user" , "content" : choice }])
1332
+ if apply_chat_template
1333
+ else choice
1334
+ ),
1335
+ f"{ target_delimiter } { cont } " ,
1336
+ )
1337
+ for choice in choices
1323
1338
]
1324
1339
else :
1325
1340
# Otherwise they are placed in the continuation
0 commit comments