Skip to content

Commit

Permalink
Update user crud methods (#19)
Browse files Browse the repository at this point in the history
* Update user crud methods

* Add new optional field
  • Loading branch information
cdpierse authored Oct 7, 2024
1 parent 33d8ccd commit 90387f0
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 4 deletions.
69 changes: 65 additions & 4 deletions docs/3_add_items_and_interactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ user = client.user.get_user(user_id)
print(user)
```

### Updating User Information

To update a user's information:

```python
updated_user = User(id="user123", properties={"name": "John Smith", "age": 31})
response = client.user.update_user(updated_user)
print(response)
```

### Deleting a User

To delete a user from the recommender system:

```python
user_id = "user123"
response = client.user.delete_user(user_id)
print(response)
```

### Checking if a User Exists

To check if a user exists in the system:

```python
user_id = "user123"
exists = client.user.exists(user_id)
print(f"User exists: {exists}")
```

## Recording User Interactions

### Adding a Single User Interaction
Expand All @@ -87,7 +117,8 @@ response = client.user.add_interaction(
user_id=user_id,
item_id=item_id,
interaction_property_name=interaction_type,
weight=weight
weight=weight,
remove_previous_interactions=False
)
print(response)
```
Expand All @@ -100,8 +131,8 @@ For bulk addition of user interactions:
from weaviate_recommend.models.data import UserInteraction

interactions = [
UserInteraction(user_id="user1", item_id="1", interaction_property_name="purchase", weight=1.0),
UserInteraction(user_id="user1", item_id="4", interaction_property_name="purchase", weight=0.5),
UserInteraction(user_id="user1", item_id="1", interaction_property_name="purchase", weight=1.0, remove_previous_interactions=False),
UserInteraction(user_id="user1", item_id="4", interaction_property_name="purchase", weight=0.5, remove_previous_interactions=False),
# add more interactions as needed
]

Expand All @@ -120,13 +151,43 @@ for interaction in interactions:
print(f"Item: {interaction.item_id}, Type: {interaction.interaction_property_name}, Weight: {interaction.weight}")
```

### Deleting User Interactions

To delete all interactions for a user:

```python
user_id = "user123"
response = client.user.delete_all_interactions(user_id)
print(response)
```

To delete interactions for a specific property:

```python
user_id = "user123"
interaction_property = "purchase"
response = client.user.delete_interactions_by_property(user_id, interaction_property)
print(response)
```

To delete interactions for a specific property and item:

```python
user_id = "user123"
interaction_property = "purchase"
item_id = "item456"
response = client.user.delete_interactions_by_property_and_item(user_id, interaction_property, item_id)
print(response)
```

## Best Practices

1. Ensure all required properties are included when adding items or creating users.
2. Use batch operations for adding multiple items or user interactions efficiently.
3. Choose appropriate weights for user interactions to reflect their importance.
4. Regularly add user interactions to keep the recommender system up-to-date.
5. Use the `get_user` and `get_user_interactions` methods to retrieve and analyze user data as needed.
5. Use the various retrieval and deletion methods to manage user data and interactions as needed.
6. Periodically clean up outdated or irrelevant user data and interactions to maintain system efficiency.

## Next Steps

Expand Down
1 change: 1 addition & 0 deletions weaviate_recommend/models/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class UserInteraction(BaseModel):
interaction_property_name: str
weight: float = 1.0
created_at: Union[str, None] = None
remove_previous_interactions: bool = False


class User(BaseModel):
Expand Down
8 changes: 8 additions & 0 deletions weaviate_recommend/models/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,11 @@ class AddUserInteractionsResponse(BaseModel):

class CreateUserResponse(BaseModel):
message: str


class DeleteUserResponse(BaseModel):
message: str


class UpdateUserResponse(BaseModel):
message: str
105 changes: 105 additions & 0 deletions weaviate_recommend/services/data/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
AddUserInteractionResponse,
AddUserInteractionsResponse,
CreateUserResponse,
DeleteUserResponse,
UpdateUserResponse,
)
from weaviate_recommend.utils import get_auth_header, get_datetime

Expand Down Expand Up @@ -124,3 +126,106 @@ def get_user(self, user_id: Union[str, UUID]) -> User:
if response.status_code != 200:
raise RecommendApiException(response.text)
return User.model_validate(response.json())

def update_user(self, user: User) -> UpdateUserResponse:
"""
Update a user by ID.
"""
response = requests.post(
f"{self.endpoint_url}/update",
json=user.model_dump(),
headers=get_auth_header(self.client._api_key),
)

if response.status_code != 200:
raise RecommendApiException(response.text)
return UpdateUserResponse.model_validate(response.json())

def delete_user(self, user_id: Union[str, UUID]) -> DeleteUserResponse:
"""
Delete a user by ID.
"""
if isinstance(user_id, UUID):
user_id = str(user_id)

response = requests.delete(
f"{self.endpoint_url}{user_id}",
headers=get_auth_header(self.client._api_key),
)
if response.status_code != 200:
raise RecommendApiException(response.text)
return DeleteUserResponse.model_validate(response.json())

def exists(self, user_id: Union[str, UUID]) -> bool:
"""
Check if a user exists by ID.
"""
if isinstance(user_id, UUID):
user_id = str(user_id)

response = requests.get(
f"{self.endpoint_url}exists/{user_id}",
headers=get_auth_header(self.client._api_key),
)
if response.status_code != 200:
raise RecommendApiException(response.text)
return response.json()

def delete_all_interactions(
self,
user_id: Union[str, UUID],
) -> DeleteUserResponse:
"""
Delete all interactions for a user.
"""
if isinstance(user_id, UUID):
user_id = str(user_id)

response = requests.delete(
f"{self.endpoint_url}{user_id}/interactions",
headers=get_auth_header(self.client._api_key),
)
if response.status_code != 200:
raise RecommendApiException(response.text)
return DeleteUserResponse.model_validate(response.json())

def delete_interactions_by_property(
self,
user_id: Union[str, UUID],
interaction_property_name: str,
) -> DeleteUserResponse:
"""
Delete all interactions for a user and a specific property.
"""
if isinstance(user_id, UUID):
user_id = str(user_id)

response = requests.delete(
f"{self.endpoint_url}{user_id}/interactions/{interaction_property_name}",
headers=get_auth_header(self.client._api_key),
)
if response.status_code != 200:
raise RecommendApiException(response.text)
return DeleteUserResponse.model_validate(response.json())

def delete_interactions_by_property_and_item(
self,
user_id: Union[str, UUID],
interaction_property_name: str,
item_id: Union[str, UUID],
) -> DeleteUserResponse:
"""
Delete specific interactions for a user, property, and item.
"""
if isinstance(user_id, UUID):
user_id = str(user_id)
if isinstance(item_id, UUID):
item_id = str(item_id)

response = requests.delete(
f"{self.endpoint_url}{user_id}/interactions/{interaction_property_name}/{item_id}",
headers=get_auth_header(self.client._api_key),
)
if response.status_code != 200:
raise RecommendApiException(response.text)
return DeleteUserResponse.model_validate(response.json())

0 comments on commit 90387f0

Please sign in to comment.