🐫 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
refactor: rework `RolePlaying` and update some docstrings (#189)
Zecheng Zhang97821051591e
diff --git a/camel/models/stub_model.py b/camel/models/stub_model.pyindex d3e5f2b5e..ee6d7d951 100644--- a/camel/models/stub_model.py+++ b/camel/models/stub_model.py@@ -24,6 +24,7 @@ class StubModel(BaseModelBackend):def __init__(self, model_type: ModelType,model_config_dict: Dict[str, Any]) -> None:r"""All arguments are unused for the dummy model."""+ super().__init__(model_type, model_config_dict)passdef run(self, messages: List[Dict]) -> Dict[str, Any]:diff --git a/camel/prompts/base.py b/camel/prompts/base.pyindex cda2492fa..2c1196189 100644--- a/camel/prompts/base.py+++ b/camel/prompts/base.py@@ -98,16 +98,16 @@ def wrap_prompt_functions(cls: T) -> T:class TextPrompt(str):r"""A class that represents a text prompt. The :obj:`TextPrompt` classextends the built-in :obj:`str` class to provide a property for retrieving- the set of key words in the prompt.+ the set of keywords in the prompt.Attributes:- key_words (set): A set of strings representing the key words in the+ key_words (set): A set of strings representing the keywords in theprompt."""@propertydef key_words(self) -> Set[str]:- r"""Returns a set of strings representing the key words in the prompt.+ r"""Returns a set of strings representing the keywords in the prompt."""from camel.utils import get_prompt_template_key_wordsreturn get_prompt_template_key_words(self)@@ -135,8 +135,7 @@ class CodePrompt(TextPrompt):r"""A class that represents a code prompt. It extends the :obj:`TextPrompt`class with a :obj:`code_type` property.- Args:- code_string (str): The code string for the prompt.+ Attributes:code_type (str, optional): The type of code. Defaults to None."""diff --git a/camel/societies/role_playing.py b/camel/societies/role_playing.pyindex d8d66efe7..360d5063f 100644--- a/camel/societies/role_playing.py+++ b/camel/societies/role_playing.py@@ -35,6 +35,8 @@ class RolePlaying:assistant.user_role_name (str): The name of the role played by the user.critic_role_name (str): The name of the role played by the critic.+ Role name with :obj:`"human"` will set critic as a :obj:`Human`+ agent, else will create a :obj:`CriticAgent`.(default: :obj:`"critic"`)task_prompt (str, optional): A prompt for the task to be performed.(default: :obj:`""`)@@ -44,6 +46,8 @@ class RolePlaying:agent. (default: :obj:`False`)with_critic_in_the_loop (bool, optional): Whether to include a criticin the loop. (default: :obj:`False`)+ critic_criteria (str, optional): Critic criteria for the critic agent.+ If not specified, set the criteria to improve task performance.model_type (ModelType, optional): The type of backend model to use.(default: :obj:`ModelType.GPT_3_5_TURBO`)task_type (TaskType, optional): The type of task to perform.@@ -95,17 +99,65 @@ class RolePlaying:self.with_critic_in_the_loop = with_critic_in_the_loopself.model_type = model_typeself.task_type = task_type+ self.task_prompt = task_prompt++ self.specified_task_prompt: Optional[TextPrompt] = None+ self.init_specified_task_prompt(assistant_role_name, user_role_name,+ task_specify_agent_kwargs,+ extend_task_specify_meta_dict,+ output_language)++ self.planned_task_prompt: Optional[TextPrompt] = None+ self.init_planned_task_prompt(task_planner_agent_kwargs,+ output_language)++ sys_msg_generator = SystemMessageGenerator(+ task_type=self.task_type, **(sys_msg_generator_kwargs or {}))+ (init_assistant_sys_msg, init_user_sys_msg,+ sys_msg_meta_dicts) = self.get_sys_message_info(+ assistant_role_name, user_role_name, sys_msg_generator,+ extend_sys_msg_meta_dicts)++ self.assistant_agent: ChatAgent+ self.user_agent: ChatAgent+ self.assistant_sys_msg: BaseMessage+ self.user_sys_msg: BaseMessage+ self.init_agents(init_assistant_sys_msg, assistant_agent_kwargs,+ init_user_sys_msg, user_agent_kwargs, output_language)++ self.critic: Optional[Union[CriticAgent, Human]] = None+ self.critic_sys_msg: Optional[BaseMessage] = None+ self.init_critic(critic_role_name, critic_criteria, critic_kwargs,+ sys_msg_generator, sys_msg_meta_dicts)++ def init_specified_task_prompt(+ self, assistant_role_name: str, user_role_name: str,+ task_specify_agent_kwargs: Optional[Dict],+ extend_task_specify_meta_dict: Optional[Dict],+ output_language: Optional[str]):+ r"""Use a task specify agent to generate a specified task prompt.+ Generated specified task prompt will be used to replace original+ task prompt. If there is no task specify agent, specified task+ prompt will not be generated.- self.specified_task_prompt: Optional[TextPrompt]- if with_task_specify:+ Args:+ assistant_role_name (str): The name of the role played by the+ assistant.+ user_role_name (str): The name of the role played by the user.+ task_specify_agent_kwargs (Dict, optional): Additional arguments+ to pass to the task specify agent.+ extend_task_specify_meta_dict (Dict, optional): A dict to extend+ the task specify meta dict with.+ output_language (str, optional): The language to be output by the+ agents.+ """+ if self.with_task_specify:task_specify_meta_dict = dict()if self.task_type in [TaskType.AI_SOCIETY, TaskType.MISALIGNMENT]:task_specify_meta_dict.update(dict(assistant_role=assistant_role_name,user_role=user_role_name))- if extend_task_specify_meta_dict is not None:- task_specify_meta_dict.update(extend_task_specify_meta_dict)-+ task_specify_meta_dict.update(extend_task_specify_meta_dict or {})task_specify_agent = TaskSpecifyAgent(self.model_type,task_type=self.task_type,@@ -113,37 +165,66 @@ class RolePlaying:**(task_specify_agent_kwargs or {}),)self.specified_task_prompt = task_specify_agent.step(- task_prompt,+ self.task_prompt,meta_dict=task_specify_meta_dict,)- task_prompt = self.specified_task_prompt- else:- self.specified_task_prompt = None+ self.task_prompt = self.specified_task_prompt++ def init_planned_task_prompt(self,+ task_planner_agent_kwargs: Optional[Dict],+ output_language: Optional[str]):+ r"""Use a task plan agent to append a planned task prompt to task+ prompt. The planned task prompt is generated based on the task+ prompt, which can be original task prompt or specified task prompt+ if available. If there is no task plan agent, planned task prompt+ will not be generated.- self.planned_task_prompt: Optional[TextPrompt]- if with_task_planner:+ Args:+ task_planner_agent_kwargs (Dict, optional): Additional arguments+ to pass to the task planner agent.+ output_language (str, optional): The language to be output by the+ agents.+ """+ if self.with_task_planner:task_planner_agent = TaskPlannerAgent(self.model_type,output_language=output_language,**(task_planner_agent_kwargs or {}),)- self.planned_task_prompt = task_planner_agent.step(task_prompt)- task_prompt = f"{task_prompt}\n{self.planned_task_prompt}"- else:- self.planned_task_prompt = None+ self.planned_task_prompt = task_planner_agent.step(+ self.task_prompt)+ self.task_prompt = (f"{self.task_prompt}\n"+ f"{self.planned_task_prompt}")- self.task_prompt = task_prompt+ def get_sys_message_info(+ self, assistant_role_name: str, user_role_name: str,+ sys_msg_generator: SystemMessageGenerator,+ extend_sys_msg_meta_dicts: Optional[List[Dict]]+ ) -> Tuple[BaseMessage, BaseMessage, List[Dict]]:+ r"""Get initial assistant and user system message with a list of+ system message meta dicts.- sys_msg_generator = SystemMessageGenerator(- task_type=self.task_type, **(sys_msg_generator_kwargs or {}))+ Args:+ assistant_role_name (str): The name of the role played by the+ assistant.+ user_role_name (str): The name of the role played by the user.+ sys_msg_generator (SystemMessageGenerator): A system message+ generator for agents.+ extend_sys_msg_meta_dicts (List[Dict], optional): A list of dicts+ to extend the system message meta dicts with.- sys_msg_meta_dicts = [dict(task=task_prompt)] * 2+ Returns:+ A tuple containing a `BaseMessage` representing the assistant's+ initial system message, a `BaseMessage` representing the user's+ initial system message, and a list of system message meta dicts.+ """+ sys_msg_meta_dicts = [dict(task=self.task_prompt) for _ in range(2)]if (extend_sys_msg_meta_dicts is None and self.task_typein [TaskType.AI_SOCIETY, TaskType.MISALIGNMENT]):extend_sys_msg_meta_dicts = [dict(assistant_role=assistant_role_name,- user_role=user_role_name)- ] * 2+ user_role=user_role_name) for _ in range(2)+ ]if extend_sys_msg_meta_dicts is not None:sys_msg_meta_dicts = [{**sys_msg_meta_dict,@@ -159,25 +240,64 @@ class RolePlaying:(user_role_name, RoleType.USER),],))+ return init_assistant_sys_msg, init_user_sys_msg, sys_msg_meta_dicts- self.assistant_agent: ChatAgent = ChatAgent(+ def init_agents(self, init_assistant_sys_msg: BaseMessage,+ assistant_agent_kwargs: Optional[Dict],+ init_user_sys_msg: BaseMessage,+ user_agent_kwargs: Optional[Dict],+ output_language: Optional[str]):+ r"""Initialize assistant and user agents with their system messages.++ Args:+ init_assistant_sys_msg (BaseMessage): Assistant agent's initial+ system message.+ assistant_agent_kwargs (Dict, optional): Additional arguments to+ pass to the assistant agent.+ init_user_sys_msg (BaseMessage): User agent's initial system+ message.+ user_agent_kwargs (Dict, optional): Additional arguments to+ pass to the user agent.+ output_language (str, optional): The language to be output by the+ agents.+ """+ self.assistant_agent = ChatAgent(init_assistant_sys_msg,- model_type,+ self.model_type,output_language=output_language,**(assistant_agent_kwargs or {}),)self.assistant_sys_msg = self.assistant_agent.system_message-- self.user_agent: ChatAgent = ChatAgent(+ self.user_agent = ChatAgent(init_user_sys_msg,- model_type,+ self.model_type,output_language=output_language,**(user_agent_kwargs or {}),)self.user_sys_msg = self.user_agent.system_message- self.critic: Union[CriticAgent, Human, None]- if with_critic_in_the_loop:+ def init_critic(self, critic_role_name: str,+ critic_criteria: Optional[str],+ critic_kwargs: Optional[Dict],+ sys_msg_generator: SystemMessageGenerator,+ sys_msg_meta_dicts: List[Dict]):+ r"""Initialize critic agent. If critic role name is :obj:`"human"`,+ create a :obj:`Human` critic agent. Else, create a :obj:`CriticAgent`+ critic agent with specified critic criteria. If the critic criteria+ is not specified, set it to improve task performance.++ Args:+ critic_role_name (str): The name of the role played by the critic.+ critic_criteria (str, optional): Critic criteria for the+ critic agent. If not specified, set the criteria to+ improve task performance.+ critic_kwargs (Dict, optional): Additional arguments to+ pass to the critic.+ sys_msg_generator (SystemMessageGenerator): A system message+ generator for agents.+ sys_msg_meta_dicts (list): A list of system message meta dicts.+ """+ if self.with_critic_in_the_loop:if critic_role_name.lower() == "human":self.critic = Human(**(critic_kwargs or {}))else:@@ -192,11 +312,9 @@ class RolePlaying:)self.critic = CriticAgent(self.critic_sys_msg,- model_type,+ self.model_type,**(critic_kwargs or {}),)- else:- self.critic = Nonedef init_chat(self) -> Tuple[BaseMessage, List[BaseMessage]]:r"""Initializes the chat by resetting both of the assistant and user@@ -205,9 +323,9 @@ class RolePlaying:user's response messages.Returns:- A tuple containing an `BaseMessage` representing the- assistant's introductory message, and a list of `BaseMessage`s- representing the user's response messages.+ A tuple containing a `BaseMessage` representing the assistant's+ introductory message, and a list of `BaseMessage` representing+ the user's response messages."""self.assistant_agent.reset()self.user_agent.reset()diff --git a/test/agents/test_role_playing.py b/test/agents/test_role_playing.pyindex cecfa3e5c..b867087b4 100644--- a/test/agents/test_role_playing.py+++ b/test/agents/test_role_playing.py@@ -13,26 +13,30 @@# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========import pytest-from camel.agents import ChatAgent+from camel.agents import ChatAgent, CriticAgent+from camel.human import Humanfrom camel.messages import BaseMessagefrom camel.societies import RolePlayingfrom camel.typing import ModelType, RoleType, TaskType-def test_role_playing_init():[email protected]("critic_role_name", ["human", "critic agent"])[email protected]("with_critic_in_the_loop", [True, False])+def test_role_playing_init(critic_role_name, with_critic_in_the_loop):role_playing = RolePlaying(assistant_role_name="assistant",user_role_name="user",+ critic_role_name=critic_role_name,task_prompt="Perform the task",with_task_specify=False,with_task_planner=False,- with_critic_in_the_loop=False,+ with_critic_in_the_loop=with_critic_in_the_loop,model_type=ModelType.GPT_3_5_TURBO,task_type=TaskType.AI_SOCIETY,)assert role_playing.with_task_specify is Falseassert role_playing.with_task_planner is False- assert role_playing.with_critic_in_the_loop is False+ assert role_playing.with_critic_in_the_loop is with_critic_in_the_loopassert role_playing.model_type == ModelType.GPT_3_5_TURBOassert role_playing.task_type == TaskType.AI_SOCIETYassert role_playing.task_prompt == "Perform the task"@@ -47,7 +51,15 @@ def test_role_playing_init():assert isinstance(role_playing.assistant_agent, ChatAgent)assert isinstance(role_playing.user_agent, ChatAgent)- assert role_playing.critic is None+ if not with_critic_in_the_loop:+ assert role_playing.critic is None+ else:+ assert role_playing.critic is not None+ if critic_role_name == "human":+ assert isinstance(role_playing.critic, Human)+ else:+ assert isinstance(role_playing.critic, CriticAgent)+ assert role_playing.critic_sys_msg is not None@pytest.mark.model_backend