Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Coding the Conditional Edges
00:00 To implement conditional edges, you need a function that decides which node to go to next based on whatever inputs that function is getting. So let me talk you through the routing function starting on line 124.
00:13
So it takes the MessagesState as an input parameter and stores that in a parameter called state so that the function can check the latest message in the state.
00:24
Remember that all nodes append their output to the messages, so if the model decides it needs a tool, it will append a structured message to messages in state.
00:36
In line 126, you access the messages attribute of the state, and you then pick up the most recent one using index -1, and then you store the most recent message in a variable called last_message.
00:51
Now it’s important to remember that last_message is not a string, it’s an AIMessage object, and an AIMessage has certain structured properties such as tool_calls.
01:04 So remember that if the LLM decides that it needs a tool, it will return a tool message, a tool call, if you like, and with the name of the tool it wants to use and the input parameters needed for that tool.
01:19
On line 127, then you check if the latest message was indeed a tool call. In other words, if the model had decided that it needed a tool. If so, return string email_tools, if not, return END.
01:34 So these are the exact names of the relevant nodes the process needs to go to next. You have now covered the agent’s state graph. In the next lesson, you’re peeling back another layer of the onion, and you will dive into the state graph that is used by the email extraction tool.
Become a Member to join the conversation.