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
Bu egzersiz
Designing Agentic Systems with LangChain
kursunun bir parçasıdırEgzersiz talimatları
- 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.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
# 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 ____