In this video, you're going to learn the fundamentals of LangGraph including nodes, edges, and graphs. From there, you're going to learn how to connect CrewAI with LangGraph so that you can build more advanced automations and level up as a CrewAI developer.
Timestamps: 00:00 Introduction 02:36 What is LangGraph 05:28 Code Overview 09:37 LangGraph Setup 16:47 Setup Nodes 31:42 Setup CrewAI 41:51 Setup Google Authentication 46:21 Create Tavily API Key 47:34 Setup Local Environment / Install Dependencies 49:21 Run Crew with LangGraph 52:38 Outro
Hey guys, in today's video, we're going to be using Crew AI and LandGraph to build an email
automation tool. This tool is going to connect to our Gmail account, pull down our latest emails
that we get, and actually write up draft emails for us to help save us a ton of time. Honestly,
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. Let's go ahead and actually hop over so you can see this
demo in action because it's going to blow your mind. Let me show you how cool this tool is.
What I've done is I've typed up myself two different example emails. I sent one saying,
hey, what do you think about LandGraph and Crew AI? Then from another email, I sent to myself,
hey, would you be interested in doing a sponsorship? Those are just two draft emails.
What this crew has done, which I think you'll find is super cool, it actually analyzed these emails
and typed up draft emails. You can see I have two draft emails waiting to send back to each one of
these emails. You can see for the YouTube collaboration, hey, thanks man for the kind
words. Could you please provide me more details about the sponsorship, which is super cool. I
didn't type any of that up. Same thing for this LandGraph email. It went ahead and said, thanks
for asking about my thoughts. It just continues to carry the conversation so I can quickly respond
back to the people who are sending messages to me. As you can see back in here over in the code,
we have our crew 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 email promotions. It's going to pull out emails
from real people, go in there, analyze the text and actually generate responses. That's what's
happening inside of our crew. I hope you guys are pumped to start building this. Per usual,
I'll have the source code down below for free. This is actually an example pulled from the crew
AI tools demo, but I'm just going to be walking you guys through it line by line. If you want to
level up as a crew AI developer and not fall behind, you're definitely going to want to watch
this video the whole way through. Also, real quick, I want to mention I created a free school community
just for you guys. It's called the AI Developer Accelerator. If you get stuck at all during this
tutorial and run into any questions, feel free to hop over there and ask your questions. I'll
answer it or someone else in the community will be sure to get you unstuck so you can keep building
out your awesome crews. I also want to mention you're going to meet a ton of other like-minded
AI developers over there from all different skill levels. You're definitely going to want to check
it out right after this video. Enough of that, let's go ahead and dive into the video.
All right, so here's a roadmap of what we're going to be covering in this video.
First, we're going to be talking about what is LandGraph. Then we're going to be talking about
why you would want to combine LandGraph and Crew AI together. From there, we're finally going to
actually dive into the code where we're going to start building out that email automation tool
where we're combining the LandGraph and Crew AI together. Let's go ahead and dive into the first
part where we're going to talk about what is LandGraph. The main reason you would want to use
LandGraph with Crew AI is because LandGraph allows you to intelligently kick off your crew
when certain conditions are met. Let's actually dive into a few concrete examples with this
diagram so you can see how powerful this tool is. What I've done is I've created a super simple
diagram inside of here with LandGraph. We have a few different actions such as wait 30 seconds
and kick off our crew. Then we have a condition where we're just going to check here in a few
seconds for like, did condition get met? Yes. Great. Go kick off the crew. If no, all right,
cool. Just go back and wait. Let's look at two different examples just so you can see this in
action. Let's imagine we were building some sort of stock analysis tool. What we would do with this
stock analysis tool is give buy or sell recommendations and some justification for
why we recommend that. Well, instead of you and I manually kicking off our crew every time we want
to do this report, we could actually have this crew respond to market conditions. For example,
let's say we were monitoring the price of Apple. It went up 5%. Well, 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 go analyze what's happening inside of that stock, write a
report for why we think it's happening, and then suggest a buy or sell alert. Oh, if it hasn't met
this condition, cool. We'll just go back and wait until something does happen interesting in the
stock market, which is super cool because this means you and I get to build tools that we just
set it, set them running, and then forget it because they're just going to be running in
the background. Or we could have something else. Imagine we were doing some sort of newsletter.
What we could do is basically build a real-time newsletter. Let's just say, imagine we were
tracking what Elon Musk was saying on X. We could say the condition could be, has Elon tweeted
something new? Great. If so, let's kick off our newsletter crew that's going to go off and write
a newsletter and send it out to all of our subscribers or put it in our draft, and we're
just going to quickly comment on his latest tweet, what we think it means and our thoughts about it,
and we could do whatever we want really, and that's the cool part. Once again, if he hasn't
sent out any tweets, great. We're just going to go back and wait for 30 seconds. I hope you see
how this actually sets up. In this super simple example, we're going to be doing something
exactly like this because we're going to be building our email automation service where
we're going to be waiting and just looking at our email inbox. We're going to check to see if we
have new emails. If so, we're going to go write our draft email. This is the exact same flow that
we're going to be building out here in just a few seconds. I hope all the dots are starting to click
now that you've seen a little bit of what LandGraph can do in these super simple examples,
but let's go ahead and actually start heading over to the code where we're actually going to
be building everything up, and we're actually going to start coding out and setting up LandGraph
inside of our application. That's where we're actually going to dive into the nitty-gritties
where you'll learn about setting up nodes, edges, and state, but all that will make more sense once
we dive over to the code. Let's go ahead and hop over. Now we're going to dive into the fun part
where we're actually going to start coding up this project. Just to help paint a roadmap of what
we're about to do next, first, we're just going to walk through all the files at a high level,
just so you understand what's inside of this project. From there, we're going to dive deep
into LandGraph, really looking at the three different files that really set up LandGraph,
which are going to be our graph, nodes, and state.py files. We're also going to really dive
into the core fundamental technologies and terms that you use to set up your LandGraph.
Then from there, we're going to hop back up and start talking about our crew, just so you
understand what it's trying to do. Then finally, what we're going to do is we're going to go off
and actually start setting up our connecting of our Gmail account over to our local crew.
That way we can actually have our crew pull down some emails and write those draft emails that you
saw in the demo at the beginning of the video. 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. Right after that, we're
going to hop over to our LandGraph files. At the top, what you'll notice inside of our source
directory file, we have pretty much the normal stuff that you would expect to see. We're going
to have our main.py. This is where we're going to kick off our LandGraph and we're going to kick
off our crew. From there, you're going to notice we have this thing called a credentials file.
You're going to make that in a little bit at the end of the video. That's going to store all of
your sensitive information for actually connecting your crew to Gmail. Then a few other things that
you'll notice is we have our requirements file. This is where we're actually going to go over and
pull down and set up all of our dependencies to basically create our Python environment to run
this tool. Let's look a deeper dive into the actual code itself. Going into the LandGraph side
of things, you'll notice there's a graph. The graph is basically, we'll dive into this in a
minute, but basically this is going to contain all of the nodes and edges inside of our graph.
Don't worry if that doesn't make sense, we're going to dive into that later. Then from there,
we're going to be able to set up each one of those nodes. A node, this is where we're going
to actually perform actions and do things at each different state. Don't worry, this will all make
sense. I'm dropping seeds. Later on when we come back to them, you'll understand, oh yeah, we did
talk about this. Then finally, we're going to go about setting state. This is just basically,
we're just trying to remember the state of things inside of our graph. That's what's happening on
the LandGraph side of things. Then if you're more familiar with Crew AI, you'll be pretty familiar
with a lot of these tools. First off, we're going to set up our crew and our crew is going to be
contained a bunch of different agents and it's going to contain a bunch of different tasks.
This should look pretty familiar to you if you've worked on crew projects in the past.
If not, don't worry, I have a ton of other crew AI related tutorials on this channel.
I'll probably pop up a card in the corner so you can watch some of the foundation videos and then
also watch some of the more advanced videos just so you can really get your feet wet and start
actually experimenting with this tool. This is all very basic stuff and you can actually see
per usual, we're going to set up our agents to do certain things such as filter emails,
actions and write responses. Then each one of those agents is going to be given tasks that
pretty much have the same name. Outside of that, we're going to do some fancy tools,
which is going to be creating a draft and that's where we're going to have our crew
create a draft email. Okay, cool. Now that you understand everything that's happening at a high
level inside of this project, now let's really go ahead and dive deep into LandGraph so we can
really start talking about some of the core terminology and to make you a graph professional.
Let's go ahead and start working on that now. All right, now we're going to dive into setting
up LandGraph. I've actually set up a ton of visuals to make this super easy for you to understand
and to grasp how everything connects. Before we dive into that, I just want to take a quick tangent
and just let you know that building graphs, which is what we're about to do right now,
is super foundational to becoming a software developer. If you went to college to learn
software development or computer science, basically you would learn in data structures
and algorithms, you would learn about graph theory. We're going to throw all that out the
window and just learn a super basic version of it for this demo right here. I'm pumped for you guys
to get to learn this because once you understand graph theory, your mind opens up to understanding
a bunch of different problems that you might not have already had a tool to solve. We're basically
building or we're adding a new tool to our tool belt, so we're going to become a better developer
and more specifically, a better AI developer. Let's go ahead and start diving in and setting
up our LandGraph. When you begin to work with LandGraph, there are four different terms that
you need to know. The first one is going to be nodes and think of nodes as different actions
that we want to take. The next term is going to be state. State is nothing more than the ability
for us to memorize what we have done in the past. How many emails have we sent? What emails have
we checked? Which draft emails have we written up? That's what states for us to help us to remember
what we've done in the past. The next two terms that you need to know are edges. Think as edges
as a connection between two nodes. You'll see this in just a second, but just edge. Remember
that is a connection between two nodes to say, can node one talk to node two? That's what that's
for. The final term that you need to know is graph. A graph is nothing more than just a collection of
nodes and edges. Cool. You now know all the terms for a LandGraph. Hey, you've already done 80% of
the work and learning it. Now, let's actually hop over and see what this looks like when you
visualize these new terms. Welcome to my beautiful Excalidraw drawing. I just want to show you all
of the new terms that you just learned. What now? Right now, what you're looking at inside of this,
this is a graph. We have nodes and edges. That's term one. It's a collection of nodes and edges.
Now, what is our nodes? If you remember, we said our nodes are different actions that we
want to perform. Let's actually hop back to our code so we can visualize these side by side.
I want you to get to the point when you look at these files, you can almost visualize one of these
graphs right here. I'm helping you become a graph visualizer. Let's look at the different actions
that we can take with our nodes. Well, the first one is we can go check emails to see which emails
we have out there. Then we can wait for the next run. We want to check emails, then wait a minute
or two because emails aren't coming in every second. Then the third action we want to perform
is new emails. That's what our three different nodes could be. We could say,
hey, go check your emails. We have another node for waiting and then we could have another node
for writing up our draft emails. Those are the potential actions that we could take.
Where LandGraph comes in and where all the magic happens is we can say how we can move between
these different nodes. For example, imagine we just checked for emails. Well, after we check
for emails, we might want to write an email. We might just want to wait if we didn't find any new
emails. That's actually where edges come in. Let's hop back over to our graph so you can see
where our different edges get added. We can see after we draft a response, we'll dive back over
here. The three different nodes we can have are check new email. These are the three different
actions. Now we can say, okay, well, how can I move between each one of these actions? The way
we move between nodes is through edges. Node one connects to node two or node two connects to node
three. You can see this one's a two-way. It's a bidirectional edge. That's how you learn it. This
is one direction, unidirectional or back and forth bidirectional. You are becoming an official
developer. This is awesome. That's what's happening with our edges. We are setting up the movement
between all of our nodes. Let's actually go ahead and translate what we're seeing in here
to our actual code. We can see we want to add an edge between draft responses and wait for the next
email. We said draft response to wait to the next email. This one we need to set up to go from left
to right, from draft to wait. The next one is we want to go from wait to check emails. We're going
to go this way this time. We're going to go from wait to check emails. Fantastic. As you can see,
we're starting to build up a flow between all of our different nodes. Then what we can do in
addition to that is there's this thing called additional conditional edges. This is where we
want to check. Like, hey, did something happen? Great. In that case, I want this node to connect
to this node. If you actually hover over it, you can see. What is the starting key? In our case,
this is the starting node. Then if it worked out, well, what do we want to have happen next? In our
case, we want to do a check new emails. We want to see if at the check node emails node, we want to
check to see if we have emails. This is a Boolean. If we do have new emails, great. We're going to
go draft a response. If not, we're going to go to wait to next run. It's like a decision tree. Hey,
do we have new emails? If we do, great. Go write a response for them. If not, we'll just wait to
the next run. Let's visualize that now that we know we can add this edge, more specifically,
a conditional edge. We agreed at check new emails is the starting point right up here.
What we're going to do is have two different types of responses that are conditional. First one is
draft responses. We'll make this one conditional so you can kind of see what's going to happen.
Fantastic. This is if we have emails to respond to. Fantastic. Then the next one,
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 if no new emails. All right, cool. What's interesting is this, what you're looking
at right here, this entire graph is exactly what this is right here. We have turned our text into
an actual visual. I hope you guys are getting the hang of understanding, oh, that's what a node is.
That's what an edge is. Oh, this is what a graph is. It's just a collection of nodes and edges.
Okay. Now let's dive into the final part of it, which is going to be state. If you remember,
state is our ability to remember what's happening inside of our graph. Let's actually dive back over
to our code and understand what was going on. We said there was three different things that
we wanted to remember. Checked email IDs. Hey, have we seen this email in the past?
The next one is just going to be emails. These are all the emails that we have. Then the last
one is going to be action required emails. Oh, we need to perform an action on this email,
such as writing a draft or something like that. Okay. That's the final thing that's going to
happen. What's interesting is each one of these nodes can update our memory. Whenever we check a
new email, well, what do we want to do? We want to add those new emails to our memory. Oh, I just
drafted a response. Okay, cool. I no longer need to write a draft. I need to update our memory to
go, yep, this one check. We have written an email, so we no longer need to act on it anymore.
I hope this is all clicked. I would definitely recommend watching this part twice because there
was a lot of new concepts, but once you basically have added this tool to your tool belt and have
fully understand how to go from one of these graph.py files over to a visual, you'll kind of
understand, oh yeah, that is how everything connects. Honestly, I'm a big fan whenever I'm
building one of these projects, I will start with a visual first and then I'll translate it over to
code. If I was building this project, I would have built this first and then I would have coded up
all of this over here. I hope that has a big help as you go off and build your own crews.
Okay, so now that we've done everything at a high level with working with LandGraph,
what I want to do next is actually dive deep into setting up the nodes so you can fully understand
what's happening inside of each one of these action steps, because this is where our nodes
are going to start updating our memory. Let's go ahead and start diving into this.
All right, so let's go ahead and look at each one of these nodes and see what actions they were
going to perform. I'm not going to go super deep into coding all this up because it's super
straightforward when you actually read the code. The 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 things
that we're working on. When you're off building your own LandGraph and crew AI tools, you'll have
a core understanding of like, oh, that's why we were doing it and that's how everything connected
up. To help visualize what's going on, I went ahead and actually typed up some additional
and updated our visualizing our diagram so you can see here are nodes and here's our state.
This way, as we go off and run through our code over here, we can hop back and forth and understand,
yep, that's how everything connects because that's the important part of this.
Okay, so we're going to speed mode through the first one, which is going to be checking our
emails. If you remember, what are we trying to do? We are trying to look at emails inside of
our inbox because we want the ability to respond to them. 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.
What we're doing is setting it up, our Gmail tools, and then the important part is we're
setting up our custom instance of starting to search through our inbox. Now, how the heck does
this actually connect over to our Gmail? Well, great question. The first one is we went over
this in the later part of the video, but we need to pull down our Gmail credentials.
That file is a super secret file, which actually includes all of the pieces necessary for these
tools to connect over to our Gmail account. That's what's happening here. Now, let's dive
into the parts that we actually have control over and how they interact with our state.
The first one is we can actually update the search parameters and search for different things inside
of our Gmail account. We could look for all emails that are basically one day old. That's what that
means. Or we can actually do something like from Brandon9598. You can actually search from a
previous certain sender. There's a bunch of different things that you can do. I would just
look up Google Docs versus Gmail and actually see what are all the different custom queries you can
do. All right. What we need to do next, this is where we're going to start getting a little bit
deeper into working in Python code. I'm going to try and make this as simple as possible.
The important part is we need to look through all the emails that we've previously looked at,
because if we've already looked at those emails, there's no reason to look at them again. That's
exactly what this is doing. We're going to do a turn area to say, hey, I want to look at my
checked email IDs state. Does it exist? Great. Give me all the items inside of it. Oh, if it
doesn't, just give me back an empty list. That's exactly what that means. Over here, just to show
you, if this list said, hey, we looked at email 1, 2, 3, 4, 5, we would actually just return that email
IDs of 1, 2, 3, 4, 5. That's exactly what's happening here. Now, 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 if they're a new email that we haven't looked at before, well,
we want to store that email so that we can later on type up a response to it, because that's our
end goal. That's what we're going to do in this for loop here. We're going to go through each one
of our new emails, and emails can be chained together in a thread, so that's why you're going
to see this term thread. Let's walk through it. We're going to iterate through each one of our
emails. If this email was already contained in our checked emails list, skip it, because we don't
want to look at it. If this thread ID is not in the thread, meaning we're looking at a completely
new thread, and does the sender of this email not equal my email? Well, where did this my email come
from? Well, if you go over to your .env file, you'll have the ability to come in here and update
your email. You'll also have the ability to update your other environment variable keys,
but more on that later. The important part is you need to keep this file very safe,
but we'll talk about how to do that later. The final part of this, just to recap,
hey, is this a new email? Did this email not already exist in an existing thread? Great.
Also, was my email not the sender of this email? 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. If we meet those three criteria, we're going to go off and start
building up our list that we have right here. We're going to add the new email thread ID over here.
That's what the append is going to do. The next thing is we're going to actually store the key
pieces of information we need about each email so that we can later respond to them, such as the ID,
the thread ID, a snippet. This is just like a quick text blurb of what was said in that email,
and then who sent the email. We want to know their email address.
After we've gone through that for each one of the emails that we've just searched through in our
for loop, we want to do the two things. We want to update our email list inside of our state,
and we also want to update our checked emails list inside of our state. What 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? I'm adding stuff after the
double stars. What does this mean? The best way I like to think of it is this double star just
expands whatever your current state is. 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. You'll see how we passed in state right here.
Our state is going to get passed around between all of our nodes, because each of our node needs
the ability to look at the current state and edit it. That's why we're having to pass it around
everywhere. What we're going to do is this state really means this. I'm going to comment it out so
you can see what actually it means. Checked email, copy this a few more times, and perfect.
This is actually what that double star syntax means. It's just a quick shorthand so you don't
have to type it out. It's basically saying, hey, expand out my current state. I actually messed up
because I should have put these to the side too. My bad. I'll do this real fast just so you guys
can see everything in action. Then the next would be emails. Then the final one would be
the action state. This is exactly what this represents. It's our current state. What is our
current state? It's these three things, our checked emails, emails, and actions required.
We've expanded them all out with our shorthand. Why is this helpful? Because in this check email
function, where we're checking to find new emails, we are responsible for updating new emails we
find and updating any checked email IDs. That way in the future, we can skip having to check
emails if we already found them. The key words in there is I'm responsible for updating it.
Well, that's exactly what our code is going to do. Our code right here is going to override
existing state. This email parameter right here, property, is going to override this one. We'll
just go ahead and delete that. This checked email new property is going to override the existing
one. Basically, the important part is anything that has ... Since these have the same property
name, the one that comes at the end is going to override the one that's the previous ones.
Later, we'll always override previous. What'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 emailed and checked
email IDs, and we have completely left alone the action required email state. That's why it's so
nice because with just doing this right here, we have updated state for the two things we wanted
to change and completely left alone the other part of our state that we just, hey, we're just
putting it to the side and going to pass it along later on when we need to use it later.
That's why this is a super quick, nice, short shortcut for updating state. You can see,
what are we doing? We're returning this updated state. That was definitely a lot, but know this
is a very common practice inside of programming. If you have any questions, once again, please drop
them below or add them over to school, but definitely watch this part at least twice
because this is super fundamental for updating and sharing state throughout our application.
Let's keep chugging along to the two other short parts of our nodes. The other one is just wait for
next run. Basically, what 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. That's what's going to happen. After we
wait for 30 seconds, we're just going to return the state that was passed to us. Hopefully, you
can start to see a pattern here to where we take in state, we update it, or we don't do anything.
We might just reference it and give it back. That's how we're sharing state between all of
our nodes. The final one is whenever we're going to be working with new emails, well, this is really
just a Boolean because if you remember back over here on our graph, whenever we were doing our
conditional edge checks, we were, hey, we're at this node, and based on if our condition is a
true or false, we want to do two things. If it's a true, go over here. If it's a false, do this.
I hope this is starting to make sense. These are really the two important nodes that you needed to
know about, the check email and the wait to next run. The third one that you'll notice in the graph
that wasn't referenced right here was draft response. Now, we have gone through the nodes.
Now, we're going to start connecting things in our graph. This is where things are getting
interesting because this is where Lang Graph and Crew AI merge. It happens right here on these
lines. Let's go ahead now and start diving through our graph.py file and walk through it line by
line so you guys will have a better understanding of how this all connects. The first thing that's
important to notice is inside of this graph, what we're doing is first, we're importing everything
that we set up previously. You can see up top from our state, we are importing our email state.
That's what we're importing right here. Then you can also see that we are setting up our nodes from
our nodes.py file. We're bringing all of our nodes and state over because now that we have
everything that we need to set up our graph, we can start setting it up down here. The other
important thing is we also have the ability to import our crew. We're just importing that from,
if you look inside of our crew.crew file. We're bringing in this email filter crew into our
workflow and basically inside of our graph. We're bringing in everything. Now, it's up to us to
connect it all together. Here's exactly what we're going to be doing. The first thing is we,
inside of our workflow, because that's how link graph works, is it's a workflow that allows us
to direct how our application flows and passes around and manages state inside of the graph.
What's actually happening? Well, right out the gate, we need to define all of the nodes inside
of this graph. Even though we've created them over here in this file, we need to add them to the
graph. It's super simple to do that. We make a new node type. Then what we do is we come over here
and we start making our workflow. A workflow is nothing more than a connection of nodes, edges,
and state. That's exactly what's happening here. You'll see we're to check that state box. Whenever
we create our state graph, which is coming from link graph, you can see we're saying,
this is the state I need you to keep track of. That's why we're doing that. We've checked off
state. Now we just need to add in nodes and edges. That's what we're going to do
right inside of here. Inside of our workflow, we're going to add a node saying our check
news email node. Well, that's the key. Well, what's the value? Well, it's a node. It's going
to be our check email node that we set over here. You'll notice right now that all of these are just
functions that take, they don't even take an input except the state. Then they just return
an updated state. That's what's expected. It's up to us to say, if you look at an add node,
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. All that means is, hey,
we need to run the node. We said nodes are actions. Highlighting over that, what we're
trying to do is we're creating all of our nodes and giving them names. 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.
Here's the new node that we haven't talked about yet, our draft responses node. Well,
what does this node need to do? Well, it actually needs to go off and kick off a crew. We're going
to dive into crew AI in just a second, but 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. Awesome. The final thing that's important inside of here
that we haven't got to fully talk about is like, okay, we now 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. Where do we start in this graph? Well, that's something
we have to define inside of our application. So we say, hey, the starting point is check new emails.
So let's go ahead and give ourselves a new, I think just like a nice starting point or starting
here. So I'll just put start. This is where we're going to start. And then finally, what we have to
do is add in those conditional edges that we talked about earlier. We've already shown how to add in
those lines right here. And just as a final thing, hey, whenever we run this node, we have two options
that can happen afterwards. The first one is, did we get new emails? Great. If we did, continue to
the next step. If not, you're done, buddy. Just end. And what does end mean? It means just go over
here and wait 30 seconds to the next run. Okay, cool. And then outside of that, edges, the main
thing to think about edges, these are just communication channels that we allow to flow
between. The important part about LandGraph is we are very strict in the way flows can happen.
So that's what's happening here. And then finally, we're just compiling this workflow
into an application that we can then later kick off. Well, 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
what we do here. We import our graph that we just created that contains the entire state,
flow, how everything's moving around. And then we just say, great, here's the app.
And let's go ahead and kick it off. And you 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.
All we're trying to do is just go off and kick off our graph that we just set up. I hope you
understand LandGraph now, because we've talked about all the core principles. You now know how
nodes, edges, starting points, conditional edges. You now have a full grasp of that.
So we're going to put LandGraph to the side now. And what we're going to do is hop over to
Crew AI and start actually investigating what the heck is happening inside of this email filtered
crew and how does it correspond to our actual state that we were setting up and using throughout
the rest of this process. So let's go ahead and start hopping over here and start analyzing our
crew. So we're going to speed mode through this part because we've already set up a bunch of
crews, but I just want to really hone in and focus on the parts that are super specific to
the overlap of Crew AI and LandGraph, because that's where the important part is.
So let's dive into the important things. So per usual, when we set up a crew,
the first thing that you really want to do is set up your agents, tasks, and tools. Let's actually
dive into each one of these. Whenever we create our crew, the first thing we do inside of our
constructor is make all of our different agents. So 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 email
action agent, and our email response writer. So let's actually dive into each one of these
and talk about the core purpose behind each one of these agents and see how they relate to one
another. Inside of this email agent file, you'll see once again, we are importing our Gmail toolkit
because we want to give our crew the ability to go off and actually write draft emails. So that's
why we're importing this tool. The first one that we want to do is whenever we kick off our crew,
is we want to let our filter agent go off and filter out the non-essential emails like newsletters
and promotions. Because once again, we only want to respond to real emails that people have sent
us. And we are going to not allow this agent to go off and delegate work to other agents. It's,
hey, here's all the emails that you got. And we'll talk about state here in a second, but I need you
to look at all the emails you got inside of the state variable that we're passing it around and
look in there and find all the emails and filter out everything that looks like junk and only give
me the emails that look like we need to respond to. And I think the best way to actually look at
this is to go off and look at the task that corresponds to this agent as well, just so we can
do a side-by-side because that's exactly, if you come down here to the kickoff part, you can actually
see that's what we're doing here. We're going to create those agents and then we're connecting
each one of those agents to a task. So we're looking at the filter agent and we can see the
filter agent is used inside of the filter email task. So let's go ahead and dive into the task.
But the thing that I think you'll find very interesting is like, hey man, we were this
whole time working with state. Well, how does state get involved in this crew? Well, it happens
right here. Whenever we set up our crew and we give ourself a task, you'll notice that our email
task takes in two things. It takes in the agent who's going to filter our emails. And then it
takes in the list of emails that we need to analyze. So that's exactly what's happening right
here. You can see we're looking at the state variable that we're passing around and we're
looking at the emails key and that emails key is just going to give us back all the different
emails, the IDs, and that snippets that we had set up previously inside of our other node where we
were checking new emails. So it's all coming together and we're passing down the emails so
we can filter through them. Let's go ahead and start looking through our task where we can
actually see what's happening inside of this filter email. Well, hey dude, analyze the batch
of emails. Don't take out the promotions. I don't want to worry about those. It's your job to only
give me the actual emails that are directed to the users. And here is your list of emails. So
Xiao did a phenomenal job of formatting this task to where it's very clear. He did a great job
delineating between here's the prompt and here's the dynamic information that I'm going to pass
to you. So it'll be very clear and easy for the crew to read. And you can see here, this is where
we're passing in our emails. And then we give a final piece of information of saying, hey,
your final answer must include the relevant, basically the relevant thread IDs and the
center use bulleted list. That's how that is set up. So now what we can do 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 move on to the next task and agent, which is going to be our email action agent. And this one
is going to be super straightforward. The important part here is we need to compile a
list of emails that we need to start responding to. Did this email that was sent to us, does it
require action on our endpoint? Great. If it does, this agent is going to determine that and set us
up for actually responding to it. So let's go ahead and look at the corresponding task for it.
So if we scroll down, you'll notice the action required email task. We're not passing any
variables here because our crew is working together. So at 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 thread 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 basically 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 address to. Just at a high level, because that was kind of a lot. What we're doing is we're
pulling out all the information and really preparing ourselves to go off and write the
email because that's the next step. We're going to draft our response. So all we're doing is say
we filtered out all the junk. Now that we're looking at real emails, we're just trying to say
like, okay, well, what do I need to do for each one of these emails? Just make it a nice bulleted
list of what action I need to perform on those emails. And the final step is going to be going
off and drafting a response based on the information we're putting together right
here. I hope that makes sense. All right. So then now we can go to the final part of our crew,
which is going to be our writer agent. At this point, let's go ahead and head over to our writer
agent. So you can actually see what it's trying to do. And the main thing per usual is we're going
to set up all the roles, goals, backstory. So, hey, you're just a skilled writer. You're going
to go off and write emails. But what's different about this one is the fact that we tell it that
you have three tools to use. So let me show you what each one of these is so that you can actually
understand what's going on. And I do think it's important at the same time, we'll dive into the
task in a second. The main thing is, hey, this agent has the ability to access basically the
internet, Tabili, probably pronouncing that wrong, is basically just a API that makes it super easy
for large language models to go off and search the internet. So you have to go off and get an
API key and we'll do that in just a second. But so, hey, this email person has the ability to
go off and search the internet to maybe get some additional information about email that they're
to write. Outside of that, it needs the ability to go off and pull more information and reanalyze
the 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. And then finally, you need the ability to go off and
actually write, basically write an email. And 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. So if I hop
over here to our tools folder, you'll see that right here, we use the tool decorator on this
function to say, hey, this is your ability. You can use this function right here to make emails.
So what does it do? It takes an information and specifically it takes in the email, the subject,
and the message of the email, the person we're trying to send it to, or multiple people, the
subject of the email and the actual body of the message. And per usual, we're going to be using
the toolkit to go off and create a draft email. And we're just going to call the draft function
to create the draft with all the information that we passed into the tool. All right, cool.
So you're now a pro on the email writer agent. Well, what does this actual task look like? What
are we telling this agent to do? If we come down here to the end, I think Xiao did a phenomenal
job of creating this task. What it's going to do is going to write a draft email. But the important
part is how do we convey that information and goal to our agent? Based on the action required
emails identified. So everything that came from the agent above, the task above, I want you to
draft responses for each one of them, ensuring that you meet all the following criteria. And
I'm not going to go into a ton of detail here, but like, hey, go off and research the topic.
So that was what Tavalli was for. Go off and Google more about this topic if you need to.
Oh, also, 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 had our get thread
tool again. So we're giving our agent all the tools it needs to perform this task. Go research
this, go find out more about this. And once you're ready to go, now it's time for you to write it. So
that's what this final part is. Now, finally use the tool provided to go off and write an email.
And it's important that you need to include the following information. The two, so who's the email
we're sending it to, the subject line and the message. That's how this all connects. And this
all gets kicked off actually inside of our crew. So if we come back over to our crew, you can see
they all get connected right here. Each one of our tasks gets connected to an agent. And then
finally, what we're going to do, tying it all full circle back to LandGraph. If you remember whenever
we were creating our graph, well, what does our graph usually do? Well, our node performs an
action and it updates state. Well, that's exactly what our crew is going to do down here at the very
end. Once we kick off our crew with all of our tasks and agents included in it, once we kick
off the crew and it takes a few minutes to run, what we're going to do with that result is we're
per usual going to go update the state. This time we're not updating emails or checked emails. We're
actually updating the action required emails. So these are the emails that we performed actions
on. In this case, we wrote up a draft email. So that's exactly what's happening inside of here.
So I hope this has all made sense in how you've seen how everything is connected, where we're
updating state, writing emails, and moving throughout our graph basically. And the part
that's interesting is because what do we do after we finish drafting response? Well, you could guess
it. Our edge tells us go back and wait. And the cycle is going to kicked off once again. So we're
going to go back to our wait node and they're just going to redo the cycle over and over and over
again. So I hope you guys have seen the power of how all this connects. And I know we've been doing
a lot of talking and we haven't actually got to set up and run it. So what I want to do next is
walk you guys through the actual process of going through the readme. And inside of here, I'm going
show you guys how to set up your environment. We're going to quickly connect up your crew to
Gmail so that we're going to go pull down your credentials. We're going to set up basically
everything that you need to and turn on everything inside of Google so that your crew has access.
And after that, we're going to go ahead and run it. So let's go ahead now and hop over to Google
so we can start pulling down your credentials. All right. So what I've done so far is I've
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. We're
trying to build a crew AI that runs on our local desktop. So when you click that link,
it'll take you over to this page right here, which will call out all the instructions and
steps you need to take in order to pull down your credentials.json. And the important thing to note
is you're trying to generate an OAuth 2.0 client ID. That's the goal. And these steps right here
show you exactly how to do it. So once you do, first is go off and click your go to credentials
button. And what this will do is it'll take you to a screen that looks just like this.
Now it's super important to notice this is inside of Google Cloud Platform. If you haven't already
signed up to use Google Cloud Platform, it's completely free to use. I would definitely
just recommend signing up and signing in so that you can see your dashboard just like this.
The next thing you need to do is create a new project. So right now mine says crew AI automations.
If yours says new project, don't worry, just click the dropdown. It'll prompt you to make a
new project and just be sure to go ahead and call it crew AI something, just so you'll have an easy
way to remember which project you were working on. Fantastic. So once you have your environment set
up to where it shows that you're logged in and you're inside of a crew AI project, you will now
start going over here to the credentials window that you're looking at right now. And the important
thing to note is we said our goal was to set up an OAuth 2.0 client ID. And since 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. So the first thing you're going to do is click create credentials up top, and then it's
going to ask you which kind. Well, in our case, we want to do an OAuth client. From there, it's going
to ask us which application type we would like to use. And back in our instructions, it says we want
to make a desktop app application. So click that, and then we're going to give ours a name. So we're
just going to call ours, I'm just going to call mine YouTube crew AI and then lang graph. So it can
take a few minutes to actually go ahead and create these credentials. But the important thing is it
will come up with this screen. Now, make sure you don't exit out of it. The thing that you need to
do is download the JSON. Because when you download this JSON file, this is the file that you actually
need to go ahead and import inside of your local project directory. And just as a recap, this is
super important to use, and you don't want to share this with the world. So how do you go about not
sharing this file with the world? The first thing you need to do is make sure whenever you drag it
over, you need to make sure that your .gitignore file includes the name of this JSON file, because
we don't want to be sharing it with the world. I cannot repeat this enough. Do not accidentally
save this to GitHub because you don't want to save your key to the world so anyone can start
actually pulling down your emails, because that's a big no-no. So make sure whenever you do pull
it down, that it says you have updated your .gitignore to say, hey, I do not want to save
our credentials. Okay, so we have downloaded our file. So what I can do is if I go ahead and hover
over our downloads, so I can see my recent download called client-secret-7456. So let me
go ahead and find that in my downloads real fast. So I'm just going to go ahead and drag in my
client's secret over here. So you can see there's just a part of it. So what I'll do is close out
of that. But the important thing is you'll notice my client secret, it's not the right name. So what
you'll need to do is click this file and then you'll need to rename it. I'm going to go ahead
and delete my old one, move it to the trash. And you need to rename this one, your credentials.json,
just like that. Perfect. So now you have your credentials.json saved to your file. Now there's
one other step that we need to take over in Google. And this wasn't called out in the original readme,
I had to update it. But if you type in the Gmail API, you'll need to type that in. And eventually
you'll want to click this right here. And what you need to do is you need to enable this in order
for your local credentials to have access, or basically this project, you want this project
to have access to the Gmail API, because that's how we were pulling down emails and writing
address. So make sure you enable it. And once you've done that, you are all good on the Google
front. What we're going to do next is keep chugging along and we're going to go back over to our .env
file because we need to do some homework. The next one is we need to update your email address.
So go ahead inside your .env file, update your email address to whatever you were using inside
of your, whatever account you're connecting it to. And then from there, you and I are going to
go update and create a Tavalli API key. So what are we going to do for this one? Well, we're going
to go to tavalli.com and we are going to go ahead... This is completely free to use. They
have a super generous pricing. So you can see, if you look at this, if you're a free, you get up to
a thousand calls per month. So we're going to go ahead and get started. I've already signed in.
So you can see, yeah, I've already signed in. If you haven't signed up, completely free.
And what you'll do is you'll get brought to a page that looks just like this,
where you'll have access to your API key. So you're going to want to click the copy button
and then come back over to your .env file and paste it in here. So you can start allowing your
crew to go off and access the internet in an intelligent way. If you don't want to use Tavalli,
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 when working with your crews. All right, cool. And the final
thing, you know how to do it at this point. You're going to go over and update your OpenAI key
inside of OpenAI. Just generate a new key, save it here. Awesome. So once we have done that,
we are in the homestretch boys. What 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. So what we're going to do is we're going to hop back over to
our terminal, set up our crew with all the requirements so we can kick this thing off.
All right. So welcome to the terminal. This is where we're going to install all of our Python
dependencies, set up our environment and go ahead and run our crew. So let's speed through this.
The first thing we need to do is go ahead and create a new Python environment that includes
all of the dependencies that we had set up earlier. Here's how we do that. We are going
to be using conda and we are going to be creating a new environment that's going to store all of our
different dependencies for this project. So what I'm going to do is create a new conda environment
with the name crew AI land graph, just so I know what this environment is for. And I'm going to
specify the Python version. I'd like to go with 3.11. So I'm going to go ahead and kick that off.
It'll create a new Python environment for me. And one thing you might notice is like,
what? What is conda? Well, conda is just a tool that allows you to quickly create these Python
environments. If you don't have conda on your system, definitely just recommend watching a quick
conda for Linux or conda for windows tutorial, and it'll walk you through the complete setup.
It's super simple, but eventually you'll get to a state where you can start running conda on your
machine and it's going to make managing all your Python environments so much easier. Fantastic.
So once we have actually created our new environment, we can actually dive into it
start using it. And right 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. And once I've done that, you'll
notice it no longer says base. It actually says the name of the new environment I just created,
which is super cool. So what we can do next is we need to go off and install all these different
requirements inside of our new Python conda environment. So we can do that with saying
pip install. So go off and install packages using pip. And I'm specifically trying to install
packages and the specified version numbers inside of my requirements.txt default. So let me go ahead
and do that. And this will take once again, just a few seconds to go off and run and install
everything inside of our project. Once we have done that, we should be good to go and run our
crew. And I'm just going to go ahead and say, before we actually run it, whenever you go to
run Python main.py, whenever you run this, which is going to kick off our land graph workflow and
go kick off our crew. Whenever I ran this the first time, what happened to me is it said,
Hey, we need to access your Gmail account. And if you remember, we went off and created all of those
new configuration files and we now give our local crew access to Gmail. So you're going to have to
do that normal thing when you use a new application saying like, Hey, do you allow Google or this tool
to access Google? The answer is yes. Because in our case, we want our local tool to access our
emails. So we're going to start pulling them out, pulling them down and writing draft emails. So
you're not going to see that screen here because I've already accepted 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. So I'm going to go ahead and kick it off just so you can see this tool in
action. So we're going to start running our crew, going to go off, check our emails, and you can
start to see the draft emails that I have typed up and sent to myself as dummy emails. So you can see,
yep, our tasks are working. Well, let's start from the get-go. We went ahead and pulled down
our draft emails from Google. And then from there, this actually, because we found emails,
if you remember LandGraph, we found emails. Fantastic. They're new emails. So let's go off
and actually start filtering them. From there, we're going to pull out the core information.
So you can see, hey, go filter these emails. Fantastic. Well, we've now analyzed all of these,
and here's the snippet for each of these emails. Well, now that we've done that, we need to pull
out the core pieces of information from each email. So that was our email action specialist.
So like I said, you can kind of see how all of it's coming together now. So we found the,
these were the filtered emails. Now we're going to pull out the key pieces of information from
each one of those emails. So this is what it must include. And as you can see, we went ahead in here
and actually start putting 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. So now we're going to go over to the
email response writer. So that was the third step in our tool. Now that we have all the information,
we're going to go off and write it. So you can see that's exactly what's happening here. We're
going to go off and search like, what the heck is crew AI? What the heck is land graph? And it
actually found information on the internet. So it's not only just using local context,
it's using the internet to help us write a response. And then from there, eventually we're
going to go off and call the create draft. And that was that custom tool we created from Google.
If you remember over here, this is our create draft tool. So it's using the tools we provide
to it and actually typed up a nice email response. Thank you for your email. I've been looking forward
to do this. It's awesome. So forth and so forth and best branding because I wrote the email and
it's going to just keep going. And then eventually after typing up those emails and responses,
we're going to get to the state to where like, yep, there's no more emails. I've already wrote
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 and over again. So all around, I hope you think this is super cool. And you now
have a new use case for using crew AI. And you have also leveled up your crew AI game because
you now know how to use land graph intelligently to go ahead and manage state and trigger off your
crew in a special way. So I hope you guys have really liked seeing how everything connects.
And I know it was a little bit longer. I really hope you guys really enjoyed and learned a ton
today, but that's a wrap for this video, guys. I hope you enjoyed learning about crew AI and land
graph and setting up your own email automation tool. 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 ad developers,
you're definitely going to check out that free school community I just created for you guys.
And outside of that, you can download the free source code below. Just click the link
down in the description. And finally, 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. But enough of that. Hope you guys have a great day. Talk to you soon.
See ya.