Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

Fix API Key Behavior and Entity Handling in Mem0 Integration (#1857)

Alessandro Romano355bf3b48b9a
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}
)