From 043d6cb56be1f4bf8f742758fac79e7099178740 Mon Sep 17 00:00:00 2001 From: xinyan Date: Sat, 29 Nov 2025 11:42:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0mem0=E7=9A=84=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/open_webui/memory/mem0.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/backend/open_webui/memory/mem0.py b/backend/open_webui/memory/mem0.py index bab715afb0..64825781ba 100644 --- a/backend/open_webui/memory/mem0.py +++ b/backend/open_webui/memory/mem0.py @@ -1,4 +1,12 @@ +from mem0 import MemoryClient +import os +from logging import getLogger +log = getLogger(__name__) + +mem0_api_key = os.getenv("MEM0_API_KEY") +memory_client = MemoryClient(api_key=mem0_api_key) + async def mem0_search(user_id: str, chat_id: str, last_message: str) -> list[str]: """ 预留的 Mem0 检索接口:当前为占位实现。 @@ -7,11 +15,14 @@ async def mem0_search(user_id: str, chat_id: str, last_message: str) -> list[str """ try: # TODO: 接入真实 Mem0 检索 - print("mem0_search") - print("user_id:", user_id) - print("chat_id:", chat_id) - print("last_message:", last_message) - return [] + log.info(f"mem0_search called with user_id: {user_id}, chat_id: {chat_id}, last_message: {last_message}") + serach_rst = memory_client.search( + query=last_message, + filters={"user_id": user_id} + ) + memories=serach_rst["results"] if "results" in serach_rst else serach_rst + log.info(f"mem0_search found {len(memories)} memories") + return [mem["text"] for mem in memories] except Exception as e: log.debug(f"Mem0 search failed: {e}") return [] @@ -24,9 +35,10 @@ async def mem0_delete(user_id: str, chat_id: str) -> bool: """ try: # TODO: 接入真实删除逻辑(如按 chat_id 过滤) - print("mem0_delete") - print("user_id:", user_id) - print("chat_id:", chat_id) + log.info(f"mem0_delete called with user_id: {user_id}, chat_id: {chat_id}") + memory_client.delete( + filters={"user_id": user_id} + ) return True except Exception as e: log.debug(f"Mem0 delete failed: {e}")