Skip to content
Open
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'])
Comment on lines 16 to 19
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

While this change is correct, there's an opportunity to improve maintainability by reducing magic strings and code duplication. The special tokens for ChatML like <|im_start|> and <|im_end|>\n are used in multiple places within this class.

Consider defining these as constants to ensure consistency and make future changes easier. For example, you could define them at the module level:

# At the top of the file
CHATML_IM_START = '<|im_start|>'
CHATML_IM_END_WITH_NL = '<|im_end|>\n'

And then use them in the ChatmlTemplateMeta class:

@dataclass
class ChatmlTemplateMeta(TemplateMeta):
    prefix: Prompt = field(default_factory=list)
    prompt: Prompt = field(
        default_factory=lambda: f'{CHATML_IM_START}user\n{{{{QUERY}}}}{CHATML_IM_END_WITH_NL}{CHATML_IM_START}assistant\n'
    )
    chat_sep: Optional[Prompt] = field(default_factory=lambda: [CHATML_IM_END_WITH_NL])
    suffix: Prompt = field(default_factory=lambda: [CHATML_IM_END_WITH_NL])
    system_prefix: Optional[Prompt] = field(
        default_factory=lambda: f'{CHATML_IM_START}system\n{{{{SYSTEM}}}}{CHATML_IM_END_WITH_NL}'
    )
    auto_add_bos: bool = True

This would make the template definition more robust and easier to maintain.

auto_add_bos: bool = True

Expand Down
Loading