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
BaseMessageclassCreating a
BaseMessageinstanceUnderstanding the properties of the
BaseMessageclassUsing the methods of the
BaseMessageclassBaseMessageSubclasses 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.ASSISTANTorRoleType.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 theRoleTypeenumeration, 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
UserChatMessageobject:
from camel.messages import UserChatMessage
user_chat_message = message.to_user_chat_message()
print(isinstance(user_chat_message, UserChatMessage))
>>> True
Converting to an
AssistantChatMessageobject:
from camel.messages import AssistantChatMessage
assistant_chat_message = message.to_assistant_chat_message()
print(isinstance(assistant_chat_message, AssistantChatMessage))
>>> True
Converting to an
OpenAIMessageobject:
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.
SystemMessageis a class for system messages used in the CAMEL chat system. By default, theroleis set to"system"and thecontentis an empty string""AssistantSystemMessageis a class for system messages from the assistant role used in the CAMEL chat system. By default, therole_typeis set toRoleType.ASSISTANT, theroleis set to"system", and thecontentis an empty string"".UserSystemMessageis a class for system messages from the user role used in the CAMEL chat system. By default, therole_typeis set toRoleType.USER, theroleis set to"system", and thecontentis an empty string"".ChatMessageis a base class for chat messages used in the CAMEL chat system. By default, thecontentis an empty string"".AssistantChatMessageis a class for chat messages from the assistant role used in the CAMEL chat system. By default, therole_typeis set toRoleType.ASSISTANT, theroleis set to"assistant", and thecontentis an empty string"".UserChatMessageis a class for chat messages from the user role used in the CAMEL chat system. By default, therole_typeis set toRoleType.USER, theroleis set to"user", and thecontentis 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.