File size: 9,347 Bytes
62dca4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | # Adapted from: https://github.com/sgl-project/sglang/blob/main/python/sglang/lang/chat_template.py#L13
from typing import List
from pydantic import BaseModel
class ChatTemplate(BaseModel):
"""
This is a dataclass for the chat template.
Args:
assistant_header(str): The header for the assistant.
user_header(str): The header for the user.
system_prompt(str): The system prompt.
end_of_turn_token(str): The end token of a turn of conversation.
"""
assistant_header: str | None
user_header: str | None
system_prompt: str | None
end_of_turn_token: str | None
parser_type: str = "general"
assistant_pattern_type: str = "general"
enable_thinking: bool = False
class TemplateRegistry:
"""
This is a registry for the chat template. Sgl-spec will register some common chat templates here.
If you have a custom chat template, you can register it via the example below.
Example:
```python
from specforge.data.template import TEMPLATE_REGISTRY, ChatTemplate
TEMPLATE_REGISTRY.register(
name="custom",
template=ChatTemplate(
assistant_header="<|start_header_id|>assistant<|end_header_id|>\n\n",
user_header="<|start_header_id|>user<|end_header_id|>",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|eot_id|>"
)
)
```
"""
def __init__(self):
self.templates = {}
def register(self, name: str, template: ChatTemplate, override: bool = False):
"""
Register a chat template for a model type.
Args:
name(str): The name of the chat template.
template(ChatTemplate): The chat template.
override(bool): Whether to override the existing template, default to False
"""
assert (
not override and name not in self.templates
), f"Chat template for the model type {name} has already been registered"
self.templates[name] = template
def get(self, name: str) -> ChatTemplate:
"""
Get the chat template for a model type.
Args:
name(str): The name of the chat template.
Returns:
ChatTemplate: The chat template.
"""
return self.templates[name]
def get_all_template_names(self) -> List[str]:
"""
Get all the template names.
Returns:
List[str]: The list of template names.
"""
return list(self.templates.keys())
# global registry
TEMPLATE_REGISTRY = TemplateRegistry()
# Register the common template here
TEMPLATE_REGISTRY.register(
name="llama3",
template=ChatTemplate(
assistant_header="<|start_header_id|>assistant<|end_header_id|>\n\n",
user_header="<|start_header_id|>user<|end_header_id|>",
system_prompt="You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.",
end_of_turn_token="<|eot_id|>",
),
)
TEMPLATE_REGISTRY.register(
name="llama4",
template=ChatTemplate(
assistant_header="<|header_start|>assistant<|header_end|>\n\n",
user_header="<|header_start|>user<|header_end|>",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|eot|>",
),
)
TEMPLATE_REGISTRY.register(
name="qwen",
template=ChatTemplate(
assistant_header="<|im_start|>assistant\n",
user_header="<|im_start|>user\n",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|im_end|>\n",
),
)
TEMPLATE_REGISTRY.register(
name="qwen2-vl",
template=ChatTemplate(
assistant_header="<|im_start|>assistant\n",
user_header="<|im_start|>user\n",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|im_end|>\n",
),
)
TEMPLATE_REGISTRY.register(
name="phi3",
template=ChatTemplate(
assistant_header="<|assistant|>\n",
user_header="<|user|>\n",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|end|>\n",
),
)
TEMPLATE_REGISTRY.register(
name="phi4",
template=ChatTemplate(
assistant_header="<|im_start|>assistant<|im_sep|>",
user_header="<|im_start|>user<|im_sep|>",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|im_end|>",
),
)
TEMPLATE_REGISTRY.register(
name="phi4-mini",
template=ChatTemplate(
assistant_header="<|assistant|>",
user_header="<|user|>",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|end|>",
),
)
TEMPLATE_REGISTRY.register(
name="gpt-oss-naive",
template=ChatTemplate(
assistant_header="<|start|>assistant<|channel|>analysis<|message|>",
user_header="<|start|>user<|message|>",
system_prompt=None,
end_of_turn_token="<|end|>",
),
)
TEMPLATE_REGISTRY.register(
name="gpt-oss",
template=ChatTemplate(
assistant_header=None, # the headers are not applicable to openai-harmony's channel tags
user_header=None,
system_prompt=None,
end_of_turn_token=None,
parser_type="openai-harmony",
),
)
TEMPLATE_REGISTRY.register(
name="deepseek-r1-distill",
template=ChatTemplate(
assistant_header="<|Assistant|>",
user_header="<|User|>",
end_of_turn_token=None,
system_prompt=None,
),
)
TEMPLATE_REGISTRY.register(
name="qwen3-thinking",
template=ChatTemplate(
assistant_header="<|im_start|>assistant\n",
user_header="<|im_start|>user\n",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|im_end|>\n",
parser_type="thinking",
enable_thinking=True,
),
)
TEMPLATE_REGISTRY.register(
name="qwen3-instruct",
template=ChatTemplate(
assistant_header="<|im_start|>assistant\n<think>\n\n</think>\n",
user_header="<|im_start|>user\n",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|im_end|>\n",
),
)
TEMPLATE_REGISTRY.register(
name="qwen3-next-thinking",
template=ChatTemplate(
assistant_header="<|im_start|>assistant\n<think>\n",
user_header="<|im_start|>user\n",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|im_end|>\n",
parser_type="thinking",
enable_thinking=True,
),
)
TEMPLATE_REGISTRY.register(
name="kimi-k2-thinking",
template=ChatTemplate(
assistant_header="<|im_assistant|>assistant<|im_middle|>",
user_header="<|im_start|>user\n",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|im_end|>",
parser_type="thinking",
enable_thinking=True,
),
)
TEMPLATE_REGISTRY.register(
name="kimi-k2-instruct",
template=ChatTemplate(
assistant_header="<|im_assistant|>assistant<|im_middle|>",
user_header="<|im_start|>user\n",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|im_end|>",
),
)
TEMPLATE_REGISTRY.register(
name="deepseek-v3",
template=ChatTemplate(
assistant_header="<|Assistant|>",
user_header="<|User|>",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|end▁of▁sentence|>",
),
)
TEMPLATE_REGISTRY.register(
name="ling-flash-2.0",
template=ChatTemplate(
assistant_header="<role>ASSISTANT</role>",
user_header="<role>HUMAN</role>",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<|role_end|>",
),
)
TEMPLATE_REGISTRY.register(
name="deepseek-v32",
template=ChatTemplate(
assistant_header="<|Assistant|>",
user_header="<|User|>",
system_prompt="",
end_of_turn_token="<|end▁of▁sentence|>",
parser_type="thinking",
enable_thinking=True,
),
)
TEMPLATE_REGISTRY.register(
name="gemma",
template=ChatTemplate(
assistant_header="<start_of_turn>model\n",
user_header="<start_of_turn>user\n",
system_prompt="You are a helpful assistant.",
end_of_turn_token="<end_of_turn>\n",
),
)
TEMPLATE_REGISTRY.register(
name="longcat",
template=ChatTemplate(
assistant_header=" ASSISTANT:",
user_header=" USER:",
system_prompt="You are a helpful assistant.",
end_of_turn_token="</longcat_s>",
assistant_pattern_type="longcat",
),
)
TEMPLATE_REGISTRY.register(
name="longcat_xml",
template=ChatTemplate(
assistant_header="<longcat_assistant>",
user_header="<longcat_user>",
system_prompt="You are a helpful assistant.",
end_of_turn_token="</longcat_s>",
),
)
|