From d8378fbb70568c1b57e709c6287d50571adac808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Tue, 16 Jun 2026 13:19:23 +0800 Subject: [PATCH] add no answer tag --- agent/mem0_manager.py | 31 +++++++++++++++++++++++++++++++ prompt/system_prompt.md | 13 +++++++++++++ 2 files changed, 44 insertions(+) diff --git a/agent/mem0_manager.py b/agent/mem0_manager.py index ed03b52..f939e86 100644 --- a/agent/mem0_manager.py +++ b/agent/mem0_manager.py @@ -120,6 +120,37 @@ except Exception as e: logger.warning(f"Failed to patch mem0 remove_code_blocks: {e}") +# Monkey patch: make PGVector.__del__ tolerate cur/conn being None. +# This project shares a single psycopg2 pool and explicitly sets vector_store.cur +# and vector_store.conn to None after releasing the connection. mem0's original +# __del__ calls self.cur.close() without a None check, raising a harmless but noisy +# "Exception ignored in __del__: AttributeError" when the instance is garbage +# collected. This replacement releases resources only when they still exist. +def _safe_pgvector_del(self) -> None: + """Safely close PGVector cursor/connection, tolerating None values.""" + try: + cur = getattr(self, "cur", None) + if cur is not None: + cur.close() + conn = getattr(self, "conn", None) + if conn is not None: + conn.close() + except Exception: + # Never raise from __del__; ignore any teardown errors + pass + + +try: + import mem0.vector_stores.pgvector as mem0_pgvector + mem0_pgvector.PGVector.__del__ = _safe_pgvector_del + logger.info("Successfully patched mem0 PGVector.__del__ to tolerate None cur/conn") +except ImportError: + # mem0 pgvector module not available; nothing to patch + pass +except Exception as e: + logger.warning(f"Failed to patch mem0 PGVector.__del__: {e}") + + class Mem0Manager: """ Mem0 connection and instance manager diff --git a/prompt/system_prompt.md b/prompt/system_prompt.md index aff46de..39a2177 100644 --- a/prompt/system_prompt.md +++ b/prompt/system_prompt.md @@ -94,3 +94,16 @@ Trace Id: {trace_id} - Even when the user writes in a different language, you MUST still reply in [{language}]. - Do NOT mix languages. Do NOT fall back to English or any other language under any circumstances. - Technical terms, code identifiers, file paths, and tool names may remain in their original form, but all surrounding text MUST be in [{language}]. + +# Unanswerable Response Specification (MANDATORY) +When you genuinely cannot answer because no relevant information was found in the knowledge base / retrieval sources (and self-knowledge fallback is unavailable or insufficient), your reply MUST include the literal sentinel marker ``. + +Rules: +- Output the marker `` **exactly as written** — it is a fixed ASCII literal. NEVER translate, rewrite, reformat, or wrap it in code blocks. +- Place the marker at the **very beginning** of your reply, immediately followed by a polite apology written in [{language}]. +- NEVER output the marker alone — it MUST be followed by an apology in [{language}] so the user sees a meaningful message. +- When you CAN answer (you found relevant information), you MUST NOT output this marker under any circumstances. +- The marker is language-independent; only the apology text after it must be in [{language}]. + +Examples: +- `Sorry, I couldn't find that information in the knowledge base.`