From 355bf3b48b9a94a60e12089ced35595d559b60f9 Mon Sep 17 00:00:00 2001 From: Alessandro Romano Date: Tue, 7 Jan 2025 18:46:10 +0100 Subject: [PATCH] Fix API Key Behavior and Entity Handling in Mem0 Integration (#1857) * docs: clarify how to specify org_id and project_id in Mem0 configuration * Add org_id and project_id to mem0 config and fix mem0 entity '400 Bad Request' * Remove ruff changes to docs --------- Co-authored-by: Brandon Hancock (bhancock_ai) <109994880+bhancockio@users.noreply.github.com> --- docs/concepts/memory.mdx | 17 +++++++++++++++++ src/crewai/memory/storage/mem0_storage.py | 18 +++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/docs/concepts/memory.mdx b/docs/concepts/memory.mdx index a7677cec1..b04b29c64 100644 --- a/docs/concepts/memory.mdx +++ b/docs/concepts/memory.mdx @@ -134,6 +134,23 @@ crew = Crew( ) ``` +## Memory Configuration Options +If you want to access a specific organization and project, you can set the `org_id` and `project_id` parameters in the memory configuration. + +```python Code +from crewai import Crew + +crew = Crew( + agents=[...], + tasks=[...], + verbose=True, + memory=True, + memory_config={ + "provider": "mem0", + "config": {"user_id": "john", "org_id": "my_org_id", "project_id": "my_project_id"}, + }, +) +``` ## Additional Embedding Providers diff --git a/src/crewai/memory/storage/mem0_storage.py b/src/crewai/memory/storage/mem0_storage.py index e4e84fab4..be889afff 100644 --- a/src/crewai/memory/storage/mem0_storage.py +++ b/src/crewai/memory/storage/mem0_storage.py @@ -27,10 +27,18 @@ class Mem0Storage(Storage): raise ValueError("User ID is required for user memory type") # API key in memory config overrides the environment variable - mem0_api_key = self.memory_config.get("config", {}).get("api_key") or os.getenv( - "MEM0_API_KEY" - ) - self.memory = MemoryClient(api_key=mem0_api_key) + config = self.memory_config.get("config", {}) + mem0_api_key = config.get("api_key") or os.getenv("MEM0_API_KEY") + mem0_org_id = config.get("org_id") + mem0_project_id = config.get("project_id") + + # Initialize MemoryClient with available parameters + if mem0_org_id and mem0_project_id: + self.memory = MemoryClient( + api_key=mem0_api_key, org_id=mem0_org_id, project_id=mem0_project_id + ) + else: + self.memory = MemoryClient(api_key=mem0_api_key) def _sanitize_role(self, role: str) -> str: """ @@ -57,7 +65,7 @@ class Mem0Storage(Storage): metadata={"type": "long_term", **metadata}, ) elif self.memory_type == "entities": - entity_name = None + entity_name = self._get_agent_name() self.memory.add( value, user_id=entity_name, metadata={"type": "entity", **metadata} )