00:00Hey guys, in today's video we're going to be using CrewAI and LRA to build an email automation tool
00:04This tool is going to connect to our Gmail account, pull down our latest emails that we receive, and actually write draft emails for us to help save us a ton of time
00:12Honestly, I'm shocked that more people aren't using these tools together, because the first time I saw this demo actually working, it blew my mind
00:20So, let's go ahead and hop over so you can see this demo in action, because it's going to blow your mind
00:25Let me show you how cool this tool is
00:27So, what I've done is I've typed up two different example emails myself
00:30I sent one saying, "Hey, what do you think about LangGraph and CrewAI?" and from another email, I sent to myself, "Hey, would..." You be interested in doing a sponsorship, you know
00:38So those are just two draft emails, and what this CrewAI has done, which I think you'll find super cool, is it actually analyzed these emails and typed up draft emails
00:45So you can see, I have two draft emails waiting to send back to each one of these emails
00:52For the YouTube collaboration, you know, like, "Hey, thanks man for the kind words
00:55Could you please provide me more details about the sponsorship?" which is super cool
00:58I didn't type any of that up
01:00Same thing for this LangGraph email
01:01It went ahead and said, "Thanks for asking about my thoughts," and it just continues to carry the conversation, so I can quickly respond back to the people who are sending messages to me, and as you..
01:10You can see back in here, over in the code, we have our CrewAI going through, and it's going to analyze all of our emails, pull out the latest ones to make sure we're not responding to junk or promotional emails
01:21It's going to pull out emails from real people, analyze the text, and actually generate responses
01:25So that's what's happening inside of our CrewAI
01:29I hope you guys are pumped to start building this, and, per usual, I'll have the source code down below for free
01:35This is actually an example pulled from the CrewAI tools demo, but I'm just going to walk you through it line by line
01:40So, if you want to level up as a CrewAI developer and not fall behind, you're definitely going to want to watch this whole video
01:47Also, real quick, I want to mention I created a free School community just for you guys
01:50It's called the AI Developer Accelerator, so if you get stuck at all during this tutorial and run into any questions, feel free to hop over there and ask
01:57I'll answer your questions, or someone else in the community will be sure to get you unstuck so you can keep building out your awesome CrewAI
02:03I also want to mention you're going to meet a ton of other like-minded AI developers over there from all different skill levels, so you're definitely going to want to check it out right after this video
02:10But enough of that, let's go ahead and dive into the video
02:13All right, so here's a roadmap of what we're going to be covering in this video
02:17First, we're going..
02:21Today, we will be talking about what L-Graph is, then we'll discuss why you might want to combine L-Graph and CrewAI together
02:25From there, we're finally going to dive into the code, where we'll start building that email automation tool that combines L-Graph and CrewAI
02:33Let's go ahead and begin with the first part, where we explain what L-Graph is
02:37The main reason you would want to use L-Graph with CrewAI is because L-Graph allows you to intelligently trigger your Crew when certain conditions are met
02:46So, let's dive into a few concrete examples with this diagram so you can see how powerful this tool is
02:52What I've done is create a super simple diagram
02:53Inside of here with LangGraph, we have a few different actions such as wait 30 seconds and kick, take off our crew, and then we have a condition where we're just going to check here in a few seconds if the 'like did' condition get met
03:05Yes? Great, go kick off the crew
03:08If no, all right, cool, just go back and wait
03:10So, let's look at two different examples just so you can see this in action
03:13Let's imagine we are building some sort of stock analysis tool, and what we would do with this stock analysis tool is give buy or sell recommendations and some justification for why we recommend that
03:24Well, instead of you and I manually kicking off our crew every time we want to do this support, we could actually have this crew respond to market conditions
03:32So, for example, Let's say we were monitoring the price of Apple
03:40It went up 5%
03:47Well, we could have this condition check to see, hey, was there a large increase in the price of the stock? If yes, cool, let's go kick off our financial analysis crew to analyze what's happening inside that stock, write a report on why we think it's happening, and then suggest a buy or sell alert
03:55Oh, if it hasn't met this condition, cool, we'll just go back and wait until something does happen
04:00Interesting in the stock market, which is super cool because this means you and I get to build tools that we just set running and then forget about, because they're just going to be operating in the background
04:08Or we could have something else, like imagine we were doing some sort of..
04:11Newsletter, what we could do is basically build a real-time newsletter
04:15So, let's just say we imagine we were tracking what Elon Musk was saying on X
04:18We could say the condition could be: has Elon tweeted something new? Great
04:23If so, let's kick off our newsletter, which is going to go out and write a newsletter and send it to all of our subscribers or put it in our draft
04:31Then we're just going to quickly comment on his latest tweet, what we think it means, and our thoughts about it
04:37We could do whatever we want, really, and that's the cool part
04:38Once again, if he hasn't sent out any tweets, great
04:41We're just going to go back and wait for 30 seconds
04:42I hope you see how this kind of setup works
04:44In this super simple example, we're..
04:56We're going to be doing something exactly like this because we're going to build our email automation service, where we're going to wait and just look at our email inbox
04:58We're going to check to see if we have new emails; if so, we're going to write our draft email
05:00This is the exact same flow that we're going to be building out here in just a few seconds
05:02I hope all the dots are starting to click now that you've seen a little bit of what LangGraph can do in these super simple examples
05:10But let's go ahead and actually start heading over to the code, where we're going to build everything up and start coding out and setting up LangGraph inside our application
05:19We're actually going to dive into the nitty-gritties, where you'll learn about setting up nodes, edges, and state
05:22But all that will make more sense once we dive over to the code, so let's go ahead and hop over
05:28Now, we're going to dive into the fun part, where we're actually going to start coding up this project
05:32Just to help paint a roadmap of what we're about to do next, first we're going to walk through all the files at a high level, just so you understand what's inside of this project
05:39From there, we're going to dive deep into LangGraph, really looking at the three different files that set up LangGraph, which are going to be our graph nodes and state Piy files, and we're also going to really dive into the core
05:50Fundamental technologies and terms that you use to set up your LangGraph, then from there, we're going to hop back up and start talking about our CrewAI, just so you understand what it's trying to do
06:00Then, finally, what we're going to do is go off and actually start setting up our connection to our Gmail account over to our local CrewAI, so that we can actually have our CrewAI pull down some emails and write those draft emails that you saw in the demo at the beginning of the video
06:14Okay, so let's go ahead and start diving into each one of these files at a super high level, just so we're on the same page
06:19Right after that, we're going to hop over to our LangGraph files
06:22Okay, so at the top, what you'll notice inside of our source..
06:26The directory file contains the typical files you would expect to see
06:28We have our main.py, which is where we'll kick off our LangGraph and start our CrewAI from there
06:35You'll notice we have a file called credentials, which you'll create later in the video
06:41This file will store all your sensitive information for connecting CrewAI to Gmail and other services
06:47Additionally, we have the requirements.txt file, where we'll specify and install all the dependencies needed to set up our Python environment to run this tool
06:53Let's take a deeper dive into the actual code itself
06:59So, going into the L-Graph side of things, you'll notice there's a graph, and the graph is basically— we'll dive into this in a minute— but basically, this is going to contain all the nodes and edges inside of our graph
07:10Don't worry if that doesn't make sense; we're going to dive into that later
07:12From there, we're going to be able to set up each one of those nodes
07:15A node is where we're going to actually perform actions and do things at each different state
07:21Don't worry, this will all make sense; I'm dropping seeds so that later on, when we come back to them, you'll understand— oh yeah, that's, we did talk about this
07:27And finally, we're going to go about setting the state
07:28You know, this is just basically—we're just trying to remember the..
07:32State of things inside our graph, so that's what's happening on the LGraph side of things
07:38If you're more familiar with CrewAI, you'll be pretty familiar with a lot of these tools
07:42First off, we're going to set up our crew, and our crew is going to contain a bunch of different agents and a bunch of different tasks
07:49So this should look pretty familiar to you if you've worked on CrewAI projects in the past
07:55If not, don't worry; I have a ton of other CrewAI-related tutorials on this channel, and I’ll probably pop up a card in the corner so you can watch some of the foundational videos and then some of the more advanced videos, so you can really get your feet wet and start
08:08Actually, I'm experimenting with this tool
08:10This is all very basic stuff, and you can actually see that, per usual, we're going to set up our agents to do certain things, such as filter emails, perform actions, and write responses
08:17Each one of those agents is going to be assigned a task that pretty much has the same name, and outside of that, we're going to do some fancy tools, which are just going to be creating a draft
08:24That's where we're going to have our crew create a draft email
08:29Okay, cool
08:34So now that you understand everything happening at a high level inside this project, let's go ahead and dive deep into LangGraph so we can start discussing some of the core terminology and help you become a LangGraph professional
08:38So, let's go ahead
08:40And start working on that now
08:43All right, so now we're going to dive into setting up LangGraph, and I've actually set up a ton of visuals to make this super easy for you to understand and to grasp how everything connects
08:50But before we dive into that, I just want to take a quick tangent and let you know that building graphs, which is what we're about to do right now, is super foundational to becoming a software developer
09:00If you went to college to learn software development or computer science, you would learn about data structures and algorithms, and you would study graph theory
09:07But we're going to throw all that out the window and just learn a super basic version of it for this demo right here, and I hope it helps you guys get started
09:15To learn this because once you kind of understand graph theory, your mind opens up to understanding a bunch of different problems that you might not have already had, like a tool to solve them
09:25So we're basically building or adding a new tool to our toolbelt, and we're going to become better developers, and more specifically, better AI developers
09:32So, let's go ahead and start diving in and setting up our LangGraph
09:36When you begin to work with LangGraph, there are four different terms that you need to know
09:39The first one is going to be nodes, and think of nodes as different actions that we want to take
09:46The next term is going to be state
09:48State is nothing more than the ability for us to remember what we have done in the past
09:53So, how many emails..
09:57Have we sent the emails? Which emails have we checked? Which draft emails have we read or written? So, that's what states are for—to help us remember what we've done in the past
10:02The next two terms that you need to know are edges, so think of edges as connections between two nodes, and you'll see this in just a second
10:11But just remember that an edge is a connection between two nodes to say, you know, can node one talk to node two? So, that's what it's for
10:18The final term that you need to know is graph, and a graph is nothing more than just a collection of nodes and edges
10:25Cool, you now know all the terms for typed graphs
10:27So, hey, you've already done 80% of the work in learning it
10:32So, now let's actually hop over and see what this looks like when you explore it practically
10:35Let's visualize these new terms
10:37So, welcome to my beautiful Excel draw drawing, and I just want to show you all of the new terms that you just learned
10:44So, what now? Right now, what you're looking at inside of this — this is a graph
10:49We have nodes and edges, so that's term one: a collection of nodes and edges
10:53Now, what are our nodes? Well, if you remember, we said our nodes are different actions that we want to perform
10:58So, let's actually hop back to our code so we can visualize these side by side
11:01I want you to reach the point where, when you look at these files, you can almost visualize one of these graphs right here
11:08So, I'm helping you become a graph visualizer
11:12Okay? So, let's look at the different actions that we can take with our nodes
11:15Well, the first one is we..
11:18You can check emails to see which emails we have out there, then we can wait for the next run
11:21So, you know, we want to check emails without waiting a minute or two because emails aren't coming in every second
11:25Then, the third action we want to perform is creating a new email
11:29That's what our three different nodes could be: we could say, "Hey, go check your emails," another node for waiting, and another node for composing our draft emails
11:37Those are the potential actions that we could take
11:41Okay, so where LangGraph comes in, and where all the magic happens, is we can specify how to move between these different nodes
11:50For example, imagine we just checked for emails; after checking, we might want to proceed to write a response
11:57Email, we might just want to wait if we didn't find any new emails
11:59So that's actually where edges come in
12:01Let's hop back over to our graph so you can see where our different edges get added
12:07After we draft a response, I'll just dive back over here
12:13The three different nodes we can have are "Check New Email," so these are the three different actions
12:16Now, we can say, "Okay, well, how can I move between each one of these actions?" And the way we move between nodes is through edges
12:23So, node one connects to node two, or node two connects to node three
12:28You can see this one's a two-way connection, so it's a bidirectional edge
12:32That's why you're learning
12:34So, this is one-way, unidirectional, or back and forth, Birectional
12:36You're becoming an..
12:37Official developer, this is awesome
12:39That's what's happening with our edges: we are setting up the movement between all of our nodes
12:44So, let's actually go ahead and translate what we're seeing here to our actual code so we can see
12:48We want to add an edge between "Draft Responses" and "Wait for the Next Email." So, we said "Draft Response" to "Wait for the Next Email." This one we need to set up to go from left to right, from, uh, "Draft" to "Wait." The next one is we want to go from "Wait" to "Check Emails," so we're going to go this way
13:09This time, we're going from "Wait" to "Check Emails." Fantastic
13:11As you can see, we're starting to build up a flow between all of our different nodes, and then, in addition to that, there's this
13:21There is something called additional conditional edges, so this is where we want to check, like, hey, did something happen? Great, and in that case, I want this node to connect to this node
13:31If you actually hover over it, you can see it
13:33So, what is the starting key? In our case, this is the starting node
13:37Then, if it worked out well, what do we want to have happen next? So, in our case, we want to check for new emails
13:47We want to see if, at the check node, the email node, we have emails
13:49So, this is a Boolean
13:51If we do have new emails, great, we're going to draft a response
13:56If not, we're going to wait for the next run
13:58So, it's kind of like a decision tree
14:01So, hey, do we have new emails? If we do, great, go write a response for them
14:03If not, we'll just wait until..
14:08Let's visualize the next run now that we know we can add this Edge, more specifically a conditional Edge
14:13So, we agreed that "Check New Emails" is the starting point right up here
14:17What we're going to do is have two different types of responses that are conditional
14:22The first one is draft responses, so we'll make this one conditional to see what happens
14:26Fantastic
14:30So, this is if we have emails to respond to—fantastic
14:33And then, after we've checked new emails, if we don't have any new emails, well, what do we need to do? We need to wait
14:39If no new emails, all right, cool
14:43So, what's interesting is this: what you're looking at right here, this entire graph, is exactly what this is right here
14:51We have turned our text into..
14:53An actual visual, uh, so I hope you guys are getting the hang of understanding
14:57Oh, that's what a node is, that's what an edge is
14:59Oh, this is what a graph is—it's just a collection of nodes and edges
15:02Okay, now let's dive into the final part of it, which is going to be state
15:06And if you remember, if you remember, state is our ability to remember what's happening inside of our graph
15:12So let's actually dive back over to our code and understand what was going on
15:14 So, we said there were three different things that we wanted to remember: checked email IDs, so, hey, have we seen this email in the past? The next one is just emails—these are all the emails that we have—and then the last one is action-required emails, so, oh, we need to perform..
15:27An action on this email, such as writing a draft or something like that, okay? So that's the final thing that's going to happen, and what's interesting is each one of these nodes can update our memory
15:37So, you know, whenever we check a new email, well, what do we want to do? We want to add those new emails to our memory
15:43Oh, I just drafted a response
15:45Okay, cool
15:47I no longer need to write a draft
15:49I need to update our memory to say, "Yep, this one checked
15:50We have written an email, so we no longer need to act on it anymore." So I hope this has all clicked
15:57I would definitely recommend watching this part twice because there were a lot of new concepts
15:59But once you have basically added this tool to your tool belt and fully understand how to go from one of these graphs..
16:07Piy files over to a visual, you’ll kind of understand
16:09Oh yeah, that is how everything connects, and honestly, I’m a big fan
16:13Whenever I’m building one of these projects, I will start with the visual first, and then I’ll translate it over to code
16:19So, if I was building this project, I would have built this first, and then I would have coded up all this over here
16:25So, I hope that has been a big help as you go off and build your own Cruise
16:29Okay, so now that we’ve done everything at a high level with the working of LangGraph, what I want to do next is actually dive deep into setting up the nodes, so you can fully understand what’s happening inside each of these action steps because this is where our nodes are going to start updating
16:43Our memory, so let's go ahead and start diving into this
16:45Alright, so let's go ahead and look at each one of these nodes and see what actions they are going to perform
16:51I'm not going to go super deep into coding all this up because it's very straightforward when you actually read the code
16:56The main thing is I just want to help show you how our nodes connect to our state, because I want you to understand the core fundamental concepts we're working on
17:04So, when you're building your own LangGraph and CrewAI tools, you'll have a core understanding of, "Oh, that's why we were doing it," and "That's how everything is connected." To help visualize what's going on, I went ahead and actually typed up some additional and updated information
17:17We're visualizing our diagram so you can see, like here, our nodes and here's our state
17:21This way, as we go off and run through our code over here, we can hop back and forth and understand
17:27Yep, that's how everything connects because that's the important part of this
17:30Okay, so we're going to speed through the first one, which is going to be checking our emails
17:36So, if you remember, what are we trying to do? We are trying to look at emails inside our inbox because we want the ability to respond to them
17:42So, what do we need to do? Well, first off, we need to import all of our Gmail tools, because that's how we're going to connect to our Gmail
17:48What we're doing is setting up our Gmail tools, and then the important part is we're setting..
17:52Let's set up our custom instance and start searching through our inbox now
17:56How does this actually connect to our Gmail? Well, great question
18:00The first step, which we covered in the later part of the video, is that we need to pull down our Gmail credentials
18:07That file is a super secret file, which actually includes all the pieces necessary for these tools to connect to our Gmail account
18:15So that's what's happening here
18:17Now, let's dive into the parts that we actually have control over and how they interact with our state
18:20The first one is that we can update the search parameters and search for different things inside our Gmail account
18:29For example, we could look for all emails that are basically one day old, so that's what..
18:34That means, or we can actually do something like from Brandon 9598, so you can search from a previous specific sender
18:41There are a bunch of different things you can do
18:44I would just look up Google Docs for Gmail and see all the different custom queries you can perform
18:48Alright, so what we need to do next is start getting a little bit deeper into working with Python code
18:55I'll try to make this as simple as possible
18:57The important part is that we need to go through all the emails we've previously reviewed because if we've already looked at those emails, there's no reason to review them again
19:06That's exactly what this is doing
19:08So, we're going to do a turn area to say, "Hey, I want..." To look at my checked email ID state, does it exist? Great, give me all the items inside of it
19:17Oh, if it doesn't, just give me back an empty list
19:20That's exactly what that means
19:22So, over here, just to show you, if this list said, hey, we looked at email IDs 1, 2, 3, 4, 5, we would actually just return those email IDs 1, 2, 3, 4, 5
19:31So, that's exactly what's happening here
19:33Now, what do we do next? We need to iterate through all of the emails that we got back from our query and start adding them to our list to check out because, you know, if there’s a new email that we haven't looked at before, we want to store that email so that we can later on, you know, type up a response to it, because that's our end goal
19:50So, that's what we're going to do in this for loop here
19:54We're going… To go through each one of our new emails, and since emails can be chained together in a thread, that's why you're going to see this term "thread." So, let's walk through it
20:02We'll iterate through each of our emails, and if this email was already contained in our checked emails list, we skip it because we don't want to look at it
20:11If this thread ID is not in the thread list, meaning this is a new thread, and if the sender of this email is not equal to my email—well, where did this "my email" come from? If you go over to your environment (.env) file, you'll have the ability to come in here and update your email
20:28You'll also have the ability to update your other environment variable keys, but more on that later
20:33The important part is you need to keep this file very safe, but we'll talk about how to do that later
20:37The final part of this, just to recap: hey, is this a new email? Did this email, uh, not already exist in the existing thread? Great
20:43Also, was my email not the, uh, sender of this email? And all this means is we're just trying to check like, hey, dude, I don't want to respond to emails that I sent; I obviously only want to respond to emails that people sent me
20:57So, if we meet those three criteria, we're going to go ahead and start building up our list that we have right here
21:03So, we're going to add the new email thread ID over here; that's what the application is going to do
21:07And the next thing is we're going to actually store the key pieces of information we need about
21:11Each email so that we can later respond to them, is just the ID—the thread ID—a snippet, so this is just like a quick text blurb of what was said in that email and then who sent the email
21:21So, we want to know their email address
21:23After we've gone through that for each one of the emails that we just searched through in our for loop, we want to do two things: we want to update our email list inside of our state, and we also want to update our checked emails ID or list inside of our state
21:37What does this even mean? Because I know this can be very overwhelming, especially for newer software developers, because you're like, "What the heck does this double star mean?" What? And I'm adding stuff after the double stars—what does this mean? Okay, so the..
21:50The best way I like to think of it is this double star just expands whatever your current state is
21:54So, if our current state — I'm going to do a split screen so you guys can actually see this line by line — here's what our current state would look like, because our state is getting passed around
22:03You'll see how we pass in the state right here
22:04Our state is going to get passed around between all of our nodes because each of our nodes needs the ability to look at the current state and edit it
22:12So that's why we're having to pass it around everywhere
22:14What we're going to do is—this state really means, um, it really means this
22:18So, I'm going to comment it out so you can kind of see what it actually means
22:21So, check email, copy this a few more times, and perfect
22:29Actually, what that double star syntax means, uh, it's just a quick shorthand so you don't have to type it out, but it's basically saying, "Hey, expand out my current state." Oh, I actually messed up because I should have put these to the side too, my bad
22:43I'll do this real fast just so you guys can see everything in action, and then the next will be emails, and the final one will be the action state
22:51So, this is exactly what this represents; it's our current state
22:55So, it's, well, what is our state? Well, it's these three things: checked emails, emails, and actions required
23:01So, we've expanded them all out with our shorthand
23:03Well, why is this helpful? Well, because in this check email function, where we're checking to find new emails, we are responsible..
23:10For updating new emails, we find and update any checked email IDs so that, in the future, we can skip having to check emails if we have already found them
23:20If the keyword in there is "I'm responsible for updating it," well, that's exactly what our code is going to do
23:27Our code right here is going to override the existing state, so this email parameter right here, this property, is going to override this one
23:36We'll just go ahead and delete that, and this checked email and new property is going to override the existing one
23:45Basically, the important part is that anything with the same property name — since these have the same property name — the one that comes at the end is going to overwrite the previous one
23:53So later, we'll always overwrite
24:01What’s cool is we are going to end up with something like this, where we have strictly updated the two properties we wanted to—the email and checked email IDs—and we have completely left alone the "action required" email state
24:09That’s why it’s so nice because, with just doing this right here, we have updated the state for the two things we wanted to change and completely left untouched the other part of our state
24:21We’re just putting it to the side and passing it along later when we need to use it
24:26So, that’s why this is a super quick, nice short shortcut for updating state
24:30You can see what we are doing—well, we’re returning this updated state
24:34So, I know that was definitely a lot, but note, this is a..
24:36This is a very common practice in programming, so if you have any questions once again, please drop them below or add them over to the school
24:44But um, I'll definitely watch this part at least twice because this is super fundamental for updating and sharing state throughout our application
24:51Let's keep chugging along to the two other short parts of the nodes
24:53The other one is just "wait for next run," basically
24:57What this means is, whenever I hit this part of our node, well, what do you want to do? I just want to go to bed for 30 seconds
25:04So that's what's going to happen, and after we wait for 30 seconds, we're just going to return the state that was passed to us
25:12Hopefully, you can start to see a pattern here, where we take in state, we update it, or we..
25:15Don't do anything; we might just reference it and give it back, so that's how we're sharing state between all of our nodes
25:22The final one is whenever we're going to be working with new emails
25:27Well, this is really just a Boolean, because if you remember back over here in our graph, whenever we were doing our conditional edge checks, we were, hey, we're at this node, and based on whether our condition is true or false, we want to do two things: if it's true, go over here; if it's false, do this
25:36I hope this is starting to make sense
25:45These are really the two important nodes that you need to know about—the check email and the wait for next run
25:54The third one that you'll notice in the graph, which wasn't referenced right here, was draft response
25:58So now we have gone through the nodes, now we're going to start connecting things in our graph, and this is where things get interesting because this is where LangGraph and CrewAI merge, and it happens right here on these lines
26:11So let's go ahead now and start diving through our graph Python file and walk through it line by line, so you guys will have a better understanding of how this all connects
26:19 The first thing that's important to notice is that inside this graph, what we're doing is first importing everything we set up previously
26:27You can see up top, from our state, we are importing our email state, so that's what we're importing right here
26:34Then you can also see that we are setting up our nodes from our nodes Python file
26:39So, we're bringing all of our nodes and state over because now that we have everything we need to set up our graph, we can start configuring it here
26:47Another important thing is that we also have the ability to import our CrewAI, and we're doing that from inside our CrewAI crew file
26:55So, we're importing the email filter crew into our workflow and, basically, into our graph
27:03Okay, so we're bringing in everything now; it's up to us to connect it all together
27:06Here's exactly what we're going to do: the first step is inside our workflow, because that's how LangGraph works — it's a workflow that allows us to control how our application flows, passes around, and manages state
27:22Inside of the graph, okay, so what's actually happening? Well, right out of the gate, we need to define all of the nodes inside of this graph
27:28Even though we've created them over here in this file, we need to add them to the graph, and it's super simple to do that
27:35So, we make a new node type, and then what we do is come over here and start making a workflow
27:43A workflow is nothing more than a connection of nodes, edges, and state, so that's exactly what's happening here
27:49You'll see we're to check that state box whenever we create our state graph, which is coming from Graph
27:56You can see we're saying this is the state I need you to keep track of, so that's why we're doing that
28:00We've checked off "State," and now we just need to add in nodes
28:05Edges, and that's what we're going to do right inside of here, inside of our workflow
28:08We're going to add a node, sync our check news email node—that's the key
28:12Well, what's the value? Well, it's a node, and it's going to be our check email node that we set over here
28:19You'll notice right now that all of these are just functions that take—they don't even take an input except the state—and then they just return an updated state
28:26So, that's what's expected
28:28It's up to us to say, if you look at an add node, the actual what's happening inside of here, what you'll notice is it's actually just taking in a key, and then it's going to take in something called a runnable
28:38And all that means is, like, hey, we need to run the node
28:42We said nodes are actions
28:45Highlighting over that, what we're trying to do is create all of our nodes and give them names
28:48So here's the name, here's the action that we already set up, here's the name of the node, here's the action we already set up, and here's the new node that we haven't talked about yet: our Draft Responses node
28:57Well, what does this node need to do? Well, it actually needs to go off and kick off a crew, and we're going to dive into CrewAI in just a second
29:07But I just want to fully paint the picture of like, yep, this node has a name of Draft Responses, and its action is to go off and run an entire crew that we're going to build in two seconds
29:12Awesome
29:17The final thing that's important inside of here that we haven't fully talked about is like, okay, we..
29:22Now we have a beautiful graph like this, and we know how we're updating state as we run through all of our different nodes because our nodes are performing actions and updating this state
29:32Where do we start in this graph? Well, that's something we have to define inside of our application
29:36So we say, hey, the starting point is checking new emails
29:39Let's go ahead and give ourselves a new starting point, just like a nice starting point
29:45I'll just put "Start." This is where we're going to begin
29:48Finally, what we have to do is add in those conditional edges that we talked about earlier
29:52We've already shown how to add in those lines right here, and just as a final step, hey, whenever we run this node, we have two options that can happen
30:00Happen afterwards, the first one is, did we get new emails? Great
30:06If we did, continue to the next step
30:08If not, you're done, buddy
30:10Just end it
30:12And what does "end" mean? It means just go over here and wait 30 seconds for the next run
30:16Okay, cool
30:20And then, outside of that, edges—the main thing to think about—edges are just communication channels that we allow to flow between
30:24The important part about LangGraph is that we are very strict in the way flows can happen
30:29So, that's what’s happening here
30:32And then, finally, we're just compiling this workflow into an application that we can then later kick off
30:38Well, where do we kick it off, you might ask? Well, if you hop back over to our file explorer and go to our main.py, you'll notice that's exactly..
30:45What we do here is import our graph that we just created, which contains the entire state flow and shows how everything's moving around
30:54Then we just say, "Great, here's the app," and let's go ahead and kick it off
30:57You just kick it off with invoke, and if you wanted to, you could pass in additional inputs right here, but we're not going to do that
31:04All we're trying to do is just go ahead and kick off our graph that we just set up
31:08I hope you understand the LGraph now, because we've talked about all the core principles
31:12You now know how nodes, edges, starting points, and conditional edges work
31:16You now have a full grasp of that
31:17So we're going to put LGraph to the side, and what we're going to do is hop over to CrewAI and start actually investigating what the heck is..
31:25Happening inside of this email filter crew and how it corresponds to our actual state that we were setting up and using throughout the rest of this process
31:35So, let's go ahead and start hopping over here and begin analyzing our crew
31:38We're going to speed through this part because we've already set up a bunch of different crews, but I just want to really hone in and focus on the parts that are super specific to the overlap of CrewAI and LangGraph, because that's where the important part is
31:55So, let's dive into the important things
31:56As usual, when we set up a crew, the first thing you really want to do is set up your agent's tasks and tools
32:02Let's actually dive into each of these
32:05Whenever we create our crew, the..
32:06The first thing we do inside our constructor is create all of our different agents
32:10So, if we hop over here to our Email Agents file, you can see that we have set up our three different agents: our Filter Agent, our Action Email Agent, and our Email Response Writer
32:22Let's actually dive into each one of these and discuss the core purpose behind each agent and how they relate to one another
32:29Inside this Email Agent file, you'll see that we are again importing our Gmail toolkit because we want to give our crew the ability to go out and actually write draft emails
32:40That's why we're importing this tool
32:42The first step when we kick off our crew is to let our filter… The agent should go off and filter out the non-essential emails, like newsletters and promotions
32:52Once again, we only want to respond to real emails that people have sent us
32:58We are not going to allow this agent to delegate work to other agents
33:03It's like, "Hey, here are all the emails you received," and we'll talk about the state here in a second
33:08But I need you to look at all the emails inside of the state variable that we're passing around, find all the emails, and filter out everything that looks like junk
33:16Only give me the emails that we need to respond to
33:21I think the best way to do this is to also look at the task that corresponds to this agent, so we can do a side-by-side comparison
33:25Because that's exactly it
33:29If you come down here to the Kickoff part, you can actually see that's what we're doing here
33:33We're going to create those agents and then connect each one of those agents to a task
33:36So, we're looking at the filter agent, and we can see the filter agent is used inside the filter email task
33:41So, let's go ahead and dive into the task
33:44But the thing that I think you'll find very interesting is, like, hey, man, we've been working with state this whole time
33:48Well, how does state get involved in this crew? Well, it happens right here, whenever we set up our crew and give ourselves a task
33:54You'll notice that our email task takes in two things: it takes in the agent who will filter our emails, and then it takes in the list of..
34:03Emails that we need to analyze, so that's exactly what's happening right here
34:07You can see we're looking at the state variable that we're passing around, and we're looking at the emails key
34:12That emails key is just going to give us back all the different emails and IDs, and that snippet that we had set up previously inside our other node where we were checking your emails
34:22So it's all coming together, and we're passing down the emails so we can filter through them
34:24Let's go ahead and start looking through our task, where we can actually see what's happening inside this filter email
34:29Well, hey dude, analyze your emails, take out the promotions
34:33I don't want to worry about those
34:35It's your job to only give me the actual..
34:37Emails that are directed to the users
34:39And here is your list of emails, so Xia did a phenomenal job of formatting this task to make it very clear
34:46He did a great job delineating between, you know, the prompt and the dynamic information that I'm going to pass into you, so it'll be very clear and easy for the crew to read
34:56You can see here, this is where we're passing in our emails, and then we give a final piece of information saying, "Hey, your final answer must include the relevant, basically the relevant thread IDs and the center," and to use a bulleted list
35:08That's how it is set up
35:10So now, after we have set up and filtered out all of our emails and know who we need to respond to, we're going to go ahead and..
35:17Move on to the next task, an agent which is going to be our email action agent
35:22This one is going to be super straightforward
35:23The important part here is that we need to compile a list of emails that we need to start responding to
35:31Did this email that was sent to us require action on our endpoint? Great
35:34If it does, this agent is going to determine that and set us up for actually responding to it
35:40So, let's go ahead and look at the corresponding task for it
35:42If we scroll down, you'll notice the action-required email task
35:46We're not passing any variables here because our crew is working together
35:51At this point, what it's going to do is say, for each email thread, I want you to pull and analyze the complete thread using only the actual..
35:58Thread ID, and you're at this point going to have the context and understand the entire sentiment of the conversation, so it's really up to you to do the following: I want you, for each one of those emails, to list out the Thread ID, a summary of the email, give me the main points, who’s talking, make sure you identify the communication style and who I need to send the email to, just at a high level because that was kind of a lot
36:21What we're doing is pulling out all the information and really preparing ourselves to go ahead and write the email
36:26That's the next step—we're going to draft our response
36:28So all we're doing is, we've filtered out all the junk, now that we're looking at real emails, we're just trying to say, "Okay, well..." - Make a nice bulleted list of the actions I need to perform on each of these emails
36:42- The final step will be to go and draft a response based on the information we're putting together right here
36:48- I hope that makes sense
36:50- Now, we can move to the final part of our crew, which is our Writer Agent
36:53- Let's go ahead and head over to our Writer Agent so you can actually see what it's trying to do
37:00- The main thing, as usual, is to set up all the roles, goals, and backstory
37:03- For example, "Hey, you're just a skilled writer, and you're going to write emails." - What's different about this one is that we tell it that you have three tools to use
37:11Let me show you what each one of these is, so that you can actually understand what's going on
37:17I do think it's important
37:19At the same time, we'll dive into the task in a second
37:21The main thing is, hey, this agent has the ability to access basically the internet
37:25Toil—probably pronouncing that wrong—is basically just an API that makes it super easy for large language models to go off and search the internet
37:34So, you have to go off and get an API key, and we'll do that in just a second
37:38But, so, hey, this email person has the ability to go off and search the internet to maybe get some additional information about an email they're about to write
37:45Outside of that, it needs the ability to go off, pull more information, and reanalyze
37:48The emails, so that's what that tool is going to do—go off and reread the entire email list in case you need any missing information
37:58And then finally, you need the ability to go off and actually write, basically, an email
38:03And this is something that we have created because, if you look in the Create Draft tool, this is something we imported from our tools folder
38:10So if I hop over here to our tools folder, you'll see that, yep, right here, we use the tool decorator on this function to specify, hey, this is your ability; you can use this function right here to make emails
38:22So, what does it do? It takes information, and specifically, it takes in the email subject and message—the email, the person we're trying to send it to, or multiple recipients
38:31The goal is to identify the subject of the email and the actual body of the message, and as usual, we're going to use the toolkit to create a draft email
38:43We'll just call the draft function to generate the draft with all the information we've provided to the tool
38:44Alright, so you're now a pro on the email writer agent
38:47But what does this actual task look like? What are we instructing this agent to do? If we scroll down to the end, I think Xia did a phenomenal job of creating this task
38:55It's going to write a draft email, but the important part is how we convey that information and goal to our agent based on the required actions and the email content identified
39:05Everything that came from the agent above
39:07I'm not going to go into a ton of detail here, but go off and research the topic
39:20That was what Tali was for—go off and Google more about this topic if you need to
39:24Also, do you not have all the information that you think you need in order to respond to this email? Well, feel free to pull down the thread again, and that's exactly why we have our Get Thread tool
39:34We're giving our agent all the tools it needs to perform this task
39:38Go research this, go find out more about it, and once you're ready to go, now it's time for you to write it
39:45That's what this final part is for
39:46Now, finally, use the tool provided
39:48To go off and write an email, it's important that you include the following information: the two, so who's the email we're sending it to, the subject line, and the message
39:56That's how this all connects and how it all gets kicked off
40:00Actually, this happens inside of our CrewAI
40:03So, if we come back—oh, if we come back over to our CrewAI—you can see they all get connected right here
40:06Each one of our tasks is connected to an agent, and then finally, what we're going to do is tie it all full circle back to LangGraph
40:12If you remember, whenever we were creating our graph, what does our graph usually do? Well, our node performs an action and updates the state
40:24That's exactly what our CrewAI is going to do down here at the very end, once we..
40:28Kick off our crew with all of our tasks and agents included in it
40:32Once we kick off the crew, it takes a few minutes to run
40:34What we're going to do with that result is, as usual, to update this date
40:39This time, we're not updating emails or checked emails; we're actually updating the action-required emails
40:43These are the emails on which we performed actions
40:47In this case, we drafted an email, so that's exactly what's happening here
40:50I hope this all makes sense and you've seen how everything is connected, where we're updating the state, writing emails, and moving throughout our graph, basically
41:00The interesting part is what we do after we finish drafting
41:04Sure, you can guess it
41:07Our Edge tells us to go back and wait, and the cycle is getting kicked off once again
41:10So, we're going to go back to our wait node and then just redo the cycle over and over again
41:15I hope you guys have seen the power of how all this connects, and I know we've been doing a lot of talking and haven't actually set up or run it yet
41:22So, what I want to do next is walk you through the actual process of going through the readme
41:26Inside of here, I'm going to show you how to set up your environment
41:29We'll quickly connect your CrewAI to Gmail so that we can pull down your credentials
41:34We're going to set up everything you need to and turn it on
41:39Everything inside Google so that your crew has access, and after that, we're going to go ahead and run it
41:43So, let's go ahead now and hop over to Google so we can start pulling down your credentials
41:47All right, so what I've done so far is clicked this link which says "Follow these Google instructions to set up your authentication credentials for a desktop application," because that's exactly what we're trying to do
42:00We're trying to build a CrewAI that runs on our local desktop
42:03When you click that link, it will take you over to this page right here, which outlines all the instructions and steps you need to take in order to pull down your credentials.json
42:11The important thing to note is you're trying to generate an OAuth..
42:152.0 Client ID, that the goal, and these steps right here show you exactly how to do it
42:22So, what you do first is go and click your "Go to Credentials" button
42:25This will take you to a screen that looks just like this
42:28Now, it's super important to notice this is inside of Google Cloud Platform
42:32If you haven't already signed up to use Google Cloud Platform, it's completely free to use
42:38I would definitely recommend signing up and signing in so that you can see your dashboard just like this
42:43The next thing you need to do is create a new project
42:45Right now, mine says "CrewAI Automations." If yours says "New Project," don't worry
42:50Just click the dropdown, and it will prompt you to make a new project
42:54Just be sure to go ahead and call it
42:56It, CrewAI, something, just so you'll have an easy way to remember which project you were working on
43:01Fantastic
43:06So once you have your environment set up to show that you're logged in and you're inside of a CrewAI project, you will now start going over here to the Credentials window that you're looking at right now
43:11The important thing to note is, we said our goal was to set up an OAuth 2.0 client ID
43:16Since I've obviously been running my Crew locally, I already have one of these client IDs, but I'm going to show you how to do it
43:23The first thing you're going to do is click "Create credentials" up top, and then it's going to ask you which kind
43:29Well, in our case, we want to do an OAuth client
43:31From there, it's going to ask us which..
43:33Application type, we would like to use, and back in our instructions, it says we want to make a desktop app
43:39So, click that, and then we're going to give ours a name
43:43I'm just going to call mine "YouTube Crew AI" and then "LangGraph." It can take a few minutes to actually generate these credentials, but the important thing is that it will come with this screen
43:55Now, make sure not to exit out of it
43:59The thing you need to do is download the JSON file because, when you download this JSON file, it is the file you actually need to go ahead and import into your local project directory
44:08Just as a recap, this is super important to keep secure, and you don't want to share this with the world
44:14So, how do you go… About not sharing this file with the world, the first thing you need to do is make sure that whenever you drag it over, your .gitignore file includes the name of this JSON file because we don't want to be sharing it with the world
44:27I cannot repeat this enough: do not accidentally save this to GitHub, because you don't want to save your key to the public
44:33Anyone could start pulling down your emails, which is a big no-no
44:37So, make sure that whenever you do pull it down, you have updated your .gitignore to specify that you do not want to save your credentials
44:42 Okay, so we have downloaded our file
44:48What I can do is, if I go ahead and hover over our downloads, I can see my recent download called "client"
44:54Secret 7456, so let me go ahead and find that in my downloads real quick
44:59I'm just going to go ahead and drag in my client secret over here, so you can see that it's just a part of it
45:08I'll close out of that, but the important thing is you'll notice my client secret isn't the right name, so what you'll need to do is click this file and then rename it
45:14I'm going to go ahead and delete my old one, move it to the trash, and rename this one to your credentials.json, just like that
45:21Perfect
45:22Now you have your credentials.json saved to your file
45:25 Now, there's one other step we need to take over in Google, and this wasn't called out in the original README
45:31I had to update it, but if you type in the Gmail API..
45:33You'll need to type that in, and eventually you'll want to click this right here
45:38What you need to do is enable this in order for your local credentials to have access, or basically, this project needs to have access to the Gmail API, because that's how we were pulling down emails and writing up drafts
45:53Make sure you enable it, and once you've done that, you're all good on the Google front
45:57What we're going to do next is keep chugging along, and we're going to go back over to our V file, because we need to do some homework
46:03The next step is to update your email address, so go ahead and open your ENV file and update your email address to whatever you were using inside your account you're connecting
46:12It to and then from there, you and I are going to go update and create a Tally API key
46:19So, what are we going to do for this one? Well, we're going to go to Val.com, and we are going to go ahead
46:23This is completely free to use, with a super generous pricing
46:28If you look at this, if you're free, you get up to 1,000 calls per month
46:32So, we're going to go ahead and get started
46:33I've already signed in, so you can see—yeah, I've already signed in
46:37If you haven't signed up, it's completely free
46:41What you'll do is you'll be brought to a page that looks just like this, where you'll have access to your API key
46:44So, you're going to want to click the copy button and then come back over to your file and paste it in here so you can start allowing
46:52Your crew to go off and access the internet in an intelligent way
46:54If you don't want to use Tali, feel free to use Serper or if you want to get fancy and try something else, you can use DuckDuckGo or whatever search tool that you like
47:03When working with your crew, alright, cool
47:05And the final thing—you know how to do it at this point—is to go over and update your OpenAI key inside of OpenAI
47:09Just generate a new key, save it here
47:12Awesome
47:14So once we have done that, we are in the home stretch, boys
47:16What we're going to do next is we have one or two more final things that we need to do, and one of them is actually going to be setting up our local environment that will be able to run this crew
47:25So, what we're going to do is we're going to hop back over to our..
47:29Let's set up our crew with all the requirements so we can kick this thing off
47:32All right, so welcome to the terminal
47:35This is where we're going to install all of our Python dependencies, set up our environment, and go ahead and run our crew
47:39So, let's speed through this
47:44The first thing we need to do is create a new Python environment that includes all of the dependencies we set up earlier
47:48Here's how we do that
47:53We are going to use Conda, and we're going to create a new environment that will store all of our dependencies for this project
47:57So, what I'm going to do is create a new Conda environment with the name CrewAI LangGraph, just so I know what this environment is for, and I'm going to specify..
48:07The Python version I’d like to go with is 3.11, so I’m going to go ahead and kick that off, and it’ll create a new Python environment for me
48:13One thing you might notice is, what is Conda? Well, Conda is just a tool that allows you to quickly create these Python environments
48:22If you don’t have Conda on your system, I definitely recommend watching a quick Conda for Linux or Conda for Windows tutorial, and it’ll walk you through the complete setup
48:30It’s super simple, but eventually you’ll get to a point where you can start running Conda on your machine, and it’s going to make managing all your Python environments so much easier
48:34Fantastic
48:38So once we have actually created our new environment, we can dive into it
48:42And start using it
48:46Right now, you'll notice I'm in my base Python environment, and I can easily switch over to my new environment by typing in this command
48:49Once I've done that, you'll notice it no longer says "base"; it actually shows the name of the new environment I just created, which is super cool
48:57 So, what we can do next is go ahead and install all these different requirements inside our new Python Conda environment
49:00We can do that by typing pip install, and I'm specifically trying to install packages with the specified version numbers inside my requirements.txt file
49:10 I'm going to go ahead and do that, and this will once again take just a few seconds to run
49:17Install everything inside our project
49:18Once we have done that, we should be good to go and run our CrewAI
49:22I'm just going to go ahead and say, before we actually run it, whenever you go to run `python main.py`, which is going to kick off our LRA workflow and start our Crew, when I ran this the first time, what happened to me was it said, "Hey, we need to access your Gmail account." If you remember, we went ahead and created all those new configuration files, and we now give our local Crew access to Gmail
49:45So, you're going to have to do that normal thing when you use a new application, like saying, "Hey, do you allow Google or this tool to access Google?" The answer is yes
49:51In our case, we want our local tool to access our..
49:56Emails, so we can start pulling them out, pulling them down, and writing draft emails
49:59You're not going to see that screen here because I've already accepted it in the past, but just notice on your end you're going to have to press Yes, I consent, and allow Google to start writing emails for me
50:07So, I'm going to go ahead and kick it off just so you can see this tool in action
50:12We're going to start running our crew, check our emails, and you can start to see the draft emails that I have typed up and sent to myself as dummy emails
50:21You can see, yep, our tasks are working well
50:23Let's start from the beginning: we went ahead and pulled down our draft emails from Google, and then from there—this actually, because we found emails, if you..
50:28Remember, LangGraph — we found emails, fantastic
50:33They're new emails, so let's go ahead and actually start filtering them from there
50:37We're going to pull out the core information so you can see
50:39Hey, go filter these emails, fantastic
50:40Well, we've now analyzed all of these, and here's the snippet for each one of these emails
50:47Now that we've done that, we need to pull out the core pieces of information from each email
50:50That was our email action specialist
50:52Like I said, you can kind of see how all of it is coming together now
50:57So, we found the filtered emails, and now we're going to pull out the key pieces of information from each one of those emails
51:03This is what it must include, and as you can see, we went ahead and actually started
51:07Putting together exactly what is needed to go off and eventually write our emails, and that's what is exactly going to happen in the next step
51:16So now, we're going to go over to the email response writer
51:18That was the third step in our tool
51:20Now that we have all the information, we're going to go ahead and write
51:23You can see that's exactly what's happening here
51:25We're going to start to evaluate our tool that we set up
51:28It's going to go off and search for things like, "What the heck is CrewAI? What the heck is LangGraph?" and it actually found information on the internet
51:32So, it's not only just using local context, it's using the internet to help us write a response
51:37Then, from there, eventually, we're going to go ahead and call the create draft function, and that will complete the process
51:43Custom tool we created from Google, if you remember
51:45Over here, this is our create draft tool, so it's using the tools we provided to it and actually typed up a nice email response
51:51"Thank you for your email
51:52I've been looking forward to doing this
51:54It's awesome," and so forth and so on
51:56And, uh, best, Brandon, because I wrote the email, and it's going to just keep going
52:00Then, eventually, after typing up those emails and responses, we're going to reach a point where, like, yep, there are no more emails
52:05I've already written draft emails, so I'm just going to go off and wait for 30 seconds, and I'm just going to continue the cycle over and over again
52:11So, all around, I hope you think this is super cool, and you now have a new use case for using CrewAI, and you..
52:16I have also leveled up your CrewAI game because you now know how to use LRA intelligently to go ahead and manage state and trigger off your Crew in a special way
52:25So I hope you guys have really liked seeing how everything connects
52:29I know it's a little bit longer, but I really hope you guys enjoyed and learned a ton today
52:34But that's a wrap for this video, guys
52:40I hope you enjoyed learning about CrewAI, LGraph, and setting up your own email automation tool
52:45 And just as a few quick recaps, if you got stuck at all during this video or if you just want to meet other like-minded AI developers, you're definitely going to want to check out that free Skool community I just created for you guys
52:50Outside of that, you can download the free..
52:52Source code below
52:54Just click the link down in the description
52:55Finally, I have a ton of other AI-related content on this channel, so you'll definitely want to check out any of those videos after watching this one, and you probably definitely want to check out the videos that YouTube is recommending right after this, wherever it's at on the screen
53:07But enough of that, hope you guys have a great day
53:10Talk to you soon
53:10See you! [Music]