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
Latihan ini adalah bagian dari kursus
Designing Agentic Systems with LangChain
Petunjuk latihan
- Specify the input type for the
stateparameter usingMessagesState. - Access the last message from the
stateusing"messages"to check for tool calls. - Check if the last message contains
tool_callsand specify the return value as"tools"if true. - Specify the return value as
ENDfor ending the conversation when notool_callsare present.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# 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 ____