Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/transformers/tokenization_mistral_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,16 +1090,11 @@ def apply_chat_template( # type: ignore[override]
If not specified, the default reasoning effort will be used.

kwargs (additional keyword arguments, *optional*):
Not supported by `MistralCommonBackend.apply_chat_template`.
Will raise an error if used.
Additional arguments passed to the mistral-common `ChatCompletionRequest.from_openai` method.

Returns:
`Union[str, list[int], list[str], list[list[int]], BatchEncoding]`: The tokenized chat so far, including control tokens. This output is ready to pass to the model, either directly or via methods like `generate()`.
"""
if kwargs:
raise ValueError(
f"Kwargs {list(kwargs.keys())} are not supported by `MistralCommonBackend.apply_chat_template`."
)
if not isinstance(truncation, bool):
raise TypeError("`truncation` must be a boolean for `apply_chat_template` method.")

Expand Down Expand Up @@ -1185,6 +1180,7 @@ def _maybe_adapt_message(message: dict[str, Any]) -> None:
tools=tools,
continue_final_message=continue_final_message,
reasoning_effort=reasoning_effort,
**kwargs,
)

tokenized_request = self.tokenizer.encode_chat_completion(chat_request)
Expand Down Expand Up @@ -1594,9 +1590,7 @@ def save_pretrained( # type: ignore[override]
@staticmethod
def _get_validation_mode(mode: str | ValidationMode) -> ValidationMode:
"""Get the validation mode from a string or a ValidationMode."""
_invalid_mode_msg = (
f"Invalid `mistral-common` tokenizer mode: {mode}. Possible values are 'finetuning' or 'test'."
)
_invalid_mode_msg = f"Invalid `mistral-common` tokenizer mode: {mode}. Possible values are {', '.join([vm.value for vm in list(ValidationMode)])}."
if isinstance(mode, str):
try:
mode = ValidationMode[mode]
Expand All @@ -1605,8 +1599,6 @@ def _get_validation_mode(mode: str | ValidationMode) -> ValidationMode:
elif not isinstance(mode, (str, ValidationMode)):
raise ValueError(_invalid_mode_msg)

if mode not in [ValidationMode.finetuning, ValidationMode.test]:
raise ValueError(_invalid_mode_msg)
return mode

def __repr__(self) -> str:
Expand Down
15 changes: 3 additions & 12 deletions tests/test_tokenization_mistral_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,11 +902,6 @@ def test_apply_chat_template_basic(self):
expected_tokenized.tokens,
)

with self.assertRaises(
ValueError, msg="Kwargs [unk_args] are not supported by `MistralCommonBackend.apply_chat_template`."
):
self.tokenizer.apply_chat_template(conversation, tokenize=True, unk_args="")

def test_apply_chat_template_continue_final_message(self):
conversation = [
{"role": "system", "content": "You are a helpful assistant."},
Expand Down Expand Up @@ -1268,12 +1263,6 @@ def test_batch_apply_chat_template(self):
self.assertEqual(text, expected.text)
self.assertEqual(token, expected.tokens)

with self.assertRaises(
ValueError,
msg="Kwargs [unk_args] are not supported by `MistralCommonBackend.batch_apply_chat_template`.",
):
self.tokenizer.apply_chat_template(conversations, tools=tools, tokenize=True, unk_args="")

def test_batch_apply_chat_template_images(self):
conversations = [
[
Expand Down Expand Up @@ -2140,10 +2129,12 @@ def test_get_validation_mode(self):
(ValidationMode.test, ValidationMode.test),
("finetuning", ValidationMode.finetuning),
(ValidationMode.finetuning, ValidationMode.finetuning),
("serving", ValidationMode.serving),
(ValidationMode.serving, ValidationMode.serving),
]:
self.assertEqual(MistralCommonBackend._get_validation_mode(mode), expected)

for invalid_mode in [("serving", ValidationMode.serving, "invalid", 1)]:
for invalid_mode in ["invalid", 1]:
with self.assertRaises(ValueError):
MistralCommonBackend._get_validation_mode(invalid_mode)

Expand Down
Loading