Add pre_llm_call hook to inject memory context before LLM
- Register pre_llm_call hook that searches memories and injects context - Add queue_prefetch_and_get() for synchronous prefetch in hook - Memory now available to AI without tool calls
This commit is contained in:
+35
@@ -287,6 +287,27 @@ class Mem0LocalMemoryProvider(MemoryProvider):
|
||||
return ""
|
||||
return f"## Mem0 Memory\n{result}"
|
||||
|
||||
def queue_prefetch_and_get(self, query: str) -> str:
|
||||
"""Sync prefetch for pre_llm_call hook - returns memory context immediately."""
|
||||
if self._is_breaker_open():
|
||||
return ""
|
||||
try:
|
||||
client = self._get_client()
|
||||
results = client.search(
|
||||
query=query,
|
||||
user_id=self._user_id,
|
||||
limit=5,
|
||||
)
|
||||
if results:
|
||||
lines = [r.get("text", "") for r in results if r.get("text")]
|
||||
if lines:
|
||||
self._record_success()
|
||||
return "\n".join(f"- {line}" for line in lines)
|
||||
except Exception as e:
|
||||
self._record_failure()
|
||||
logger.debug("Mem0 prefetch failed: %s", e)
|
||||
return ""
|
||||
|
||||
def queue_prefetch(self, query: str, *, session_id: str = "") -> None:
|
||||
"""Queue async prefetch for next turn (called before LLM request)."""
|
||||
if self._is_breaker_open():
|
||||
@@ -463,3 +484,17 @@ def register(ctx) -> None:
|
||||
handler=make_handler(schema["name"]),
|
||||
)
|
||||
logger.debug("Registered tool: %s", schema["name"])
|
||||
|
||||
# Register pre_llm_call hook to inject memory context
|
||||
def pre_llm_call_hook(user_message: str, **kwargs) -> dict:
|
||||
"""Inject memory context into user message before LLM call."""
|
||||
try:
|
||||
results = provider.queue_prefetch_and_get(user_message)
|
||||
if results:
|
||||
return {"context": results}
|
||||
except Exception as e:
|
||||
logger.debug("Mem0 pre_llm_call hook failed: %s", e)
|
||||
return {}
|
||||
|
||||
ctx.register_hook("pre_llm_call", pre_llm_call_hook)
|
||||
logger.debug("Registered pre_llm_call hook")
|
||||
|
||||
Reference in New Issue
Block a user