🐫 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: add termination parameter to ChatAgent step and astep (#2285)
Ahmed Awelkair A0cd23f6397f0
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.pyindex 2df0620f1..a0bd70076 100644--- a/camel/agents/chat_agent.py+++ b/camel/agents/chat_agent.py@@ -16,6 +16,7 @@ from __future__ import annotationsimport jsonimport loggingimport textwrap+import threadingimport uuidfrom collections import defaultdictfrom datetime import datetime@@ -151,6 +152,9 @@ class ChatAgent(BaseAgent):model calling at each step. (default: :obj:`False`)agent_id (str, optional): The ID of the agent. If not provided, arandom UUID will be generated. (default: :obj:`None`)+ stop_event (Optional[threading.Event], optional): Event to signal+ termination of the agent's operation. When set, the agent will+ terminate its execution. (default: :obj:`None`)"""def __init__(@@ -182,6 +186,7 @@ class ChatAgent(BaseAgent):scheduling_strategy: str = "round_robin",single_iteration: bool = False,agent_id: Optional[str] = None,+ stop_event: Optional[threading.Event] = None,) -> None:# Resolve model backends and set up model managerresolved_models = self._resolve_models(model)@@ -252,6 +257,7 @@ class ChatAgent(BaseAgent):self.terminated = Falseself.response_terminators = response_terminators or []self.single_iteration = single_iteration+ self.stop_event = stop_eventdef reset(self):r"""Resets the :obj:`ChatAgent` to its initial state."""@@ -757,6 +763,13 @@ class ChatAgent(BaseAgent):self._get_full_tool_schemas(),)+ # Terminate Agent if stop_event is set+ if self.stop_event and self.stop_event.is_set():+ # Use the _step_token_exceed to terminate the agent with reason+ return self._step_token_exceed(+ num_tokens, tool_call_records, "termination_triggered"+ )+if tool_call_requests := response.tool_call_requests:# Process all tool callsfor tool_call_request in tool_call_requests:@@ -849,6 +862,13 @@ class ChatAgent(BaseAgent):self._get_full_tool_schemas(),)+ # Terminate Agent if stop_event is set+ if self.stop_event and self.stop_event.is_set():+ # Use the _step_token_exceed to terminate the agent with reason+ return self._step_token_exceed(+ num_tokens, tool_call_records, "termination_triggered"+ )+if tool_call_requests := response.tool_call_requests:# Process all tool callsfor tool_call_request in tool_call_requests:diff --git a/camel/responses/agent_responses.py b/camel/responses/agent_responses.pyindex 3fa960f0f..0d7b1ec97 100644--- a/camel/responses/agent_responses.py+++ b/camel/responses/agent_responses.py@@ -39,8 +39,5 @@ class ChatAgentResponse(BaseModel):@propertydef msg(self):if len(self.msgs) != 1:- raise RuntimeError(- "Property msg is only available "- "for a single message in msgs."- )+ return Nonereturn self.msgs[0]diff --git a/camel/societies/role_playing.py b/camel/societies/role_playing.pyindex be77e1f40..2c6c05cc4 100644--- a/camel/societies/role_playing.py+++ b/camel/societies/role_playing.py@@ -12,6 +12,7 @@# limitations under the License.# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========import logging+import threadingfrom typing import Dict, List, Optional, Sequence, Tuple, Unionfrom camel.agents import (@@ -77,6 +78,9 @@ class RolePlaying:task specify meta dict with. (default: :obj:`None`)output_language (str, optional): The language to be output by theagents. (default: :obj:`None`)+ stop_event (Optional[threading.Event], optional): Event to signal+ termination of the agent's operation. When set, the agent will+ terminate its execution. (default: :obj:`None`)"""def __init__(@@ -101,6 +105,7 @@ class RolePlaying:extend_sys_msg_meta_dicts: Optional[List[Dict]] = None,extend_task_specify_meta_dict: Optional[Dict] = None,output_language: Optional[str] = None,+ stop_event: Optional[threading.Event] = None,) -> None:if model is not None:logger.warning(@@ -156,6 +161,7 @@ class RolePlaying:assistant_agent_kwargs=assistant_agent_kwargs,user_agent_kwargs=user_agent_kwargs,output_language=output_language,+ stop_event=stop_event,)self.critic: Optional[Union[CriticAgent, Human]] = Noneself.critic_sys_msg: Optional[BaseMessage] = None@@ -316,6 +322,7 @@ class RolePlaying:assistant_agent_kwargs: Optional[Dict] = None,user_agent_kwargs: Optional[Dict] = None,output_language: Optional[str] = None,+ stop_event: Optional[threading.Event] = None,) -> None:r"""Initialize assistant and user agents with their system messages.@@ -330,6 +337,9 @@ class RolePlaying:pass to the user agent. (default: :obj:`None`)output_language (str, optional): The language to be output by theagents. (default: :obj:`None`)+ stop_event (Optional[threading.Event], optional): Event to signal+ termination of the agent's operation. When set, the agent will+ terminate its execution. (default: :obj:`None`)"""if self.model is not None:if assistant_agent_kwargs is None:@@ -344,6 +354,7 @@ class RolePlaying:self.assistant_agent = ChatAgent(init_assistant_sys_msg,output_language=output_language,+ stop_event=stop_event,**(assistant_agent_kwargs or {}),)self.assistant_sys_msg = self.assistant_agent.system_message@@ -351,6 +362,7 @@ class RolePlaying:self.user_agent = ChatAgent(init_user_sys_msg,output_language=output_language,+ stop_event=stop_event,**(user_agent_kwargs or {}),)self.user_sys_msg = self.user_agent.system_message