Introduction to Conversation Chains
In this video I will show you how to create a chatbot similar to ChatGPT without writing a single line of code using Flowwise AI. This means that you will be able to have back and forth conversations with the chatbot and we will also have a look at storing and retrieving multiple conversations just like ChatGPT.
Let's get started by creating a new chat flow. And let's save our flow and let's give it a name. So in the previous videos we spent a lot of time looking at these LLM chains and we learned that with these LLM chains we could provide a model and a prompt template and these chains are perfect for one time uses where we wanted to generate a recipe or a joke etc. But in this video we'll actually move on to a different type of chain and that is the conversation chain. So let's go to add nodes and let's add this conversation chain to the canvas.
And we can see that this chain is different to the LLM chain in a few ways. It still takes in a chat model as input but we can now see that the prompt template is actually optional and we now have a memory property which is mandatory. And this memory property will be used to store the conversation history. And that is one of the major differences between a standard LLM chain and a conversation chain. The conversation chain is able to remember our conversation history and it is therefore able to recall information.
Building a Chatbot with Buffer Memory
Let's start by adding our model. So under add nodes let's go to chat models and let's add our chat open AI model and then let's connect this model to our chain. Let's add our credentials. I'll simply leave the model name as GPT 3.5 turbo and let's set the temperature to something like 0.7.
Now let's have a look at this memory node. Let's go to add nodes then let's go down to memory and here we have a couple of options to select from. But let's start by adding a buffer memory node and let's add this to the chain as well. Believe it or not that's all we need to build a chatbot in Flowwise. But during this video I am going to show you how we can greatly improve this chatbot.
Let's test this by saving this chat flow. Let's open chat and let's check if this is working. I'll start off by saying hello and we do get our response back. Now let's test out the memory. I'll say something like my name is Leon and now if I ask it what is my name we can see that the chatbot is able to answer this question because of the memory that we assigned to it.
Viewing and Managing Conversations
Let's dive a bit deeper. We can view past conversations with our chatbots by going to settings and then clicking on view messages. Here you are able to filter based on date and source and at the moment we only have one conversation and if we click on it we can view the entire conversation transcript.
This is useful for when we deploy our chatbots to production for instance to applications or websites and here we can view all the conversations that our users have had with our chatbot and we can use this to fine tune and improve our chatbots.
Customizing Personality with a System Message
The prompt template node is optional but it does have a very important use case and we will have a look at it in a second. But if we wanted to prime this model we could simply click on additional parameters and here we can see the system message that is assigned to this model at the moment. We can of course change this to whatever we want and let's say something like "You are a pirate called John".
Let's save this and then let's test out this chat. I'm actually going to clear this previous chat and let's try this again. Let's say hello and we can see exactly how the system message is affecting the behavior of our chatbot. But we can also add a chat prompt template and I will show you one possible use case for why you might want to do that.
Using Chat Prompt Templates for Advanced Control
Let's click on add nodes, let's go to prompts and let's select this chat prompt template. Then let's also connect this template to our chain. Now with the chat prompt template we can do a couple of things. We can enter a system message similar to what we had in this additional parameters and we can then manipulate the human message or simply grab the message from the chat box.
We can of course also click on Lang chain hub and this will give us access to a whole bunch of pre-created templates. So I could just select on model and let's select open AI GPT 3.5 turbo. Then under use case we could select something like chatbots and let's click on search and here we can find a whole bunch of pre-created templates that we could use. But I'm not going to do that here.
Instead let's add a system message like, "your name is hope rhyme every sentence." Then in the human message let's simply add a placeholder for our input and let's click on format prompt values. Then for input let's click on edit and when we click on this field we can simply select the question from the chat box and let's go ahead and save this flow and let's test it.
I'm actually going to clear this previous chat and let's enter something like how are you and now we get a response like I am doing swell thank you for asking hope I can say the same for you in every tasking. So this is indeed rhyming every sentence.
Example: Combining Chains for Dynamic Prompts
Another reason why you might want to use a prompt template is to include prompt chaining in your application. We had a look at prompt chaining in the previous video so I am going to go through this quite quickly but let's say we wanted to include an LLM chain and let's give this chain a model as well.
So under LLMs let's select open AI and let's add it to the canvas. Let's attach our model to the chain. Let's select our credentials. I'll just leave it on GPT 3.5 Turbo instruct and let's also add a prompt by adding a prompt template. Let's attach the prompt template to this chain and for the prompt template I'm simply going to enter select a random holiday only return the name of the holiday.
I'll give this chain a name as well like holiday chain. Let's change the output to output prediction and now we can actually pass the output of this chain into the chat prompt template and we can include that value in this chat prompt template by adding a placeholder by using these curly braces and let's just call it holiday. Let's change this text to something like answer the user's questions in the context of a holiday and then in format prompt values let's add holiday let's edit this value and when we click on this field we can now select the output of this holiday chain.
Let's close this and let's save this flow and now when we chat to this application it should be in the theme of some holiday. What are you celebrating and saying that it's celebrating Independence Day and if we have a look in the terminal we can actually see that Independence Day was generated by the LLM chain. Alright so I'm actually going to remove these nodes but that is one possible use case for why you would want to use prompt templates but I'm actually going to remove this and simply stick with the system message within this additional parameters.
Let's have a look at input moderation. We can use input moderation to block any harmful or unwanted messages from the user.
So let's open add nodes and let's open this moderation node and at the moment there are two possible nodes to select from. Let's start with the basic one and that is the simple prompt moderation node. Let's connect this node to our chain and what we can do with this node is censor certain words or phrases. So if I wanted to block certain phrases I could say something like frogs are ugly. Of course in a real application you would use not safe for work which I can't include on YouTube but let's say this was a not safe for work phrase. Let's save this flow then in the chat window let's type frogs are ugly and we instantly get this message back. We can also change the error message in this block.
Let's remove this node and let's have a look at the other type of moderation node and that is the open AI moderation node. This node is useful when you are using open AI models as open AI themselves have a list of blocked phrases and words. And to use this is very simple simply attach it to the chain then select your open AI credentials and that's all you have to do.
Now if we enter something that violates the open AI policies we will get a rejection back.
Setting Up Long-Term Memory with Upstash Redis
Now lastly let's have a look at long term memory within these applications. At the moment we can only have a single conversation with our bot and if we clear this conversation it's actually completely removed from the database as well. But you might want to do something similar to chatgpt where you can simply select a pass conversation and simply continue with that conversation.
This is really easy to implement and for this we'll actually use a different memory node. So let's delete this buffer memory node then let's click on add nodes and within memory let's select the upstache Redis backed chat memory node and let's connect this to our chain.
So how this is going to work is the conversation history is actually going to be stored against a database and we will assign a unique session ID per conversation. We can then use that ID to retrieve pass conversations. In order to set up this node we need to set up upstache credentials. We can do that by clicking on this arrow then click on create new. We can give our credential a name I'll call mine upstache Redis and now we need to provide an upstache Redis REST token.
In order to get your token go to upstache.com and then click on log in. After logging in ensure that you have Redis selected and then click on create database. Let's give our database a name I'll call mine Flowwise AI select a region and then click on create. Then scroll down to this REST API section and here you will find two buttons for copying the URL and the REST token. Let's start by copying the token and let's paste it into this field and let's click on add.
Now we also need to provide our REST URL. Let's grab that as well by clicking on copy URL and then it's pasted into this field. Now as I mentioned earlier we do have to specify a unique identifier for our conversations. We can do that by clicking on additional parameters and here we can specify a session ID. I'll just call mine chat1 and we can also set a session timeout and this means that the session will automatically be deleted from the database after a certain period of time. You can simply leave this blank to ensure that the conversation never gets deleted. Let's close this pop up.
Testing Multi-Session Persistent Memory
Let's save this flow and let's click on chat. Let's just type something like what is your name and we do get a response. Let's also say something like my name is Leon and let's ask it what is my name and it was able to remember my name but the big difference between this node and the buffer memory node is that this conversation is now being recorded in a persisted database.
So if I go back to upstash and let's click on data browser you will now notice this entry called chat1 and within this we can see the conversation history being recorded. We can also change this to a different session ID so let's start a new conversation called chat2. Let's save this and let's enter something like the passphrase is flow wise is awesome and let's ask it what is the passphrase and it's able to recall that passphrase and if we go back to upstash and refresh this we can now see that second conversation being recorded as well.
So now we can simply swap between the two conversations for instance if I change this back to chat1 and save this we can now ask this what is the passphrase and it actually won't know because the passphrase was part of chat2 but if I ask it what is my name it is able to recall that information because that is something we mentioned as part of this conversation and that my friends is how we build a chatbot using flow wise.