Working with the BaseMessage
Class#
In this tutorial, we will explore the BaseMessage
, SystemMessage
, AssistantSystemMessage
, UserSystemMessage
, ChatMessage
, AssistantChatMessage
, and UserChatMessage
class. The topics covered include:
Introduction to the
BaseMessage
classCreating a
BaseMessage
instanceUnderstanding the properties of the
BaseMessage
classUsing the methods of the
BaseMessage
classBaseMessage
Subclasses of
BaseMessage
Introduction#
The BaseMessage
class is the base class for message objects used in the CAMEL chat system. It is designed to provide a consistent structure for the messages in the system and allow for easy conversion between different message types.
Creating a BaseMessage
Instance#
To create a BaseMessage
instance, you need to provide the following arguments:
role_name
: The name of the user or assistant role.role_type
: The type of role, eitherRoleType.ASSISTANT
orRoleType.USER
.meta_dict
: An optional metadata dictionary for the message.role
: The role of the message in the OpenAI chat system, either"system"
,"user"
, or"assistant"
.content
: The content of the message.
Here’s an example of creating a BaseMessage
instance:
from camel.messages import BaseMessage, RoleType
message = BaseMessage(
role_name="test_user",
role_type=RoleType.USER,
meta_dict={"key": "value"},
role="user",
content="test content"
)
Understanding the Properties of the BaseMessage Class#
The BaseMessage
class has the following properties:
role_name
: A string representing the name of the user or assistant role.role_type
: An instance of theRoleType
enumeration, indicating whether the role is a user or an assistant.meta_dict
: A dictionary containing additional metadata for the message.role
: A string representing the role of the message in the OpenAI chat system.content
: A string containing the content of the message.
In the example provided earlier, the role_name
is "test_user"
, the role_type
is RoleType.USER
, the meta_dict
is {"key": "value"}
, the role
is "user"
, and the content
is "test content"
.
Using the Methods of the BaseMessage
Class#
The BaseMessage
class provides methods for converting the message to different message types, such as UserChatMessage
, AssistantChatMessage
, and various OpenAI message types. Here are some examples:
Converting to a
UserChatMessage
object:
from camel.messages import UserChatMessage
user_chat_message = message.to_user_chat_message()
print(isinstance(user_chat_message, UserChatMessage))
>>> True
Converting to an
AssistantChatMessage
object:
from camel.messages import AssistantChatMessage
assistant_chat_message = message.to_assistant_chat_message()
print(isinstance(assistant_chat_message, AssistantChatMessage))
>>> True
Converting to an
OpenAIMessage
object:
openai_message = message.to_openai_message()
print(openai_message == {"role": "user", "content": "test content"})
>>> True
Converting to a dictionary:
message_dict = message.to_dict()
print(message_dict == {
"role_name": "test_user",
"role_type": "USER",
"key": "value",
"role": "user",
"content": "test content"
})
>>> True
These methods allow you to convert a BaseMessage
instance into different message types depending on your needs.
Subclasses of BaseMessage
#
In this session, we will introduce the subclasses of the BaseMessage
class. Each of these subclasses has some default properties pre-defined, making it convenient to create message instances of specific types.
SystemMessage
is a class for system messages used in the CAMEL chat system. By default, therole
is set to"system"
and thecontent
is an empty string""
AssistantSystemMessage
is a class for system messages from the assistant role used in the CAMEL chat system. By default, therole_type
is set toRoleType.ASSISTANT
, therole
is set to"system"
, and thecontent
is an empty string""
.UserSystemMessage
is a class for system messages from the user role used in the CAMEL chat system. By default, therole_type
is set toRoleType.USER
, therole
is set to"system"
, and thecontent
is an empty string""
.ChatMessage
is a base class for chat messages used in the CAMEL chat system. By default, thecontent
is an empty string""
.AssistantChatMessage
is a class for chat messages from the assistant role used in the CAMEL chat system. By default, therole_type
is set toRoleType.ASSISTANT
, therole
is set to"assistant"
, and thecontent
is an empty string""
.UserChatMessage
is a class for chat messages from the user role used in the CAMEL chat system. By default, therole_type
is set toRoleType.USER
, therole
is set to"user"
, and thecontent
is an empty string""
.
These subclasses allow you to create message instances with specific roles and types easily. You can use them to create, manage, and manipulate messages in the CAMEL chat system with more context and clarity.