To tool or not to tool!
1. To tool or not to tool!
Let's pick up where we left off in Chapter 1.2. The linear agent
We built our very first agent, which was a linear workflow that took a user input and passed it to a tool-aware LLM node. This LLM then either requested a tool call or responded as normal. The output of the LLM was then fed into the tool node, which triggered a tool if the LLM decided that one was needed to complete the task. This agent was able to respond to some inputs, such as requesting stock data or company summaries, but it wasn't able to respond to queries that don't require tool calls. This is because the LLM output would have been a response to the user, and when this reaches the tool node, it doesn't know what to do with it. Additionally, the Python tool wasn't able to create plots, as this tool requires the data loaded by the stock performance data tool. Our linear workflow simply isn't able to accommodate this iterative and collaborative way of working. Let's adjust this single-agent architecture.3. Conditional edges
We'll introduce conditional edges, which like normal edges allow the passage of information in a particular direction. However, conditional edges can route information to different nodes depending on the result of a condition. Let's see how this would work in our agent.4. Conditional edges
The LLM receives a prompt, and it will make a tool call if it determines a tool call is required to complete the task.5. Conditional edges
The conditional edge checks to see if a tool call is requested by the LLM, and if it is, the graph routes to the tool node.6. Conditional edges
The tool response is then returned to the LLM, which can then determine if the task has been completed and route to the end, or request another tool call to start the cycle again.7. Conditional edges
If, instead, the LLM determines that a tool isn't required to respond to the prompt, a tool call isn't made, and thus the tool condition fails. The conditional edge then routes to the end node with a standard LLM response.8. Conditional edges in practice
If we ask for a plot of the closing price of a particular company,9. Conditional edges in practice
the LLM may choose to call the stock data tool, this data will be fed back to the LLM and stored in the graph state. The LLM should realize that the task hasn't been completed. It'll make another tool call,10. Conditional edges in practice
triggering the Python tool to generate a plot using the data stored in the graph state messages.11. Let's practice!
In the exercises, we'll build this conditional edge using very similar syntax to what we've seen previously, defining each node and edge in turn. We'll also show you a quick LangGraph shortcut for defining single-agents with fewer lines of code. These shortcuts will become more useful as we make the step up to multi-agents later in the chapter. See you in a moment!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.