Skip to content

Commit

Permalink
Fix API Key Behavior and Entity Handling in Mem0 Integration (#1857)
Browse files Browse the repository at this point in the history
* 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) <[email protected]>
  • Loading branch information
pigna90 and bhancockio authored Jan 7, 2025
1 parent 0e94236 commit 355bf3b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
17 changes: 17 additions & 0 deletions docs/concepts/memory.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 13 additions & 5 deletions src/crewai/memory/storage/mem0_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ def __init__(self, type, crew=None):
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:
"""
Expand All @@ -57,7 +65,7 @@ def save(self, value: Any, metadata: Dict[str, Any]) -> None:
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}
)
Expand Down

0 comments on commit 355bf3b

Please sign in to comment.