Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Building an Agent State Graph: Nodes

00:00 In this lesson, you will start building your agents. Now, the way you will tackle this is like peeling back the layers of an onion, or a top-down approach, if you prefer.

00:10 So first, you will build the agent state graph. Then you peel back a layer and explore the tools and the nodes and the conditional edges of your state graph.

00:19 You peel back another layer, and you’ll be looking at the state graph of the extract data from email tool. And then peeling back the final layer, as you’ll explore one of the LangChain chains that that tool is relying on.

00:35 One final note before you start coding, you will remember that the agent state graph from before has a model node and a tool node, and in the code you will be exploring, the model node is actually called email_agent, as opposed to email_model.

00:50 But what’s in a name? As you will see, that node invokes the OpenAI model, the LLM, so the email_agent node is just the model node. Right, let’s do some coding.

01:03 From the graphs subfolder of your downloaded code, please open email_agent.py. Now to create the agent graph, you will need to build the agent workflow.

01:14 And LangGraph makes the necessary objects available to you. They are imported in line 8 if you’re interested, but I’d like to focus on the code starting at line 132, where these objects are being used.

01:26 So on line 132, you create an empty workflow object as a state graph. Remember, the state is a central memory object that is available to all the nodes in the graph, and the state you’re using for the agent is the MessagesState.

01:43 Now MessagesState is a predefined state that only has one attribute called messages, and each node in your agent will append its output to the messages attribute in MessagesState.

01:57 So it’s important that you understand how that works. Now at the moment, your workflow is just a blank canvas. So on line 134, you add your first node to the workflow.

02:10 So the .add_node() method takes two input parameters. Firstly, the name of your node. So this determines how your node appears in the graph, but also how you refer to that node in the code, as you will see in a minute.

02:25 The second parameter is the action this node will take. So in this case, the action is defined in a Python function called call_agent_model_node, with underscores.

02:37 What this function does exactly, you will cover later. So on line 134, you’ve added your model node or your agent node. On line 135, you now add your tools node.

02:50 Its name is email_tools, and the action is defined in tool_node. Now, what is tool_node? Well, in LangGraph, a tool node is a specialized pre-built execution engine, and it is designed to automate the process of invoking tools.

03:09 So in your case, these tools are Python functions. So those tools are requested by the model if the model decides it needs them. So the LLM, the model, decides if it needs a tool.

03:21 If so, it emits an AIMessage object that has a tool name and the tool argument values. The tool node then reads the requested tool call IDs. It executes the matching Python functions and then appends its output to the MessagesState so the model has access to it.

03:42 Again, it is important that you see how that works. So far, you’ve put the email_agent and the email_tools nodes on your graph. The start and end nodes are added automatically for you.

03:57 In the next lesson, then, you’ll add the edges, so the connections between the nodes.

Become a Member to join the conversation.