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
2 changes: 1 addition & 1 deletion swift/llm/template/template/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ChatmlTemplateMeta(TemplateMeta):
prefix: Prompt = field(default_factory=list)
prompt: Prompt = field(default_factory=lambda: ['<|im_start|>user\n{{QUERY}}<|im_end|>\n<|im_start|>assistant\n'])
chat_sep: Optional[Prompt] = field(default_factory=lambda: ['<|im_end|>\n'])
suffix: Prompt = field(default_factory=lambda: ['<|im_end|>'])
suffix: Prompt = field(default_factory=lambda: ['<|im_end|>\n'])
system_prefix: Optional[Prompt] = field(default_factory=lambda: ['<|im_start|>system\n{{SYSTEM}}<|im_end|>\n'])
auto_add_bos: bool = True

Expand Down
18 changes: 10 additions & 8 deletions swift/llm/template/template_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,20 @@ def init(self, tokenizer: PreTrainedTokenizerBase) -> None:
value = self._token_attr_to_id(tokenizer, value)
setattr(self, key, value)

if self.suffix and self.suffix[-1] not in self.stop_words:
self.stop_words.append(self.suffix[-1])
suffix_stop = self.suffix[-1] if self.suffix else None
if isinstance(suffix_stop, str):
suffix_stop = suffix_stop.strip()
if suffix_stop and suffix_stop not in self.stop_words:
self.stop_words.append(suffix_stop)
if tokenizer.eos_token not in self.stop_words:
self.stop_words.append(tokenizer.eos_token)

self.stop_token_id = tokenizer.eos_token_id
if self.suffix:
suffix_tokens = self.suffix[-1]
if isinstance(suffix_tokens, str):
stop_token_id = tokenizer.convert_tokens_to_ids(suffix_tokens)
elif isinstance(suffix_tokens, list) and len(suffix_tokens) == 1:
stop_token_id = suffix_tokens[0]
if suffix_stop:
if isinstance(suffix_stop, str):
stop_token_id = tokenizer.convert_tokens_to_ids(suffix_stop)
elif isinstance(suffix_stop, list) and len(suffix_stop) == 1:
stop_token_id = suffix_stop[0]
else:
stop_token_id = None
Comment on lines +127 to 132
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block can be slightly simplified by initializing stop_token_id to None before the if/elif chain. This removes the else block and makes the logic a bit more direct and easier to read.

Suggested change
if isinstance(suffix_stop, str):
stop_token_id = tokenizer.convert_tokens_to_ids(suffix_stop)
elif isinstance(suffix_stop, list) and len(suffix_stop) == 1:
stop_token_id = suffix_stop[0]
else:
stop_token_id = None
stop_token_id = None
if isinstance(suffix_stop, str):
stop_token_id = tokenizer.convert_tokens_to_ids(suffix_stop)
elif isinstance(suffix_stop, list) and len(suffix_stop) == 1:
stop_token_id = suffix_stop[0]

if stop_token_id is not None:
Expand Down
Loading