-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[router] add tokenizer chat template support #9370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: Chang Su <chang.s.su@oracle.com>
There was a problem hiding this 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 @slin1237, 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 introduces comprehensive chat template support to the SGL Router's Rust tokenizer, enabling it to load and apply chat templates from various sources, including tokenizer_config.json files and custom Jinja files. This enhancement ensures the Rust tokenizer's behavior is consistent with SGLang's existing tokenizer manager, providing greater flexibility in handling conversational AI models.
Highlights
- New Chat Template Module: A dedicated
chat_template.rsmodule has been added, providing core functionalities for defining chat messages (ChatMessage) and processing them using Jinja2 templates viaChatTemplateProcessor. - Flexible Template Loading: The
HuggingFaceTokenizernow supports loading chat templates fromtokenizer_config.jsonby default, or from a specified.jinjafile, with the latter taking precedence. - Programmatic Template Control: A new
set_chat_templatemethod allows developers to dynamically set or override the chat template after the tokenizer has been initialized. - Updated Tokenizer Factory: The tokenizer factory functions have been extended to incorporate the new chat template loading logic, simplifying tokenizer creation with template support.
- Expanded Test Coverage: New unit and integration tests have been added to thoroughly validate the chat template processing and loading mechanisms, including tests for different template styles (e.g., Llama, ChatML).
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
-
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. ↩
There was a problem hiding this 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 introduces chat template support for the SGLang Router Tokenizer, a valuable addition that aligns its functionality with the existing SGLang tokenizer manager. The implementation is well-structured, with clear separation of concerns into a new chat_template module and good integration into the existing tokenizer factory and HuggingFace tokenizer implementation. The test coverage for the new functionality is also comprehensive, covering various template styles and loading mechanisms.
I've identified a couple of opportunities for performance improvements in the chat_template.rs file, primarily related to avoiding repeated work in template processing. These are detailed in the review comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of apply_chat_template creates a new minijinja::Environment and parses the template on every call. This is inefficient as it involves repeated work for a template that doesn't change.
For better performance, I recommend refactoring ChatTemplateProcessor to parse the template only once during its initialization. You could store the compiled template or the minijinja::Environment instance within the ChatTemplateProcessor struct.
This would likely involve:
- Changing
ChatTemplateProcessor::newto return aResultand perform the one-time parsing. - Modifying
HuggingFaceTokenizerto store anOption<ChatTemplateProcessor>instead ofOption<String>, and initializing it when the tokenizer is loaded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an unnecessary clone of the bos_token and eos_token strings here. You can avoid this by passing a string slice (&str) to the context, which minijinja supports. Using as_deref is more efficient as it avoids allocating a new string if the token is present.
| bos_token => self.bos_token.clone().unwrap_or_default(), | |
| eos_token => self.eos_token.clone().unwrap_or_default() | |
| bos_token => self.bos_token.as_deref().unwrap_or_default(), | |
| eos_token => self.eos_token.as_deref().unwrap_or_default() |
9acc1c7 to
33fbef7
Compare
33fbef7 to
715c119
Compare
Co-authored-by: Chang Su <chang.s.su@oracle.com>
Overview
Introduce chat template implementation in SGL Router Tokenizer, which now fully supports loading templates from multiple sources, matching the behavior of SGLang existing tokenizer manager
Implementation Details
1. Template Loading Priority
When a chat template path is explicitly provided, it overrides any existing template in the tokenizer:
2. Template Sources
Our implementation supports loading templates from:
3. Key Features Implemented
a. Loading from Custom .jinja File
b. Setting Template After Creation
c. Factory Functions
4. Usage Examples
Example 1: Load with Custom Template
Example 2: Override Template After Loading
Example 3: Apply Chat Template
Checklist