Message Cookbook#
You can also check this cookbook in colab here.
This notebook demonstrates how to set up and leverage CAMELβs ability to use BaseMessage
class.
In this notebook, youβll explore:
CAMEL: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.
BaseMessage: 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.
In this tutorial, we will explore the BaseMessage
class. The topics covered include:
Introduction to the
BaseMessage
class.Creating a
BaseMessage
instance.Understanding the properties of the
BaseMessage
class.Using the methods of the
BaseMessage
class.Give message to
ChatAgent
π¦ Installation#
First, install the CAMEL package with all its dependencies:
[ ]:
!pip install "camel-ai==0.2.16"
π Setting Up API Keys#
Youβll need to set up your API keys for OpenAI. This ensures that the tools can interact with external services securely.
[13]:
import os
from getpass import getpass
# Prompt for the API key securely
openai_api_key = getpass('Enter your API key: ')
os.environ["OPENAI_API_KEY"] = openai_api_key
You can go to here to get free API Key from AgentOps
[2]:
import os
from getpass import getpass
# Prompt for the AgentOps API key securely
agentops_api_key = getpass('Enter your API key: ')
os.environ["AGENTOPS_API_KEY"] = agentops_api_key
Your can go to here to get API Key from Mistral AI with free credits.
[ ]:
# Prompt for the API key securely
mistral_api_key = getpass('Enter your API key: ')
os.environ["MISTRAL_API_KEY"] = mistral_api_key
Give message to ChatAgent
directly#
You can simply pass text message to ChatAgent
[14]:
from camel.agents import ChatAgent
# Define system message
sys_msg = "You are a helpful assistant."
# Set agent
camel_agent = ChatAgent(system_message=sys_msg)
# Set user message
user_msg = """Say hi to CAMEL AI, one open-source community dedicated to the
study of autonomous and communicative agents."""
# Get response information
response = camel_agent.step(user_msg)
print(response.msgs[0].content)
Hello, CAMEL AI! It's great to connect with a community dedicated to the study of autonomous and communicative agents. Your work in this fascinating field is important for advancing our understanding of AI and its applications. How can I assist you today?
Give message to ChatAgent
via BaseMessage
#
For more complex message usage like mulit-modal message, we suggest using BaseMessage
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
.content
: The content of the message.meta_dict
: An metadata dictionary for the message.
Below are optional arguments you can pass:
video_bytes
: Optional bytes of a video associated with the message.image_list
: Optional list of PIL Image objects associated with the message.image_detail
: Detail level of the images associated with the message. Default is βautoβ.video_detail
: Detail level of the videos associated with the message. Default is βlowβ.
Hereβs an example of creating a BaseMessage
instance:
[15]:
from camel.messages import BaseMessage
from camel.types import RoleType
message = BaseMessage(
role_name="test_user",
role_type=RoleType.USER,
content="test content",
meta_dict={}
)
Additionally, the BaseMessage class provides class methods to easily create user and assistant agent messages:
Creating a user agent message:
[16]:
from camel.messages import BaseMessage
user_message = BaseMessage.make_user_message(
role_name="user_name",
content="test content for user",
)
Creating an assistant agent message:
[17]:
from camel.messages import BaseMessage
assistant_message = BaseMessage.make_assistant_message(
role_name="assistant_name",
content="test content for assistant",
)
Using the Methods of the BaseMessage
Class#
The BaseMessage
class offers several methods:
Creating a new instance with updated content:
[18]:
new_message = message.create_new_instance("new test content")
print(isinstance(new_message, BaseMessage))
True
Converting to an
OpenAIMessage
object:
[19]:
from camel.types import OpenAIBackendRole
openai_message = message.to_openai_message(role_at_backend=OpenAIBackendRole.USER)
print(openai_message == {"role": "user", "content": "test content"})
True
Converting to an
OpenAISystemMessage
object:
[20]:
openai_system_message = message.to_openai_system_message()
print(openai_system_message == {"role": "system", "content": "test content"})
True
Converting to an
OpenAIUserMessage
object:
[21]:
openai_user_message = message.to_openai_user_message()
print(openai_user_message == {"role": "user", "content": "test content"})
True
Converting to an
OpenAIAssistantMessage
object:
[22]:
openai_assistant_message = message.to_openai_assistant_message()
print(openai_assistant_message == {"role": "assistant", "content": "test content"})
True
Converting to a dictionary:
[23]:
message_dict = message.to_dict()
print(message_dict == {
"role_name": "test_user",
"role_type": "USER",
"content": "test content"
})
True
These methods allow you to convert a BaseMessage
instance into different message types depending on your needs.
Give BaseMessage
to ChatAgent
#
[24]:
from io import BytesIO
import requests
from PIL import Image
from camel.agents import ChatAgent
from camel.messages import BaseMessage
# URL of the image
url = "https://raw.githubusercontent.com/camel-ai/camel/master/misc/logo_light.png"
response = requests.get(url)
img = Image.open(BytesIO(response.content))
# Define system message
sys_msg = BaseMessage.make_assistant_message(
role_name="Assistant",
content="You are a helpful assistant.",
)
# Set agent
camel_agent = ChatAgent(system_message=sys_msg)
# Set user message
user_msg = BaseMessage.make_user_message(
role_name="User", content="""what's in the image?""", image_list=[img]
)
# Get response information
response = camel_agent.step(user_msg)
print(response.msgs[0].content)
The image features a logo for "CAMEL-AI." It includes a stylized depiction of a camel to the left, followed by the text "CAMEL-AI" in a bold, purple font.
π Highlights#
This notebook has guided you through setting up and converting BaseMessage
to different types of messages. These components play essential roles in the CAMEL chat system, facilitating the creation, management, and interpretation of messages with clarity.
Key tools utilized in this notebook include:
CAMEL: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.
BaseMessage: 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.
Thatβs everything: Got questions about π« CAMEL-AI? Join us on Discord! Whether you want to share feedback, explore the latest in multi-agent systems, get support, or connect with others on exciting projects, weβd love to have you in the community! π€
Check out some of our other work:
π« Creating Your First CAMEL Agent free Colab
Graph RAG Cookbook free Colab
π§ββοΈ Create A Hackathon Judge Committee with Workforce free Colab
π₯ 3 ways to ingest data from websites with Firecrawl & CAMEL free Colab
π¦₯ Agentic SFT Data Generation with CAMEL and Mistral Models, Fine-Tuned with Unsloth free Colab
Thanks from everyone at π« CAMEL-AI