diff --git a/src/memos/api/handlers/chat_handler.py b/src/memos/api/handlers/chat_handler.py index 8292e027b..e847a0397 100644 --- a/src/memos/api/handlers/chat_handler.py +++ b/src/memos/api/handlers/chat_handler.py @@ -1323,12 +1323,14 @@ def run_async_in_thread(): ) # Add exception handling for the background task task.add_done_callback( - lambda t: self.logger.error( - f"Error in background post-chat processing for user {user_id}: {t.exception()}", - exc_info=True, + lambda t: ( + self.logger.error( + f"Error in background post-chat processing for user {user_id}: {t.exception()}", + exc_info=True, + ) + if t.exception() + else None ) - if t.exception() - else None ) except RuntimeError: # No event loop, run in a new thread with context propagation @@ -1390,12 +1392,14 @@ def run_async_in_thread(): ) ) task.add_done_callback( - lambda t: self.logger.error( - f"Error in background add to memory for user {user_id}: {t.exception()}", - exc_info=True, + lambda t: ( + self.logger.error( + f"Error in background add to memory for user {user_id}: {t.exception()}", + exc_info=True, + ) + if t.exception() + else None ) - if t.exception() - else None ) except RuntimeError: thread = ContextThread( diff --git a/src/memos/api/handlers/search_handler.py b/src/memos/api/handlers/search_handler.py index 6eda1e2aa..f8ab90470 100644 --- a/src/memos/api/handlers/search_handler.py +++ b/src/memos/api/handlers/search_handler.py @@ -11,6 +11,7 @@ from typing import Any from memos.api.handlers.base_handler import BaseHandler, HandlerDependencies +from memos.api.handlers.formatters_handler import rerank_knowledge_mem from memos.api.product_models import APISearchRequest, SearchResponse from memos.log import get_logger from memos.memories.textual.tree_text_memory.retrieve.retrieve_utils import ( @@ -93,6 +94,15 @@ def handle_search_memories(self, search_req: APISearchRequest) -> SearchResponse results = self._mmr_dedup_text_memories(results, original_top_k, pref_top_k) self._strip_embeddings(results) + text_mem = results["text_mem"] + results["text_mem"] = rerank_knowledge_mem( + self.reranker, + query=search_req.query, + text_mem=text_mem, + top_k=original_top_k, + file_mem_proportion=0.5, + ) + self.logger.info( f"[SearchHandler] Final search results: count={len(results)} results={results}" ) diff --git a/src/memos/mem_os/product.py b/src/memos/mem_os/product.py index 77a5e70c9..b2c74c384 100644 --- a/src/memos/mem_os/product.py +++ b/src/memos/mem_os/product.py @@ -798,12 +798,14 @@ def run_async_in_thread(): ) # Add exception handling for the background task task.add_done_callback( - lambda t: logger.error( - f"Error in background post-chat processing for user {user_id}: {t.exception()}", - exc_info=True, + lambda t: ( + logger.error( + f"Error in background post-chat processing for user {user_id}: {t.exception()}", + exc_info=True, + ) + if t.exception() + else None ) - if t.exception() - else None ) except RuntimeError: # No event loop, run in a new thread with context propagation diff --git a/src/memos/mem_os/product_server.py b/src/memos/mem_os/product_server.py index 758f2794d..80aefea85 100644 --- a/src/memos/mem_os/product_server.py +++ b/src/memos/mem_os/product_server.py @@ -437,12 +437,14 @@ def run_async_in_thread(): ) # Add exception handling for the background task task.add_done_callback( - lambda t: logger.error( - f"Error in background post-chat processing for user {user_id}: {t.exception()}", - exc_info=True, + lambda t: ( + logger.error( + f"Error in background post-chat processing for user {user_id}: {t.exception()}", + exc_info=True, + ) + if t.exception() + else None ) - if t.exception() - else None ) except RuntimeError: # No event loop, run in a new thread with context propagation