Get startedGet started for free

Define a function that stops the chatbot

Now that your chatbot's state is set up, you need to build functions that manage the workflow across your chatbot's graph. To start with, you'll build a should_continue() function that checks for tool calls in the chatbot's last message within state which has been loaded for you. If there are no tool calls, the chatbot comes to a stop. If a tool is called, the chatbot moves on to the next task. To manage your messages, the following modules have been imported for you.

from langgraph.graph import MessagesState, START, END

This exercise is part of the course

Designing Agentic Systems with LangChain

View Course

Exercise instructions

  • Specify the input type for the state parameter using MessagesState.
  • Access the last message from the state using "messages" to check for tool calls.
  • Check if the last message contains tool_calls and specify the return value as "tools" if true.
  • Specify the return value as END for ending the conversation when no tool_calls are present.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Use MessagesState to define the state of the function
def should_continue(state: ____):
    
    # Get the last message from the state
    last_message = ____["____"][____]
    
    # Check if the last message includes tool calls
    if ____.____:
        return "____"
    
    # End the conversation if no tool calls are present
    return ____
Edit and Run Code