Putting a model in a node
A node is a function, and calling a model is something a function can do. There is no new LangGraph idea in this lesson at all.
from pretend_model import PretendModel
from langchain_core.messages import HumanMessage
from langgraph.graph import StateGraph, START, MessagesState
model = PretendModel()
def call_model(state):
return {"messages": [model.invoke(state["messages"])]}
builder = StateGraph(MessagesState)
builder.add_node("call_model", call_model)
builder.add_edge(START, "call_model")
graph = builder.compile()
result = graph.invoke({"messages": [HumanMessage("Where is order A17?")]})
print(result["messages"][-1].content)The node is one line
def call_model(state):
return {"messages": [model.invoke(state["messages"])]}It hands the whole conversation to the model, gets one message back, and returns it. add_messages, which came with MessagesState, adds it to the list.
Notice you pass the entire conversation on every call. A model does not remember the last thing you said to it. The list is the memory, which is why lesson 20 is about keeping that list between runs.
Telling it how to behave
A system message goes at the front of the conversation. Put it in when you call the model, rather than in the state, and it applies to every turn without being stored over and over.
from pretend_model import PretendModel
from langchain_core.messages import HumanMessage, SystemMessage
from langgraph.graph import StateGraph, START, MessagesState
model = PretendModel()
SYSTEM = SystemMessage("You are a support agent. Be brief.")
def call_model(state):
return {"messages": [model.invoke([SYSTEM] + state["messages"])]}
builder = StateGraph(MessagesState)
builder.add_node("call_model", call_model)
builder.add_edge(START, "call_model")
result = builder.compile().invoke({"messages": [HumanMessage("Hello")]})
print("messages in state:", [m.type for m in result["messages"]])The system message never appears in the state. It was added on the way in and thrown away afterwards, which is what you want: it is an instruction, not part of the conversation.
- Print
result["messages"]in full and confirm the system message is absent. - Put the system message in the state instead, and invoke twice, and watch it pile up.
- Ask something with an order id in it and compare the reply.
Every expert started right here.