-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Python: fix(mem0): support mem0ai 2.x in Mem0ContextProvider #6129
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,7 +60,10 @@ def __init__( | |
| source_id: Unique identifier for this provider instance. | ||
| mem0_client: A pre-created Mem0 MemoryClient or None to create a default client. | ||
| api_key: The API key for authenticating with the Mem0 API. | ||
| application_id: The application ID for scoping memories. | ||
| application_id: The application ID for scoping memories. Platform-only: | ||
| the OSS ``AsyncMemory`` client does not recognize an application | ||
| scope (it scopes only by user_id/agent_id/run_id), so an OSS client | ||
| requires user_id or agent_id and application_id alone is rejected. | ||
| agent_id: The agent ID for scoping memories. | ||
| user_id: The user ID for scoping memories. | ||
| context_prompt: The prompt to prepend to retrieved memories. | ||
|
|
@@ -108,16 +111,17 @@ async def before_run( | |
|
|
||
| filters = self._build_filters() | ||
|
|
||
| # AsyncMemory (OSS) expects user_id/agent_id/run_id as direct kwargs | ||
| # AsyncMemoryClient (Platform) expects them in a filters dict | ||
| search_kwargs: dict[str, Any] = {"query": input_text} | ||
| # mem0 >=2.0: both OSS (AsyncMemory) and Platform (AsyncMemoryClient) take | ||
| # entity IDs in a filters dict (top-level entity kwargs are rejected). OSS only | ||
| # recognizes user_id/agent_id/run_id as entities, so app_id (Platform-only) is | ||
| # dropped for the OSS client to avoid it being treated as a non-matching | ||
| # metadata filter that would silently exclude all results. | ||
| if isinstance(self.mem0_client, AsyncMemory): | ||
| search_kwargs.update(filters) | ||
| else: | ||
| search_kwargs["filters"] = filters | ||
| filters = {key: value for key, value in filters.items() if key != "app_id"} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we update the corresponding platform add path to use this same 2.x
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| search_response: _MemorySearchResponse_v1_1 | _MemorySearchResponse_v2 = await self.mem0_client.search( # type: ignore[misc] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned above _MemorySearchResponse_v1_1 can be deprecated |
||
| **search_kwargs, | ||
| query=input_text, | ||
| filters=filters, | ||
|
youneshima marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| if isinstance(search_response, list): | ||
|
|
@@ -169,9 +173,14 @@ def get_role_value(role: Any) -> str: | |
| # -- Internal methods ------------------------------------------------------ | ||
|
|
||
| def _validate_filters(self) -> None: | ||
| """Validates that at least one filter is provided.""" | ||
| """Validates that at least one usable filter is provided for the configured client.""" | ||
| if not self.agent_id and not self.user_id and not self.application_id: | ||
| raise ValueError("At least one of the filters: agent_id, user_id, or application_id is required.") | ||
| if isinstance(self.mem0_client, AsyncMemory) and not self.user_id and not self.agent_id: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see the intent but messaging can be improved, right now even if users are sending application_id (in OSS client) it would seem as if it matters but actually it doesn't |
||
| raise ValueError( | ||
| "application_id is not supported by the OSS AsyncMemory client, which scopes " | ||
| "memories only by user_id/agent_id. Provide user_id or agent_id." | ||
| ) | ||
|
|
||
| def _build_filters(self) -> dict[str, Any]: | ||
| """Build search filters from initialization parameters.""" | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.