refactor: SFT 统一 messages 格式 + ChatML 纯 jinja 渲染
This commit is contained in:
+2
-2
@@ -34,8 +34,8 @@ def main():
|
||||
parser.add_argument(
|
||||
"-t",
|
||||
"--tokenizer",
|
||||
default="./tokenizer.json",
|
||||
help="Tokenizer path (default: ./tokenizer.json)",
|
||||
default="./tokenizer",
|
||||
help="Tokenizer dir (default: ./tokenizer)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-s",
|
||||
|
||||
@@ -6,10 +6,13 @@ def process_func(input_dict: dict):
|
||||
instruction = input_dict["instruction"]
|
||||
inp = input_dict.get("input", "")
|
||||
if inp:
|
||||
query = instruction + "\n" + inp
|
||||
content = instruction + "\n" + inp
|
||||
else:
|
||||
query = instruction
|
||||
return {"query": query, "response": input_dict["output"]}
|
||||
content = instruction
|
||||
return {"messages": [
|
||||
{"role": "user", "content": content},
|
||||
{"role": "assistant", "content": input_dict["output"]},
|
||||
]}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -3,7 +3,10 @@ from pipeline import export_dataset
|
||||
|
||||
|
||||
def process_func(input_dict: dict):
|
||||
return {"query": input_dict["instruction"], "response": input_dict["output"]}
|
||||
return {"messages": [
|
||||
{"role": "user", "content": input_dict["instruction"]},
|
||||
{"role": "assistant", "content": input_dict["output"]},
|
||||
]}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -3,7 +3,10 @@ from pipeline import export_dataset
|
||||
|
||||
|
||||
def process_func(input_dict: dict):
|
||||
return {"query": input_dict["instruction"], "response": input_dict["response"]}
|
||||
return {"messages": [
|
||||
{"role": "user", "content": input_dict["instruction"]},
|
||||
{"role": "assistant", "content": input_dict["response"]},
|
||||
]}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -3,7 +3,10 @@ from pipeline import export_dataset
|
||||
|
||||
|
||||
def process_func(sample: dict) -> dict:
|
||||
return {"query": sample["query"], "response": sample["response"]}
|
||||
return {"messages": [
|
||||
{"role": "user", "content": sample["query"]},
|
||||
{"role": "assistant", "content": sample["response"]},
|
||||
]}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -2,13 +2,30 @@ from datasets import load_dataset
|
||||
from pipeline import export_dataset
|
||||
|
||||
|
||||
ROLE_MAP = {"system": "system", "human": "user", "gpt": "assistant"}
|
||||
|
||||
|
||||
def process_func(input_dict: dict):
|
||||
conversations = input_dict["conversations"]
|
||||
|
||||
system_msgs = []
|
||||
idx = 0
|
||||
if conversations and conversations[0]["from"] == "system":
|
||||
system_msgs.append({
|
||||
"role": "system",
|
||||
"content": conversations[0]["value"],
|
||||
})
|
||||
idx = 1
|
||||
|
||||
examples = []
|
||||
for i in range(0, len(conversations) - 1, 2):
|
||||
user_msg = conversations[i]["value"]
|
||||
assistant_msg = conversations[i + 1]["value"]
|
||||
examples.append({"query": user_msg, "response": assistant_msg})
|
||||
for i in range(idx, len(conversations) - 1, 2):
|
||||
user_msg = conversations[i]
|
||||
assistant_msg = conversations[i + 1]
|
||||
messages = system_msgs + [
|
||||
{"role": ROLE_MAP[user_msg["from"]], "content": user_msg["value"]},
|
||||
{"role": ROLE_MAP[assistant_msg["from"]], "content": assistant_msg["value"]},
|
||||
]
|
||||
examples.append({"messages": messages})
|
||||
return examples
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user