refactor: SFT 统一 messages 格式 + ChatML 纯 jinja 渲染

This commit is contained in:
2026-07-04 14:32:35 +08:00
parent 06735b9cb3
commit 816c02dab0
11 changed files with 177 additions and 89 deletions
+12 -6
View File
@@ -13,22 +13,28 @@ from pipeline.processors import (
class DummyTokenizer:
im_end = "<|im_end|>"
def __init__(self):
self._special_token_map = {}
self._chat_template = None
def encode(self, text: str, add_special_tokens: bool = False):
return [ord(c) for c in text]
def decode(self, tokens, skip_special_tokens=True):
return "".join(chr(t) for t in tokens)
def token_to_id(self, token: str):
return ord(token)
def apply_chat_template(
self, messages, add_generation_prompt=True, tokenize=True
):
def set_chat_template(self, template):
self._chat_template = template
def apply_chat_template(self, messages, add_generation_prompt=True, tokenize=True):
text = ""
for m in messages:
text += f"<|im_start|>{m['role']}\n{m['content']}<|im_end|>\n"
text += f"<imstart>{m['role']}\n{m['content']}<imend>\n"
if add_generation_prompt:
text += "<|im_start|>assistant\n"
text += "<imstart>assistant\n"
return self.encode(text) if tokenize else text