[PARSER][Mistral] unified engine-based parser for reasoning and tool calls (#48947)
This commit is contained in:
@@ -21,7 +21,6 @@ from mistral_common.tokens.tokenizers.base import (
|
||||
)
|
||||
from mistral_common.tokens.tokenizers.instruct import (
|
||||
InstructTokenizerBase,
|
||||
InstructTokenizerV13,
|
||||
)
|
||||
from mistral_common.tokens.tokenizers.mistral import (
|
||||
MistralTokenizer as MistralCommonTokenizer,
|
||||
@@ -461,6 +460,7 @@ class MistralTokenizer(TokenizerLike):
|
||||
def convert_tokens_to_string(self, tokens: list[str]) -> str:
|
||||
to_decode_special_tokens = {
|
||||
SpecialTokens.tool_calls,
|
||||
SpecialTokens.args,
|
||||
SpecialTokens.begin_think,
|
||||
SpecialTokens.end_think,
|
||||
}
|
||||
@@ -517,11 +517,20 @@ class MistralTokenizer(TokenizerLike):
|
||||
non_skip_special_tokens_ids = {
|
||||
self.tokenizer.get_special_token(SpecialTokens.tool_calls),
|
||||
}
|
||||
if isinstance(self.instruct, InstructTokenizerV13):
|
||||
if self.instruct.BEGIN_THINK:
|
||||
non_skip_special_tokens_ids.add(self.instruct.BEGIN_THINK)
|
||||
if self.instruct.END_THINK:
|
||||
non_skip_special_tokens_ids.add(self.instruct.END_THINK)
|
||||
# [ARGS] only exists in v11+ tool-call tokenizers; older tokenizers
|
||||
# raise (Tekken) or return unk (SPM) for it.
|
||||
if self.tokenizer.is_special(SpecialTokens.args):
|
||||
non_skip_special_tokens_ids.add(
|
||||
self.tokenizer.get_special_token(SpecialTokens.args)
|
||||
)
|
||||
# [THINK]/[/THINK] only exist in v13+ reasoning tokenizers; use the
|
||||
# same is_special gate as [ARGS] above so newer versions are covered
|
||||
# without an isinstance check on the instruct tokenizer.
|
||||
for think_token in (SpecialTokens.begin_think, SpecialTokens.end_think):
|
||||
if self.tokenizer.is_special(think_token):
|
||||
non_skip_special_tokens_ids.add(
|
||||
self.tokenizer.get_special_token(think_token)
|
||||
)
|
||||
|
||||
ids_kept = [
|
||||
i
|
||||
|
||||
Reference in New Issue
Block a user