[docs]classChatMessage(BaseMessage):"""Message that can be assigned an arbitrary speaker (i.e. role)."""role:str"""The speaker / role of the Message."""type:Literal["chat"]="chat""""The type of the message (used during serialization). Defaults to "chat"."""@classmethoddefget_lc_namespace(cls)->list[str]:"""Get the namespace of the langchain object. Default is ["langchain", "schema", "messages"]. """return["langchain","schema","messages"]
ChatMessage.model_rebuild()
[docs]classChatMessageChunk(ChatMessage,BaseMessageChunk):"""Chat Message chunk."""# Ignoring mypy re-assignment here since we're overriding the value# to make sure that the chunk variant can be discriminated from the# non-chunk variant.type:Literal["ChatMessageChunk"]="ChatMessageChunk"# type: ignore"""The type of the message (used during serialization). Defaults to "ChatMessageChunk"."""@classmethoddefget_lc_namespace(cls)->list[str]:"""Get the namespace of the langchain object. Default is ["langchain", "schema", "messages"]. """return["langchain","schema","messages"]def__add__(self,other:Any)->BaseMessageChunk:# type: ignoreifisinstance(other,ChatMessageChunk):ifself.role!=other.role:msg="Cannot concatenate ChatMessageChunks with different roles."raiseValueError(msg)returnself.__class__(role=self.role,content=merge_content(self.content,other.content),additional_kwargs=merge_dicts(self.additional_kwargs,other.additional_kwargs),response_metadata=merge_dicts(self.response_metadata,other.response_metadata),id=self.id,)elifisinstance(other,BaseMessageChunk):returnself.__class__(role=self.role,content=merge_content(self.content,other.content),additional_kwargs=merge_dicts(self.additional_kwargs,other.additional_kwargs),response_metadata=merge_dicts(self.response_metadata,other.response_metadata),id=self.id,)else:returnsuper().__add__(other)