Introduction to Flowise ChatFlows
Hello and welcome back. Now that we have Flowise setup and had a look at the user interface, we can now create our very first chat flow. In the Flowise dashboard, we can create a new chat flow by clicking on 'Add New'. You will now see a blank canvas. Using the plus button, you can now add nodes to the canvas.
In this list, we can see all the available nodes that can be added to our canvas, of which there are quite a few. We will have a look at some of these nodes throughout this series. But if you want more information on what each of these nodes do, I suggest using the LangChain documentation as reference. Flowise uses LangChain behind the scenes, and the LangChain documentation can be a very useful resource for making sense of the Flowise nodes.
Let's start by creating a very simple interaction with an AI model. In this example, we will create an application whereby we will ask the AI to generate a dad joke based on a subject that we passed to it. In order to get any kind of response from our application, we need to add either a chain node or an agent node. We will delve into the differences in this video.
Building a Simple LLM Chain
For a simple interaction with a model, we can use chains. Chains are the most basic node for producing some sort of output from our application. You will notice that we have access to quite a few different chains, for instance, the LLM chain, which is a basic interaction with an AI model. Then we also have more advanced chains like the Conversation Chain, which can be used for a back and forth conversation with the model whereby the model needs to remember past conversations. But because this is our first chat flow, let's use the LLM chain.
We can drag and drop this chain onto the canvas. This node will tell us what other nodes are required in order for this node to function. For an LLM chain, we need to provide a language model and a prompt template. Let's add a large language model to our project. So we can do that by clicking on this add button, then we can guide to LLMs and within LLMs we have access to a few models including OpenAI, Hugging Face, and the Cohere models.
OpenAI is really easy to integrate, and since we've already generated our OpenAI API key in the previous video, we will continue to use OpenAI. Let's drag and drop the OpenAI node onto the canvas.
Configuring the LLM and Prompt Nodes
We now need to add our OpenAI API key to this node. So we can paste our API key in this input box. Optionally, we can change the model that we'd like to use from this drop-down list, and we can also set the temperature of our model: 0 being factual and one being creative. So I will use a number in between, like 0.7.
Under additional parameters, we can set other parameters like the maximum amount of tokens that we are willing to spend for each API call. We can now connect the OpenAI node to the chain by dragging a link from this node to the Language Model input in the chain. Now we need to add a prompt template. Prompt templates are used to capture the input from the user and then format the text in a way that will influence the AI's response.
We can add a prompt template by clicking on Add, then under prompts, we can select the Prompt Template node and drag that onto our canvas. We can now also connect this prompt template node to the LLM chain. In the prompt template, we can now provide a template string. For example: create a funny dad joke about weddings. We will leave the format prompt values box empty for now. So what this LLM chain node will do, it will take the input from the prompt template and then pass it to our OpenAI model.
Testing and Using Prompt Variables
We can test this out by saving our chat flow. We can give it a name like 'Dad Jokes' and we can save this. We can then go ahead and test our application by clicking on the chat window. Then within the chat, we can type any text. I will just type 'test' and press enter. And within less than a second, we got this response back from the AI saying, 'What did the groom say when he saw his bride walking down the aisle? Wow, she really took the plunge.'
So at the moment, this chain is simply grabbing the text from the template and then passing it to the model. And at the moment, it is ignoring the message that we are sending to the model. So we need to get the user message into this template somehow. And how we can do that is, let's say we want to make the subject of the joke a variable input from the user. So to make this variable, we can remove this text and then add a curly bracket followed by any text. I will call it 'subject' and we can close it off with another curly bracket.
Let's go ahead and save this and let's run the chat again. And let's enter a different subject like 'dogs' and let's press enter. And now we can see a message being returned that takes the user input into account.
Let's now have a look at prompt values. Prompt values allows us to add additional variables to our template text. As an example, let's replace 'funny' with a variable and we'll call it joke_type. We can now set a value for joke_type in the prompt values box. We can set our variables like the example showed in this box. We need to add an opening curly bracket and then end this off with a closing curly bracket. In quotes, we can now type the name of the variable, like joke_type. We can close that off with quotes, colon, and then the value of this variable, which was 'funny', and we can close that off in quotes as well. We can save this and then run the chat again. And we'll type the subject as 'cats'. And as you can see, it still works. We can now easily change the type of joke by replacing the value of this variable, and let's make it 'sad' and let's try again. We'll pass in 'cats' as the input, and this time we are getting a joke that's a bit more on the sad side.
You might be wondering how Flowise knows that the input from the user should be mapped to the 'subject' variable. And that's quite easy. Flowise will first have a look at the prompt values, and any variable in the template that is not defined in the prompt values list will be assumed to be the user input. Also, we can call this variable whatever we want. It doesn't have to be 'subject', we can change it to something like 'text', and this will still work as you can see. If we want we can explicitly say to the application the 'text' should be mapped to the user input by adding a comma at the end of this variable. Then in quotes, we will specify the variable of 'text' and set that equal to the user input. There is a specific format for pointing at the user input, and we can see that by expanding this box. And on the right hand side, we can see all the variables that are available in this session. And if we click on this, it will automatically add the user's input to the prompt values. And internally, Flowise is referring to the user input as 'question' with double curly brackets. I will just remove this from here and then add it to the 'text' variable. Also very important is that this user input must also be wrapped in quotes. Let's save our chat flow and let's test it. I'll make the subject 'clowns' and as you can see we are getting a response from the AI.
Introducing Chat Models and System Messages
These simple LLM chains are useful for scenarios where you need a simple, once-off interaction with your model, for instance, asking it to generate jokes or generate titles for blogs or generate articles, etc.
Let's move on to chat models. Let's go back to our dashboard. Let's create a new chat flow. Now let's talk about chat models. Chat models are way more sophisticated than the standard LLM models, and they allow for additional features. Let's start by adding a chain to our project. And to demonstrate the basic functionality of a chat model, let's use a simple LLM chain for now. We can drag this onto the canvas. And for the language model, we will click on 'Add', but instead of using LLMs, we will look at chat models instead.
Within chat models, we can grab the ChatOpenAI node. For this node, we need to paste in the OpenAI API key, and under model name, you will now notice that we have access to different GPT models, including GPT-3.5 Turbo, which powers ChatGPT, as well as the newer and more sophisticated GPT-4. Let's leave this model on GPT-3.5 Turbo, and for the temperature, I will change it to 0.7. And we can connect this LLM to our chain. Then for the prompt, we can click on 'Add node' and we can then open up prompts. But this time, instead of using the standard prompt template, I will now use the Chat Prompt Template, and we can connect this to our chain.
The Chat Prompt Template looks very similar to the normal prompt template, but we now have access to a system message as well as the human message, which is effectively the text being passed from the chat. And as before, we can set prompt values as well. For the human message, I will simply call it 'text'. You can call it anything you want. Then in the system message, we can now prime our model. Priming is a way to tell the AI how it needs to behave and what its role is. For fun, let's do the following. We will say, 'You are a salty old pirate looking for treasure.' Let's go ahead and save this chat flow. We can call this 'Chat Model Demo' and let's run this by clicking on chat. And in the message, I'll just type 'hello', and in the response, we can see that the AI is now responding as if it's a pirate, and that was because of the system message that we used to prime the model. This can make for some very fun role-playing applications.
Creating a Conversational Chatbot with Memory
Let's now move on to another use case for using chat models. I'm going to remove the chat model template as well as the LLM chain. Let's click on node, then within chains, instead of using LLM chain, let's use the Conversation Chain. And let's spend a minute talking about what we're trying to do here. The benefit of using chat models is the ability of the model to remember past messages in the conversation, and it will remember details of the conversation based on the chat history. And to make this all work, we will use a conversation chain. The conversation chain takes an LLM as input, but in order for the chain to remember the chat history and context, we also need to specify memory.
So to create a memory node, we will click on 'Add nodes', then we will open 'Memory', and from memory, we will grab 'Buffer Memory'. You can leave the default values in these fields as this is a bit technical and it's not necessary to change these values. All we have to do is connect our Buffer Memory node to our chain node. Let's go ahead and save these changes. Then let's open up our chat window. I'm going to clear the previous chat and then back in the chat window, let's say 'hello' just to test this, and we are getting a response back.
So let's now test the memory of this chatbot. First, I will say, 'My name is Leon.' We can now test if the memory is working by asking the AI what our name is. 'What is my name?' And because of the Conversation Chain, OpenAI was able to pick up from the previous conversations and therefore it remembered that my name is Leon. We now have a ChatGPT-like chatbot which we can ask questions and it will remember context from our previous chat messages. As with ChatGPT, we can ask it coding questions as well, and our bot will respond with code snippets and more, just like ChatGPT.
Next, let's chat about agents. First, I want to talk about the shortcomings of using LLMs and what agents do to solve these issues. Large language models can only respond using the data that they were trained on. If you had to ask OpenAI about information that is recent, for instance, 'What is Flowise AI?', we get a response saying that it doesn't know. This is because the GPT models were trained on data up to 2021. Additionally, the GPT models are notoriously bad at doing math. We can solve these issues by introducing agents to our flows.
Agents will first send the prompt to the model and then determine the accuracy of the result being returned. If the model does not know the answer or has a very low confidence level in the answer that it provided, agents can assign tools to the AI to get more accurate results. Let's look at this example where I am the agent and you are the model. Imagine if I ask you a question like, 'What is Flowise AI?' You might have an idea, but for the most part, you are uncertain of the answer, or you might even take a guess with a low confidence level in your answer. Being the agent, I can now provide you with a tool to assist you in answering the question. For instance, I could hand you a phone with internet access, and you would be able to find this information with no problem. The same thing with math. It might be difficult to solve certain math equations by yourself, but if I gave you a calculator, it becomes so much easier. So enough talk, let's see this in action.
Let's go back to the dashboard and let's create a new chat flow. Let's save this and call it 'Agent Demo'. In our node list, instead of adding a chain, we will now add an agent, and more specifically, let's use the Conversational Agent. This node takes in the language model as input as well as memory. This is in order for the model to remember our chat history, but it also takes in a list of allowed tools, for instance, internet access and a calculator. Let's start off with adding our language model. So in nodes, go to chat models and let's add the ChatOpenAI node. Let's paste in our API key. We will leave the model as GPT 3.5 Turbo, and for the temperature, I will change it to 0.7. And let's connect this node to the agent.
Let's also add buffer memory by clicking on nodes. Let's now add a memory node by clicking on 'Add nodes', then let's go down to 'Memory' and let's drag in the Buffer Memory node. And let's connect our buffer memory now to our agent. And now we can assign a list of tools that should be made available to our agent. We can do this by clicking on 'Add node'. Then within this list, go down to 'Tools'. And from 'Tools', we can see a list of all the available tools supported by LangChain and Flowise. In our example, we want access to the calculator tool as well as SerpAPI. SerpAPI will give our model access to Google search. So let's first add the SerpAPI node to our canvas as well as the Calculator node. I'll just grab Calculator and drop it in as well.
We can now connect these tools to our agent by dragging the node and connecting it to the 'Allowed Tools' parameter. We can do the same thing for SerpAPI. We now have our model set up as well as the buffer memory for the chat history, and we've assigned two tools to our agent. So if we ask a question that OpenAI cannot answer accurately, the agent will assign a suitable tool to the model for getting more accurate results. For SerpAPI, we do need to provide a SerpAPI API key. And for the calculator, we don't have to configure anything. Let's get our SerpAPI key. To do this, go to serpapi.com and create a new account. After signing in, go to your account, then click on 'API Key', and on this page, you can simply copy this key and then paste it into the SerpAPI key field.
Testing the Agent's Advanced Capabilities
We can now save this chat flow and then click on 'chat' and let's test this out. In the previous example, we saw that OpenAI couldn't tell us what Flowise AI is. So let's try it again by typing, 'What is Flowise AI?' and send this. And this time, we did get an accurate answer back from the model.
Let's try another example, and this time, this example will make use of our buffer memory. Let's clear this chat. Then let's type the following: 'Who is Olivia Wilde's boyfriend?' Our application used SerpAPI to find the name of Olivia Wilde's boyfriend. Let's ask a follow-up question. 'How old is he?' And in this message, we are not specifying Jason's name, but what we want to test is that OpenAI will assume that we are asking about Jason since this is the name in the chat history. Let's send this. Let's rephrase the question a bit. Let's rephrase the question: 'What is his age?' And this time, we did get Jason's age. This would not have been possible without giving our model access to the internet. Let's also try out the calculator. 'What is his age to the power of 0.23?' And as you can see, the calculator was used to calculate this value.
Conclusion and What's Next
We have now had a look at how we can create simple LLM chatbots as well as ChatGPT-like chatbots that remember context and can access tools to perform advanced features. In the next video, we will have a look at how we can upload files like PDF documents and text files that contain information about our business, and then we can get our chatbot to answer questions contained in those documents. Also, if you want me to cover any topics on LangChain or Flowise, then please leave your suggestions in the comments below. I'll see you in the next one. Bye bye.