Defining nodes and edges for flexible function calling
1. Defining nodes and edges for flexible function calling
You're on a roll! Now that our2. Building a multi-tool workflow
palindrome, historical events, and Wikipedia tools are ready to go, let's incorporate them into a new chatbot workflow.3. Define workflow functions
Let's start building the first of two workflow functions, which is a stopping function that decides whether to4. Define workflow functions
call tools or5. Define workflow functions
end the conversation if there is no tool call in the last message.6. Define workflow functions
The next function is a dynamic tool caller that returns a tool response if a tool call is present,7. Define workflow functions
or invokes the LLM with the chatbot node if there are no tool calls. Once these functions are set up, we'll define8. Define workflow functions
the full graph. To build the workflow,9. Create a stop condition function
we'll import the MessagesState, START, and END modules from langgraph.graph. Then, we'll define the stop condition function called "should_continue" that accepts MessagesState to define the graph state. We'll then extract the last message from "messages" in "state" and save it as last_message. Then we'll check if this message has tool calls, such as "wikipedia", returning "tools" if the condition is true. If not, the conversation is ended using "END".10. Create a dynamic tool caller
Next, we'll create our dynamic tool caller named "call_model", which also accepts MessagesState as the graph state, saving the last message as a variable called last_message. Then we'll use isinstance() to check if this message is an AIMessage from the chatbot and whether it features tool_calls. If so, we'll return the "response" from "tool_calls", passed to the content field of the AIMessage. If there are no tool_calls, we'll invoke the model using all of the "messages" in the state, passed as an argument to the .invoke() method before it's applied to model_with_tools. Since we've now got our functions,11. Create the graph
let's create the full graph! Our graph state called "workflow" will be created for us using the MessagesState passed to StateGraph().12. Create the graph
Then we'll add our nodes to the workflow, mapping the "chatbot" to the "call_model" function, and "tools" to our tool_node.13. Create the graph
We'll then add a START node that initializes the "chatbot" node, before adding14. Create the graph
conditional edges from the "chatbot" to the "END" or "tools" nodes using our "should_continue" function. Next, we'll15. Create the graph
add another edge from the "tools" node back to the "chatbot", completing the conversation loop.16. Adding memory
We'll then create a MemorySaver() object called "memory", which we'll use to enhance our chatbot with memory. Next, we'll apply the .compile() method to our workflow, passing in the memory to the "checkpointer" to create the chatbot application, before displaying the LangGraph diagram. Later on, we'll test this this workflow with different queries, but first,17. Let's practice!
let's practice building the workflow from scratch!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.