I am trying to use middleware to summarize and compress context. But it is appended by default. If I want to override it, what should I do? This is my code; it is appended by default.
from langchain.agents.middleware import before_model
from langchain_core.messages import SystemMessage
from model.llm.deepseek.deepseek_llm import deepseek
@before_model
def summary_messages(state,runtime):
print(state["messages"])
print(len(state["messages"]))
if len(state["messages"]) > 20:
result = deepseek.invoke(
f"总结一下对话记录生成概要{state['messages']}"
)
return {"messages":[result]}
return None
there are some built-in middleware - why not using them?
Prebuilt middleware - Docs by LangChain
if you want your own middleware, use RemoveMessage(id=REMOVE_ALL_MESSAGES) before the new messages to clear the existing state, then add the summary + recent messages:
return {
"messages": [
RemoveMessage(id=REMOVE_ALL_MESSAGES),
HumanMessage(content=f"Summary:\n\n{result.content}"),
*messages[-5:],
]
}
summarize all messages except the last 5