Writing the stand-in model
The spy showed the question and the shape of the answer. A model that decides by rule rather than by thinking is now about ten lines.
Two rules. Throw away anything too short to be a fact, and throw away anything that opens like politeness. What is left is kept. It is the crudest possible version of what a real extractor does, and it is enough to make every lesson in this course run.
class PretendModel(BaseChatModel):
"""Keeps every sentence of the new messages as a memory."""
@property
def _llm_type(self):
return "pretend"_llm_type is the one property LangChain insists on; it is a name for logs. The work happens in _generate.
def _generate(self, messages, stop=None, run_manager=None, **kwargs):
asked = str(messages[-1].content)
said = asked.split("## New Messages")[-1].split("## Observation Date")[0]
said = re.sub(r"(?m)^\s*(user|assistant):\s*", "", said)
parts = [s.strip() for s in re.split(r"[.\n]", said) if len(s.strip()) > 8]
kept = [s for s in parts if not s.lower().startswith(POLITE)]
reply = json.dumps({"memory": [
{"id": str(i), "text": text, "linked_memory_ids": []}
for i, text in enumerate(kept)
]})
return ChatResult(generations=[ChatGeneration(message=AIMessage(content=reply))])The last message is the long instruction from lesson 4, so the first two lines cut it down to the part that matters: everything between New Messages and Observation Date. The third strips the user: and assistant: labels off the front of each line. What is left is split into sentences, the short ones and the polite ones are dropped, and each survivor becomes a memory in the shape Mem0 asked for.
What it gets right and wrong
It keeps real sentences and drops the thanks, which was one of the two things the dictionary in lesson 2 could not do. It does it by looking at the first word rather than by understanding, so I would like to thank your team for the fast delivery sails straight through. It also never notices that two sentences mean the same thing, and never rewrites I prefer email into the user prefers email the way a real model would. Those are differences you can see, which makes them safe.
Why the sentence length check matters
Drop it and the splitter produces fragments: an empty string after the final full stop, a stray ok, the word and on its own line. Every one of those becomes a memory, and a store full of fragments is worse than no store, because searches start matching them.
- Lower the length check from 8 to 2 and look at the memories you get.
- Add a rule that drops any sentence starting with thanks.
- Return an empty
memorylist and confirm thatadd()stores nothing.
Every expert started right here.