🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: Support Multiple System Messages with Better API (#3167)
OmCheeLin104da6355f29
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.pyindex 4aeed3111..a686219dc 100644--- a/camel/agents/chat_agent.py+++ b/camel/agents/chat_agent.py@@ -491,9 +491,7 @@ class ChatAgent(BaseAgent):# Set up system message and initialize messagesself._original_system_message = (- BaseMessage.make_assistant_message(- role_name="Assistant", content=system_message- )+ BaseMessage.make_system_message(system_message)if isinstance(system_message, str)else system_message)@@ -1629,10 +1627,7 @@ class ChatAgent(BaseAgent):content = self._original_system_message.content + language_promptreturn self._original_system_message.create_new_instance(content)else:- return BaseMessage.make_assistant_message(- role_name="Assistant",- content=language_prompt,- )+ return BaseMessage.make_system_message(language_prompt)def init_messages(self) -> None:r"""Initializes the stored messages list with the current system@@ -1650,6 +1645,62 @@ class ChatAgent(BaseAgent):))+ def update_system_message(+ self,+ system_message: Union[BaseMessage, str],+ reset_memory: bool = True,+ ) -> None:+ r"""Update the system message.+ It will reset conversation with new system message.++ Args:+ system_message (Union[BaseMessage, str]): The new system message.+ Can be either a BaseMessage object or a string.+ If a string is provided, it will be converted+ into a BaseMessage object.+ reset_memory (bool):+ Whether to reinitialize conversation messages after updating+ the system message. Defaults to True.+ """+ if system_message is None:+ raise ValueError("system_message is required and cannot be None. ")+ self._original_system_message = (+ BaseMessage.make_system_message(system_message)+ if isinstance(system_message, str)+ else system_message+ )+ self._system_message = (+ self._generate_system_message_for_output_language()+ )+ if reset_memory:+ self.init_messages()++ def append_to_system_message(+ self, content: str, reset_memory: bool = True+ ) -> None:+ """Append additional context to existing system message.++ Args:+ content (str): The additional system message.+ reset_memory (bool):+ Whether to reinitialize conversation messages after appending+ additional context. Defaults to True.+ """+ original_content = (+ self._original_system_message.content+ if self._original_system_message+ else ""+ )+ new_system_message = original_content + '\n' + content+ self._original_system_message = BaseMessage.make_system_message(+ new_system_message+ )+ self._system_message = (+ self._generate_system_message_for_output_language()+ )+ if reset_memory:+ self.init_messages()+def reset_to_original_system_message(self) -> None:r"""Reset system message to original, removing any appended context.diff --git a/camel/messages/base.py b/camel/messages/base.pyindex b55ef48c5..337c5e3f4 100644--- a/camel/messages/base.py+++ b/camel/messages/base.py@@ -178,6 +178,32 @@ class BaseMessage:OpenAIVisionDetailType(video_detail).value,)+ @classmethod+ def make_system_message(+ cls,+ content: str,+ role_name: str = "System",+ meta_dict: Optional[Dict[str, str]] = None,+ ) -> "BaseMessage":+ r"""Create a new system message.++ Args:+ content (str): The content of the system message.+ role_name (str): The name of the system role.+ (default: :obj:`"System"`)+ meta_dict (Optional[Dict[str, str]]): Additional metadata+ dictionary for the message.++ Returns:+ BaseMessage: The new system message.+ """+ return cls(+ role_name,+ RoleType.SYSTEM,+ meta_dict,+ content,+ )+def create_new_instance(self, content: str) -> "BaseMessage":r"""Create a new instance of the :obj:`BaseMessage` with updatedcontent.diff --git a/camel/types/enums.py b/camel/types/enums.pyindex e66d213f1..cec536c54 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -24,6 +24,7 @@ logger = get_logger(__name__)class RoleType(Enum):ASSISTANT = "assistant"USER = "user"+ SYSTEM = "system"CRITIC = "critic"EMBODIMENT = "embodiment"DEFAULT = "default"diff --git a/test/toolkits/test_openai_function.py b/test/toolkits/test_openai_function.pyindex 2544ebe95..eae85ca4e 100644--- a/test/toolkits/test_openai_function.py+++ b/test/toolkits/test_openai_function.py@@ -104,6 +104,7 @@ def test_get_openai_tool_schema():'enum': ['assistant','user',+ 'system','critic','embodiment','default',@@ -126,6 +127,7 @@ def test_get_openai_tool_schema():'enum': ['assistant','user',+ 'system','critic','embodiment','default',