繁体   English   中英

使用 Huggingface 变形金刚的聊天机器人

[英]Chatbot using Huggingface Transformers

我想使用 Huggingface Transformers 来实现一个聊天机器人。 目前,我有如下所示的代码。 变压器 model 已经考虑了过去用户输入的历史。

在构建聊天机器人时,我还需要考虑其他什么(附加代码)吗?

其次,如何修改我的代码以使用 TensorFlow 而不是 PyTorch 运行?

稍后,我还计划在其他数据上微调 model。 我还计划测试不同的模型,例如 BlenderBot 和 GPT2。 我认为测试这个不同的模型应该像在AutoTokenizer.from_pretrained("microsoft/DialoGPT-small") AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")替换相应的 model 一样简单

from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")

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')

    # 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)))

以下是使用DialoGPT model 和 Tensorflow 的示例:

from transformers import TFAutoModelForCausalLM, AutoTokenizer, BlenderbotTokenizer, TFBlenderbotForConditionalGeneration
import tensorflow as tf

chat_bots = {
    'BlenderBot': [BlenderbotTokenizer.from_pretrained('facebook/blenderbot-400M-distill'), TFT5ForConditionalGeneration.from_pretrained('facebook/blenderbot-400M-distill')],
    'DialoGPT': [AutoTokenizer.from_pretrained("microsoft/DialoGPT-small"), TFAutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")],
} 
key = 'DialoGPT'
tokenizer, model = chat_bots[key]

for step in range(5):
    new_user_input_ids = tokenizer.encode(input(">> User:") + tokenizer.eos_token, return_tensors='tf')
    if step > 0:
      bot_input_ids = tf.concat([chat_history_ids, new_user_input_ids], axis=-1)  
    else:
      bot_input_ids = new_user_input_ids

    chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)

    print(key + ": {}".format(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)))
>> User:How are you?
DialoGPT: I'm here
>> User:Why are you here
DialoGPT: I'm here
>> User:But why
DialoGPT: I'm here
>> User:Where is here
DialoGPT: Where is where?
>> User:Here
DialoGPT: Where is here?

如果您想比较不同的聊天机器人,您可能需要调整它们的解码器参数,因为它们并不总是相同的。 例如,使用BlenderBot和 50 的max_length ,您会在当前代码中得到这种响应:

>> User:How are you?
BlenderBot: ! I am am great! how how how are are are???

一般来说,您应该问自己哪些特殊字符对聊天机器人很重要(取决于您的域),哪些字符应该/可以省略?

您还应该尝试不同的解码方法,例如贪婪搜索、光束搜索、随机采样、top-k 采样和核采样,并找出最适合您的用例的解码方法。 有关此主题的更多信息,请查看此帖子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM