What is BaseMessage?

The BaseMessage class is the backbone for all message objects in the CAMEL chat system. It offers a consistent structure for agent communication and easy conversion between message types.

Explore and run every code sample directly in this Colab notebook.


Get Started

Creating a `BaseMessage`

To create a BaseMessage instance, supply these arguments:

  • role_name: Name of the user or assistant
  • role_type: RoleType.ASSISTANT or RoleType.USER
  • content: The actual message text
  • meta_dict (optional): Additional metadata
  • video_bytes (optional): Attach video bytes
  • image_list (optional): List of PIL Image objects
  • image_detail (optional): Level of image detail (default: “auto”)
  • video_detail (optional): Level of video detail (default: “low”)
Example:
from camel.messages import BaseMessage
from camel.types import RoleType

message = BaseMessage(
role_name="test_user",
role_type=RoleType.USER,
content="test content"
)

Convenient Constructors

Easily create messages for user or assistant agents:

user_message = BaseMessage.make_user_message(
    role_name="user_name",
    content="test content for user",
)

assistant_message = BaseMessage.make_assistant_message(
    role_name="assistant_name",
    content="test content for assistant",
)

Methods in the BaseMessage Class

Flexible Message Manipulation

The BaseMessage class lets you:

  • Create a new instance with updated content:


    new_message = message.create_new_instance(“new test content”)
  • Convert to OpenAI message formats:


    openai_message = message.to_openai_message(role_at_backend=OpenAIBackendRole.USER)


    openai_system_message = message.to_openai_system_message()
    openai_user_message = message.to_openai_user_message()

    openai_assistant_message = message.to_openai_assistant_message()

  • Convert to a Python dictionary:


    message_dict = message.to_dict()

These methods allow you to transform a BaseMessage into the right format for different LLM APIs and agent flows.


Using BaseMessage with ChatAgent

Rich Message Example

You can send multimodal messages (including images) to your agents:
from io import BytesIO
import requests
from PIL import Image

from camel.agents import ChatAgent
from camel.messages import BaseMessage

# Download an image
url = "https://raw.githubusercontent.com/camel-ai/camel/master/misc/logo_light.png"
img = Image.open(BytesIO(requests.get(url).content))

# Build system and user messages
sys_msg = BaseMessage.make_assistant_message(
    role_name="Assistant",
    content="You are a helpful assistant.",
)

user_msg = BaseMessage.make_user_message(
    role_name="User", content="what's in the image?", image_list=[img]
)

# Create agent and send message
camel_agent = ChatAgent(system_message=sys_msg)
response = camel_agent.step(user_msg)
print(response.msgs[0].content)

The image features a logo for “CAMEL-AI.” It includes a stylized purple camel graphic alongside the text “CAMEL-AI,” which is also in purple. The design appears modern and is likely related to artificial intelligence.


Conclusion

Summary

The BaseMessage class is essential for structured, clear, and flexible communication in the CAMEL-AI ecosystem—making it simple to create, convert, and handle messages across any workflow.

For further details, check out the key modules documentation.