MemoryOS 2.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (315) hide show
  1. memoryos-2.0.3.dist-info/METADATA +418 -0
  2. memoryos-2.0.3.dist-info/RECORD +315 -0
  3. memoryos-2.0.3.dist-info/WHEEL +4 -0
  4. memoryos-2.0.3.dist-info/entry_points.txt +3 -0
  5. memoryos-2.0.3.dist-info/licenses/LICENSE +201 -0
  6. memos/__init__.py +20 -0
  7. memos/api/client.py +571 -0
  8. memos/api/config.py +1018 -0
  9. memos/api/context/dependencies.py +50 -0
  10. memos/api/exceptions.py +53 -0
  11. memos/api/handlers/__init__.py +62 -0
  12. memos/api/handlers/add_handler.py +158 -0
  13. memos/api/handlers/base_handler.py +194 -0
  14. memos/api/handlers/chat_handler.py +1401 -0
  15. memos/api/handlers/component_init.py +388 -0
  16. memos/api/handlers/config_builders.py +190 -0
  17. memos/api/handlers/feedback_handler.py +93 -0
  18. memos/api/handlers/formatters_handler.py +237 -0
  19. memos/api/handlers/memory_handler.py +316 -0
  20. memos/api/handlers/scheduler_handler.py +497 -0
  21. memos/api/handlers/search_handler.py +222 -0
  22. memos/api/handlers/suggestion_handler.py +117 -0
  23. memos/api/mcp_serve.py +614 -0
  24. memos/api/middleware/request_context.py +101 -0
  25. memos/api/product_api.py +38 -0
  26. memos/api/product_models.py +1206 -0
  27. memos/api/routers/__init__.py +1 -0
  28. memos/api/routers/product_router.py +477 -0
  29. memos/api/routers/server_router.py +394 -0
  30. memos/api/server_api.py +44 -0
  31. memos/api/start_api.py +433 -0
  32. memos/chunkers/__init__.py +4 -0
  33. memos/chunkers/base.py +24 -0
  34. memos/chunkers/charactertext_chunker.py +41 -0
  35. memos/chunkers/factory.py +24 -0
  36. memos/chunkers/markdown_chunker.py +62 -0
  37. memos/chunkers/sentence_chunker.py +54 -0
  38. memos/chunkers/simple_chunker.py +50 -0
  39. memos/cli.py +113 -0
  40. memos/configs/__init__.py +0 -0
  41. memos/configs/base.py +82 -0
  42. memos/configs/chunker.py +59 -0
  43. memos/configs/embedder.py +88 -0
  44. memos/configs/graph_db.py +236 -0
  45. memos/configs/internet_retriever.py +100 -0
  46. memos/configs/llm.py +151 -0
  47. memos/configs/mem_agent.py +54 -0
  48. memos/configs/mem_chat.py +81 -0
  49. memos/configs/mem_cube.py +105 -0
  50. memos/configs/mem_os.py +83 -0
  51. memos/configs/mem_reader.py +91 -0
  52. memos/configs/mem_scheduler.py +385 -0
  53. memos/configs/mem_user.py +70 -0
  54. memos/configs/memory.py +324 -0
  55. memos/configs/parser.py +38 -0
  56. memos/configs/reranker.py +18 -0
  57. memos/configs/utils.py +8 -0
  58. memos/configs/vec_db.py +80 -0
  59. memos/context/context.py +355 -0
  60. memos/dependency.py +52 -0
  61. memos/deprecation.py +262 -0
  62. memos/embedders/__init__.py +0 -0
  63. memos/embedders/ark.py +95 -0
  64. memos/embedders/base.py +106 -0
  65. memos/embedders/factory.py +29 -0
  66. memos/embedders/ollama.py +77 -0
  67. memos/embedders/sentence_transformer.py +49 -0
  68. memos/embedders/universal_api.py +51 -0
  69. memos/exceptions.py +30 -0
  70. memos/graph_dbs/__init__.py +0 -0
  71. memos/graph_dbs/base.py +274 -0
  72. memos/graph_dbs/factory.py +27 -0
  73. memos/graph_dbs/item.py +46 -0
  74. memos/graph_dbs/nebular.py +1794 -0
  75. memos/graph_dbs/neo4j.py +1942 -0
  76. memos/graph_dbs/neo4j_community.py +1058 -0
  77. memos/graph_dbs/polardb.py +5446 -0
  78. memos/hello_world.py +97 -0
  79. memos/llms/__init__.py +0 -0
  80. memos/llms/base.py +25 -0
  81. memos/llms/deepseek.py +13 -0
  82. memos/llms/factory.py +38 -0
  83. memos/llms/hf.py +443 -0
  84. memos/llms/hf_singleton.py +114 -0
  85. memos/llms/ollama.py +135 -0
  86. memos/llms/openai.py +222 -0
  87. memos/llms/openai_new.py +198 -0
  88. memos/llms/qwen.py +13 -0
  89. memos/llms/utils.py +14 -0
  90. memos/llms/vllm.py +218 -0
  91. memos/log.py +237 -0
  92. memos/mem_agent/base.py +19 -0
  93. memos/mem_agent/deepsearch_agent.py +391 -0
  94. memos/mem_agent/factory.py +36 -0
  95. memos/mem_chat/__init__.py +0 -0
  96. memos/mem_chat/base.py +30 -0
  97. memos/mem_chat/factory.py +21 -0
  98. memos/mem_chat/simple.py +200 -0
  99. memos/mem_cube/__init__.py +0 -0
  100. memos/mem_cube/base.py +30 -0
  101. memos/mem_cube/general.py +240 -0
  102. memos/mem_cube/navie.py +172 -0
  103. memos/mem_cube/utils.py +169 -0
  104. memos/mem_feedback/base.py +15 -0
  105. memos/mem_feedback/feedback.py +1192 -0
  106. memos/mem_feedback/simple_feedback.py +40 -0
  107. memos/mem_feedback/utils.py +230 -0
  108. memos/mem_os/client.py +5 -0
  109. memos/mem_os/core.py +1203 -0
  110. memos/mem_os/main.py +582 -0
  111. memos/mem_os/product.py +1608 -0
  112. memos/mem_os/product_server.py +455 -0
  113. memos/mem_os/utils/default_config.py +359 -0
  114. memos/mem_os/utils/format_utils.py +1403 -0
  115. memos/mem_os/utils/reference_utils.py +162 -0
  116. memos/mem_reader/__init__.py +0 -0
  117. memos/mem_reader/base.py +47 -0
  118. memos/mem_reader/factory.py +53 -0
  119. memos/mem_reader/memory.py +298 -0
  120. memos/mem_reader/multi_modal_struct.py +965 -0
  121. memos/mem_reader/read_multi_modal/__init__.py +43 -0
  122. memos/mem_reader/read_multi_modal/assistant_parser.py +311 -0
  123. memos/mem_reader/read_multi_modal/base.py +273 -0
  124. memos/mem_reader/read_multi_modal/file_content_parser.py +826 -0
  125. memos/mem_reader/read_multi_modal/image_parser.py +359 -0
  126. memos/mem_reader/read_multi_modal/multi_modal_parser.py +252 -0
  127. memos/mem_reader/read_multi_modal/string_parser.py +139 -0
  128. memos/mem_reader/read_multi_modal/system_parser.py +327 -0
  129. memos/mem_reader/read_multi_modal/text_content_parser.py +131 -0
  130. memos/mem_reader/read_multi_modal/tool_parser.py +210 -0
  131. memos/mem_reader/read_multi_modal/user_parser.py +218 -0
  132. memos/mem_reader/read_multi_modal/utils.py +358 -0
  133. memos/mem_reader/simple_struct.py +912 -0
  134. memos/mem_reader/strategy_struct.py +163 -0
  135. memos/mem_reader/utils.py +157 -0
  136. memos/mem_scheduler/__init__.py +0 -0
  137. memos/mem_scheduler/analyzer/__init__.py +0 -0
  138. memos/mem_scheduler/analyzer/api_analyzer.py +714 -0
  139. memos/mem_scheduler/analyzer/eval_analyzer.py +219 -0
  140. memos/mem_scheduler/analyzer/mos_for_test_scheduler.py +571 -0
  141. memos/mem_scheduler/analyzer/scheduler_for_eval.py +280 -0
  142. memos/mem_scheduler/base_scheduler.py +1319 -0
  143. memos/mem_scheduler/general_modules/__init__.py +0 -0
  144. memos/mem_scheduler/general_modules/api_misc.py +137 -0
  145. memos/mem_scheduler/general_modules/base.py +80 -0
  146. memos/mem_scheduler/general_modules/init_components_for_scheduler.py +425 -0
  147. memos/mem_scheduler/general_modules/misc.py +313 -0
  148. memos/mem_scheduler/general_modules/scheduler_logger.py +389 -0
  149. memos/mem_scheduler/general_modules/task_threads.py +315 -0
  150. memos/mem_scheduler/general_scheduler.py +1495 -0
  151. memos/mem_scheduler/memory_manage_modules/__init__.py +5 -0
  152. memos/mem_scheduler/memory_manage_modules/memory_filter.py +306 -0
  153. memos/mem_scheduler/memory_manage_modules/retriever.py +547 -0
  154. memos/mem_scheduler/monitors/__init__.py +0 -0
  155. memos/mem_scheduler/monitors/dispatcher_monitor.py +366 -0
  156. memos/mem_scheduler/monitors/general_monitor.py +394 -0
  157. memos/mem_scheduler/monitors/task_schedule_monitor.py +254 -0
  158. memos/mem_scheduler/optimized_scheduler.py +410 -0
  159. memos/mem_scheduler/orm_modules/__init__.py +0 -0
  160. memos/mem_scheduler/orm_modules/api_redis_model.py +518 -0
  161. memos/mem_scheduler/orm_modules/base_model.py +729 -0
  162. memos/mem_scheduler/orm_modules/monitor_models.py +261 -0
  163. memos/mem_scheduler/orm_modules/redis_model.py +699 -0
  164. memos/mem_scheduler/scheduler_factory.py +23 -0
  165. memos/mem_scheduler/schemas/__init__.py +0 -0
  166. memos/mem_scheduler/schemas/analyzer_schemas.py +52 -0
  167. memos/mem_scheduler/schemas/api_schemas.py +233 -0
  168. memos/mem_scheduler/schemas/general_schemas.py +55 -0
  169. memos/mem_scheduler/schemas/message_schemas.py +173 -0
  170. memos/mem_scheduler/schemas/monitor_schemas.py +406 -0
  171. memos/mem_scheduler/schemas/task_schemas.py +132 -0
  172. memos/mem_scheduler/task_schedule_modules/__init__.py +0 -0
  173. memos/mem_scheduler/task_schedule_modules/dispatcher.py +740 -0
  174. memos/mem_scheduler/task_schedule_modules/local_queue.py +247 -0
  175. memos/mem_scheduler/task_schedule_modules/orchestrator.py +74 -0
  176. memos/mem_scheduler/task_schedule_modules/redis_queue.py +1385 -0
  177. memos/mem_scheduler/task_schedule_modules/task_queue.py +162 -0
  178. memos/mem_scheduler/utils/__init__.py +0 -0
  179. memos/mem_scheduler/utils/api_utils.py +77 -0
  180. memos/mem_scheduler/utils/config_utils.py +100 -0
  181. memos/mem_scheduler/utils/db_utils.py +50 -0
  182. memos/mem_scheduler/utils/filter_utils.py +176 -0
  183. memos/mem_scheduler/utils/metrics.py +125 -0
  184. memos/mem_scheduler/utils/misc_utils.py +290 -0
  185. memos/mem_scheduler/utils/monitor_event_utils.py +67 -0
  186. memos/mem_scheduler/utils/status_tracker.py +229 -0
  187. memos/mem_scheduler/webservice_modules/__init__.py +0 -0
  188. memos/mem_scheduler/webservice_modules/rabbitmq_service.py +485 -0
  189. memos/mem_scheduler/webservice_modules/redis_service.py +380 -0
  190. memos/mem_user/factory.py +94 -0
  191. memos/mem_user/mysql_persistent_user_manager.py +271 -0
  192. memos/mem_user/mysql_user_manager.py +502 -0
  193. memos/mem_user/persistent_factory.py +98 -0
  194. memos/mem_user/persistent_user_manager.py +260 -0
  195. memos/mem_user/redis_persistent_user_manager.py +225 -0
  196. memos/mem_user/user_manager.py +488 -0
  197. memos/memories/__init__.py +0 -0
  198. memos/memories/activation/__init__.py +0 -0
  199. memos/memories/activation/base.py +42 -0
  200. memos/memories/activation/item.py +56 -0
  201. memos/memories/activation/kv.py +292 -0
  202. memos/memories/activation/vllmkv.py +219 -0
  203. memos/memories/base.py +19 -0
  204. memos/memories/factory.py +42 -0
  205. memos/memories/parametric/__init__.py +0 -0
  206. memos/memories/parametric/base.py +19 -0
  207. memos/memories/parametric/item.py +11 -0
  208. memos/memories/parametric/lora.py +41 -0
  209. memos/memories/textual/__init__.py +0 -0
  210. memos/memories/textual/base.py +92 -0
  211. memos/memories/textual/general.py +236 -0
  212. memos/memories/textual/item.py +304 -0
  213. memos/memories/textual/naive.py +187 -0
  214. memos/memories/textual/prefer_text_memory/__init__.py +0 -0
  215. memos/memories/textual/prefer_text_memory/adder.py +504 -0
  216. memos/memories/textual/prefer_text_memory/config.py +106 -0
  217. memos/memories/textual/prefer_text_memory/extractor.py +221 -0
  218. memos/memories/textual/prefer_text_memory/factory.py +85 -0
  219. memos/memories/textual/prefer_text_memory/retrievers.py +177 -0
  220. memos/memories/textual/prefer_text_memory/spliter.py +132 -0
  221. memos/memories/textual/prefer_text_memory/utils.py +93 -0
  222. memos/memories/textual/preference.py +344 -0
  223. memos/memories/textual/simple_preference.py +161 -0
  224. memos/memories/textual/simple_tree.py +69 -0
  225. memos/memories/textual/tree.py +459 -0
  226. memos/memories/textual/tree_text_memory/__init__.py +0 -0
  227. memos/memories/textual/tree_text_memory/organize/__init__.py +0 -0
  228. memos/memories/textual/tree_text_memory/organize/handler.py +184 -0
  229. memos/memories/textual/tree_text_memory/organize/manager.py +518 -0
  230. memos/memories/textual/tree_text_memory/organize/relation_reason_detector.py +238 -0
  231. memos/memories/textual/tree_text_memory/organize/reorganizer.py +622 -0
  232. memos/memories/textual/tree_text_memory/retrieve/__init__.py +0 -0
  233. memos/memories/textual/tree_text_memory/retrieve/advanced_searcher.py +364 -0
  234. memos/memories/textual/tree_text_memory/retrieve/bm25_util.py +186 -0
  235. memos/memories/textual/tree_text_memory/retrieve/bochasearch.py +419 -0
  236. memos/memories/textual/tree_text_memory/retrieve/internet_retriever.py +270 -0
  237. memos/memories/textual/tree_text_memory/retrieve/internet_retriever_factory.py +102 -0
  238. memos/memories/textual/tree_text_memory/retrieve/reasoner.py +61 -0
  239. memos/memories/textual/tree_text_memory/retrieve/recall.py +497 -0
  240. memos/memories/textual/tree_text_memory/retrieve/reranker.py +111 -0
  241. memos/memories/textual/tree_text_memory/retrieve/retrieval_mid_structs.py +16 -0
  242. memos/memories/textual/tree_text_memory/retrieve/retrieve_utils.py +472 -0
  243. memos/memories/textual/tree_text_memory/retrieve/searcher.py +848 -0
  244. memos/memories/textual/tree_text_memory/retrieve/task_goal_parser.py +135 -0
  245. memos/memories/textual/tree_text_memory/retrieve/utils.py +54 -0
  246. memos/memories/textual/tree_text_memory/retrieve/xinyusearch.py +387 -0
  247. memos/memos_tools/dinding_report_bot.py +453 -0
  248. memos/memos_tools/lockfree_dict.py +120 -0
  249. memos/memos_tools/notification_service.py +44 -0
  250. memos/memos_tools/notification_utils.py +142 -0
  251. memos/memos_tools/singleton.py +174 -0
  252. memos/memos_tools/thread_safe_dict.py +310 -0
  253. memos/memos_tools/thread_safe_dict_segment.py +382 -0
  254. memos/multi_mem_cube/__init__.py +0 -0
  255. memos/multi_mem_cube/composite_cube.py +86 -0
  256. memos/multi_mem_cube/single_cube.py +874 -0
  257. memos/multi_mem_cube/views.py +54 -0
  258. memos/parsers/__init__.py +0 -0
  259. memos/parsers/base.py +15 -0
  260. memos/parsers/factory.py +21 -0
  261. memos/parsers/markitdown.py +28 -0
  262. memos/reranker/__init__.py +4 -0
  263. memos/reranker/base.py +25 -0
  264. memos/reranker/concat.py +103 -0
  265. memos/reranker/cosine_local.py +102 -0
  266. memos/reranker/factory.py +72 -0
  267. memos/reranker/http_bge.py +324 -0
  268. memos/reranker/http_bge_strategy.py +327 -0
  269. memos/reranker/noop.py +19 -0
  270. memos/reranker/strategies/__init__.py +4 -0
  271. memos/reranker/strategies/base.py +61 -0
  272. memos/reranker/strategies/concat_background.py +94 -0
  273. memos/reranker/strategies/concat_docsource.py +110 -0
  274. memos/reranker/strategies/dialogue_common.py +109 -0
  275. memos/reranker/strategies/factory.py +31 -0
  276. memos/reranker/strategies/single_turn.py +107 -0
  277. memos/reranker/strategies/singleturn_outmem.py +98 -0
  278. memos/settings.py +10 -0
  279. memos/templates/__init__.py +0 -0
  280. memos/templates/advanced_search_prompts.py +211 -0
  281. memos/templates/cloud_service_prompt.py +107 -0
  282. memos/templates/instruction_completion.py +66 -0
  283. memos/templates/mem_agent_prompts.py +85 -0
  284. memos/templates/mem_feedback_prompts.py +822 -0
  285. memos/templates/mem_reader_prompts.py +1096 -0
  286. memos/templates/mem_reader_strategy_prompts.py +238 -0
  287. memos/templates/mem_scheduler_prompts.py +626 -0
  288. memos/templates/mem_search_prompts.py +93 -0
  289. memos/templates/mos_prompts.py +403 -0
  290. memos/templates/prefer_complete_prompt.py +735 -0
  291. memos/templates/tool_mem_prompts.py +139 -0
  292. memos/templates/tree_reorganize_prompts.py +230 -0
  293. memos/types/__init__.py +34 -0
  294. memos/types/general_types.py +151 -0
  295. memos/types/openai_chat_completion_types/__init__.py +15 -0
  296. memos/types/openai_chat_completion_types/chat_completion_assistant_message_param.py +56 -0
  297. memos/types/openai_chat_completion_types/chat_completion_content_part_image_param.py +27 -0
  298. memos/types/openai_chat_completion_types/chat_completion_content_part_input_audio_param.py +23 -0
  299. memos/types/openai_chat_completion_types/chat_completion_content_part_param.py +43 -0
  300. memos/types/openai_chat_completion_types/chat_completion_content_part_refusal_param.py +16 -0
  301. memos/types/openai_chat_completion_types/chat_completion_content_part_text_param.py +16 -0
  302. memos/types/openai_chat_completion_types/chat_completion_message_custom_tool_call_param.py +27 -0
  303. memos/types/openai_chat_completion_types/chat_completion_message_function_tool_call_param.py +32 -0
  304. memos/types/openai_chat_completion_types/chat_completion_message_param.py +18 -0
  305. memos/types/openai_chat_completion_types/chat_completion_message_tool_call_union_param.py +15 -0
  306. memos/types/openai_chat_completion_types/chat_completion_system_message_param.py +36 -0
  307. memos/types/openai_chat_completion_types/chat_completion_tool_message_param.py +30 -0
  308. memos/types/openai_chat_completion_types/chat_completion_user_message_param.py +34 -0
  309. memos/utils.py +123 -0
  310. memos/vec_dbs/__init__.py +0 -0
  311. memos/vec_dbs/base.py +117 -0
  312. memos/vec_dbs/factory.py +23 -0
  313. memos/vec_dbs/item.py +50 -0
  314. memos/vec_dbs/milvus.py +654 -0
  315. memos/vec_dbs/qdrant.py +355 -0
