Yes, you can replace the UpdateMessage class with a setter method in your Message class to update its attributes. The setter method will allow you to directly update the attributes within an existing Message object. Below is an updated implementation of the Message class that includes a setter method, update_message:
from typing import Union, Optional, List
from datetime import datetime
from pydantic import BaseModel
class Document(BaseModel):
pass # Define the Document class
class Citation(BaseModel):
pass # Define the Citation class
class File(BaseModel):
pass # Define the File class
class Annotation(BaseModel):
pass # Define the Annotation class
class MessageAgent(BaseModel):
pass # Define the MessageAgent class
class MessageBase(BaseModel):
pass # Define the MessageBase class
class Message(MessageBase):
id: str
created_at: datetime
updated_at: datetime
generation_id: Union[str, None]
position: int
is_active: bool
documents: List[Document]
citations: List[Citation]
files: List[File]
annotations: List[Annotation]
agent: MessageAgent
class Config:
from_attributes = True
def update_message(self, annotations: Optional[List[Annotation]] = None, description: Optional[str] = None):
if annotations is not None:
self.annotations = annotations
# If you had a 'description' attribute in the Message class, you'd update it here
# Example:
# if description is not None:
# self.description = description
# Also include logic to update `updated_at`
self.updated_at = datetime.now()
# Example usage:
message = Message(
id="msg1",
created_at=datetime.now(),
updated_at=datetime.now(),
generation_id=None,
position=1,
is_active=True,
documents=[],
citations=[],
files=[],
annotations=[],
agent=MessageAgent()
)
# Updating the message
message.update_message(annotations=[Annotation()])
# verifying the updated fields
print(message.annotations) # Should print the updated annotations
print(message.updated_at) # Should print the updated timestamp
Key Points:
- update_message Method: This method uses optional parameters to update instance attributes.
- Optional Parameters: It includes parameters for attributes you intend to update (
annotations and description in this case).
- Timestamp Update: It updates the
updated_at attribute to the current time after any change.
Extending the Method:
If there are other attributes within the UpdateMessage class (e.g., description), ensure that they are also part of the Message class. If so, update those attributes similarly within the update_message method.
Note:
The example assumes that your Message class should hold all possible attributes to be updated, so include any others from your original UpdateMessage class directly in the Message class if they exist.