Cyclical graph setup
The next big step in our code is setting up our graphs using LangGraph:
- First, we import some important packages to get us started:
from langgraph.graph import END, StateGraph
from langgraph.prebuilt import ToolNode
This code imports the following necessary classes and functions from the
langgraph
library:END
: A special node representing the end of the workflowStateGraph
: A class for defining the state graph of the workflowToolNode
: A class for defining a node that represents a tool or action
- We then pass
AgentState
as an argument to theStateGraph
class we just imported for defining the state graph of the workflow:workflow = StateGraph(AgentState)
This creates a new instance of
StateGraph
calledworkflow
and defines a new graph for thatworkflow
StateGraph
instance. - Next, we define the nodes we will cycle between and assign our node functions to them:
workflow.add_node("agent", agent)Â Â # agent
retrieve = ToolNode(tools)
workflow.add_node...