Skip to content

Conversation

popomen
Copy link

@popomen popomen commented Aug 28, 2025

The input_ids obtained by executing apply_chat_template via HF processor still contain a \n (newline character) following <|im_end|>.

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

Write the detail information belongs to this PR.

Experiment results

Paste your experiment result here(if needed).

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @popomen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a minor formatting issue within the ChatML template implementation. It specifically fixes the suffix used in the template by ensuring it includes a newline character. This change is crucial for maintaining correct prompt formatting when generating text with ChatML-based models, preventing potential issues with tokenization or model understanding due to missing line breaks.

Highlights

  • ChatML Template Formatting: Corrected the suffix definition within the ChatmlTemplateMeta class in swift/llm/template/template/utils.py by appending a newline character (\n) to the ['<|im_end|>'] token. This ensures proper line termination for ChatML templates.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request fixes a bug in the ChatML template by adding a newline character to the suffix. The change is correct and makes the suffix consistent with other parts of the template. I've also added a suggestion to refactor the template definition to use constants for special tokens, which would improve maintainability by reducing magic strings and code duplication.

Comment on lines 16 to 19
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'])
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.

@Jintao-Huang
Copy link
Collaborator

This change is quite significant, so we won't merge it in this PR. We'll open a new PR for review and testing. Thank you!

@Jintao-Huang Jintao-Huang added the bug Something isn't working label Aug 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants