🐫 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: RolePlaying support async run (#1787)
Wendong-Fan9a8d84c14698
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.ymlindex 45de29263..b1610ad62 100644--- a/.github/ISSUE_TEMPLATE/bug_report.yml+++ b/.github/ISSUE_TEMPLATE/bug_report.yml@@ -26,7 +26,7 @@ body:attributes:label: What version of camel are you using?description: Run command `python3 -c 'print(__import__("camel").__version__)'` in your shell and paste the output here.- placeholder: E.g., 0.2.23+ placeholder: E.g., 0.2.24validations:required: truediff --git a/camel/__init__.py b/camel/__init__.pyindex c233a508c..374339974 100644--- a/camel/__init__.py+++ b/camel/__init__.py@@ -14,7 +14,7 @@from camel.logger import disable_logging, enable_logging, set_log_level-__version__ = '0.2.23'+__version__ = '0.2.24'__all__ = ['__version__',diff --git a/camel/societies/role_playing.py b/camel/societies/role_playing.pyindex ed408d9ae..be77e1f40 100644--- a/camel/societies/role_playing.py+++ b/camel/societies/role_playing.py@@ -468,6 +468,42 @@ class RolePlaying:return init_msg+ async def ainit_chat(+ self, init_msg_content: Optional[str] = None+ ) -> BaseMessage:+ r"""Asynchronously initializes the chat by resetting both of the+ assistant and user agents. Returns an initial message for the+ role-playing session.++ Args:+ init_msg_content (str, optional): A user-specified initial message.+ Will be sent to the role-playing session as the initial+ message. (default: :obj:`None`)++ Returns:+ BaseMessage: A single `BaseMessage` representing the initial+ message.+ """+ # Currently, reset() is synchronous, but if it becomes async in the+ # future, we can await it here+ self.assistant_agent.reset()+ self.user_agent.reset()+ default_init_msg_content = (+ "Now start to give me instructions one by one. "+ "Only reply with Instruction and Input."+ )+ if init_msg_content is None:+ init_msg_content = default_init_msg_content++ # Initialize a message sent by the assistant+ init_msg = BaseMessage.make_assistant_message(+ role_name=getattr(self.assistant_sys_msg, 'role_name', None)+ or "assistant",+ content=init_msg_content,+ )++ return init_msg+def step(self,assistant_msg: BaseMessage,@@ -549,3 +585,86 @@ class RolePlaying:info=user_response.info,),)++ async def astep(+ self,+ assistant_msg: BaseMessage,+ ) -> Tuple[ChatAgentResponse, ChatAgentResponse]:+ r"""Asynchronously advances the conversation by taking a message from+ the assistant, processing it using the user agent, and then processing+ the resulting message using the assistant agent. Returns a tuple+ containing the resulting assistant message, whether the assistant+ agent terminated the conversation, and any additional assistant+ information, as well as a tuple containing the resulting user message,+ whether the user agent terminated the conversation, and any additional+ user information.++ Args:+ assistant_msg: A `BaseMessage` representing the message from the+ assistant.++ Returns:+ Tuple[ChatAgentResponse, ChatAgentResponse]: A tuple containing two+ ChatAgentResponse: the first struct contains the resulting+ assistant message, whether the assistant agent terminated the+ conversation, and any additional assistant information; the+ second struct contains the resulting user message, whether the+ user agent terminated the conversation, and any additional user+ information.+ """+ user_response = await self.user_agent.astep(assistant_msg)+ if user_response.terminated or user_response.msgs is None:+ return (+ ChatAgentResponse(msgs=[], terminated=False, info={}),+ ChatAgentResponse(+ msgs=[],+ terminated=user_response.terminated,+ info=user_response.info,+ ),+ )+ user_msg = self._reduce_message_options(user_response.msgs)++ # To prevent recording the same memory more than once (once in chat+ # step and once in role play), and the model generates only one+ # response when multi-response support is enabled.+ if (+ 'n' in self.user_agent.model_backend.model_config_dict.keys()+ and self.user_agent.model_backend.model_config_dict['n'] > 1+ ):+ self.user_agent.record_message(user_msg)++ assistant_response = await self.assistant_agent.astep(user_msg)+ if assistant_response.terminated or assistant_response.msgs is None:+ return (+ ChatAgentResponse(+ msgs=[],+ terminated=assistant_response.terminated,+ info=assistant_response.info,+ ),+ ChatAgentResponse(+ msgs=[user_msg], terminated=False, info=user_response.info+ ),+ )+ assistant_msg = self._reduce_message_options(assistant_response.msgs)++ # To prevent recording the same memory more than once (once in chat+ # step and once in role play), and the model generates only one+ # response when multi-response support is enabled.+ if (+ 'n' in self.assistant_agent.model_backend.model_config_dict.keys()+ and self.assistant_agent.model_backend.model_config_dict['n'] > 1+ ):+ self.assistant_agent.record_message(assistant_msg)++ return (+ ChatAgentResponse(+ msgs=[assistant_msg],+ terminated=assistant_response.terminated,+ info=assistant_response.info,+ ),+ ChatAgentResponse(+ msgs=[user_msg],+ terminated=user_response.terminated,+ info=user_response.info,+ ),+ )diff --git a/docs/conf.py b/docs/conf.pyindex 36c99b18e..4e0e33360 100644--- a/docs/conf.py+++ b/docs/conf.py@@ -27,7 +27,7 @@ sys.path.insert(0, os.path.abspath('..'))project = 'CAMEL'copyright = '2024, CAMEL-AI.org'author = 'CAMEL-AI.org'-release = '0.2.23'+release = '0.2.24'html_favicon = ('https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'diff --git a/docs/key_modules/loaders.md b/docs/key_modules/loaders.mdindex 8a6d7f85e..81a604ac0 100644--- a/docs/key_modules/loaders.md+++ b/docs/key_modules/loaders.md@@ -340,14 +340,14 @@ response = jina_reader.read_content("https://docs.camel-ai.org/")print(response)``````markdown->>>Welcome to CAMEL’s documentation! — CAMEL 0.2.23 documentation+>>>Welcome to CAMEL’s documentation! — CAMEL 0.2.24 documentation===============[Skip to main content](https://docs.camel-ai.org/#main-content)Back to top Ctrl+K- [ CAMEL 0.2.23](https://docs.camel-ai.org/#)+ [ CAMEL 0.2.24](https://docs.camel-ai.org/#)Search Ctrl+Kdiff --git a/pyproject.toml b/pyproject.tomlindex 8cfd945aa..87f62d446 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "hatchling.build"[project]name = "camel-ai"-version = "0.2.23"+version = "0.2.24"description = "Communicative Agents for AI Society Study"authors = [{ name = "CAMEL-AI.org" }]requires-python = ">=3.10,<3.13"diff --git a/test/agents/test_role_playing.py b/test/agents/test_role_playing.pyindex 5a12808b9..dddeee698 100644--- a/test/agents/test_role_playing.py+++ b/test/agents/test_role_playing.py@@ -194,28 +194,50 @@ def test_role_playing_step(@pytest.mark.model_backend-def test_role_playing_with_function(step_call_count=3):+ "task_type, extend_sys_msg_meta_dicts, extend_task_specify_meta_dict",+ [+ (TaskType.AI_SOCIETY, None, None),+ (+ TaskType.CODE,+ [dict(domain="science", language="python")] * 2,+ dict(domain="science", language="python"),+ ),+ (TaskType.MISALIGNMENT, None, None),+ ],+)+async def test_role_playing_astep(+ task_type,+ extend_sys_msg_meta_dicts,+ extend_task_specify_meta_dict,+ step_call_count=3,+):if model is not None:model.run = MagicMock(return_value=model_backend_rsp)- tools = MathToolkit().get_tools()-role_playing = RolePlaying(assistant_role_name="AI Assistant",- assistant_agent_kwargs=dict(- model=model,- tools=tools,- ),+ assistant_agent_kwargs=dict(model=model),user_role_name="AI User",user_agent_kwargs=dict(model=model),task_prompt="Perform the task",task_specify_agent_kwargs=dict(model=model),- task_type=TaskType.AI_SOCIETY,+ task_type=task_type,+ extend_sys_msg_meta_dicts=extend_sys_msg_meta_dicts,+ extend_task_specify_meta_dict=extend_task_specify_meta_dict,+ )+ init_assistant_msg = BaseMessage.make_assistant_message(+ role_name="AI Assistant", content="Hello")+ print(role_playing.assistant_agent.system_message)+ print(role_playing.user_agent.system_message)- input_msg = role_playing.init_chat()for i in range(step_call_count):- assistant_response, user_response = role_playing.step(input_msg)+ assistant_response, user_response = await role_playing.astep(+ init_assistant_msg+ )+for response in (assistant_response, user_response):assert isinstance(response.msgs, list@@ -237,6 +259,67 @@ def test_role_playing_with_function(step_call_count=3):), f"Error in round {i+1}: response.info is not a dict"[email protected]_backend+def test_role_playing_with_function(step_call_count=3):+ if model is not None:+ model.run = MagicMock(return_value=model_backend_rsp)++ tools = MathToolkit().get_tools()+ role_playing = RolePlaying(+ assistant_role_name="AI Assistant",+ assistant_agent_kwargs=dict(+ model=model,+ tools=tools,+ ),+ user_role_name="AI User",+ user_agent_kwargs=dict(model=model),+ task_prompt="Perform the task",+ task_specify_agent_kwargs=dict(model=model),+ task_type=TaskType.AI_SOCIETY,+ )++ input_msg = role_playing.init_chat()+ for _ in range(step_call_count):+ assistant_response, user_response = role_playing.step(input_msg)+ for response in (assistant_response, user_response):+ assert isinstance(response.msgs, list)+ assert len(response.msgs) == 1+ assert isinstance(response.msgs[0], BaseMessage)+ assert isinstance(response.terminated, bool)+ assert response.terminated is False+ assert isinstance(response.info, dict)++[email protected]_backend[email protected]("init_msg_content", [None, "Custom init message"])+async def test_role_playing_ainit_chat(init_msg_content):+ if model is not None:+ model.run = MagicMock(return_value=model_backend_rsp)++ role_playing = RolePlaying(+ assistant_role_name="AI Assistant",+ assistant_agent_kwargs=dict(model=model),+ user_role_name="AI User",+ user_agent_kwargs=dict(model=model),+ task_prompt="Perform the task",+ task_specify_agent_kwargs=dict(model=model),+ task_type=TaskType.AI_SOCIETY,+ )++ init_msg = await role_playing.ainit_chat(init_msg_content)+ assert isinstance(init_msg, BaseMessage)+ assert init_msg.role_type == RoleType.ASSISTANT+ assert init_msg.role_name == "AI Assistant"+ if init_msg_content is not None:+ assert init_msg.content == init_msg_content+ else:+ assert init_msg.content == (+ "Now start to give me instructions one by one. "+ "Only reply with Instruction and Input."+ )++def test_role_playing_role_sequence(model=None,):diff --git a/uv.lock b/uv.lockindex 3fc842da8..6126ac5d7 100644--- a/uv.lock+++ b/uv.lock@@ -510,7 +510,7 @@ wheels = [[[package]]name = "camel-ai"-version = "0.2.23"+version = "0.2.24"source = { editable = "." }dependencies = [{ name = "colorama" },