【问题标题】:Chatbot using Huggingface Transformers使用 Huggingface 变形金刚的聊天机器人
【发布时间】:2021-12-31 11:23:54
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: tensorflow chatbot huggingface-transformers blenderbot


    【解决方案1】:

    这里是使用 DialoGPT 模型和 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 采样和核采样,并找出最适合您的用例的方法。有关此主题的更多信息,请查看post

    【讨论】:

    • 非常感谢。与您发布的聊天机器人的对话没有多大意义。 ;) 在 Huggingface 上是否没有针对 GPT2 和 BlenderBot 的预训练模型,可以开箱即用?
    • 有......正如我的回答中所发布的,但正如我所说,它们可能需要不同的参数,这就是为什么你必须稍微调整你的代码。
    猜你喜欢
    • 2020-11-18
    • 2021-01-18
    • 1970-01-01
    • 2020-08-24
    • 2020-04-15
    • 1970-01-01
    • 2020-05-25
    • 2020-11-06
    • 2022-12-23
    相关资源
    最近更新 更多