💬聊天模板
了解聊天模板的基础知识和自定义选项,包括 Conversational、ChatML、ShareGPT、Alpaca 格式等!
最后更新于
这有帮助吗?
这有帮助吗?
from unsloth.chat_templates import CHAT_TEMPLATES
print(list(CHAT_TEMPLATES.keys()))['unsloth', 'zephyr', 'chatml', 'mistral', 'llama', 'vicuna', 'vicuna_old', 'vicuna old', 'alpaca', 'gemma', 'gemma_chatml', 'gemma2', 'gemma2_chatml', 'llama-3', 'llama3', 'phi-3', 'phi-35', 'phi-3.5', 'llama-3.1', 'llama-31', 'llama-3.2', 'llama-3.3', 'llama-32', 'llama-33', 'qwen-2.5', 'qwen-25', 'qwen25', 'qwen2.5', 'phi-4', 'gemma-3', 'gemma3']from unsloth.chat_templates import get_chat_template
tokenizer = get_chat_template(
tokenizer,
chat_template = "gemma-3", # 将其改为正确的 chat_template 名称
)def formatting_prompts_func(examples):
convos = examples["conversations"]
texts = [tokenizer.apply_chat_template(convo, tokenize = False, add_generation_prompt = False) for convo in convos]
return { "text" : texts, }# 导入并加载数据集
from datasets import load_dataset
dataset = load_dataset("repo_name/dataset_name", split = "train")
# 使用 map 方法将格式化函数应用到你的数据集
dataset = dataset.map(formatting_prompts_func, batched = True,)# 导入数据集
from datasets import load_dataset
dataset = load_dataset("mlabonne/FineTome-100k", split = "train")
# 如有必要,将你的数据集转换为 "role"/"content" 格式
from unsloth.chat_templates import standardize_sharegpt
dataset = standardize_sharegpt(dataset)
# 使用 map 方法将格式化函数应用到你的数据集
dataset = dataset.map(formatting_prompts_func, batched = True,)[
[{'from': 'human', 'value': 'Hi there!'},
{'from': 'gpt', 'value': 'Hi how can I help?'},
{'from': 'human', 'value': 'What is 2+2?'}],
[{'from': 'human', 'value': 'What's your name?'},
{'from': 'gpt', 'value': 'I'm Daniel!'},
{'from': 'human', 'value': 'Ok! Nice!'},
{'from': 'gpt', 'value': 'What can I do for you?'},
{'from': 'human', 'value': 'Oh nothing :)'},],
]from unsloth.chat_templates import get_chat_template
tokenizer = get_chat_template(
tokenizer,
chat_template = "chatml", # 支持 zephyr, chatml, mistral, llama, alpaca, vicuna, vicuna_old, unsloth
mapping = {"role" : "from", "content" : "value", "user" : "human", "assistant" : "gpt"}, # ShareGPT 风格
map_eos_token = True, # 改为将 <|im_end|> 映射到 </s>
)
def formatting_prompts_func(examples):
convos = examples["conversations"]
texts = [tokenizer.apply_chat_template(convo, tokenize = False, add_generation_prompt = False) for convo in convos]
return { "text" : texts, }
pass
from datasets import load_dataset
dataset = load_dataset("philschmid/guanaco-sharegpt-style", split = "train")
dataset = dataset.map(formatting_prompts_func, batched = True,)unsloth_template = \
"{{ bos_token }}"\
"{{ '你是一个对用户有帮助的助手\n' }}"\
"</div>"\
"<div data-gb-custom-block data-tag="for">"\
"<div data-gb-custom-block data-tag="if" data-0='role' data-1='role' data-2='] == ' data-3='user'>"\
"{{ '>>> User: ' + message['content'] + '\n' }}"\
"<div data-gb-custom-block data-tag="elif" data-0='role' data-1='role' data-2='] == ' data-3='assistant'></div>"\
"{{ '>>> Assistant: ' + message['content'] + eos_token + '\n' }}"\
"</div>"\
"</div>"\
"<div data-gb-custom-block data-tag="if">"\
"{{ '>>> Assistant: ' }}"\
"</div>"
unsloth_eos_token = "eos_token"
tokenizer = get_chat_template(
tokenizer,
chat_template = (unsloth_template, unsloth_eos_token,), # 你必须提供一个模板和 EOS token
mapping = {"role" : "from", "content" : "value", "user" : "human", "assistant" : "gpt"}, # ShareGPT 风格
map_eos_token = True, # 改为将 <|im_end|> 映射到 </s>
)