Custom instructions API improvements (#2140)
Co-authored-by: Deshraj Yadav <deshrajdry@gmail.com>
This commit is contained in:
@@ -111,6 +111,13 @@ class MemoryClient:
|
||||
params = self._prepare_params()
|
||||
response = self.client.get("/v1/ping/", params=params)
|
||||
response.raise_for_status()
|
||||
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get('org_id') and data.get('project_id'):
|
||||
self.org_id = data.get('org_id')
|
||||
self.project_id = data.get('project_id')
|
||||
|
||||
except httpx.HTTPStatusError:
|
||||
raise ValueError("Invalid API Key. Please get a valid API Key from https://app.mem0.ai")
|
||||
|
||||
@@ -400,14 +407,14 @@ class MemoryClient:
|
||||
return response.json()
|
||||
|
||||
@api_error_handler
|
||||
def get_custom_instructions_and_categories(self, fields: List[str]) -> Dict[str, Any]:
|
||||
def get_project(self, fields: Optional[List[str]]=None) -> Dict[str, Any]:
|
||||
"""Get instructions or categories for the current project.
|
||||
|
||||
Args:
|
||||
fields: List of field names to retrieve (e.g. ['custom_instructions', 'custom_categories'])
|
||||
fields: List of fields to retrieve
|
||||
|
||||
Returns:
|
||||
Dictionary containing the requested instructions or categories.
|
||||
Dictionary containing the requested fields.
|
||||
|
||||
Raises:
|
||||
APIError: If the API request fails.
|
||||
@@ -418,20 +425,20 @@ class MemoryClient:
|
||||
|
||||
params = self._prepare_params({"fields": fields})
|
||||
response = self.client.get(
|
||||
f"/api/v1/orgs/organizations/{self.org_id}/projects/{self.project_id}/custom-instructions-and-categories/",
|
||||
f"/api/v1/orgs/organizations/{self.org_id}/projects/{self.project_id}/",
|
||||
params=params,
|
||||
)
|
||||
response.raise_for_status()
|
||||
capture_client_event("client.get_custom_instructions_and_categories", self, {"fields": fields})
|
||||
capture_client_event("client.get_project_details", self, {"fields": fields})
|
||||
return response.json()
|
||||
|
||||
@api_error_handler
|
||||
def update_custom_instructions_and_categories(self, fields: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Update instructions or categories for the current project.
|
||||
def update_project(self, custom_instructions: Optional[str]=None, custom_categories: Optional[List[str]]=None) -> Dict[str, Any]:
|
||||
"""Update the project settings.
|
||||
|
||||
Args:
|
||||
fields: Dictionary of fields to update
|
||||
(e.g. {"custom_categories": "new instructions", "custom_categories": ["cat1"]})
|
||||
custom_instructions: New instructions for the project
|
||||
custom_categories: New categories for the project
|
||||
|
||||
Returns:
|
||||
Dictionary containing the API response.
|
||||
@@ -443,12 +450,16 @@ class MemoryClient:
|
||||
if not (self.org_id and self.project_id):
|
||||
raise ValueError("org_id and project_id must be set to update instructions or categories")
|
||||
|
||||
response = self.client.post(
|
||||
f"/api/v1/orgs/organizations/{self.org_id}/projects/{self.project_id}/custom-instructions-and-categories/",
|
||||
json=fields,
|
||||
if custom_instructions is None and custom_categories is None:
|
||||
raise ValueError("Currently we only support updating custom_instructions or custom_categories, so you must provide at least one of them")
|
||||
|
||||
payload = self._prepare_params({"custom_instructions": custom_instructions, "custom_categories": custom_categories})
|
||||
response = self.client.patch(
|
||||
f"/api/v1/orgs/organizations/{self.org_id}/projects/{self.project_id}/",
|
||||
json=payload,
|
||||
)
|
||||
response.raise_for_status()
|
||||
capture_client_event("client.update_custom_instructions_and_categories", self, {"fields": list(fields.keys())})
|
||||
capture_client_event("client.update_project", self, {"custom_instructions": custom_instructions, "custom_categories": custom_categories})
|
||||
return response.json()
|
||||
|
||||
def chat(self):
|
||||
@@ -505,17 +516,19 @@ class MemoryClient:
|
||||
"Note that org_name/project_name are deprecated."
|
||||
)
|
||||
|
||||
# Add org_id and project_id if available
|
||||
if self.org_id:
|
||||
# Add org_id and project_id if both are available
|
||||
if self.org_id and self.project_id:
|
||||
kwargs["org_id"] = self.org_id
|
||||
if self.project_id:
|
||||
kwargs["project_id"] = self.project_id
|
||||
elif self.org_id or self.project_id:
|
||||
raise ValueError("Please provide both org_id and project_id")
|
||||
|
||||
# Add deprecated org_name and project_name for backward compatibility
|
||||
if self.organization:
|
||||
# Add deprecated org_name and project_name if both are available
|
||||
if self.organization and self.project:
|
||||
kwargs["org_name"] = self.organization
|
||||
if self.project:
|
||||
kwargs["project_name"] = self.project
|
||||
elif self.organization or self.project:
|
||||
raise ValueError("Please provide both org_name and project_name")
|
||||
|
||||
return {k: v for k, v in kwargs.items() if v is not None}
|
||||
|
||||
@@ -731,33 +744,35 @@ class AsyncMemoryClient:
|
||||
return response.json()
|
||||
|
||||
@api_error_handler
|
||||
async def get_custom_instructions_and_categories(self, fields: List[str]) -> Dict[str, Any]:
|
||||
async def get_project(self, fields: Optional[List[str]]=None) -> Dict[str, Any]:
|
||||
if not (self.sync_client.org_id and self.sync_client.project_id):
|
||||
raise ValueError("org_id and project_id must be set to access instructions or categories")
|
||||
|
||||
params = self.sync_client._prepare_params({"fields": fields})
|
||||
|
||||
params = self._prepare_params({"fields": fields})
|
||||
response = await self.async_client.get(
|
||||
f"/api/v1/orgs/organizations/{self.sync_client.org_id}/projects/{self.sync_client.project_id}/custom-instructions-and-categories/",
|
||||
f"/api/v1/orgs/organizations/{self.sync_client.org_id}/projects/{self.sync_client.project_id}/",
|
||||
params=params,
|
||||
)
|
||||
response.raise_for_status()
|
||||
capture_client_event(
|
||||
"async_client.get_custom_instructions_and_categories", self.sync_client, {"fields": fields}
|
||||
"async_client.get_project", self.sync_client, {"fields": fields}
|
||||
)
|
||||
return response.json()
|
||||
|
||||
@api_error_handler
|
||||
async def update_custom_instructions_and_categories(self, fields: Dict[str, Any]) -> Dict[str, Any]:
|
||||
async def update_project(self, custom_instructions: Optional[str], custom_categories: Optional[List[str]]) -> Dict[str, Any]:
|
||||
if not (self.sync_client.org_id and self.sync_client.project_id):
|
||||
raise ValueError("org_id and project_id must be set to update instructions or categories")
|
||||
|
||||
response = await self.async_client.post(
|
||||
f"/api/v1/orgs/organizations/{self.sync_client.org_id}/projects/{self.sync_client.project_id}/custom-instructions-and-categories/",
|
||||
json=fields,
|
||||
payload = self.sync_client._prepare_params({"custom_instructions": custom_instructions, "custom_categories": custom_categories})
|
||||
response = await self.async_client.patch(
|
||||
f"/api/v1/orgs/organizations/{self.sync_client.org_id}/projects/{self.sync_client.project_id}/",
|
||||
json=payload,
|
||||
)
|
||||
response.raise_for_status()
|
||||
capture_client_event(
|
||||
"async_client.update_custom_instructions_and_categories", self.sync_client, {"fields": list(fields.keys())}
|
||||
"async_client.update_project", self.sync_client, {"custom_instructions": custom_instructions, "custom_categories": custom_categories}
|
||||
)
|
||||
return response.json()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user