diff --git a/docs/api-reference/overview.mdx b/docs/api-reference/overview.mdx index 99b393fc..c68708bb 100644 --- a/docs/api-reference/overview.mdx +++ b/docs/api-reference/overview.mdx @@ -33,8 +33,8 @@ Example with the mem0 Python package: from mem0 import MemoryClient client = MemoryClient( - organization_name='YOUR_ORG_NAME', - project_name='YOUR_PROJECT_NAME', + organization='YOUR_ORG_NAME', + project='YOUR_PROJECT_NAME', ) ``` diff --git a/mem0/client/main.py b/mem0/client/main.py index 60424f52..7ab46030 100644 --- a/mem0/client/main.py +++ b/mem0/client/main.py @@ -55,19 +55,29 @@ class MemoryClient: client (httpx.Client): The HTTP client used for making API requests. """ - def __init__(self, api_key: Optional[str] = None, host: Optional[str] = None): + def __init__( + self, + api_key: Optional[str] = None, + host: Optional[str] = None, + organization: Optional[str] = None, + project: Optional[str] = None + ): """Initialize the MemoryClient. Args: api_key: The API key for authenticating with the Mem0 API. If not provided, it will attempt to use the MEM0_API_KEY environment variable. host: The base URL for the Mem0 API. Defaults to "https://api.mem0.ai". + org_name: The name of the organization. Optional. + project_name: The name of the project. Optional. Raises: ValueError: If no API key is provided or found in the environment. """ self.api_key = api_key or os.getenv("MEM0_API_KEY") self.host = host or "https://api.mem0.ai" + self.organization = organization + self.project = project self.user_id = get_user_id() if not self.api_key: @@ -103,6 +113,7 @@ class MemoryClient: Raises: APIError: If the API request fails. """ + kwargs.update({"org_name": self.organization, "project_name": self.project}) payload = self._prepare_payload(messages, kwargs) response = self.client.post("/v1/memories/", json=payload) response.raise_for_status() @@ -140,6 +151,7 @@ class MemoryClient: Raises: APIError: If the API request fails. """ + kwargs.update({"org_name": self.organization, "project_name": self.project}) params = self._prepare_params(kwargs) response = self.client.get("/v1/memories/", params=params) response.raise_for_status() @@ -166,6 +178,7 @@ class MemoryClient: APIError: If the API request fails. """ payload = {"query": query} + kwargs.update({"org_name": self.organization, "project_name": self.project}) payload.update({k: v for k, v in kwargs.items() if v is not None}) response = self.client.post(f"/{version}/memories/search/", json=payload) response.raise_for_status() @@ -217,6 +230,7 @@ class MemoryClient: Raises: APIError: If the API request fails. """ + kwargs.update({"org_name": self.organization, "project_name": self.project}) params = self._prepare_params(kwargs) response = self.client.delete("/v1/memories/", params=params) response.raise_for_status() @@ -244,7 +258,8 @@ class MemoryClient: @api_error_handler def users(self): """Get all users, agents, and sessions for which memories exist.""" - response = self.client.get("/v1/entities/") + params = {"org_name": self.organization, "project_name": self.project} + response = self.client.get("/v1/entities/", params=params) response.raise_for_status() capture_client_event("client.users", self) return response.json() @@ -252,9 +267,12 @@ class MemoryClient: @api_error_handler def delete_users(self) -> Dict[str, str]: """Delete all users, agents, or sessions.""" + params = {"org_name": self.organization, "project_name": self.project} entities = self.users() for entity in entities["results"]: - response = self.client.delete(f"/v1/entities/{entity['type']}/{entity['id']}/") + response = self.client.delete( + f"/v1/entities/{entity['type']}/{entity['id']}/", params=params + ) response.raise_for_status() capture_client_event("client.delete_users", self)