message: added 'equals()' function and improved robustness and debugging
This commit is contained in:
parent
05557cceb5
commit
6845fd8419
@ -222,12 +222,36 @@ class Message():
|
||||
ai_yaml_key: ClassVar[str] = 'ai'
|
||||
model_yaml_key: ClassVar[str] = 'model'
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
# convert some types that are often set wrong
|
||||
if self.tags is not None and not isinstance(self.tags, set):
|
||||
self.tags = set(self.tags)
|
||||
if self.file_path is not None and not isinstance(self.file_path, pathlib.Path):
|
||||
self.file_path = pathlib.Path(self.file_path)
|
||||
|
||||
def __hash__(self) -> int:
|
||||
"""
|
||||
The hash value is computed based on immutable members.
|
||||
"""
|
||||
return hash((self.question, self.answer))
|
||||
|
||||
def equals(self, other: MessageInst, tags: bool = True, ai: bool = True,
|
||||
model: bool = True, file_path: bool = True, verbose: bool = False) -> bool:
|
||||
"""
|
||||
Compare this message with another one, including the metadata.
|
||||
Return True if everything is identical, False otherwise.
|
||||
"""
|
||||
equal: bool = ((not tags or (self.tags == other.tags))
|
||||
and (not ai or (self.ai == other.ai)) # noqa: W503
|
||||
and (not model or (self.model == other.model)) # noqa: W503
|
||||
and (not file_path or (self.file_path == other.file_path)) # noqa: W503
|
||||
and (self == other)) # noqa: W503
|
||||
if not equal and verbose:
|
||||
print("Messages not equal:")
|
||||
print(self)
|
||||
print(other)
|
||||
return equal
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls: Type[MessageInst], data: dict[str, Any]) -> MessageInst:
|
||||
"""
|
||||
@ -418,9 +442,6 @@ class Message():
|
||||
output.append(self.answer)
|
||||
return '\n'.join(output)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.to_str(True, True, False)
|
||||
|
||||
def to_file(self, file_path: Optional[pathlib.Path]=None) -> None: # noqa: 11
|
||||
"""
|
||||
Write a Message to the given file. Type is determined based on the suffix.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user