-
Notifications
You must be signed in to change notification settings - Fork 1
feat: integrate Mem0 with ADK memory service #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fe8c89f
feat: integrate Mem0 with ADK memory service
QueryPlanner d79e4c1
fix: use plain user_id in Mem0MemoryService search
QueryPlanner 7f7e4ba
fix: pass memory_service to Runner in AdkRuntime
QueryPlanner 8dafdee
fix: address PR review and CI failures
QueryPlanner 4bb019a
fix: address PR review feedback
QueryPlanner 35ef664
test: add test for Mem0MemoryService happy path
QueryPlanner 91261bd
fix: make date tests timezone-independent
QueryPlanner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| """Memory service that bridges Mem0 to ADK's BaseMemoryService interface.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import logging | ||
| from collections.abc import Mapping, Sequence | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from google.adk.events.event import Event | ||
| from google.adk.memory.base_memory_service import ( | ||
| BaseMemoryService, | ||
| SearchMemoryResponse, | ||
| ) | ||
| from google.adk.memory.memory_entry import MemoryEntry | ||
| from google.adk.sessions.session import Session | ||
| from google.genai import types | ||
|
|
||
| if TYPE_CHECKING: | ||
| from mem0 import Memory | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Mem0MemoryService(BaseMemoryService): | ||
| """Memory service backed by Mem0 OSS. | ||
|
|
||
| Wraps the existing Mem0 client to provide ADK-compatible memory operations. | ||
| Memories are managed manually via save_memory tool (no automatic session ingestion). | ||
| """ | ||
|
|
||
| def __init__(self, client: Memory): | ||
| self._client = client | ||
|
|
||
| async def add_session_to_memory(self, session: Session) -> None: | ||
| """Not used - user chose manual memory management via save_memory tool.""" | ||
| pass | ||
|
|
||
| async def add_events_to_memory( | ||
| self, | ||
| *, | ||
| app_name: str, | ||
| user_id: str, | ||
| events: Sequence[Event], | ||
| session_id: str | None = None, | ||
| custom_metadata: Mapping[str, object] | None = None, | ||
| ) -> None: | ||
| """Not used - user chose manual memory management.""" | ||
| pass | ||
|
|
||
| async def search_memory( | ||
| self, *, app_name: str, user_id: str, query: str | ||
| ) -> SearchMemoryResponse: | ||
| """Search memories via Mem0 and convert to ADK format. | ||
|
|
||
| Args: | ||
| app_name: The application name (unused, user_id is passed directly). | ||
| user_id: The user identifier. | ||
| query: The search query. | ||
|
|
||
| Returns: | ||
| SearchMemoryResponse with matching MemoryEntry objects. | ||
| """ | ||
| from .config import get_search_limit | ||
|
|
||
| limit = get_search_limit() | ||
|
|
||
| try: | ||
| result = await asyncio.to_thread( | ||
| self._client.search, query=query, user_id=user_id, limit=limit | ||
| ) | ||
|
|
||
| raw_results = ( | ||
| result.get("results", []) if isinstance(result, dict) else result | ||
| ) or [] | ||
|
|
||
| memories: list[MemoryEntry] = [] | ||
| for m in raw_results: | ||
| if not isinstance(m, dict): | ||
| continue | ||
| memory_text = m.get("memory", "") | ||
| if not memory_text: | ||
| continue | ||
|
|
||
| memories.append( | ||
| MemoryEntry( | ||
| content=types.Content( | ||
| role="user", | ||
| parts=[types.Part(text=memory_text)], | ||
| ), | ||
| id=m.get("id"), | ||
| ) | ||
| ) | ||
|
|
||
| logger.debug( | ||
| "Found %d memories for query '%s' (user: %s)", | ||
| len(memories), | ||
| query[:30], | ||
| user_id, | ||
| ) | ||
| return SearchMemoryResponse(memories=memories) | ||
|
|
||
| except Exception: | ||
| logger.exception("Failed to search memories for user %s", user_id) | ||
| return SearchMemoryResponse(memories=[]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
create_memory_servicecall for themem0://scheme relies on a registration that currently happens insrc/blacki/server.py. Ifcreate_adk_runtimeis called from a context whereserver.pyhas not been imported (e.g., a standalone script or certain test configurations), this will raise aValueErrorbecause the scheme won't be registered in the globalservice_registry. Consider moving the registration to a more central location or ensuring it happens lazily within this function if not already present.