@@ -90,8 +90,21 @@ def apply_chat_template(
90
90
91
91
# Apply the chat template to the prompt, adding the generation prompt
92
92
if "prompt" in example :
93
+ last_role = example ["prompt" ][- 1 ]["role" ]
94
+ if last_role == "user" :
95
+ add_generation_prompt = True
96
+ continue_final_message = False
97
+ elif last_role == "assistant" :
98
+ add_generation_prompt = False
99
+ continue_final_message = True
100
+ else :
101
+ raise ValueError (f"Invalid role in the last message: { last_role } " )
93
102
prompt = tokenizer .apply_chat_template (
94
- example ["prompt" ], tools = tools , tokenize = False , add_generation_prompt = True
103
+ example ["prompt" ],
104
+ tools = tools ,
105
+ continue_final_message = continue_final_message ,
106
+ tokenize = False ,
107
+ add_generation_prompt = add_generation_prompt ,
95
108
)
96
109
97
110
# Apply the chat template to the entire prompt + completion
@@ -180,10 +193,13 @@ def maybe_apply_chat_template(
180
193
Returns:
181
194
`dict[str, str]`: The formatted example with the chat template applied.
182
195
183
- Note :
184
- This function does not alter the keys, except for Language modeling dataset, where `"messages"` is replaced by
196
+ Notes :
197
+ - This function does not alter the keys, except for Language modeling dataset, where `"messages"` is replaced by
185
198
`"text"`.
186
199
200
+ - In case of prompt-only data, if the last role is `"user"`, the generation prompt is added to the prompt. Else,
201
+ if the last role is `"assistant"`, the final message is continued.
202
+
187
203
Example:
188
204
189
205
```python
0 commit comments