feat(chat): add friendly name aliases for chat templates (fixes #976)#1460
Open
octo-patch wants to merge 1 commit into
Open
feat(chat): add friendly name aliases for chat templates (fixes #976)#1460octo-patch wants to merge 1 commit into
octo-patch wants to merge 1 commit into
Conversation
…nce-ai#976) Users can now pass short names like "llama2", "ChatML", "mistral", or "qwen3" to any chat_template parameter instead of requiring the full Jinja2 template string. Lookup is case-insensitive and strips spaces, dashes, underscores, and dots (e.g. "Llama 3.2", "llama-3.2", and "llama32" all resolve to Llama3dot2ChatTemplate). Resolves the TODO at guidance/chat.py that asked for an alias system.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #976.
Users currently cannot pass friendly names like
"llama2"or"ChatML"to thechat_templateparameter — they must supply the full Jinja2 template string. This PR implements the alias system that was explicitly called out in two TODO comments inchat.py:Changes
guidance/chat.py— updatesload_template_class()to check a newCHAT_TEMPLATE_NAME_MAPdictionary before falling through to the Jinja2 template string cache. Lookup is case-insensitive and strips spaces, dashes, underscores, and dots, so all of these work:CHAT_TEMPLATE_NAME_MAP— a new module-level dict mapping normalized alias keys to template classes. Covers all templates currently defined in the module: ChatML, Llama 2/3/3.2, Phi-3 mini/small/medium, Phi-4 mini, Mistral 7B Instruct, Gemma 2 9B, Qwen 2.5, and Qwen 3.tests/unit/test_chat.py(new) — unit tests covering all alias variants, case-insensitivity, separator stripping,Nonefallback, unknown-name warning, and map-key normalization invariant.Backwards compatibility
Existing code that passes full Jinja2 template strings continues to work unchanged — the name map check only runs first; if no alias matches, the code falls through to
CHAT_TEMPLATE_CACHEexactly as before.