Custom instructions: steering what is kept
The model decides what is worth keeping, and its idea of worth is generic. When your application cares about particular things, you can say so, through a field with an exact name.
The field
MemoryConfig has a custom_instructions field, described in the library as custom instructions for fact extraction. Whatever you put there is added to the instruction Mem0 sends the model. It goes in the same configuration dictionary from lesson 7, so the helper passes it through.
from pretend_mem0 import memory
shop = memory(custom_instructions="""
Only keep facts about delivery addresses and contact preferences.
Ignore anything about orders, refunds or timing.
""")Proving it arrives
A model that reports whether the rule reached it settles the question, rather than guessing from what got stored.
class Spy(BaseChatModel):
@property
def _llm_type(self):
return "spy"
def _generate(self, messages, stop=None, run_manager=None, **kwargs):
whole = "\n".join(str(m.content) for m in messages)
print("rule reached the model:", "ONLY ADDRESSES" in whole)
return ChatResult(generations=[ChatGeneration(
message=AIMessage(content='{"memory": []}'))])from spy_rule import Spy, build
build(custom_instructions="ONLY ADDRESSES").add("Deliver to my office.", user_id="ravi")
build(custom_fact_extraction_prompt="ONLY ADDRESSES").add("Deliver to my office.", user_id="ravi")
build().add("Deliver to my office.", user_id="ravi", prompt="ONLY ADDRESSES")Three lines, three answers. The first and the third reached the model. The middle one did not, and nothing complained.
The name in the middle
custom_fact_extraction_prompt is the name used in a lot of Mem0 material. It is not a field on MemoryConfig in this version, and the configuration model ignores keys it does not recognise rather than rejecting them. So it is accepted, stored nowhere, and has no effect at all.
MemoryConfig.model_fields before you blame the model.Per call instead of per memory
add also takes a prompt, which overrides the instruction for that one call. Use the configuration for a rule that is always true of your application, and the argument when one particular call needs different treatment, such as importing a batch of records that are already clean.
What to put in one
- What you want kept, in the words your domain uses.
- What you want ignored, which is usually the longer list and the more valuable half.
- How you want it written: third person, one fact per memory, no pronouns.
The stand-in model in this course keeps every sentence by rule and does not read instructions, so what you can verify here is that the rule is delivered. What it does to the output you see in lesson 19, with a real model behind it.
- Print
sorted(MemoryConfig.model_fields)and check the exact spelling yourself. - Pass a key that is definitely nonsense and confirm it is accepted without complaint.
- Write a
custom_instructionsfor your own application, listing three things to ignore.
You understood something today that you didn't yesterday.