1、介绍
对话机器人本质上是一个用来模型人类对话或聊天的计算机程序,接受人类的自然语言作为输入,并给出合适回复。
分类:
问答机器人,闲聊机器人,任务型对话机器人
按照答案产生的逻辑
- 检索对话机器人和生成式对话机器人
指令微调的方式,赋予回答问题的能力
训练单轮问答模型,计算Loss时只计算output部分
2、代码实战
2.1、导包
from datasets import load_dataset, Dataset
from transformers import AutoTokenizer, AutoModelForCausalLM, DataCollatorForSeq2Seq, TrainingArguments, Trainer
2.2、加载数据
ds = Dataset.load_from_disk("./alpaca_data_zh/")
2.3、数据处理
tokenizer = AutoTokenizer.from_pretrained("../Model/bloom-389m-zh")
tokenizer
def process_func(example):
MAX_LENGTH = 256
input_ids, attention_mask, labels = [], [], []
instruction = tokenizer("\n".join(["Human: " + example["instruction"], example["input"]]).strip() + "\n\nAssistant: ")
response = tokenizer(example["output"] + tokenizer.eos_token)
input_ids = instruction["input_ids"] + response["input_ids"]
attention_mask = instruction["attention_mask"] + response["attention_mask"]
labels = [-100] * len(instruction["input_ids"]) + response["input_ids"]
if len(input_ids) > MAX_LENGTH:
input_ids = input_ids[:MAX_LENGTH]
attention_mask = attention_mask[:MAX_LENGTH]
labels = labels[:MAX_LENGTH]
return {
"input_ids": input_ids,
"attention_mask": attention_mask,
"labels": labels
}
tokenized_ds = ds.map(process_func, remove_columns=ds.column_names)
tokenized_ds
2.4、创建模型
tokenized_ds = ds.map(process_func, remove_columns=ds.column_names)
tokenized_ds
2.5、配置训练参数
args = TrainingArguments(
output_dir="./chatbot",
per_device_train_batch_size=4,
gradient_accumulation_steps=4,
logging_steps=10,
num_train_epochs=1
)
2.6、创建训练器
trainer = Trainer(
args=args,
model=model,
train_dataset=tokenized_ds,
data_collator=DataCollatorForSeq2Seq(tokenizer, padding=True, )
)
2.7、模型训练
trainer.train()
2.8、模型推理
from transformers import pipeline
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
ipt = "Human: {}\n{}".format("考试有哪些技巧?", "").strip() + "\n\nAssistant: "
pipe(ipt, max_length=256, do_sample=True, temperature=0.5)