@@ -0,0 +1,735 @@
1
+ NAIVE_EXPLICIT_PREFERENCE_EXTRACT_PROMPT = """
2
+ You are a preference extraction assistant.
3
+ Please extract the user's explicitly mentioned preferences from the following conversation.
4
+
5
+ Notes:
6
+ - A preference means the user's explicit attitude or choice toward something. It is not limited to words like "like/dislike/want/don't want/prefer".
7
+ - This includes, but is not limited to, any user's explicitly expressed inclination, desire, rejection, or priority that counts as an explicit preference.
8
+ - Focus on extracting the user's preferences in query. Do not extract preferences from the assistant's responses unless the user explicitly agrees with or endorses the assistant's suggestions.
9
+ - When the user modifies or updates their preferences for the same topic or event, extract the complete evolution process of their preference changes, including both the original and updated preferences.
10
+
11
+ Requirements:
12
+ 1. Keep only the preferences explicitly mentioned by the user. Do not infer or assume. If the user mentions reasons for their preferences, include those reasons as well.
13
+ 2. Output should be a list of entries concise natural language summaries and the corresponding context summary, context summary must contain complete information of the conversation fragment that the preference is mentioned.
14
+ 3. If multiple preferences are mentioned within the same topic or domain, you MUST combine them into a single entry, keep each entry information complete. Different topics of preferences should be divided into multiple entries.
15
+ 4. If no explicit preference can be reasonably extracted, return [].
16
+
17
+ Conversation:
18
+ {qa_pair}
19
+
20
+ Find ALL explicit preferences. If no explicit preferences found, return []. Output JSON only:
21
+ ```json
22
+ [
23
+ {
24
+ "explicit_preference": "A short natural language summary of the preferences",
25
+ "context_summary": "The corresponding context summary, which is a summary of the corresponding conversation, do not lack any scenario information",
26
+ "reasoning": "reasoning process to find the explicit preferences"
27
+ "topic": "preference topic, which can only belong to one topic or domain, such as: sports, hotel, education, etc.",
28
+ },
29
+ ]
30
+ ```
31
+ """
32
+
33
+
34
+ NAIVE_EXPLICIT_PREFERENCE_EXTRACT_PROMPT_ZH = """
35
+ 你是一个偏好提取助手。
36
+ 请从以下对话中提取用户明确提及的偏好。
37
+
38
+ 注意事项:
39
+ - 偏好是指用户对某事物的明确态度或选择,不仅限于"喜欢/不喜欢/想要/不想要/偏好"等词汇。
40
+ - 包括但不限于用户明确表达的任何倾向、渴望、拒绝或优先级,这些都算作显式偏好。
41
+ - 重点提取用户在查询中的偏好。不要从助手的回复中提取偏好,除非用户明确同意或认可助手的建议。
42
+ - 当用户针对同一主题或事件修改或更新其偏好时,提取其偏好变化的完整演变过程,包括原始偏好和更新后的偏好。
43
+
44
+ 要求:
45
+ 1. 只保留用户明确提到的偏好,不要推断或假设。如果用户提到了偏好的原因,也要包含这些原因。
46
+ 2. 输出应该是一个条目列表,包含简洁的自然语言摘要和相应的上下文摘要,上下文摘要必须包含提到偏好的对话片段的完整信息。
47
+ 3. 如果在同一主题或领域内提到了多个偏好,你必须将它们合并为一个条目,保持每个条目信息完整。不同话题的偏好要分为多个条目。
48
+ 4. 如果没有可以合理提取的显式偏好,返回[]。
49
+
50
+ 对话:
51
+ {qa_pair}
52
+
53
+ 找出所有显式偏好。如果没有找到显式偏好,返回[]。仅输出JSON:
54
+ ```json
55
+ [
56
+ {
57
+ "explicit_preference": "偏好的简短自然语言摘要,需要描述为“用户偏好于/不喜欢/想要/不想要/偏好什么”",
58
+ "context_summary": "对应的上下文摘要,即对应对话的摘要,不要遗漏任何场景信息",
59
+ "reasoning": "寻找显式偏好的推理过程",
60
+ "topic": "偏好所属的主题或领域,例如:体育、酒店、教育等, topic只能属于一个主题或领域",
61
+ },
62
+ ]
63
+ ```
64
+ """
65
+
66
+
67
+ NAIVE_IMPLICIT_PREFERENCE_EXTRACT_PROMPT = """
68
+ You are a preference inference assistant. Please extract **implicit preferences** from the following conversation
69
+ (preferences that the user did not explicitly state but can be reasonably inferred from their underlying motivations, behavioral patterns, decision-making logic, and latent needs).
70
+
71
+ Notes:
72
+ - For Assistant's responses or suggestions, they can only be extracted as the user's implicit preferences if there is evidence in subsequent conversation that the user implicitly accepted them (e.g., adoption, agreement, acting on the suggestion, etc.). Assistant suggestions alone do not constitute user preferences.
73
+ - For conversations with only one question-answer turn (single Q&A), implicit preferences cannot be extracted due to insufficient context and behavioral patterns. Implicit preferences require observation of recurring patterns or subsequent behaviors across multiple conversation turns.
74
+
75
+ Counter-examples:
76
+ 【Counter-example 1 - Assistant suggestion not accepted by user】
77
+ Conversation:
78
+ User: I want to buy a phone, any recommendations?
79
+ Assistant: I suggest considering the iPhone 15 Pro, it has powerful performance and great camera quality.
80
+ User: What about the iPhone 16?
81
+ Assistant: The iPhone 16 is expected to be released in September 2026, it will have a new design and features.
82
+
83
+ Analysis: Although the Assistant recommended iPhone, the user showed no acceptance (e.g., "okay", "I'll consider it", or follow-up questions about iPhone), so this cannot be extracted as the user's implicit preference.
84
+ Result: Cannot extract implicit preference
85
+
86
+ 【Counter-example 2 - Single question-answer situation】
87
+ Conversation:
88
+ User: Any good movies recently?
89
+ Assistant: "Dune 2" has good reviews, it's a sci-fi epic genre.
90
+
91
+ Analysis: This is just a single simple Q&A exchange. The user provided no further feedback or behavior, lacking sufficient context to infer user preferences for sci-fi movies or other hidden tendencies.
92
+ Result: Cannot extract implicit preference
93
+
94
+ - Implicit preferences refer to user inclinations or choices that are not directly expressed, but can be deeply inferred by analyzing:
95
+ * **Hidden motivations**: What underlying needs or goals might drive the user's behavior?
96
+ * **Behavioral patterns**: What recurring patterns or tendencies can be observed?
97
+ * **Decision-making logic**: What reasoning or trade-offs might the user be considering?
98
+ * **Latent preferences**: What preferences might the user have but haven't yet articulated?
99
+ * **Contextual signals**: What do the user's choices, comparisons, exclusions, or scenario selections reveal about their deeper preferences?
100
+ - Do not treat explicitly stated preferences as implicit preferences; this prompt is only for inferring preferences that are not directly mentioned.
101
+ - Go beyond surface-level facts to understand the user's hidden possibilities and underlying logic.
102
+
103
+ Requirements:
104
+ 1. Only make inferences when there is sufficient evidence in the conversation; avoid unsupported or far-fetched guesses.
105
+ 2. Inferred implicit preferences must not conflict with explicit preferences.
106
+ 3. For implicit_preference: only output the preference statement itself; do not include any extra explanation, reasoning, or confidence information. Put all reasoning and explanation in the reasoning field.
107
+ 4. In the reasoning field, explicitly explain the underlying logic and hidden motivations you identified.
108
+ 5. Different topics of preferences should be divided into multiple entries.
109
+ 6. If no implicit preference can be reasonably inferred, return [].
110
+
111
+ Conversation:
112
+ {qa_pair}
113
+
114
+ Output format:
115
+ [
116
+ ```json
117
+ {
118
+ "implicit_preference": "A concise natural language statement of the implicit preferences reasonably inferred from the conversation, or an empty string",
119
+ "context_summary": "The corresponding context summary, which is a summary of the corresponding conversation, do not lack any scenario information",
120
+ "reasoning": "Explain the underlying logic, hidden motivations, and behavioral patterns that led to this inference",
121
+ "topic": "preference topic, which can only belong to one topic or domain, such as: sports, hotel, education, etc.",
122
+ }
123
+ ]
124
+ ```
125
+ Don't output anything except the JSON.
126
+ """
127
+
128
+
129
+ NAIVE_IMPLICIT_PREFERENCE_EXTRACT_PROMPT_ZH = """
130
+ 你是一个偏好推理助手。请从以下对话中提取**隐式偏好**
131
+ (用户没有明确表述,但可以通过分析其潜在动机、行为模式、决策逻辑和隐藏需求深度推断出的偏好)。
132
+
133
+ 注意事项:
134
+ - 对于Assistant的回答内容或建议,只有在后续对话中用户表现出隐含接受(如采纳、认同、按建议行动等)的情况下,才能将相关内容提取为用户的隐式偏好。单纯的Assistant建议本身不构成用户偏好。
135
+ - 对于只有一轮问答(一问一答)的对话,由于缺乏足够的上下文和行为模式,不能提取隐式偏好。隐式偏好需要从多轮对话中观察到的重复模式或后续行为来推断。
136
+
137
+ 反例示例:
138
+ 【反例1 - 未被用户认可的Assistant建议】
139
+ 对话:
140
+ User: 我想买个手机,有什么推荐吗?
141
+ Assistant: 建议你考虑iPhone 15 Pro,性能强大,拍照效果好。
142
+ User: iPhone 16 怎么样?
143
+ Assistant: iPhone 16 预计将在2026年9月发布,会有新的设计和功能。
144
+
145
+ 分析:虽然Assistant推荐了iPhone,但用户没有表现出任何接受态度(如"好的"、"我会考虑"、后续询问iPhone相关问题等),因此不能提取为用户的隐式偏好。
146
+ 结果:无法提取隐式偏好
147
+
148
+ 【反例2 - 只有一问一答的情况】
149
+ 对话:
150
+ User: 最近有什么好看的电影吗?
151
+ Assistant: 《沙丘2》口碑不错,是科幻史诗类型的。
152
+
153
+ 分析:这只是一轮简单问答,用户没有进一步的反馈或行为,缺乏足够的上下文来推断用户对科幻电影的偏好或其他隐藏倾向。
154
+ 结果:无法提取隐式偏好
155
+
156
+ - 隐式偏好是指用户未直接表达,但可以通过深入分析以下方面推断出的倾向或选择:
157
+ * **隐藏动机**:什么样的潜在需求或目标可能驱动用户的行为?
158
+ * **行为模式**:可以观察到什么样的重复模式或倾向?
159
+ * **决策逻辑**:用户可能在考虑什么样的推理或权衡?
160
+ * **潜在偏好**:用户可能有但尚未明确表达的偏好是什么?
161
+ * **情境信号**:用户的选择、比较、排除或场景选择揭示了什么样的深层偏好?
162
+ - 不要将明确陈述的偏好视为隐式偏好;此提示仅用于推断未直接提及的偏好。
163
+ - 超越表面事实,理解用户的隐藏可能性和背后的逻辑。
164
+
165
+ 要求:
166
+ 1. 仅在对话中有充分证据时进行推断;避免无根据或牵强的猜测。
167
+ 2. 推断的隐式偏好不得与显式偏好冲突。
168
+ 3. 对于 implicit_preference:仅输出偏好陈述本身;不要包含任何额外的解释、推理或置信度信息。将所有推理和解释放在 reasoning 字段中。
169
+ 4. 在 reasoning 字段中,明确解释你识别出的底层逻辑和隐藏动机。
170
+ 5. 如果在同一主题或领域内提到了多个偏好,你必须将它们合并为一个条目,保持每个条目信息完整。不同话题的偏好要分为多个条目。
171
+ 6. 如果没有可以合理推断的隐式偏好,返回[]。
172
+
173
+ 对话:
174
+ {qa_pair}
175
+
176
+ 输出格式:
177
+ ```json
178
+ [
179
+ {
180
+ "implicit_preference": "从对话中合理推断出的隐式偏好的简洁自然语言陈述,或空字符串",
181
+ "context_summary": "对应的上下文摘要,即对应对话的摘要,不要遗漏任何场景信息",
182
+ "reasoning": "解释推断出该偏好的底层逻辑、隐藏动机和行为模式",
183
+ "topic": "偏好所属的主题或领域,例如:体育、酒店、教育等, topic只能属于一个主题或领域",
184
+ }
185
+ ]
186
+ ```
187
+ 除JSON外不要输出任何其他内容。
188
+ """
189
+
190
+
191
+ NAIVE_JUDGE_DUP_WITH_TEXT_MEM_PROMPT = """
192
+ You are a content comparison expert. Your task is to determine whether each new preference information already exists in the retrieved text memories.
193
+
194
+ **Task:** For each new preference, check if its content/topic/intent is already present in any of the retrieved text memories.
195
+
196
+ **Input Structure:**
197
+ - New preferences: Array of objects, each with "id" and "memory" fields
198
+ - Retrieved memories: Array of objects, each with "id" and "memory" fields
199
+
200
+ **Judgment Criteria:**
201
+ - If the core content, topic, or intent of a new preference is **already covered** in any retrieved memory, mark as "exists" (true).
202
+ - Consider both semantic similarity and topic overlap - even if wording differs, if the meaning is the same, it counts as existing.
203
+ - If the new preference introduces **new information, different topic, or unique content** not found in retrieved memories, mark as "exists" (false).
204
+ - Focus on the substantive content rather than minor phrasing differences.
205
+
206
+ **Output Format (JSON):**
207
+ ```json
208
+ {
209
+ "new_preference_id": "ID of the new preference being evaluated",
210
+ "exists": true/false,
211
+ "reasoning": "Brief explanation of your judgment, citing which retrieved memory contains similar content (if exists=true) or why it's new content (if exists=false)",
212
+ "matched_memory_id": "If exists=true, indicate which retrieved memory id matches; otherwise null"
213
+ }
214
+ ```
215
+ **New Preferences (array):**
216
+ {new_preference}
217
+
218
+ **Retrieved Text Memories (array):**
219
+ {retrieved_memories}
220
+
221
+ Output only the JSON response, no additional text.
222
+ """
223
+
224
+
225
+ NAIVE_JUDGE_DUP_WITH_TEXT_MEM_PROMPT_ZH = """
226
+ 你是一个内容比较专家。你的任务是判断每个新的偏好信息是否已经存在于召回的文本记忆中。
227
+
228
+ **任务:** 对每个新偏好,检查其内容/主题/意图是否已经在任何召回的文本记忆中存在。
229
+
230
+ **输入结构:**
231
+ - 新偏好:对象数组,每个对象包含"id"和"memory"字段
232
+ - 召回记忆:对象数组,每个对象包含"id"和"memory"字段
233
+
234
+ **判断标准:**
235
+ - 如果新偏好的核心内容、主题或意图**已经被覆盖**在任何召回的记忆中,标记为"exists"(true)。
236
+ - 考虑语义相似性和主题重叠 - 即使措辞不同,如果含义相同,也算作已存在。
237
+ - 如果新偏好引入了**新信息、不同主题或独特内容**,且在召回记忆中未找到,标记为"exists"(false)。
238
+ - 关注实质性内容,而非细微的表达差异。
239
+
240
+ **输出格式(JSON):**
241
+ ```json
242
+ {
243
+ "new_preference_id": "正在评估的新偏好ID",
244
+ "exists": true/false,
245
+ "reasoning": "简要说明你的判断理由,引用包含相似内容的召回记忆(如果exists=true)或说明为什么是新内容(如果exists=false)",
246
+ "matched_memory_id": "如果exists=true,指出匹配的召回记忆id;否则为null"
247
+ }
248
+ ```
249
+ **新偏好(数组):**
250
+ {new_preference}
251
+
252
+ **召回的文本记忆(数组):**
253
+ {retrieved_memories}
254
+
255
+ 只输出JSON响应,不要输出其他任何文本。
256
+ """
257
+
258
+
259
+ NAIVE_JUDGE_UPDATE_OR_ADD_PROMPT = """
260
+ You are a content comparison expert. Now you are given old and new information, each containing a question, answer topic name and topic description.
261
+ Please judge whether these two information express the **same question or core content**, regardless of expression differences, details or example differences. The judgment criteria are as follows:
262
+
263
+ - Core content is consistent, that is, the essence of the question, goal or core concept to be solved is the same, it counts as "same".
264
+ - Different expressions, different examples, but the core meaning is consistent, also counts as "same".
265
+ - If the question goals, concepts involved or solution ideas are different, it counts as "different".
266
+
267
+ Please output JSON format:
268
+ {
269
+ "is_same": true/false,
270
+ "reasoning": "Briefly explain the judgment basis, highlighting whether the core content is consistent"
271
+ }
272
+
273
+ **Old Information:**
274
+ {old_information}
275
+
276
+ **New Information:**
277
+ {new_information}
278
+ """
279
+
280
+
281
+ NAIVE_JUDGE_UPDATE_OR_ADD_PROMPT_ZH = """
282
+ 你是一个内容比较专家。现在给你旧信息和新信息,每个信息都包含问题、答案主题名称和主题描述。
283
+ 请判断这两个信息是否表达**相同的问题或核心内容**,不考虑表达差异、细节或示例差异。判断标准如下:
284
+
285
+ - 核心内容一致,即要解决的问题本质、目标或核心概念相同,算作"相同"。
286
+ - 表达方式不同、示例不同,但核心含义一致,也算作"相同"。
287
+ - 如果问题目标、涉及的概念或解决思路不同,则算作"不同"。
288
+
289
+ 请输出JSON格式:
290
+ {
291
+ "is_same": true/false,
292
+ "reasoning": "简要解释判断依据,突出核心内容是否一致"
293
+ }
294
+
295
+ **旧信息:**
296
+ {old_information}
297
+
298
+ **新信息:**
299
+ {new_information}
300
+ """
301
+
302
+
303
+ NAIVE_JUDGE_UPDATE_OR_ADD_PROMPT_FINE = """
304
+ You are a preference memory comparison expert. Analyze if the new preference memory describes the same topic as any retrieved memories by considering BOTH the memory field and preference field. At most one retrieved memory can match the new memory.
305
+
306
+ **Task:** Compare the new preference memory with retrieved memories to determine if they discuss the same topic and whether an update is needed.
307
+
308
+ **Comparison Criteria:**
309
+ - **Memory field**: Compare the core topics, scenarios, and contexts described
310
+ - **Preference field**: Compare the actual preference statements, choices, and attitudes expressed
311
+ - **Same topic**: Both memory AND preference content relate to the same subject matter
312
+ - **Different topics**: Either memory OR preference content differs significantly
313
+ - **Content evolution**: Same topic but preference has changed/evolved or memory has been updated
314
+ - **Identical content**: Both memory and preference fields are essentially the same
315
+
316
+ **Decision Logic:**
317
+ - Same core topic (both memory and preference) = need to check if update is needed
318
+ - Different topics (either memory or preference differs) = no update needed
319
+ - If same topic but content has changed/evolved = update needed
320
+ - If same topic and content is identical = update needed
321
+
322
+ **Output JSON:**
323
+ ```json
324
+ {
325
+ "need_update": true/false,
326
+ "id": "ID of the memory being updated (empty string if no update needed)",
327
+ "new_memory": "Updated memory field with merged/evolved memory content (empty string if no update needed)",
328
+ "new_preference": "Updated preference field with merged/evolved preference content (empty string if no update needed)",
329
+ "reasoning": "Brief explanation of the comparison considering both memory and preference fields"
330
+ }
331
+ ```
332
+
333
+ **New preference memory:**
334
+ {new_memory}
335
+
336
+ **Retrieved preference memories:**
337
+ {retrieved_memories}
338
+ """
339
+
340
+
341
+ NAIVE_JUDGE_UPDATE_OR_ADD_PROMPT_FINE_ZH = """
342
+ 你是一个偏好记忆比较专家。通过同时考虑 memory 字段和 preference 字段,分析新的偏好记忆是否与任何召回记忆描述相同的主题。最多只有一个召回记忆可以与新记忆匹配。
343
+
344
+ **任务:** 比较新的偏好记忆与召回记忆,以确定它们是否讨论相同的主题以及是否需要更新。
345
+
346
+ **比较标准:**
347
+ - **Memory 字段**:比较所描述的核心主题、场景和上下文
348
+ - **Preference 字段**:比较表达的实际偏好陈述、选择和态度
349
+ - **相同主题**:memory 和 preference 内容都涉及相同的主题
350
+ - **不同主题**:memory 或 preference 内容有显著差异
351
+ - **内容演变**:相同主题但偏好已改变/演变或记忆已更新
352
+ - **内容相同**:memory 和 preference 字段本质上相同
353
+
354
+ **决策逻辑:**
355
+ - 核心主题相同(memory 和 preference 都相同)= 需要检查是否需要更新
356
+ - 主题不同(memory 或 preference 有差异)= 不需要更新
357
+ - 如果主题相同但内容已改变/演变 = 需要更新
358
+ - 如果主题相同且内容完全相同 = 需要更新
359
+
360
+ **输出 JSON:**
361
+ ```json
362
+ {
363
+ "need_update": true/false,
364
+ "id": "正在更新的记忆的ID(如果不需要更新则为空字符串)",
365
+ "new_memory": "合并/演变后的更新 memory 字段(如果不需要更新则为空字符串)",
366
+ "new_preference": "合并/演变后的更新 preference 字段(如果不需要更新则为空字符串)",
367
+ "reasoning": "简要解释比较结果,同时考虑 memory 和 preference 字段"
368
+ }
369
+ ```
370
+
371
+ **新的偏好记忆:**
372
+ {new_memory}
373
+
374
+ **召回的偏好记忆:**
375
+ {retrieved_memories}
376
+ """
377
+
378
+
379
+ NAIVE_JUDGE_UPDATE_OR_ADD_PROMPT_OP_TRACE = """
380
+ # User Preference Memory Management Agent
381
+
382
+ You are a **User Preference Memory Management Agent**.
383
+ Your goal is to maintain a user's long-term **preference memory base** by analyzing new preference information and determining how it should update existing memories.
384
+
385
+ Each memory entry contains three fields:
386
+ - **id**: a unique identifier for the memory.
387
+ - **context_summary**: a factual summary of the dialogue or situation from which the preference was extracted.
388
+ - **preference**: the extracted statement describing the user's preference or tendency.
389
+
390
+ When updating a preference, you should also integrate and update the corresponding `context_summary` to ensure both fields stay semantically consistent.
391
+
392
+ You must produce a complete **operation trace**, showing which memory entries (identified by unique IDs) should be **added**, **updated**, or **deleted**.
393
+
394
+ ## Input Format
395
+
396
+ New preference memories (new_memories):
397
+ {new_memories}
398
+
399
+ Retrieved preference memories (retrieved_memories):
400
+ {retrieved_memories}
401
+ ## Task Instructions
402
+
403
+ 1. For each new memory, analyze its relationship with the retrieved memories:
404
+ - If a new memory is **unrelated** to all retrieved memories → perform `"ADD"` (insert as a new independent memory);
405
+ - If a new memory is **related** to one or more retrieved memories → perform `"UPDATE"` on those related retrieved memories (refine, supplement, or merge both the `preference` and the `context_summary`, while preserving change history trajectory information);
406
+ - If one or more retrieved memories are merged into one updated memory → perform `"DELETE"` on those retrieved memories.
407
+
408
+ 2. **Important**: Only retrieved memories that are related to the new memories should be updated or deleted. Retrieved memories that are unrelated to any new memory must be preserved.
409
+
410
+ 3. If multiple retrieved memories describe the same preference theme, merge them into one updated memory entry, combining both their `preference` information and their `context_summary` in a coherent and concise way.
411
+
412
+ 4. Output a structured list of **operation traces**, each explicitly stating:
413
+ - which memory (by ID) is affected,
414
+ - what operation is performed,
415
+ - the before/after `preference` and `context_summary`,
416
+ - and the reasoning behind it.
417
+
418
+ ## Output Format (JSON)
419
+
420
+ {
421
+ "trace": [
422
+ {
423
+ "op_id": "op_1",
424
+ "type": "ADD" | "UPDATE" | "DELETE",
425
+ "target_id": "(the old memory ID; null if ADD)",
426
+ "old_preference": "(the old preference text; null if ADD)",
427
+ "old_context_summary": "(the old context summary; null if ADD)",
428
+ "new_preference": "(the updated or newly created preference, if applicable)",
429
+ "new_context_summary": "(the updated or newly created context summary, if applicable)",
430
+ "reason": "(brief natural-language explanation for the decision)"
431
+ }
432
+ ]
433
+ }
434
+
435
+ ## Output Requirements
436
+
437
+ - The output **must** be valid JSON.
438
+ - Each operation must include both `preference` and `context_summary` updates where applicable.
439
+ - Each operation must include a clear `reason`.
440
+ - Multiple retrieved memories may be merged into one unified updated memory.
441
+ - Do **not** include any explanatory text outside the JSON.
442
+ """
443
+
444
+
445
+ NAIVE_JUDGE_UPDATE_OR_ADD_PROMPT_OP_TRACE_ZH = """
446
+ # 用户偏好记忆管理代理
447
+
448
+ 你是一个**用户偏好记忆管理代理**。
449
+ 你的目标是通过分析新的偏好信息并确定如何更新现有记忆,来维护用户的长期**偏好记忆库**。
450
+
451
+ 每个记忆条目包含三个字段:
452
+ - **id**:记忆的唯一标识符。
453
+ - **context_summary**:从中提取偏好的对话或情境的事实摘要。
454
+ - **preference**:描述用户偏好或倾向的提取陈述。
455
+
456
+ 更新偏好时,你还应该整合并更新相应的 `context_summary`,以确保两个字段保持语义一致。
457
+
458
+ 你必须生成完整的**操作跟踪**,显示应该**添加**、**更新**或**删除**哪些记忆条目(通过唯一 ID 标识)。
459
+
460
+ ## 输入格式
461
+
462
+ 新的偏好记忆 (new_memories):
463
+ {new_memories}
464
+
465
+ 召回的偏好记忆 (retrieved_memories):
466
+ {retrieved_memories}
467
+ ## 任务说明
468
+
469
+ 1. 对于每个新记忆,分析其与召回记忆的关系:
470
+ - 如果新记忆与所有召回记忆**无关** → 执行 `"ADD"`(作为新的独立记忆插入);
471
+ - 如果新记忆与一个或多个召回记忆**相关** → 对这些相关的召回记忆执行 `"UPDATE"`(细化、补充或合并 `preference` 和 `context_summary`,同时保留变化历史轨迹信息);
472
+ - 如果一个或多个召回记忆被合并到一个更新的记忆中 → 对这些召回记忆执行 `"DELETE"`。
473
+
474
+ 2. **重要**:只有与新记忆相关的召回记忆才应该被更新或删除。与任何新记忆都无关的召回记忆必须保留。
475
+
476
+ 3. 如果多个召回记忆描述相同的偏好主题,将它们合并为一个更新的记忆条目,以连贯简洁的方式结合它们的 `preference` 信息和 `context_summary`。
477
+
478
+ 4. 输出结构化的**操作跟踪**列表,每个操作明确说明:
479
+ - 受影响的记忆(通过 ID);
480
+ - 执行的操作类型;
481
+ - 更新前后的 `preference` 和 `context_summary`;
482
+ - 以及决策的原因。
483
+
484
+ ## 输出格式 (JSON)
485
+
486
+ {
487
+ "trace": [
488
+ {
489
+ "op_id": "op_1",
490
+ "type": "ADD" | "UPDATE" | "DELETE",
491
+ "target_id": "(旧记忆 ID;如果是 ADD 则为 null)",
492
+ "old_preference": "(旧的偏好文本;如果是 ADD 则为 null)",
493
+ "old_context_summary": "(旧的上下文摘要;如果是 ADD 则为 null)",
494
+ "new_preference": "(更新或新创建的偏好,如果适用)",
495
+ "new_context_summary": "(更新或新创建的上下文摘要,如果适用)",
496
+ "reason": "(决策的简要自然语言解释)"
497
+ }
498
+ ]
499
+ }
500
+
501
+ ## 输出要求
502
+
503
+ - 输出**必须**是有效的 JSON。
504
+ - 每个操作必须包含 `preference` 和 `context_summary` 的更新(如果适用)。
505
+ - 每个操作必须包含清晰的 `reason`。
506
+ - 多个召回记忆可以合并为一个统一的更新记忆。
507
+ - **不要**在 JSON 之外包含任何解释性文本。
508
+ """
509
+
510
+
511
+ NAIVE_JUDGE_UPDATE_OR_ADD_PROMPT_OP_TRACE_WITH_ONE_SHOT = """
512
+ # User Preference Memory Management Agent
513
+
514
+ You are a **User Preference Memory Management Agent**.
515
+ Your goal is to maintain a user's long-term **preference memory base** by analyzing new preference information and determining how it should update existing memories.
516
+
517
+ Each memory entry contains three fields:
518
+ - **id**: a unique identifier for the memory.
519
+ - **context_summary**: a factual summary of the dialogue or situation from which the preference was extracted.
520
+ - **preference**: the extracted statement describing the user's preference or tendency.
521
+
522
+ When updating a preference, you should also integrate and update the corresponding `context_summary` to ensure both fields stay semantically consistent.
523
+
524
+ You must produce a complete **operation trace**, showing which memory entries (identified by unique IDs) should be **added**, **updated**, or **deleted**, and then output the **final memory state** after all operations.
525
+
526
+ ## Input Format
527
+
528
+ New preference memories (new_memories):
529
+ {new_memories}
530
+
531
+ Retrieved preference memories (retrieved_memories):
532
+ {retrieved_memories}
533
+ ## Task Instructions
534
+
535
+ 1. For each new memory, analyze its relationship with the retrieved memories:
536
+ - If a new memory is **unrelated** to all retrieved memories → perform `"ADD"` (insert as a new independent memory);
537
+ - If a new memory is **related** to one or more retrieved memories → perform `"UPDATE"` on those related retrieved memories (refine, supplement, or merge both the `preference` and the `context_summary`, while preserving change history trajectory information);
538
+ - If one or more retrieved memories are merged into one updated memory → perform `"DELETE"` on those retrieved memories.
539
+
540
+ 2. **Important**: Only retrieved memories that are related to the new memories should be updated or deleted. Retrieved memories that are unrelated to any new memory must be preserved as-is in the final state.
541
+
542
+ 3. If multiple retrieved memories describe the same preference theme, merge them into one updated memory entry, combining both their `preference` information and their `context_summary` in a coherent and concise way.
543
+
544
+ 4. Output a structured list of **operation traces**, each explicitly stating:
545
+ - which memory (by ID) is affected,
546
+ - what operation is performed,
547
+ - the before/after `preference` and `context_summary`,
548
+ - and the reasoning behind it.
549
+
550
+ 5. Output the **final memory state (after_update_state)**, representing the complete preference memory base after applying all operations. This must include:
551
+ - All newly added memories (from ADD operations)
552
+ - All updated memories (from UPDATE operations)
553
+ - All unrelated retrieved memories that were preserved unchanged
554
+
555
+ ## Output Format (JSON)
556
+
557
+ {
558
+ "trace": [
559
+ {
560
+ "op_id": "op_1",
561
+ "type": "ADD" | "UPDATE" | "DELETE",
562
+ "target_id": "(the old memory ID; null if ADD)",
563
+ "old_preference": "(the old preference text; null if ADD)",
564
+ "old_context_summary": "(the old context summary; null if ADD)",
565
+ "new_preference": "(the updated or newly created preference, if applicable)",
566
+ "new_context_summary": "(the updated or newly created context summary, if applicable)",
567
+ "reason": "(brief natural-language explanation for the decision)"
568
+ }
569
+ ],
570
+ "after_update_state": [
571
+ {
572
+ "id": "id1",
573
+ "context_summary": "updated factual summary of the context",
574
+ "preference": "updated or final preference text"
575
+ }
576
+ ]
577
+ }
578
+
579
+ ## Example
580
+
581
+ **Input:**
582
+ new_memories:
583
+ [
584
+ {
585
+ "id": "new_id1",
586
+ "context_summary": "During a recent chat about study habits, the user mentioned that he often studies in quiet coffee shops and has started preferring lattes over Americanos, which he only drinks occasionally.",
587
+ "preference": "User now prefers lattes but occasionally drinks Americanos; he also enjoys studying in quiet coffee shops."
588
+ },
589
+ {
590
+ "id": "new_id2",
591
+ "context_summary": "The user mentioned in a conversation about beverages that he has recently started enjoying green tea in the morning.",
592
+ "preference": "User now enjoys drinking green tea in the morning."
593
+ },
594
+ {
595
+ "id": "new_id3",
596
+ "context_summary": "The user shared that he has recently started learning to play the guitar and practices for about 30 minutes every evening.",
597
+ "preference": "User enjoys playing guitar and practices regularly in the evenings."
598
+ }
599
+ ]
600
+
601
+ retrieved_memories:
602
+ [
603
+ {
604
+ "id": "id1",
605
+ "context_summary": "The user previously said he likes coffee in general.",
606
+ "preference": "User likes coffee."
607
+ },
608
+ {
609
+ "id": "id2",
610
+ "context_summary": "The user once mentioned preferring Americanos during work breaks.",
611
+ "preference": "User prefers Americanos."
612
+ },
613
+ {
614
+ "id": "id3",
615
+ "context_summary": "The user said he often works from home",
616
+ "preference": "User likes working from home."
617
+ },
618
+ {
619
+ "id": "id4",
620
+ "context_summary": "The user noted he doesn't drink tea very often.",
621
+ "preference": "User has no particular interest in tea."
622
+ },
623
+ {
624
+ "id": "id5",
625
+ "context_summary": "The user mentioned he enjoys running in the park on weekends.",
626
+ "preference": "User likes running outdoors on weekends."
627
+ }
628
+ ]
629
+
630
+ **Output:**
631
+ {
632
+ "trace": [
633
+ {
634
+ "op_id": "op_1",
635
+ "type": "UPDATE",
636
+ "target_id": "id1",
637
+ "old_preference": "User likes coffee.",
638
+ "old_context_summary": "The user previously said he likes coffee in general.",
639
+ "new_preference": "User likes coffee, especially lattes, but occasionally drinks Americanos.",
640
+ "new_context_summary": "The user discussed his coffee habits, stating he now prefers lattes but only occasionally drinks Americanos",
641
+ "reason": "New memory new_id1 refines and expands the coffee preference and context while preserving frequency semantics ('occasionally')."
642
+ },
643
+ {
644
+ "op_id": "op_2",
645
+ "type": "DELETE",
646
+ "target_id": "id2",
647
+ "old_preference": "User prefers Americanos.",
648
+ "old_context_summary": "The user once mentioned preferring Americanos during work breaks.",
649
+ "new_preference": null,
650
+ "new_context_summary": null,
651
+ "reason": "This old memory is now merged into the updated coffee preference (id1)."
652
+ },
653
+ {
654
+ "op_id": "op_3",
655
+ "type": "UPDATE",
656
+ "target_id": "id3",
657
+ "old_preference": "User likes working from home.",
658
+ "old_context_summary": "The user said he often works from home.",
659
+ "new_preference": "User now prefers studying in quiet coffee shops instead of working from home.",
660
+ "new_context_summary": "The user mentioned shifting from working at home to studying in quiet cafes, reflecting a new preferred environment.",
661
+ "reason": "New memory new_id1 indicates a preference change for the working environment."
662
+ },
663
+ {
664
+ "op_id": "op_4",
665
+ "type": "UPDATE",
666
+ "target_id": "id4",
667
+ "old_preference": "User has no particular interest in tea.",
668
+ "old_context_summary": "The user noted he doesn't drink tea very often.",
669
+ "new_preference": "The user does not drink tea very often before, but now enjoys drinking green tea in the morning.",
670
+ "new_context_summary": "The user mentioned that he has recently started enjoying green tea in the morning.",
671
+ "reason": "New memory new_id2 indicates a preference change for tea consumption."
672
+ },
673
+ {
674
+ "op_id": "op_5",
675
+ "type": "ADD",
676
+ "target_id": "new_id3",
677
+ "old_preference": null,
678
+ "old_context_summary": null,
679
+ "new_preference": "User enjoys playing guitar and practices regularly in the evenings.",
680
+ "new_context_summary": "The user shared that he has recently started learning to play the guitar and practices for about 30 minutes every evening.",
681
+ "reason": "This is a completely new preference unrelated to any existing memories, so it should be added as a new entry."
682
+ }
683
+ ],
684
+ "after_update_state": [
685
+ {
686
+ "id": "id1",
687
+ "context_summary": "The user discussed his coffee habits, saying he now prefers lattes but only occasionally drinks Americanos.",
688
+ "preference": "User likes coffee, especially lattes, but occasionally drinks Americanos."
689
+ },
690
+ {
691
+ "id": "id3",
692
+ "context_summary": "The user mentioned shifting from working at home to studying in quiet cafes, reflecting a new preferred environment.",
693
+ "preference": "User now prefers studying in quiet coffee shops instead of working from home."
694
+ },
695
+ {
696
+ "id": "id4",
697
+ "context_summary": "The user mentioned that he has recently started enjoying green tea in the morning.",
698
+ "preference": "The user does not drink tea very often before, but now enjoys drinking green tea in the morning."
699
+ },
700
+ {
701
+ "id": "id5",
702
+ "context_summary": "The user mentioned he enjoys running in the park on weekends.",
703
+ "preference": "User likes running outdoors on weekends."
704
+ },
705
+ {
706
+ "id": "new_id3",
707
+ "context_summary": "The user shared that he has recently started learning to play the guitar and practices for about 30 minutes every evening.",
708
+ "preference": "User enjoys playing guitar and practices regularly in the evenings."
709
+ }
710
+ ]
711
+ }
712
+
713
+ ## Output Requirements
714
+
715
+ - The output **must** be valid JSON.
716
+ - Each operation must include both `preference` and `context_summary` updates where applicable.
717
+ - Each operation must include a clear `reason`.
718
+ - Multiple retrieved memories may be merged into one unified updated memory.
719
+ - `after_update_state` must reflect the final, post-update state of the preference memory base.
720
+ - Do **not** include any explanatory text outside the JSON.
721
+ """
722
+
723
+
724
+ PREF_INSTRUCTIONS = """
725
+ # Note:
726
+ Fact memory are summaries of facts, while preference memory are summaries of user preferences.
727
+ Your response must not violate any of the user's preferences, whether explicit or implicit, and briefly explain why you answer this way to avoid conflicts.
728
+ """
729
+
730
+
731
+ PREF_INSTRUCTIONS_ZH = """
732
+ # 注意:
733
+ 事实记忆是事实的摘要,而偏好记忆是用户偏好的摘要。
734
+ 你的回复不得违反用户的任何偏好,无论是显式偏好还是隐式偏好,并简要解释你为什么这样回答以避免冲突。
735
+ """