How to Create an Email AI Agent in n8n β
n8nAgingπ
2024-10-22
Build Process β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β BUILD CHATBOT WORKFLOW β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β PLANNING β
β ββββββββ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Define βββββΊβ Choose βββββΊβ Setup β β
β β Scope β β Stack β β Project β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β
β βΌ β
β DEVELOPMENT β
β βββββββββββ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Build βββββΊβ Add AI βββββΊβ Test & β β
β β Features β β Logic β β Debug β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β
β βΌ β
β DEPLOYMENT β
β ββββββββββ β
β βββββββββββββββ βββββββββββββββ β
β β Deploy βββββΊβ Chatbot β βββ LIVE! β
β β to Cloud β β Running β β
β βββββββββββββββ βββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββKey Components β
- Agent configuration
- Tool integration
- Memory management
Transcript β
[00:00] I spend a lot of time reading and responding to emails, so I figured I would automate some of this work by using AI agents. In this video, we will create an AI assistant that will respond to emails relating to sponsorship opportunities for my channel. Of course, these emails could be about anything, but I wanted to give you a real world example. This work photo is a lot more than simply respond to emails though. If the email is related to a sponsorship, then we do a few cool things. First, we extract information
[00:31] about the lead and then store it in a database. Second, the assistant will then formulate a response based on some training data like our rates, terms and conditions. Then finally, the email will be saved as a draft which we can then review before sending. Or when we feel confident in our solution, simply send the response immediately. So let's go ahead and build this workflow. If you're completely new to N8N, then I highly recommend checking out my beginner series on using N8N. You can
[01:03] definitely use the free local instance of N8N to follow along with this tutorial, but I'll simply use my cloud instance. Let's start by adding a new workflow and I'm going to delete this default trigger, and then let's search for the Gmail node, and within Gmail, let's select the on message received trigger. Then under credentials, let's click on create new credential. I'm going to rename this to Gmail YouTube tutorial. And now in order to connect N8N to our Gmail
[01:34] account, we need to provide a client ID and a client secret. To get those keys, go to cloud.google.com, then click on console. Then from this dashboard, let's create a new project by clicking on this drop down, then click on new project. I'll call mine N8N email automation. Let's create this project. Then let's click on select project, or you can simply click on this drop down and select your project from here. Then in the navigation menu, go to APIs and services,
[02:06] then go to library and in the search, enter Gmail, then click on the Gmail API and click on enable to enable this API. Now that we've enabled this API, let's set up our OAuth consent screen. For user type, let's select external and let's click on create. Let's give our app a name like N8N email automation. Let's also select our support email, then let's scroll all the way down and let's enter a developer contact information and let's save
[02:38] and continue. We don't have to do anything within the scope section, so scroll all the way down and click on save and continue. We don't have to do anything under test users either, so click save and continue and under the summary section, scroll down to the bottom and click on back to dashboard and this is super important. Click on publish app and then click on confirm. Finally, let's set up our credentials. Let's click on credentials, then let's click on create credentials. Let's click on OAuth client
[03:09] ID for the application type, select web application, give it a name like N8N email automation and now we also have to add an authorized redirect URI. When we go back to N8N, we can simply copy this redirect URL over here and then add it to the authorized redirect URIs. Then let's click on create. Now let's copy the client ID, add it to N8N and let's also copy the client secret and add it to N8N as well. We
[03:41] can authorize our email using the sign in with Google button, then select your email. If you see this warning, simply click on advanced, then click on this link to continue. Let's select all of these permissions and let's click on continue and that's it. We can now close this popup and our Gmail account has now been connected to N8N. Now for the poll times, I'll tell N8N to look for new emails every minute and for the event, we'll trigger this whenever a new message is received. And if I look at that email account, only after one email
[04:12] sitting in the inbox and that's the security alert from Google. Now if I go back to N8N and click on fetch test event, we should see the contents of that email on the right hand side. And if you look at this in the schema view, we can see the snippet which is just a short snippet from the actual email and not the entire payload, which is not ideal. So let's actually disable the simplify button, then let's click on this again and this time you'll notice a lot more information pulling through. And if we scroll down, we can see the email
[04:44] content in HTML format and the cleaner text version of the body. We'll use the body of the email along with the sender's email address and the subject line of the email to formulate a context for the LLM. So what I like to do is to add a set node, which we can use to create a brand new variable in our flow by combining values from this Gmail trigger. Let me show you how that works. Let's click on this area to create a new field and let's call this
[05:14] field email context. This is of type string, but for the value, let's go to expression and let's expand this. I want to grab certain values from the email and then format it into context for our AI assistant. So things that I want to pass to the assistant are the sender's email address, also the email subject and also the email body. Now for the sender email address, we can simply scroll down to this from section and within from we'll simply grab the
[05:46] email address field. Then let's do the same thing for the subject like so and for the email body, let's add this text field from the Gmail trigger. So now we've taken those individual fields and generated a context that should look something like this and we can pause this context to our agents further down in this workflow. Now let's go back to the canvas. Let's run this node and this node will pass back one value with all of that information formatted into a single string. Now the first thing we want to do
[06:17] is to use an AI assistant to determine if this email is related to a sponsorship opportunity or not. If it's not related to a sponsorship opportunity, then we'll simply do nothing. Otherwise we want to proceed. So how do we do that? Let's add a new node, then under advanced AI, let's add the AI agent node. So that should look something like this. We can go ahead and delete this chat message received node as we won't be using it. The AI agent node requires a chat model. So let's add one
[06:49] and I'm going to use the open AI chat model and I'll simply select my open AI credentials and for the model I will be using GPT40 Mini. It's a super cheap model and the results are fantastic. Now for the AI agent itself, let's change the prompt to define below and in the text field let's switch over to expression and let's expand this view. So what we want to do at this point is to get this AI assistant to have a look at our email context to determine
[07:19] if this is a sponsorship opportunity. So we could enter something like your role is to determine if an email is related to a sponsorship deal or not. Now in order to work with response from this model, we do need a structured output. So effectively I want to tell this agent to return a value which should only be true or false and we can use that boolean value to figure out which route to take in this application. So let's add something like respond with a JSON object with the
[07:50] following fields is sponsorship which can be either true or false and for debugging sake I'm actually going to add another field to the structure called reasoning. I simply want to allow him to explain why it decided whether this email is a sponsorship or not and finally I like to add something like think carefully about your answer. Now all that's left is to grab this email context and add it to our prompt. So effectively our AI assistant will
[08:21] see this prompt coming in. Now that we have our prompt sorted, it's also enabled require specific output format. This is now telling us to attach an output parser. We can use output parsers to define a JSON schema that we expect from the agent. So under output parser let's add structured output parser then for the schema type I'm actually going to change this to define below. We can expand this view and I'm simply going to change their examples. So instead of state I want
[08:52] is sponsorship and this should not be a string but instead let's change this to boolean. So true or false. Let's also change cities to reasoning which is of type string. Let's remove this items property and that should be it. Let's go ahead and test our agent. So let's click on test step and now we can see the output from this agent. We get our is sponsorship property which is false and in the reasoning we can see why the AI assistant determine
[09:22] that this is not a sponsorship opportunity and it's saying that this is some security alert from Google which is 100% correct. So let's actually create some test data for this by sending a sponsorship email to this email address. So I've gone ahead and sent myself this email which looks something like this. My name is John, marketing manager at the cognitive and some information about their platform. So let's run this workflow again in N8N and let's see what we get back. Let's double click on the agent and this
[09:52] time we can see that is sponsorship is set to true and for the reasoning the assistant says that the email discusses a collaboration to promote a platform. Great this is perfect. Now that we have the response in a structured format we can decide whether or not we want to respond to the email. So let's add a new node and more specifically the if node. Under conditions let's add the is sponsorship property as a value then because a sponsorship is
[10:22] actually a boolean we need to change this drop down to boolean and let's change it to is true. That's all we have to do in this if node. So let's go back and now we can take this process down different paths. If this email is a sponsorship then we want to go down the true path. If it's not then we'll be taking down the false path and we don't actually want to do anything. So for the false path let's add a special node called no operation do nothing and that's all we have to do for the
[10:52] false path. Now for the true path I want to do two things. First I want to extract information from the email like the lead name the lead company and the email address and store it in a database table and we will be using air table for that. Secondly we want to use a large language model to formulate a response based on our training data and then send that response via gmail. Let's start with air table. Let's add a new node. Let's add the air table node and let's
[11:24] select create a record. Now we just have to set up a table in air table. This is really easy. Go to air table dot com and sign into your account. Then let's create a new workspace. Let's call it sponsors tutorial and then let's create a new table. Let's start from scratch. Let's rename this base to sponsors. Let's also rename this table to sponsors as well and then for the columns I'll simply leave the name but let's go ahead and delete these other columns. So
[11:55] I'll delete this one. Let's delete assignee. Let's also delete status and I'm also going to delete all of these empty rows and let's add a column for the lead's email address. For the type I'll select single line text. Let's create this field. Let's add another column. Let's call this company. Let's also select single line text and just to keep things interesting. Let's also store the reasoning value like so and let's make this long text. Let's create this field. Great. Now when we go
[12:27] back to n8n we need to set up a connection between n8n and air table. So in this drop down click on create new credential and all we have to do is provide an access token. That's super simple to do. With in air table go to your account. Go to builder hub and under personal access tokens create a new token. Give it any name like sponsors as an example. Then under scopes we need to add the data records read scope. We also
[12:58] need to add data records write and finally let's also add schema bases read and I simply found these values from the n8n screen over here. Let's add a base and let's select our sponsors base and finally let's create our token. Let's copy this and let's add it to n8n. Let's save this and if everything was done correctly you should get this connection tested successfully message. So now that that's been set up we can select our base from this drop down list
[13:30] like so. Let's also select our table which we call sponsors and now you should see the columns that you created in your table. I'm simply going to execute the previous nodes but now in the previous nodes we actually don't have all of this information. Looking at the email I want to extract the name of this lead called John along with the platform name that they're trying to promote as the company name. We also want their email address and of course the reasoning from the previous agent. So
[14:00] getting the email is easy enough as we can simply go to this gmail trigger node and if we scroll down we can grab this from email address over here. We can also get the reasoning value from this agent output like so but now how do we get the person's name and the company name. Thankfully that's easy as well. Let's simply add those to our agent node. So let's expand this prompt and let's add those two fields to our JSON structure. So let's add the lead name. So for the description I'm saying
[14:31] the name of the person as per the email body leave blank if not a sponsored email. Let's also add lead company the company wanting to sponsor leave blank if not a sponsored email. Now let's go back to our agent and it's also add those fields to our output parser. So let's open the output parser. Let's expand this view and below reasoning let's enter lead name which is of type string and it's also add lead company which is also of
[15:03] type string and believe it or not that's how easy it is to work with unstructured data. Let's run this agent again and as you can see the name and the company is being pulled from the email. So back in our air table node I'll simply execute the previous steps again and now we can just drag the name across and we can also add in the company. Now let's test this air table step. We can see that the record was indeed added and if we go back to air table we simply go back to
[15:33] the table itself and look at that we can see our potential sponsor in this table. Now let's work on the response email. For this we will be using a large language model so under advanced AI I'm simply going to add an open AI node and for the action let's add message a model. I'll select my open AI credentials and from the list of models let's select GPT40 mini. Now for the first message I'm actually going to change the role to a system role and these are effectively instructions to
[16:05] the LLM. With intext I'll switch over to expression and let's expand this. Now before you get scared by the following prompt I just want to explain that this prompt is quite long but that is because I wanted to add as much information and instructions about my channel and things like my rates terms and conditions. But you really do not have to make it this complex. Simply tailor this prompt around your specific needs. So I'll take you through this prompt briefly. You work for a YouTube channel
[16:36] called Leon Fansel. Your role is to respond to sponsorship opportunities by drafting a response email. Carefully look at the below email and write the friendly and professional response. Only return the body of the email in HTML format. Do not write ahead only the body and the email must include the following information and then I go over things like the channel details the sponsorship rates and then just simple terms and conditions. Nothing too crazy and by the way this is not accurate to my channel.
[17:07] This is not my rate and these are not my terms and conditions. I simply generated all of this using chat GPT. So this is just a sample for this tutorial. Now that we've provided a system prompt let's add a message. We'll leave the role as user. For the text let's go over to expression. Let's expand this. Let's enter email context and below that let's simply go to this edit fields node and let's drop in our email context like so. Let's try this out. So
[17:38] because this is using HTML it might be a little bit difficult to read but this does contain our email response. Now let's actually send this email back to the user. Let's add another node more specifically the Gmail node. So now we can decide whether we want to instantly reply to the email or save a draft first. I'll actually show you both techniques. First let's reply to the email and then after that I'll show you how to save a draft. So let's select reply to a message. Let's select our Gmail credentials.
[18:09] Then for the message id let's simply go back to the Gmail trigger and let's grab our message id. And for the message content we'll simply grab the content from our LLM. Let's go back to the canvas and let's execute this workflow. We can see that the LLM is basically generating the response and we can see that the email was sent. Let's go back to Gmail. Let's click on send and let's open up this response. And here we can see the response that was automatically sent by our AI Assistant. Unfortunately it included
[18:40] these funny characters at the start but we can easily fix that by going back to our LLM and in the system prompt. Let's also add do not include things like that funny HTML characters. Great and if we run this again that issue should be resolved. So let's have a look at the response now and that looks much better. So let's try this again and this time those funny characters should be removed. Right let's go back to Gmail. Let's go to sent. Let's have a look at this response and that
[19:10] looks way better. So at the moment our responses are sent immediately but we might want to save these responses as drafts first so that we can approve it. Now let's go back to our workflow and let's open up this Gmail node. This changed the resource from message to draft and for the operation let's select create. For the subject line let's actually go back to the email trigger and let's simply grab the same subject like so and let's prefix it with something like read like
[19:40] reply. We won't change the message we simply grabbing it from the LLMs response but we do want to attach this draft email to the original email. So under options let's add thread id and then under the email trigger let's grab the thread id like so. Let's save this workflow and I'm actually going to send a brand new email just to test this with some new data. Great so we've received the email now let's run this workflow and now if we go back
[20:11] to our emails and we refresh this we can see that a draft response was indeed created and because we specified the thread id that draft is assigned to the original email but if I open this up I can see that this does not look correct we've got all of these HTML tags everywhere and that is because in this Gmail node I forgot to change the email type from text to HTML. So I'm actually going to delete this all draft then let's execute this workflow again and if we go back to our drafts
[20:42] let's refresh this page I can see the new draft so let's open this up and that looks way better but note at this stage the stall won't work as we haven't specified the recipient so under options let's add a new option let's click on to email and then let's scroll down to the Gmail trigger and let's simply add the from email address like so then let's run this workflow again let's switch back to Gmail let's refresh this let's open up this draft and now
[21:12] we can see the sender's email address and if we're happy with the email we can simply click on scene. I hope you enjoyed this video if you did please hit the like button and subscribe to my channel to support my work. If you would like to learn more about N8N then check out this N8N playlist over here I'll see you in the next one bye bye