DialoGPT 是一款用于多轮对话的最先进大规模预训练对话响应生成模型。 人工评估结果表明,在单轮对话图灵测试中,DialoGPT 生成的响应质量可与人类响应相媲美。 该模型是在来自 Reddit 讨论帖的 1.47 亿条多轮对话数据上训练而成的。
| 角色 | 响应 |
|---|---|
| 用户 | 金钱能买到幸福吗? |
| 机器人 | 这取决于你花多少钱在上面。 |
| 用户 | 买幸福的最好方法是什么? |
| 机器人 | 你只需要在 20 岁出头就成为百万富翁,然后你就能幸福了。 |
| 用户 | 这太难了! |
| 机器人 | 你根本不知道成为百万富翁并保持幸福有多难。富人拥有大量金钱是有原因的。 |
有关 DialoGPT 的预处理、训练和完整详细信息,请参见原始 DialoGPT 代码库
ArXiv 论文:https://arxiv.org/abs/1911.00536
现在,我们可以尝试将该模型作为聊天伙伴来使用了!
from openmind import AutoTokenizer, AutoModelForCausalLM, is_torch_npu_available
from openmind_hub import snapshot_download
import torch
import argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_name_or_path",
type=str,
help="Path to model",
default="models/DialoGPT-medium",
)
args = parser.parse_args()
return args
def main():
args = parse_args()
model_path = args.model_name_or_path
if is_torch_npu_available():
device = "npu:0"
else:
device = "cpu"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path).to(device)
# Let's chat for 5 lines
for step in range(5):
# encode the new user input, add the eos_token and return a tensor in Pytorch
new_user_input_ids = tokenizer.encode(input(">> User:") + tokenizer.eos_token, return_tensors='pt').to(device)
# append the new user input tokens to the chat history
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if step > 0 else new_user_input_ids
# generated a response while limiting the total chat history to 1000 tokens,
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
# pretty print last ouput tokens from bot
print("DialoGPT: {}".format(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)))
if __name__ == "__main__":
main()