
Mem0 如何用 Temporal Reasoning 处理 yesterday、last week 等时间相关查询【免费下载链接】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如果你用 Mem0 给 Agent 存了大量记忆会遇到一个典型问题用户问“我上周在哪开的会”时检索只按语义相似度排序而“昨天在巴黎开会”和“上周在东京开会”两条记忆在语义上几乎一样排序无法区分。Mem0 Platform v3 的 Temporal Reasoning 就是解决这个问题的它在检索时自动比较查询里的时间表达与记忆中的事件日期对日期匹配的记忆加一个排序加分temporal boost不改写记忆本身。前提条件Temporal Reasoning 只在 Mem0 Platform v3 上自动运行OSS SDK 不提供该功能。需要 Mem0 Platform 账户和 API keyPython 3.10 或 Node.js 18。功能说明见 Temporal Reasoning 文档。机制时间表达如何参与排序查询中可以直接使用yesterday、last week、tomorrow、currently、as of March 2025这类时间表达。Mem0 会把它们与存储记忆中提取出的日期和日期范围做比较命中事件日期的记忆获得加分。官方文档给的例子是用户maya-demo存了三条记忆——“Yesterday I met Maya at the Orion conference in Paris.”“Last week I met Maya at the Orion conference in Tokyo.”“Last year I met Maya at the Orion conference in Lisbon.”此时搜索Which city did I meet Maya in at the Orion conference last week?Tokyo 那条记忆获得 temporal boost 并排在第一位这是文档示例结果实际排序取决于你的数据。检索返回的还是普通的记忆结果只是用 temporal boost 重新排序不返回额外的时间字段。准备安装 SDK 并初始化客户端按 Quickstart 安装并设置 API key。Python 示例如下your-api-key替换为你在 Platform 控制台获取的 keypip install mem0aifrom mem0 import MemoryClient client MemoryClient(api_keyyour-api-key)JavaScript 对应npm install mem0aiimport MemoryClient from mem0ai; const client new MemoryClient({ apiKey: your-api-key });注意这里用的是 Platform 客户端MemoryClient。如果用的是 OSS 侧的Memory类并传入reference_date/referenceDate会收到明确报错The reference_date parameter is not supported by the OSS Memory SDK.这可以从 OSS 侧的错误处理 中得到确认。写入带时间信息的记忆用标准的add写入对话时间信息写在消息内容里即可memories [ Yesterday I met Maya at the Orion conference in Paris., Last week I met Maya at the Orion conference in Tokyo., Last year I met Maya at the Orion conference in Lisbon., ] for text in memories: messages [{role: user, content: text}] client.add(messages, user_idmaya-demo)client.add的用法与 Quickstart 中的示例一致user_id是实体作用域标识搜索时要用同一个值过滤。新记忆的日期通常几秒内就会影响检索结果无需等待或手动触发。可选导入历史对话时用 timestamp 保留原始时间如果你是批量导入过去的对话记录created_at会是导入时间而不是对话发生时间。Memory Timestamps 功能 允许在添加时指定 Unix 时间戳自 1970-01-01 UTC 起的秒数覆盖默认时间from datetime import datetime, timedelta five_days_ago datetime.now() - timedelta(days5) unix_timestamp int(five_days_ago.timestamp()) messages [ {role: user, content: Im travelling to SF} ] client.add(messages, user_iduser1, timestampunix_timestamp)JavaScript 中参数名同为timestampconst messages [ {role: user, content: Im travelling to SF} ]; client.add(messages, { userId: user1, timestamp: unixTimestamp });对应关系总结来自 Temporal Reasoning 文档操作PythonTypeScript用途添加记忆timestamptimestamp保留导入对话的原始时间搜索记忆reference_datereferenceDate模拟在某个指定日期时间搜索执行时间相关搜索直接调用client.search查询文本里带上时间表达filters里传实体 IDresults client.search( Which city did I meet Maya in at the Orion conference last week?, filters{user_id: maya-demo}, )const results await client.search( Which city did I meet Maya in at the Orion conference last week?, { filters: { user_id: maya-demo } } );按 Search Memories API 的约束实体 IDuser_id、agent_id、app_id、run_id必须放在filters对象内顶层传会被 400 拒绝至少要传一个实体 ID。返回结果中每条记忆的score是语义、BM25、实体匹配多路信号融合后的[0, 1]值。验证方式检查返回的results列表顺序。时间匹配的记忆上例是 Tokyo 那条应排在第一位。文档明确说明返回的是“usual memory results, reordered using the temporal boost”——即字段结构与普通搜索一致只有顺序变化。用 reference_date 模拟历史时间点Temporal Reasoning 的“当前时间”默认是搜索发起的时刻。如果需要复现或审计过去某个时间点会命中什么记忆传reference_dateMem0 会把它当作那次搜索的当前时间results client.search( What happened last week?, filters{user_id: user-123}, reference_date2025-03-21T00:00:00Z, )const results await client.search(What happened last week?, { filters: { user_id: user-123 }, referenceDate: 2025-03-21T00:00:00Z, });格式说明来自 SDK 类型定义SearchMemoryOptionsreference_date接受YYYY-MM-DD或 Unix timestamp上述带T00:00:00Z的写法是文档原样给出的示例。TypeScript 客户端把referenceDate序列化为 API 请求体中的reference_date可参考 客户端测试 确认这一映射。限制与边界仅 Mem0 Platform v3 生效OSS SDK 没有 Temporal Reasoning传入时间参数会直接报错而不是静默忽略。时间表达是查询文本的一部分机制文档只列出了yesterday、last week、tomorrow、currently、as of March 2025这几个例子没有给出完整支持词表如果你的表达不生效先换成文档中列出的形式再验证。该功能不返回独立的“时间匹配分数”只能在结果顺序上体现score字段仍是融合后的综合分数。新记忆的日期生效需要几秒刚写入立即搜索可能还没体现时间加分。需要进一步查看检索参数top_k、threshold、rerank等默认值时参考 Search Memories API reference涉及记忆导入时间保真的完整示例见 Memory Timestamps。【免费下载链接】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),仅供参考