Introduction to Prompt Chaining
Let's have a look at prompt chaining. Prompt chaining allows us to combine several chains and models to produce an output for our application. This is one of the core benefits of using LangChain and Flowise, and it makes it possible to build advanced AI-driven applications.
Let's have a look at a simple example where we combine three chains in our application. First, go back to the Flowise dashboard and create a new chat flow. Go ahead and save this chat flow and let's call it "prompt chain". Before we continue, let me explain what we'll be building in this application. This might be a simple application, but it does demonstrate the power and flexibility that you have when combining different chains and AI models.
This application will contain three chains. In the first chain, we will ask the AI to give us an ingredient for a recipe that matches the name of a public holiday which we as the user will provide to the application. In the second chain, we will ask our model to generate a unique recipe based on the public holiday and the main ingredient provided by the first model, and this chain needs to produce a recipe with step-by-step instructions and an ingredient list. For the third chain, we will prompt the AI to behave like a food critic which will analyze the public holiday and the recipe generated by the previous chain and then produce a review. Let's go through this step by step.
Building the First Chain: Ingredient Generation
Let's start with the first chain. This chain will be responsible with coming up with the ingredient that matches the public holiday. First, let's add our chain, and for this, I'll use a simple LLM chain. Of course, you are more than welcome to make use of any of these other chains in your application, but let's keep things simple for this demo.
Our chain also needs an LLM. I will simply use the OpenAI LLM and then connect that to our chain. Let's also add our prompt template. So from nodes, I'll go down to prompts and then grab the prompt template node and let's connect this to the chain. Let's also add in our API key. And for the prompt template, let's enter something like: "Assistant which will respond with a suitable main ingredient for a recipe based on a public holiday provided by the user." And we'll set public holiday equal to a prompt value called "holiday". And then in the prompt value list, we can give holiday a value. So we can do that by clicking on the edit button, and for the value, we will select the value provided by the user in the chat box, and we can close this popup.
Let's go ahead and test this by saving the chat flow, and let's enter a public holiday like Halloween. And we get this main ingredient coming back as pumpkins. Great.
Adding the Second Chain: Recipe Creation
We now want to add the second step to this chain. For the second chain, we want the model to generate a recipe that is related to the theme and uses the main ingredient generated by this chain. Let's close the chat window and let's give this chain a name like "Ingredient Chain".
Let's add our second chain. Under nodes, I'll go to chains, I'll grab another LLM chain and add that to the canvas. Let's also add an llm. Under nodes, I'll just go to LLMs and I'll grab the OpenAI node. Again, feel free to use any chain and LLM of your choice. Let's connect the LLM to the chain and let's add our API key. For the prompt, let's also add a prompt template. So under nodes, I'll go to prompts and let's grab prompt template and add that to the canvas. And let's connect our prompt template to the chain.
Let's also go ahead and enter the template for this prompt, something like: "You are an experienced Chef that creates unique food recipes based on a public holiday and a main ingredient that matches that holiday." And we'll also add "public holiday" to this prompt as well as the prompt value called "holiday". And just below this, we'll set the main ingredient to a prompt value of "ingredient".
It's very important to note that when doing prompt chaining, the first variable in the prompt template will always be assumed to be the value passed in by the user, like the public holiday. Let's go ahead and set these prompt values. For holiday, I'll click on edit, and this is the value coming in from the user. For the ingredient, we also need to select the value, but we do not want this to be the value provided by the user. But instead, this value needs to be passed in from the previous chain. So let's close this pop-up and let me show you how to create this prompt chain.
You will notice that on the prompt template node, to the left, we've got a little node that we can attach here, and this will be the input from the previous chain. This means that we need to connect the output from this LLM chain to this prompt template. We can do that by clicking on this drop-down and changing the output from LLM chain to "Output prediction". So instead of the result being written to the chat box, the result will instead be passed along to another node. So now we can simply drag and connect this LLM chain to the prompt template. Now let's click on format prompt values again and let's edit the value of ingredient. Now when we click on this box, we can see another value showing up in this list, and this is the output prediction coming from the ingredient chain. We can now close this box. Also, let's give this chain a name, something like "Chef Chain", and let's save this.
Debugging Prompt Chains
Let's go ahead and test this as well. I'll clear the chat history and let's type in a holiday like Easter. And we can see a recipe being generated, and this recipe is for an Easter roasted lamb with garlic and rosemary. The result that we see in the chat will always be the result coming from the last chain in our application, which at the moment is this chef chain over here. But how do we know if this is correct? In other words, how do we know what the main ingredient was that was generated by the ingredient chain?
For that, we can have a look at debugging. When starting up Flowise, you typically run yarn start, but in order to add debugging, you can simply add a space --debug=true, and that will now start Flowise in debugging mode. So let's have a look at what we get in the debugger when running this exact same chat. Let's just save our Flowise project and let's run this chat. I'm going to clear the chat history as well so that we have a clean start. To make things even more visible, I'm going to put the debugger to the side of our application. Now let's provide a public holiday name, let's do Halloween again.
Now, have a look at the output in the chat. We can see the recipe being generated, and on the right-hand side, we see a lot of information being written to the console. What we can see at the top is that we are calling the first chain with a value of Halloween. This is because in our prompt value list, we set the user's input to a variable called holiday. We can see this by going to the first prompt template and clicking on format prompt values, and this is where it's getting that name from. We set holiday equal to the user's input. Then we can see the output of the prompt template was formatted into this: "You are an AI assistant which will respond with a suitable main ingredient for the recipe based on a public holiday provided by the user," followed by public holiday and the value of that variable. So this is a great way to ensure that our variables are indeed pulling through in the prompt.
Then when we scroll down to Output prediction, we can see the output as being "main ingredient Pumpkins". This output prediction is the output being produced by this chain. Now we can see the start of the second chain, and that's this chain over here, the chef chain. We can also see the value of the two variables being passed to this chain. Just as a reminder, we can see those variables by clicking on format prompt values, and we have a variable for Holiday, which is equal to the user's input, and the variable called ingredient, which is equal to the output prediction from the previous chain. And in the debugger, we can see that value as being "main ingredient pumpkins". We can also see the final result of the prompt and we can see that for public holiday the value is Halloween, and Main Ingredient is indeed set to Pumpkins. So that confirms that our prompt template is correct.
But let me show you a little gotcha. Let's say that we didn't specify the value of holiday and we only specify the value of Main Ingredient. And this is just to show you an issue that you most likely will run into while building your applications. Let's go ahead and delete public holiday for now, we will add it back in a minute. Also, in the prompt values box, let's go ahead and delete holiday. So at the moment, we are not assigning the user's input to any variables in this second chain.
If we go ahead and run this, you will notice an issue. Let's provide a public holiday again, something like Easter. The chat will go ahead and generate some recipe, but if we look at the debugger, we can see something funny. First, we can see the first chain running with the input of holiday, and this is coming from the user. We can then see the output prediction from this chain, which says that a suitable main ingredient is lamb. Then we call the second chain in our application, but we can see an issue. We only have one variable called ingredient, and the value of that ingredient is set to Easter, which is not correct. If we have a look at the prompt values, we can see that ingredient is set to the output prediction of the previous chain, so we would expect this value to be equal to this output prediction.
This is a small gotcha that can definitely trip you up if you are not aware of this. The basic rule here is that the value passed in from the chat by the user, like Easter, must always be passed into the prompt template. The first variable in the prompt template will always be assumed to be the input from the user. Because we only have one variable in this prompt template, the value that we assign in the prompt template values is ignored, and it is assumed that this value is the input from the user.
So my advice to you is to always include the input from the user in each of your chains and simply add it somewhere in the prompt template. Let's add that value back in, and I'll set holiday equals to Holiday. And let's add it to the prompt values as well as holiday, and we'll set holiday equal to the input from the user. Let's save this and let's test it out again. For the holiday, I'll enter Christmas. And let's check if this is working. Scoll down in the terminal. So we can see that the first chain is being called with holiday equals to Christmas, and in the output prediction, we get the main ingredient of turkey.
We can now see the second chain being called with the ingredient set as turkey and holiday set as Christmas. So hopefully you can see that as long as we assign the user's input to some variable, everything else seems to work. And if you scroll down in the console, we can see the output being generated from the second chain and we can get our final result, and this is the result that is finally written to the chat box. Hopefully, as you can see, the debugger is really helpful for having a view of the data being passed between chains.
Adding the Third Chain: The Food Critic
Let's get back to our application. I'll go ahead and clear this chat history. And let's now add a third chain to our application. I'll click on Add Nodes, I'll select a chain like LLM Chain again. Let's assign our model by going to LLMs and I'll select OpenAI, and let's connect this to our chain. And let's also add our OpenAI API key. Let's also go ahead and add our prompt template by clicking on Add Nodes, Prompts, and Prompt Template, and let's connect this template to our chain.
Let's enter our prompt template. We can do something like: "You are a food critic that will review a food recipe based on a public holiday." Let's also provide these variables to the prompt template, like holiday is equal to a variable called "holiday", and recipe is equal to a variable called "recipe". We can now assign values to these variables by clicking on format prompt values. For holiday, I will assign the input from the user. For recipe, we want to pass in the value from the previous chain. Let's please close this pop-up box. Let's change the output from the previous chain to "Output prediction" and let's connect this chain to the prompt template. Let's click on format prompt values again. For the recipe, let's now change it to "Chef Chain", and let's close this pop-up. I will also go ahead and give this chain a name like "Critic Chain". Let's save this.
And let's also have a look at the debugger while we run this. In the chat window, let's provide our public holiday as Easter again and let's press enter. And in the chat we can see the review coming through as well. Something in the lines of, "This Easter roasted lamb looks and smells divine. The lamb was cooked to perfection with a juicy interior and crispy exterior." Let's have a look at the debugger. We can see the first chain being called with the holiday Easter. And if we look at the output prediction, the main ingredient is roasted ham, lamb, and asparagus. Then the second chain was called with the ingredients as well as the holiday. And for its output prediction, we can see the recipe with the ingredients and cooking instructions. Then for the third chain, we can see that holiday was indeed passed in as Easter and the full recipe was passed in from the previous chain as well. And we can also see the entire recipe being added to the prompt, and this then gives us the final result. Note that sometimes the formatting might look a bit strange, and that is very much related to the model that you are using.
Swapping Models and Conclusion
I do want to show you that it is possible to swap out these OpenAI models. So let's say instead of using this model, let's delete it and instead replace it with one of the chat models. So I'll just go to Chat Models and select ChatOpenAI, and let's drop that into this canvas and hook it up to the chain like so. Let's also place in our API key and save this.
Let's run the chat and let's give it a different holiday name, something like Halloween again. Let's press enter, and now you can see that the quality of the output has drastically increased due to the advanced nature of the GPT-3 model. So depending on the application that you are building, different models might be more ideal for specific steps in the chain. So go ahead and experiment.
If you like this video, please consider liking the video and subscribing to my channel. I'll see you in the next one. Bye bye.