ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Mem0 如何用 custom_instructions 控制记忆只提取领域相关事实

2026/9/9 23:51:34 拓冰建站 浏览量
Mem0 如何用 custom_instructions 控制记忆只提取领域相关事实 Mem0 如何用 custom_instructions 控制记忆只提取领域相关事实【免费下载链接】embedchainThe Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.项目地址: https://gitcode.com/GitHub_Trending/em/embedchain如果你在用 Mem0embedchain 仓库中的 Memory Layer for AI Agents给 AI Agent 或应用做长期记忆经常会遇到同一个问题对话里的寒暄、天气、兴趣爱好等无关内容也会被存进向量库污染后续检索结果。Mem0 OSS 提供custom_instructions参数TypeScript 中为customInstructions让你用一段提示词加少量示例来限定事实提取的范围——只有符合你领域定义的 facts 才会变成记忆条目无关消息则返回空的results。适用前提Python 3.10 或 Node.js 18一个可用的 LLM provider下文主路径用 OpenAI需要OPENAI_API_KEY。准备条件安装 SDKpip install mem0ai # Python npm install mem0ai # TypeScript设置 API keyOpenAI 作为 LLM 和 embedder 的默认 providerexport OPENAI_API_KEYyour-openai-api-key旧版本注意custom_fact_extraction_prompt参数已更名为custom_instructions从老版本升级时要同步改配置来源custom-instructions.mdx。编写领域限定提示词提示词由两部分组成允许提取的事实类型说明以及贴近真实用户消息的 few-shot 示例。Mem0 文档给出的订单支持场景示例如下custom_instructions Please only extract entities containing customer support information, order details, and user information. Here are some few shot examples: Input: Hi. Output: {facts : []} Input: The weather is nice today. Output: {facts : []} Input: My order #12345 hasnt arrived yet. Output: {facts : [Order #12345 not received]} Input: Im John Doe, and Id like to return the shoes I bought last week. Output: {facts : [Customer name: John Doe, Wants to return shoes, Purchase made last week]} Input: I ordered a red shirt, size medium, but received a blue one instead. Output: {facts : [Ordered red shirt, size medium, Received blue shirt instead]} Return the facts and customer information in a json format as shown above. TypeScript 中是同一段内容赋给const customInstructions。写提示词时文档给了几条明确的写法要求明确允许的事实类型说明要保留哪些实体或短语指令越具体提取器越聚焦指令太宽泛会让无关事实混进来。同时给正例和反例反例输出为{facts: []}让模型学会跳过目标领域外的内容。示例保持简短并贴近真实消息的大小写、标点和语气。只返回带facts键的 JSON不要加多余键便于下游解析。输出是结构化 JSON 的facts数组Mem0 会把数组里的每条 fact 转成独立的记忆条目。把提示词加载进 Memory 配置custom_instructions与模型配置放在同一个 config 里from mem0 import Memory config { llm: { provider: openai, config: { model: gpt-5-mini, temperature: 0.2, max_tokens: 2000, } }, custom_instructions: custom_instructions, } m Memory.from_config(config)TypeScript 版本键名为 camelCaseimport { Memory } from mem0ai/oss; const config { llm: { provider: openai, config: { apiKey: process.env.OPENAI_API_KEY ?? , model: gpt-4-turbo-preview, temperature: 0.2, maxTokens: 1500, }, }, customInstructions: customInstructions, }; const memory new Memory(config);注意 Python 与 TypeScript 的键名差异Python 用custom_instructionsTypeScript 用customInstructions。如果你用的是自托管 Qdrant 之外的向量库还需要在 config 里补充vector_store和embedder段见 OSS 配置文档本文主路径沿用默认组件即可。验证领域内消息拆成记忆领域外消息被过滤文档给出的验证方式就是两组对照的add调用。领域内消息预期拆分为多条记忆m.add(Yesterday, I ordered a laptop, the order id is 12345, user_idalice)await memory.add(Yesterday, I ordered a laptop, the order id is 12345, { userId: user123 });文档示例输出示例结果实际措辞可能略有差异{ results: [ {memory: Ordered a laptop, event: ADD}, {memory: Order ID: 12345, event: ADD}, {memory: Order placed yesterday, event: ADD} ] }领域外消息预期被完全过滤m.add(I like going to hikes, user_idalice)await memory.add(I like going to hikes, { userId: user123 });{ results: [] }判断标准很直接输出只包含你提示词中描述的事实且每条 fact 存为独立记忆无关消息的results为空数组说明提示词成功忽略了目标领域外的内容。初始化后先用已知示例跑一次add确认响应能拆分成独立 facts是文档建议的最小冒烟测试。持续运行时的检查项文档在 “Verify the feature is working” 一节给出的运行期检查方法上线期间记录每次调用的日志确认facts数组符合你的 schema检查无关消息是否返回空results数组每次修改提示词后跑一遍回归样本确认之前能正确提取的事实仍然通过。文档同时提示提示词要与真实对话记录测试宽泛的提示词会导致无关事实漏网。其他实践建议包括给提示词变更加版本号以便回滚、定期抽查已存记忆以尽早发现提取漂移。局限与边界custom_instructions控制的是“提取哪些事实”不改变存储结构Mem0 的 add 流程是增量存储新记忆不会覆盖或删除已有记忆。Python 与 TypeScript 的示例中 LLM 模型名不同Python 示例为gpt-5-miniTypeScript 示例为gpt-4-turbo-preview可按你自己的 provider 替换提取用的 temperature 建议保持在 0.2 附近或更低让记忆提取更确定。如果你走的是自托管 REST 服务server 目录而非 SDKsetup wizard 第 4 步同样支持生成和编辑 custom instructions让服务端记忆系统知道优先保留什么。更完整的组合用法可以看 support-inbox 场景文档 和 custom instructions 功能页。【免费下载链接】embedchainThe Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.项目地址: https://gitcode.com/GitHub_Trending/em/embedchain创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考