Skip to content

Commit

Permalink
Merge pull request #993 from Pingdred/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Pingdred authored Dec 20, 2024
2 parents 1f5c3b0 + b8cd171 commit db9431a
Showing 1 changed file with 120 additions and 21 deletions.
141 changes: 120 additions & 21 deletions core/cat/convo/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,11 @@ class Message(BaseModelDict):
----------
user_id : str
Unique identifier for the user associated with the message.
who : str
The name of the message author.
when : Optional[float]
The timestamp when the message was sent.
when : float
The timestamp when the message was sent.
"""

user_id: str
who: str
when: float = Field(default_factory=time.time)


Expand All @@ -82,6 +78,12 @@ class ConversationMessage(Message):
Attributes
----------
user_id : str
Unique identifier for the user associated with the message.
when : float
The timestamp when the message was sent. Defaults to the current time.
who : str
The name of the message author.
text : Optional[str], default=None
The text content of the message.
image : Optional[str], default=None
Expand All @@ -90,10 +92,47 @@ class ConversationMessage(Message):
Audio file URLs or base64 data URIs that represent audio associated with the message.
"""

who: str
text: Optional[str] = None
image: Optional[str] = None
audio: Optional[str] = None

# massage was used in the old history instead of text
# we need to keep it for backward compatibility
def __init__(self, **data):

if "message" in data:
deprecation_warning("The `message` parameter is deprecated. Use `text` instead.")
data["text"] = data.pop("message")

super().__init__(**data)

@computed_field
@property
def message(self) -> str:
"""
This attribute is deprecated. Use `text` instead.
The text content of the message. Use `text` instead.
Returns
-------
str
The text content of the message.
"""
deprecation_warning("The `message` attribute is deprecated. Use `text` instead.")
return self.text

@message.setter
def message(self, value):
deprecation_warning("The `message` attribute is deprecated. Use `text` instead.")
self.text = value

@property
def role(self) -> None:
"""The role of the message author."""
return None


class CatMessage(ConversationMessage):
"""
Expand All @@ -103,6 +142,18 @@ class CatMessage(ConversationMessage):
----------
type : str
The type of message. Defaults to "chat".
user_id : str
Unique identifier for the user associated with the message.
when : float
The timestamp when the message was sent. Defaults to the current time.
who : str
The name of the message author.
text : Optional[str], default=None
The text content of the message.
image : Optional[str], default=None
Image file URLs or base64 data URIs that represent image associated with the message.
audio : Optional[str], default=None
Audio file URLs or base64 data URIs that represent audio associated with the message.
why : Optional[MessageWhy]
Additional contextual information related to the message.
Expand All @@ -115,21 +166,22 @@ class CatMessage(ConversationMessage):
type: str = "chat" # For now is always "chat" and is not used
why: Optional[MessageWhy] = None

def langchainfy(self) -> AIMessage:
"""
Convert the internal CatMessage to a LangChain AIMessage.
def __init__(
self,
user_id: str,
who: str = "AI",
text: Optional[str] = None,
image: Optional[str] = None,
audio: Optional[str] = None,
why: Optional[MessageWhy] = None,
**kwargs,
):
if "content" in kwargs:
deprecation_warning("The `content` parameter is deprecated. Use `text` instead.")
text = kwargs.pop("content") # Map 'content' to 'text'

super().__init__(user_id=user_id, text=text, image=image, audio=audio, why=why, who=who, **kwargs)

Returns
-------
AIMessage
The LangChain AIMessage converted from the internal CatMessage.
"""

return AIMessage(
name=self.who,
content=self.text
)

@computed_field
@property
def content(self) -> str:
Expand All @@ -151,17 +203,57 @@ def content(self, value):
deprecation_warning("The `content` attribute is deprecated. Use `text` instead.")
self.text = value

@property
def role(self) -> Role:
"""The role of the message author."""
return Role.AI

def langchainfy(self) -> AIMessage:
"""
Convert the internal CatMessage to a LangChain AIMessage.
Returns
-------
AIMessage
The LangChain AIMessage converted from the internal CatMessage.
"""

return AIMessage(
name=self.who,
content=self.text
)


class UserMessage(ConversationMessage):
"""
Represents a message from a user, containing text and optional multimedia content such as image and audio.
This class is used to encapsulate the details of a message sent by a user, including the user's identifier,
the text content of the message, and any associated multimedia content such as image or audio files.
Attributes
----------
user_id : str
Unique identifier for the user associated with the message.
when : float
The timestamp when the message was sent. Defaults to the current time.
who : str
The name of the message author.
text : Optional[str], default=None
The text content of the message.
image : Optional[str], default=None
Image file URLs or base64 data URIs that represent image associated with the message.
audio : Optional[str], default=None
Audio file URLs or base64 data URIs that represent audio associated with the message.
"""

who: str = "Human"

@property
def role(self) -> Role:
"""The role of the message author."""
return Role.Human

def langchainfy(self) -> HumanMessage:
"""
Convert the internal UserMessage to a LangChain HumanMessage.
Expand All @@ -188,7 +280,14 @@ def langchainfy(self) -> HumanMessage:
)

def langchainfy_image(self) -> dict:
"""Format an image to be sent as a data URI."""
"""
Format an image to be sent as a data URI.
Returns
-------
dict
A dictionary containing the image data URI.
"""

# If the image is a URL, download it and encode it as a data URI
if self.image.startswith("http"):
Expand Down

0 comments on commit db9431a

Please sign in to comment.