How To Make Custom Tools in n8n to Automate ANYTHING with AI
25.63k views4131 WordsCopy TextShare
Mike Powers
Get n8n for FREE here!
https://n8n.partnerlinks.io/6frxnvjudp7l
Workflows coming soon!
https://mike...
Video Transcript:
today we're going to go over how you can make custom functions and tools inside of n to make really super customized workflows and automations with AI that will blow your mind these custom tools can be used to pretty much do anything you want inside of an AI chat bot like send emails schedule meetings book appointments to help make your life easier if you haven't been following my channel n is quickly becoming one of my favorite tools to automate all kinds of stuff with you can hook it up to any API you can think of but we're specifically going to be looking at the AI agent feature today which will allow us to build our custom tools and functions do some really cool capabilities if you want to work with me in any kind of AI automations project make sure to book a free time below down in the description but anyway let's get on with n and build some tools so I'm over on n and I'm currently running this on the free version of n that is self-hosted if you don't know how to get Nan running for free on your own computer you can check out my video somewhere up here where I go through step by step of how you can install it of course if you don't want to install on your computer you can always use the paid version I'll have a link to where you can get that down description below but from here we're going to be checking out this kind of workflow that I have on the screen right now and this is taking advantage of the AI agent node and from here all we have to do is come to the plus icon here and click on Advanced Ai and we're going to be using the AI agent right here it's a very simple AI agent has a couple different things we'll go over there'll be a chat trigger already attached to it on your first goahead you can only have one of these in your workflow at a time so that's why it's not connected there but once it is the AI agent will then take the Json chat input from our chat session if I make a test chat tell me a joke you'll notice we get an error on the first run conversational agent requires a chat model in order to fix this we need to actually include a model inside of our AI agent so we need to drag this out right here we can pick from one of the AI models listed on the right here I'm going to use the open AI chat model because I already have this set up I'll just drag it over to my open AI chat model we can come into here and put inside our open API key you can get on over on the open AI website and then also pick your preferred chat model here sticking with 3. 5 turbo then we can come into here ask it to tell me a joke one more time and then hopefully look at that sure here's a joke for you why could have leopard play hide-and seek because he was always spotted wow isn't that fun we've got our AI chatbot working inside of N and the cool part about n is we can actually take this input and this output we get from our chatbot and use it in whatever kind of other automation we want so you can take this put it in an Excel spreadsheet or maybe send it to a different kind of service or a CRM or any kind of different tool you can use these inputs here and then have them go to whatever kind of you know Excel or sheets or literally anything you want but today we're going to be focusing on making tools and functions which these will unlock some really cool capabilities before we get into it though I want to mentioned we do have this memory feature here part of our AI agent you can drag this out and you can add a simple memory to your chatbot as well you can stick with the window buff for memory this is the one that I would usually use also the output parser is another tool that comes with the AI agent you want your outputs to be formatted in a specific way this is how you could do so here for our purposes we're not really going to be needing this and now we come to the tools option right here and this is where we have a lot of different things that we can do to add some really cool functionality to our AI agent first be a calculator AI can't do math it's actually because they're all trained on text not trained on math so having a calculator tool just makes it easier for the AI to do arithmetic then there's custom python or JavaScript tools search apis Wikipedia Wolfram Alpha or the custom workflow tool which we're going to be checking out today we have the calculator tool this one has been kind of Hit or Miss for me but if we just say what is we'll type in a number times type in another number and let's see if it gets it right the number it got was 271 666 well the right answer is 271 8493 okay so that did not work but if I were to add the calculator tool to the mix here look at that now when I copy The Prompt and paste it back in here look at that we get the correct number 271 849 38 we know that it used the calculator tool when it uses this little check mark here and if we come into the agent you'll see it even gives us our correct output here indicating that we got the correct response from our calculator tool isn't that handy but now we have some other tools here first one I want to check out is the random color tool if I were to come into our chot and just say give me a random color it probably will still give us a color one of the many random colors you might get is indigo but what if we want to actually make this a specific set of random colors this is just based off a regular chat gbt queer that you would call inside of a Champa we use this random color tool here for instance this will actually give us only a random color color by the colors we Define in our colors list here so if I actually got rid of some of these colors here I'll take out all of them besides red or green will actually only call from these different colors and how we can set this up is we basically have our name here which this will be kind of what the AI agent uses to make a specific call if it thinks it needs to use a function in order to fulfill a user's request so this name and then this description kind of go hand in hand in determining whether or not the AI agent gets called to do something with a tool right so by naming this my color selector it's telling the AI agent if the user wants to select a color this is the tool to use and then we describe it further saying call this tool to get a random color the input should be a string with comma separated names of each colors to exclude so this description here is basically telling what happens when this tool gets called and then it goes through and it runs this JavaScript and it will return a color from the list of colors here if I were to come into here give me a random color we get green right maybe I say it again we're only going to get the colors that were in that list so we have red here if I say it again we have green again right so now we're only getting the colors from that tool and you'll notice that we're calling the tool based off this little check mark here awesome now we can even do things like getting the current time so chat gbt doesn't actually know what time it is or any AI model for that matter and if we were to come into here and say what time is it it would have no clue but if we have our get time function here this is a simp Le JavaScript function which is just called get current time call this tool to get the current time and we have a simple JavaScript function here I literally made this in chat gbt all I did was say make me a simple JavaScript function to return the local time if we were to hook up our tool to the current time and then we'll say what time is it it is 12156 a. m.
it is very late but now we have time capabilities inside of our chat but and we can make these functions as complex as we want to do individual tasks right and just a quick tip for when you're making these functions especially when you're trying to make them in chat gbt you wanted to return a variable based off one function right so you really want to have just one function made and then it returns one variable so you can see this const current time calls this get current time function which runs this this returns a Time string which then gets returned as the current time the current time will then be sent over to the bot to then display to us before I get into it you can use things like the search API Wikipedia wallframe Alpha they're very simple but what we're really going to be checking out is the custom n workflows and this is when you can get kind of workflow Inception inside of your Bot so if you want your chat bot to basically run an entire other automated workflow on autopilot this is how you would do it so you can take these tools right and you can make a custom n workflow and these are basically like the last JavaScript or python functions except instead of calling a JavaScript code you would call an entirely new workflow and this is where things get kind of crazy we're going to start off with this making event tool here so you can see inside I have this make event name for a tool then I have a little description call this tool when the user wants to schedule an event and sent over the event time in ISO format in EST time that is given from the user I need the time in standard format to see the time if someone says March 1st at 1: p. m. I give like a little ISO time and then send this time over we can keep the source on database and we can set the response property name to response here this is just basically what it's looking for when the function is is finally called and it gets returned I'll explain this a little bit more in a second here but lastly we need the workflow ID and this will be an entirely separate workflow different from the one that we're working on so if we look up at the URL here you'll see we have this workflow ID right here this is for this workflow right here if we make a new workflow I have this new workflow here called make event you'll see it has this new tag right here with this new ID because we're going to be using this make event workflow I'm going to copy this workflow ID and then I'm going to put this inside of our make event workflow ID here and now when we have that whenever we want to use this tool it will call this function send over the required variables to this workflow ID and how this works is it basically takes the information pushes it to this workflow ID and then on our workflow in order to get this to trigger we need to use an execute workflow trigger so it's literally just this execute workflow trigger right here whenever the tool in the AI agent is called it will run this trigger on this workflow based off the workflow ID that we have defined here and this will return a very small Json input of the variables that is passing through it'll basically look like this query time Farm request this will be the time that was given inside of the workflow when it gets sent over to the command so we can use this Json right here to pull this query variable from the Json that was sent over over when it calls this workflow right here and it sends it over to Google Calendar we can use our jason.
query right here to get the iso date that was sent over from the chat boot right so we can use this json. query to get this variable to then when we test it it's empty right now so it won't make anything I think it'll do something yeah for right now it makes an event but then we can have it create an event like so and once it creates an event we can come into here and then this will just send back a response like the response from here this response is looking for a response and this response will be the event has been booked so if we test this step out you'll see we have a response here the event has been booked look at that so now when we save this function we come into chatbot here and we have this set up accordingly again I'll save it as well I'll say make a meeting for March 20th at 10 a. m.
and if all goes well the event has been booked we got that response from our workflow in that response variable that I just showed you if I come on over to my Google Calendar event you'll notice I have a new event on March 20th at 10:00 a. m. look at that and you can come in here you can add all kinds of things like the titles or if you want to add location or if you want to add a Google meeting all types of things you can do to make this more custom in here you can you know add things like attendees locations and you can pull this information from other sources as well so if you have like a list or something maybe people you want to add that's how you could do that there move on to another one in my last video I did on N I actually showed you how you can scrape data with n all through a Google request and we can actually call that same function in our chat box so I have this new function here called get LinkedIn URLs run this script whenever the user wants to scrape LinkedIn and get LinkedIn URLs just run this tool when the user wants to scrape send over the page number with the page number you want to scrape doing the same thing here database response and we're putting in the workflow ID of our workflow here so I have this one here for our new workflow and get rid of that this will be the same thing this will send over our query with our basically page number like so this is how it will look when it gets sent over if we head on over to our LinkedIn field here we have our similar execute workflow trigger which this will trigger when this gets called based on the tool if we rig it up like so we have our link with our string which is basically just our link right here and then we're getting our page string value right here so this will be the Json query that we get from our API request and this will change depending on what number it pulls from our AI chat boot so we'll keep this on Jason query and then this will do is it'll combine this link URL with the page number to get our new page URL if you don't know how Google URLs work when finding LinkedIn URLs basically we want to use this start equals 40 every 10 is an additional page so this would technically be page five of this specific Google link search URL of only people on LinkedIn so we're scraping people only on this LinkedIn page here with these LinkedIn profiles so that's how we can do this here and this will basically just make our specific URL that we're going to scrape that goes through another HTTP request which then just scrapes the entire page this goes through another function here which then scrapes that entire page for only the LinkedIn URLs then it puts those LinkedIn URLs in a Google sheet and then at the very end if all goes well it will send over a response success and all that happened based off of a query page here and then the search so you could come in here honestly if you wanted to and have it so if you wanted the chatbot to accept keywords and then send over keywords as well you could have it say like hey I want only chief officers in the United States on page five and it will go ahead and accept those variables into the execute workflow trigger those fields in here set up the URL and then scrape the URL and add those LinkedIn URLs to your Google sheet if you want to do it that way right um for this we're going to make it pretty simple let's give this a test so if I head on over to our chat by here we'll set this up and I'll test in our chat and then say can you scrape page one of LinkedIn and then I'll send this request bring over the tab here and you'll notice in my URLs we get our 10 URLs from page one and we can even come into here and then say can you scrape page two of LinkedIn come back over here hopefully look at that we get page two and we can do the same thing for can you give me page three look at that we get page three page four and we get even more LinkedIn URLs look at that all through an automated chat bot isn't that sweet but you get the idea right so you can pass in the variable from the pages you get all different URLs based on those pages and then lastly the last tool that I want to show you guys is the sending email tool so if you want this bot to send out emails for you you can do all of it in here as well so it works very similar to the other ones we have a send email name here call this tool when the user wants to send an email I don't think we actually need this here but we basically have the workflow ID from the send email workflow here and this one's kind of tricky basically has two different AI agents so we have a similar execute workflow here which takes in the email and the query of the user that is sending the email then we have this AI chat bot here to do the the following so it takes in the Json query that we get from our chatbot in the AI agent here so it'll send over this Json query it'll go into here and then we'll say from this give me a simple Json formatted with email address to give me the user's email the email needs to be sent to and the email message to make a unique message based on the prompt you are Mike The Helpful AI bot respond to the email I need this in Json format so this basically kind of saying take what the AI from the last AI gave you and then put that into a simple Json format with the message and the address and this will just be the two variables that we need in order to send our email basically who it's going to and what the message is and I have this second step here which kind of was a bit tricky to to set up and get this working there's probably definitely a better way to do this but this will extract only the email address from The Prompt and then parse it so I have two separate variables to work with cuz it was kind of hard getting both of them to be put up in the send email node here at the very end you'll notice I have this AI Json output from the last last output and then the AI agent item from the email format from the very first email call that we did right so we have the two different agents kind of making the two different variables here this is coming from my Horizon Labs group email here and then that will just send over our test email based off of the email address it gave us and the whatever kind of email it came up with that's how that works there and then we can even have this send back a edit Fields here and let's make this manual mapping and we'll name this response and then we'll say email sent and now we'll save this one too as well so now we're sending back our response as email sent and if we check out our thing here it's accepting the response property and now let's link this up here so now if I were to say this for instance send an email to Mike Powers official gmail.