Custom tools make it easy to add custom functionality to our Flowise chatbots. Flowise already offers a wide range of tools out of the box, and we had a look at a few of these in this series, like the Serp API tool which allowed our bot to perform Google searches. But you might want to create your very own tools for truly unique experiences.
Using Flowise's tools feature, we can easily create our very own custom tools. There is quite a bit to cover here, so I'm going to split this video into two parts. First, we will have a look at creating a custom tool which will add items to a to-do list. We will ask the chatbot to add an item to a to-do list, and the bot will automatically add the item to a Google spreadsheet. In this part of the video, you will learn how to call API endpoints like webhooks. In the second part of this video, we will create a custom tool that uses node dependencies. Enough talk, let's get started.
Setting Up the Basic Chat Flow
Open Flowise and create a new chat flow. Let's save this chat flow and give it a name. I'll call mine Custom Tool Demo. Let's go to Nodes, then click on Agents, and let's add the OpenAI Function Agent to the canvas. Then go back to Nodes, then go to Chat Models, and let's add the Chat OpenAI model to the canvas. Let's connect our model to the agent like so. Then go ahead and select your credentials. We can leave the model as GPT-3.5 Turbo, and for the temperature, I'll set a value of 0.7.
Next, let's go ahead and add memory to our canvas. Let's go to Nodes, then go to Memory, and let's simply add the Buffer Memory node to the canvas. And let's connect our memory node to the agent as well. Great. Now let's go ahead and add the Custom Tools node to the canvas. Let's go to Nodes, then go to Tools, and then add the Custom Tool node to the canvas. And let's connect our Custom Tool to the agent as well.
Please take note that you are able to add multiple custom tools to your project. All you have to do is drag another Custom Tool node to the canvas and simply hook this up to the tools input on the agent. But for this demo, we'll simply use a single tool.
We can now create a tool in one of two ways. You can simply click on Create New and that will take you to the tools tab, or we can save our project, click on back, and from the Flowise dashboard, we can click on Tools. And under Tools, we can then click on create. Either approach will take you to this pop-up.
Let's create the tool which will add to-do items to our to-do list. First, we need to give the tool a name. I will call mine add_to_do_tool. Names need to follow a specific naming convention. Typically this involves lowercase characters with underscores separating words. Let's go ahead and add a description for the tool. The description is extremely important as this will tell the agent what the tool does and when to use it. So I will add a description like 'Use this tool when adding an item to the to-do list.'
If you want, you can also provide an icon for this tool. I will skip this step. Let's have a look at the output schema. The output schema lists the properties for the code, in other words, the input parameters that the code expects to receive. Let's add a new property by clicking on Add New. Let's give this property a name like to_do_text. This property name follows the same naming convention with lowercase characters and words separated by underscores. Then for the type, I'll select string. The description is important as this will tell the AI model how this information should be extracted from the conversation. By entering something like 'The to-do item description', the model will try to extract the to-do item's description from the conversation and then pass that into this property. Lastly, we can specify whether this property is required or optional. Because this field is required, I will check this box.
Then scrolling down, we can now go ahead and write the logic for this function. For this, we will write some JavaScript code. Don't worry if you're not too familiar with JavaScript, I will try to keep this as simple as possible. Also note, you can see an example implementation by clicking on 'See Example.' For now, I'm going to remove the sample code and let's walk through this step by step. We can add all sorts of logic to this function, but at the end of processing, we simply need to return a value back to the chatbot. We can return a value by typing the return keyword and in single quotes, we can return a string value, for example, 'Item added successfully'. JavaScript statements need to end with a semicolon, like so. Let's go ahead and save this function by clicking on Add.
Initial Test and Agent Action Analysis
Let's go back to our chat flow. In the Custom Tool node, go ahead and click on the dropdown, then select the add_to_do_tool. Let's save our chat flow. Let's open up our chat. First, let's ensure that the chatbot is working. I'll just say, 'Hello'. And yes, we are getting a response. Let's try to add an item to our to-do by typing something like 'add buy milk to the to-do list'. And let's run this.
And we are getting a response back from the bot saying that 'buy milk was added to the to-do list'. And take note that the bot was able to extract 'buy milk' from the prompt as the to-do text. Let's also have a look at the console. Initially, we can see the first prompt, which was just 'Hello', and we can then see the response from the bot saying, 'Hi there, how can I assist you today?'
Then we sent the prompt, 'Add buy milk to the to-do list'. And then scrolling down, we can see a step that is called Agent Action. And in Agent Action, we can see that a tool was used, and specifically the add_to_do_tool. And the input into this tool was the to_do property with the value 'buy milk'. This indicates that our custom tool was selected by the agent and the property was successfully populated. And in scrolling down towards the end of that action, we can see that the response that came back from the tool was 'item added successfully'. Fantastic.
Let's make another change to our tool. Let's go back to the Flowise dashboard. Let's click on Tools, then let's click on add_to_do_tool. Let's go down to the code and this time let's try a negative test. Let's replace this text with something that indicates that the item could not be added, something like, 'Could not add item to the to-do list.' And let's save this. Let's go back to our chat flow like so and let's run the test again. I'll just pass in the same prompt again like so. But we can see an issue here. We are getting the same response back saying that 'buy milk was added to the to-do list successfully.' And if we have a look at the response from the latest call, we can see the text 'item added successfully' coming back again.
Please take note of this because this is something that tripped me up initially. It's recommended to restart Flowise after making changes to the tools. So I'm just going to stop Flowise, I'll clear the console, and let's start Flowise again. Let's refresh this page and in the chat, let's ask it again to add 'buy milk' to the to-do list. And this time we are seeing a negative response. So please ensure to restart Flowise whenever you make changes to the tool.
Let's continue with our logic to add these to-do items to a Google spreadsheet. First, let's create our Google spreadsheet. After logging into Google spreadsheets, create a new spreadsheet and then give it a name. I'll just call mine 'To-do List' and let's go ahead and create the title for this column like so. Let's also go ahead and make this title bold. When asking our chatbot to add items to this list, we will see them added to this column. And that is all we have to do for the spreadsheet.
Now, in order to make this process as simple as possible, we will be making use of a service called make.com. make.com is similar to services like Zapier and makes it easy for chatbots to automate business processes. Make provides access to hundreds of third-party applications and complex flows. For this tutorial, we will simply need the ability to add entries to a Google sheet. Go over to make.com and create your free account. After logging in, we need to create a new scenario. We can do that by clicking on Scenarios and then we can go ahead and click on Create New Scenario. But to speed things up, we'll actually make use of templates. So click on Templates. Within the templates page, we can find all sorts of integration templates that we can use. What we are interested in is a webhook that we can call to add items to Google spreadsheets. So in the search box type in 'webhooks'. In the list of results, look for 'webhooks to Google Sheets integration'.
You will then be presented with this pop-up. Let's click on Add. We can then give our webhook a name. I will call mine 'Flowise AI' and then click on Save. You will now see an API endpoint that we need to call. And at the moment make.com is waiting for us to perform an initial call to this endpoint. First, let's copy this endpoint. For the initial call, we will use Postman. In Postman, let's open up a new tab and let's paste in that endpoint. We also need to change the method from GET to POST. Click on the Body tab, select raw, and ensure that the type is set to JSON. The JSON structure starts by using curly braces. Then for the property value, I will simply call it toDoText, colon, and then any sample value. I'll just enter 'text'. Let's go ahead and run this by clicking Send. In the response, I'm getting 'Accepted' and if we go back to make.com, we get this message saying 'Successfully determined'.
Let's click on Continue. In the second step, we need to select the Google spreadsheet which we want to use. Initially, you might have to create a connection to your Google account, so just click on Add, but I'll skip that step as I've already done that. Then I can go ahead and select the spreadsheet that I'd like to use. So in the dropdown, I simply search for my to-do spreadsheet. Then within the spreadsheet, we can select a specific sheet that we'd like to use. I'll just select Sheet1. Now we need to map the values from the API endpoint to the column in the spreadsheet. Our spreadsheet only has one column called 'To-do Description'. We can then click on the input for this field, and this will show us the property value that we passed in through the webhook. We can just drag and drop this value into the input field for the description. If you created more than one column in your spreadsheet, you can simply map the values from your Postman call to these columns. After doing this mapping, we can simply click on Continue and that is it.
Let's go ahead and rename this scenario to 'Flowise Demo'. Then also let's toggle this switch from Off to On. Let's save this. We can actually check that this is working by going back to Postman, and let's run this again. And if we go back to our Google Sheet, we can see that that value was added successfully. Great. Now that our Google Sheet and our Make scenario is working, we can finalize the code in Flowise.
Implementing the Final JavaScript Code
Let's go back to Flowise. To keep things neat, I'm going to clear this chat. Then let's go back to the dashboard, let's open up Tools, and let's select our add_to_do_tool. Let's scroll down to the code. To simplify things, I'm actually going to remove this code and let's click on See Example, because we will be doing something very similar to this. I'll clean this up by deleting these comments and we will replace the URL with the URL of our make.com webhook. You can copy that endpoint from make.com, but I'll just grab it from Postman. And let's replace this value with the webhook endpoint. Then for the options, we will change the method from GET to POST. We will leave the headers as content-type: application/json.
We also do not have to change any of this logic in the try-catch block, but we do have to pass a body property as well. So after the closing bracket for the headers, let's type comma. Then on the next line we will add a property called body, colon, then JSON.stringify(), and within brackets we'll add an opening and closing curly braces. And within our curly braces we are effectively passing this value. So I'll just copy this line over here. I will then paste it within these curly braces, and instead of hard-coding text, we will rather paste in the property which we received over here, the property called to_do_text. And I'll add it over here. Now, to tell this code that this is a variable or a property coming in from the function, we simply need to add a dollar sign at the start of this property. And that is it.
Let's go ahead and save this tool. And just a reminder, after making changes to the tools, we need to restart Flowise. So in the console, I'll press Ctrl+C and I'll clear the console just to keep things neat, and I'll start Flowise.
Now let's go back to our chat flow and let's test this.
Final Test and Conversational Example
Let's run that same instruction again, 'Add buy milk to the to-do list'. And we are getting a response saying 'I've added buy milk to the to-do list'. And let's have a look at our Google sheet. And we can see that that item was successfully added to our spreadsheet. Let's try another example. And this time I'll leave the spreadsheet open on the right-hand side of the screen.
Let's try a simply more complex scenario. Let's ask the bot, 'What will be a good habit to learn this year?' Let's run this. And the bot is coming back with a response saying that practicing mindfulness and meditation would be a good skill to learn this year. So let's ask the bot to add this to my to-do list. And after a few seconds, we can see the response saying, 'I've added practice mindfulness or meditation to your to-do list.' and in the spreadsheet, this text was added successfully.
Advanced Use Cases and Conclusion
Creating custom tools is a lot of fun, and as an added bonus, you can create custom tools that call other Flowise chat flows, since each chat flow comes with its own API endpoint. As a reminder, you can get those endpoints by clicking on API Endpoint and then going to JavaScript. You can, in theory, simply copy this code and then add this to a custom tool. That way your chat flows can call one another using custom tools. If you enjoyed this video, then please consider subscribing to my channel. I'll see you in the next one. Bye bye.