In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s start by installing the transformers library:
pip install transformers
Once we install the library, we can move on. We will work with the ‘blenderbot-400M-distill’ model from Meta. This is a small open-source model (700 MB) that performs relatively well. Let’s start with the pipeline.
from transformers import pipeline chatbot = pipeline(task="conversational", model="./models/facebook/blenderbot-400M-distill")
At this point, I will initiate the conversation by passing the following message:
My name is George and I’m from Greece. Have you ever been to my country?
user_message = """ My name is George and I'm from Greece. Have you ever been to my country? """ from transformers import Conversation conversation = Conversation(user_message) print(conversation)
Output:
Conversation id: b1f14acb-7d8c-47c8-9fd4-4c587acbd6ad user: My name is George and I am from Greece. Have you ever been to my country?
Let’s see what answer we get.
conversation = chatbot(conversation) print(conversation)
Output:
Conversation id: b1f14acb-7d8c-47c8-9fd4-4c587acbd6ad user: My name is George and I am from Greece. Have you ever been to my country? assistant: I have not, but I would love to go one day. I hear it's beautiful there.
The chatbot responded:
assistant: I have not, but I would love to go one day. I hear it’s beautiful there.
Now, we will ask the chatbot the following question:
What is my name?
print(chatbot(Conversation("What is my name?")))
Output:
Conversation id: 0d716331-6af0-47a5-8c8a-30c40a4af8ee user: What is my name? assistant: I don't know. What do you do for a living? I'm an accountant.
As we can see, the chatbot gave an unrelated response because it did not remember the previous conversation. To include prior conversations in the LLM’s context, we should add a ‘message’ to the chat history. Let’s see how to do it.
conversation.add_message( {"role": "user", "content": """ What is my name? """ }) print(conversation)
Output:
Conversation id: dc38a6d7-41f9-4eb3-adff-a63451d07199 user: My name is George and I'm from Greece. Have you ever been to my country? assistant: I have not, but I would love to go one day. I hear it's beautiful there. user: What is my name?
Now, we can pass the conversation to the chatbot:
conversation = chatbot(conversation) print(conversation)
Output:
Conversation id: dc38a6d7-41f9-4eb3-adff-a63451d07199 user: My name is George and I'm from Greece. Have you ever been to my country? assistant: I have not, but I would love to go one day. I hear it's beautiful there. user: What is my name? assistant: George is a beautiful name. I love Greek food. What is your favorite food?
As we can see, we managed to pass the history to the model and got a proper answer!
References
[1] Deeplearning.ai