🐫 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
Add `CriticAgent` (#112)
Guohao Li1313816ff760
diff --git a/camel/agents/__init__.py b/camel/agents/__init__.pyindex 1acca7b5a..5ae677cb1 100644--- a/camel/agents/__init__.py+++ b/camel/agents/__init__.py@@ -1,10 +1,12 @@from .chat_agent import ChatAgentfrom .task_agent import TaskPlannerAgent, TaskSpecifyAgent+from .critic_agent import CriticAgentfrom .role_playing import RolePlaying__all__ = ['ChatAgent','TaskSpecifyAgent','TaskPlannerAgent',+ 'CriticAgent','RolePlaying',]diff --git a/camel/agents/critic_agent.py b/camel/agents/critic_agent.pynew file mode 100644index 000000000..6e8b50549--- /dev/null+++ b/camel/agents/critic_agent.py@@ -0,0 +1,162 @@+import copy+import random+import warnings+from typing import Any, Dict, List, Optional++from colorama import Fore++from camel.agents import ChatAgent+from camel.messages import ChatMessage, SystemMessage+from camel.typing import ModelType+from camel.utils import get_first_int, print_text_animated+++class CriticAgent(ChatAgent):+ r"""A class for the critic agent that assists in selecting an option.++ Args:+ system_message (SystemMessage): The system message for the critic+ agent.+ model (ModelType, optional): The LLM model to use for generating+ responses. (default :obj:`ModelType.GPT_3_5_TURBO`)+ model_config (Any, optional): Configuration options for the LLM model.+ (default: :obj:`None`)+ message_window_size (int, optional): The maximum number of previous+ messages to include in the context window. If `None`, no windowing+ is performed. (default: :obj:`6`)+ retry_attempts (int, optional): The number of retry attempts if the+ critic fails to return a valid option. (default: :obj:`2`)+ verbose (bool, optional): Whether to print the critic's messages.+ menu_color (Any): The color of the menu options displayed to the user.+ (default: :obj:`Fore.MAGENTA`)+ """++ def __init__(+ self,+ system_message: SystemMessage,+ model: ModelType = ModelType.GPT_3_5_TURBO,+ model_config: Any = None,+ message_window_size: int = 6,+ retry_attempts: int = 2,+ verbose: bool = False,+ menu_color: Any = Fore.MAGENTA,+ ) -> None:+ super().__init__(system_message, model, model_config,+ message_window_size)+ self.options_dict: Dict[str, str] = dict()+ self.retry_attempts = retry_attempts+ self.verbose = verbose+ self.menu_color = menu_color++ def flatten_options(self, messages: List[ChatMessage]) -> str:+ r"""Flattens the options to the critic.++ Args:+ messages (List[ChatMessage]): A list of `ChatMessage` objects.++ Returns:+ str: A string containing the flattened options to the critic.+ """+ options = [message.content for message in messages]+ flatten_options = (+ f"> Proposals from "+ f"{messages[0].role_name} ({messages[0].role_type}). "+ "Please choose an option:\n")+ for index, option in enumerate(options):+ flatten_options += f"Option {index + 1}:\n{option}\n\n"+ self.options_dict[str(index + 1)] = option+ format = (+ f"Please first enter your choice ([1-{len(self.options_dict)}]) "+ "and then your explanation and comparison: ")+ return flatten_options + format++ def get_option(self, input_message: ChatMessage) -> str:+ r"""Gets the option selected by the critic.++ Args:+ input_message (ChatMessage): A `ChatMessage` object representing+ the input message.++ Returns:+ str: The option selected by the critic.+ """+ # TODO: Add support for editing options by the critic.+ msg_content = input_message.content+ i = 0+ while i < self.retry_attempts:+ critic_msgs, terminated, _ = super().step(input_message)++ if critic_msgs is None:+ raise RuntimeError("Got None critic messages.")+ if terminated:+ raise RuntimeError("Critic step failed.")++ critic_msg = critic_msgs[0]+ self.update_messages(critic_msg)+ if self.verbose:+ print_text_animated(self.menu_color + "\n> Critic response: "+ f"\x1b[3m{critic_msg.content}\x1b[0m\n")+ choice = self.parse_critic(critic_msg)++ if choice in self.options_dict:+ return self.options_dict[choice]+ else:+ input_message = ChatMessage(+ role_name=input_message.role_name,+ role_type=input_message.role_type,+ meta_dict=input_message.meta_dict,+ role=input_message.role,+ content="> Invalid choice. Please choose again.\n" ++ msg_content,+ )+ i += 1+ warnings.warn("Critic failed to get a valid option. "+ f"After {self.retry_attempts} attempts. "+ "Returning a random option.")+ return random.choice(list(self.options_dict.values()))++ def parse_critic(self, critic_msg: ChatMessage) -> Optional[str]:+ r"""Parses the critic's message and extracts the choice.++ Args:+ critic_msg (ChatMessage): A `ChatMessage` object representing the+ critic's response.++ Returns:+ Optional[str]: The critic's choice as a string, or None if the+ message could not be parsed.+ """+ choice = str(get_first_int(critic_msg.content))+ return choice++ def step(self, messages: List[ChatMessage]) -> ChatMessage:+ r"""Performs one step of the conversation by flattening options to the+ critic, getting the option, and parsing the choice.++ Args:+ messages (List[ChatMessage]): A list of ChatMessage objects.++ Returns:+ ChatMessage: A `ChatMessage` object representing the critic's+ choice.+ """+ meta_chat_message = ChatMessage(+ role_name=messages[0].role_name,+ role_type=messages[0].role_type,+ meta_dict=messages[0].meta_dict,+ role=messages[0].role,+ content="",+ )++ flatten_options = self.flatten_options(messages)+ if self.verbose:+ print_text_animated(self.menu_color ++ f"\x1b[3m{flatten_options}\x1b[0m\n")+ input_msg = copy.deepcopy(meta_chat_message)+ input_msg.content = flatten_options++ option = self.get_option(input_msg.to_user_chat_message())+ output_msg = copy.deepcopy(meta_chat_message)+ output_msg.content = option++ return output_msgdiff --git a/camel/agents/role_playing.py b/camel/agents/role_playing.pyindex 358ccec5b..764e3cf5c 100644--- a/camel/agents/role_playing.py+++ b/camel/agents/role_playing.py@@ -1,6 +1,11 @@from typing import Dict, List, Optional, Tuple-from camel.agents import ChatAgent, TaskPlannerAgent, TaskSpecifyAgent+from camel.agents import (+ ChatAgent,+ CriticAgent,+ TaskPlannerAgent,+ TaskSpecifyAgent,+)from camel.generators import SystemMessageGeneratorfrom camel.human import Humanfrom camel.messages import AssistantChatMessage, ChatMessage, UserChatMessage@@ -14,14 +19,16 @@ class RolePlaying:assistant_role_name (str): The name of the role played by theassistant.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.+ (default: :obj:`"critic"`)task_prompt (str, optional): A prompt for the task to be performed.(default: :obj:`""`)with_task_specify (bool, optional): Whether to use a task specifyagent. (default: :obj:`True`)with_task_planner (bool, optional): Whether to use a task planneragent. (default: :obj:`False`)- with_human_in_the_loop (bool, optional): Whether to include a human in- the loop. (default: :obj:`False`)+ with_critic_in_the_loop (bool, optional): Whether to include a critic+ in the loop. (default: :obj:`False`)mode_type (ModelType, optional): The type of GPT model to use.(default: :obj:`ModelType.GPT_3_5_TURBO`)task_type (TaskType, optional): The type of task to perform.@@ -34,8 +41,8 @@ class RolePlaying:pass to the task specify agent. (default: :obj:`None`)task_planner_agent_kwargs (Dict, optional): Additional arguments topass to the task planner agent. (default: :obj:`None`)- human_kwargs (Dict, optional): Additional arguments to pass to the- human. (default: :obj:`None`)+ critic_kwargs (Dict, optional): Additional arguments to pass to the+ critic. (default: :obj:`None`)sys_msg_generator_kwargs (Dict, optional): Additional arguments topass to the system message generator. (default: :obj:`None`)extend_sys_msg_meta_dicts (List[Dict], optional): A list of dicts to@@ -48,24 +55,26 @@ class RolePlaying:self,assistant_role_name: str,user_role_name: str,+ critic_role_name: str = "critic",task_prompt: str = "",with_task_specify: bool = True,with_task_planner: bool = False,- with_human_in_the_loop: bool = False,+ with_critic_in_the_loop: bool = False,+ critic_criteria: Optional[str] = None,mode_type: ModelType = ModelType.GPT_3_5_TURBO,task_type: TaskType = TaskType.AI_SOCIETY,assistant_agent_kwargs: Optional[Dict] = None,user_agent_kwargs: Optional[Dict] = None,task_specify_agent_kwargs: Optional[Dict] = None,task_planner_agent_kwargs: Optional[Dict] = None,- human_kwargs: Optional[Dict] = None,+ critic_kwargs: Optional[Dict] = None,sys_msg_generator_kwargs: Optional[Dict] = None,extend_sys_msg_meta_dicts: Optional[List[Dict]] = None,extend_task_specify_meta_dict: Optional[Dict] = None,) -> None:self.with_task_specify = with_task_specifyself.with_task_planner = with_task_planner- self.with_human_in_the_loop = with_human_in_the_loop+ self.with_critic_in_the_loop = with_critic_in_the_loopself.mode_type = mode_typeself.task_type = task_type@@ -140,10 +149,26 @@ class RolePlaying:**(user_agent_kwargs or {}),)- if with_human_in_the_loop:- self.human = Human(**(human_kwargs or {}))+ if with_critic_in_the_loop:+ if critic_role_name.lower() == "human":+ self.critic = Human(**(critic_kwargs or {}))+ else:+ critic_criteria = (critic_criteria+ or "improving the task performance")+ critic_msg_meta_dict = dict(critic_role=critic_role_name,+ criteria=critic_criteria,+ **sys_msg_meta_dicts[0])+ self.critic_sys_msg = sys_msg_generator.from_dict(+ critic_msg_meta_dict,+ role_tuple=(critic_role_name, RoleType.CRITIC),+ )+ self.critic = CriticAgent(+ self.critic_sys_msg,+ mode_type,+ **(critic_kwargs or {}),+ )else:- self.human = None+ self.critic = Nonedef init_chat(self) -> Tuple[AssistantChatMessage, List[ChatMessage]]:r"""Initializes the chat by resetting both the assistant and user@@ -180,7 +205,7 @@ class RolePlaying:messages: List[ChatMessage],) -> ChatMessage:r"""Processes a list of chat messages, returning the processed message.- If multiple messages are provided and `with_human_in_the_loop`+ If multiple messages are provided and `with_critic_in_the_loop`is `False`, raises a `ValueError`. If no messages are provided, alsoraises a `ValueError`.@@ -192,11 +217,11 @@ class RolePlaying:"""if len(messages) == 0:raise ValueError("No messages to process.")- if len(messages) > 1 and not self.with_human_in_the_loop:+ if len(messages) > 1 and not self.with_critic_in_the_loop:raise ValueError("Got than one message to process. "f"Num of messages: {len(messages)}.")- elif self.with_human_in_the_loop and self.human is not None:- processed_msg = self.human.step(messages)+ elif self.with_critic_in_the_loop and self.critic is not None:+ processed_msg = self.critic.step(messages)else:processed_msg = messages[0]diff --git a/camel/generators.py b/camel/generators.pyindex 45d8ba562..c16cb4628 100644--- a/camel/generators.py+++ b/camel/generators.py@@ -37,13 +37,20 @@ class SystemMessageGenerator:task_type,RoleType.USER,)+ critic_prompt_template = PromptTemplateGenerator(+ ).get_system_prompt(+ task_type,+ RoleType.CRITIC,+ )self.sys_prompts: Dict[RoleType, str] = dict()self.sys_prompts[RoleType.ASSISTANT] = assistant_prompt_templateself.sys_prompts[RoleType.USER] = user_prompt_template+ self.sys_prompts[RoleType.CRITIC] = critic_prompt_templateself.sys_msg_meta_dict_keys = (assistant_prompt_template.key_words- | user_prompt_template.key_words)+ | user_prompt_template.key_words+ | critic_prompt_template.key_words)if RoleType.DEFAULT not in self.sys_prompts:self.sys_prompts[RoleType.DEFAULT] = "You are a helpful assistant."diff --git a/camel/human.py b/camel/human.pyindex 47b0aeee6..badd6aa72 100644--- a/camel/human.py+++ b/camel/human.py@@ -49,8 +49,9 @@ class Human:f"{messages[0].role_name} ({messages[0].role_type}). ""Please choose an option:\n")for index, option in enumerate(options):- print_text_animated(self.menu_color +- f"\x1b[3m{index + 1}:\n{option}\x1b[0m\n")+ print_text_animated(+ self.menu_color ++ f"\x1b[3mOption {index + 1}:\n{option}\x1b[0m\n")self.options_dict[str(index + 1)] = optiondef get_input(self) -> str:diff --git a/camel/prompts/ai_society.py b/camel/prompts/ai_society.pyindex 7708ff11d..db1aafca8 100644--- a/camel/prompts/ai_society.py+++ b/camel/prompts/ai_society.py@@ -89,6 +89,12 @@ Keep giving me instructions and necessary inputs until you think the task is comWhen the task is completed, you must only reply with a single word <CAMEL_TASK_DONE>.Never say <CAMEL_TASK_DONE> unless my responses have solved your task.""")+ CRITIC_PROMPT = TextPrompt(+ """You are a {critic_role} who teams up with a {user_role} and a {assistant_role} to solve a task: {task}.+Your job is to select an option from their proposals and provides your explanations.+Your selection criteria are {criteria}.+You always have to choose an option from the proposals.""")+def __init__(self, *args: Any, **kwargs: Any) -> None:super().__init__(*args, **kwargs)self.update({@@ -98,4 +104,5 @@ Never say <CAMEL_TASK_DONE> unless my responses have solved your task.""")"task_specify_prompt": self.TASK_SPECIFY_PROMPT,RoleType.ASSISTANT: self.ASSISTANT_PROMPT,RoleType.USER: self.USER_PROMPT,+ RoleType.CRITIC: self.CRITIC_PROMPT,})diff --git a/camel/prompts/code.py b/camel/prompts/code.pyindex 63c2be0ab..c3742e2fb 100644--- a/camel/prompts/code.py+++ b/camel/prompts/code.py@@ -64,7 +64,7 @@ Always end <YOUR_SOLUTION> with: Next request.""")We share a common interest in collaborating to successfully complete a task.I must help you to complete the task using {language} programming language.Here is the task: {task}. Never forget our task!-You must instruct me based on my expertise and your needs to complete the task ONLY in the following two ways:+You must instruct me based on my expertise and your needs to solve the task ONLY in the following two ways:1. Instruct with a necessary input:Instruction: <YOUR_INSTRUCTION>diff --git a/camel/typing.py b/camel/typing.pyindex b64cc92d6..7dc1b0a35 100644--- a/camel/typing.py+++ b/camel/typing.py@@ -1,10 +1,10 @@from enum import Enum-# The values should be the same as the prompt file namesclass RoleType(Enum):ASSISTANT = "assistant"USER = "user"+ CRITIC = "critic"DEFAULT = "default"@@ -14,7 +14,6 @@ class ModelType(Enum):GPT_4_32k = "gpt-4-32k"-# The values should be the same as the prompt dir namesclass TaskType(Enum):AI_SOCIETY = "ai_society"CODE = "code"diff --git a/camel/utils.py b/camel/utils.pyindex 4bec48c3c..fcfddf9e2 100644--- a/camel/utils.py+++ b/camel/utils.py@@ -2,7 +2,7 @@ import osimport reimport timefrom functools import wraps-from typing import Any, Callable, List, Set+from typing import Any, Callable, List, Optional, Setimport tiktoken@@ -152,3 +152,22 @@ def get_prompt_template_key_words(template: str) -> Set[str]:{'name', 'status'}"""return set(re.findall(r'{([^}]*)}', template))+++def get_first_int(string: str) -> Optional[int]:+ r"""Returns the first integer number found in the given string.++ If no integer number is found, returns None.++ Args:+ string (str): The input string.++ Returns:+ int or None: The first integer number found in the string, or None if+ no integer number is found.+ """+ match = re.search(r'\d+', string)+ if match:+ return int(match.group())+ else:+ return Nonediff --git a/examples/ai_society/role_playing_with_critic.py b/examples/ai_society/role_playing_with_critic.pynew file mode 100644index 000000000..b839a887f--- /dev/null+++ b/examples/ai_society/role_playing_with_critic.py@@ -0,0 +1,68 @@+from colorama import Fore++from camel.agents import RolePlaying+from camel.configs import ChatGPTConfig+from camel.utils import print_text_animated+++def main() -> None:+ task_prompt = "Write a research proposal for large-scale language models"+ model_config = ChatGPTConfig(temperature=0.8, n=3)+ assistant_agent_kwargs = dict(model_config=model_config)+ user_agent_kwargs = dict(model_config=model_config)+ critic_kwargs = dict(verbose=True)+ role_play_session = RolePlaying(+ "PhD Student",+ "Postdoc",+ critic_role_name="Professor",+ task_prompt=task_prompt,+ with_task_specify=True,+ with_critic_in_the_loop=True,+ assistant_agent_kwargs=assistant_agent_kwargs,+ user_agent_kwargs=user_agent_kwargs,+ critic_kwargs=critic_kwargs,+ )++ print(+ Fore.GREEN ++ f"AI Assistant sys message:\n{role_play_session.assistant_sys_msg}\n")+ print(Fore.BLUE ++ f"AI User sys message:\n{role_play_session.user_sys_msg}\n")+ print(Fore.MAGENTA ++ f"Critic sys message:\n{role_play_session.critic_sys_msg}\n")++ print(Fore.YELLOW + f"Original task prompt:\n{task_prompt}\n")+ print(+ Fore.CYAN ++ f"Specified task prompt:\n{role_play_session.specified_task_prompt}\n")+ print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")++ chat_turn_limit, n = 50, 0+ assistant_msg, _ = role_play_session.init_chat()+ while n < chat_turn_limit:+ n += 1+ assistant_return, user_return = role_play_session.step(assistant_msg)+ assistant_msg, assistant_terminated, assistant_info = assistant_return+ user_msg, user_terminated, user_info = user_return++ if assistant_terminated:+ print(Fore.GREEN ++ ("AI Assistant terminated. "+ f"Reason: {assistant_info['termination_reasons']}."))+ break+ if user_terminated:+ print(Fore.GREEN ++ ("AI User terminated. "+ f"Reason: {user_info['termination_reasons']}."))+ break++ print_text_animated(Fore.BLUE + f"AI User:\n\n{user_msg.content}\n")+ print_text_animated(Fore.GREEN ++ f"AI Assistant:\n\n{assistant_msg.content}\n")++ if "CAMEL_TASK_DONE" in user_msg.content:+ break+++if __name__ == "__main__":+ main()diff --git a/examples/ai_society/role_playing_with_human.py b/examples/ai_society/role_playing_with_human.pyindex 71acb3e11..86622c6ad 100644--- a/examples/ai_society/role_playing_with_human.py+++ b/examples/ai_society/role_playing_with_human.py@@ -13,9 +13,10 @@ def main() -> None:role_play_session = RolePlaying("AGI","Writer",+ critic_role_name="human",task_prompt=task_prompt,with_task_specify=True,- with_human_in_the_loop=True,+ with_critic_in_the_loop=True,assistant_agent_kwargs=assistant_agent_kwargs,user_agent_kwargs=user_agent_kwargs,)diff --git a/test/agents/test_critic_agent.py b/test/agents/test_critic_agent.pynew file mode 100644index 000000000..5c49243f2--- /dev/null+++ b/test/agents/test_critic_agent.py@@ -0,0 +1,111 @@+import pytest++from camel.agents import CriticAgent+from camel.messages import ChatMessage, SystemMessage+from camel.typing import RoleType+from camel.utils import openai_api_key_required+++def critic_agent() -> CriticAgent:+ return CriticAgent(+ SystemMessage(+ "critic",+ RoleType.CRITIC,+ content=("You are a critic who assists in selecting an option "+ "and provides explanations. "+ "Your favorite fruit is Apple. "+ "You always have to choose an option."),+ ))+++def test_flatten_options(critic_agent: CriticAgent):+ messages = [+ ChatMessage(+ role_name="user",+ role_type=RoleType.USER,+ meta_dict=dict(),+ role="user",+ content="Apple",+ ),+ ChatMessage(+ role_name="user",+ role_type=RoleType.USER,+ meta_dict=dict(),+ role="user",+ content="Banana",+ ),+ ]+ expected_output = (f"> Proposals from user ({str(RoleType.USER)}). "+ "Please choose an option:\n"+ "Option 1:\nApple\n\n"+ "Option 2:\nBanana\n\n"+ f"Please first enter your choice ([1-{len(messages)}]) "+ "and then your explanation and comparison: ")+ assert critic_agent.flatten_options(messages) == expected_output+++@openai_api_key_required+def test_get_option(critic_agent: CriticAgent):+ messages = [+ ChatMessage(+ role_name="user",+ role_type=RoleType.USER,+ meta_dict=dict(),+ role="user",+ content="Apple",+ ),+ ChatMessage(+ role_name="user",+ role_type=RoleType.USER,+ meta_dict=dict(),+ role="user",+ content="Banana",+ ),+ ]+ flatten_options = critic_agent.flatten_options(messages)+ input_message = ChatMessage(+ role_name="user",+ role_type=RoleType.USER,+ meta_dict=dict(),+ role="user",+ content=flatten_options,+ )+ assert critic_agent.options_dict == {"1": "Apple", "2": "Banana"}+ assert critic_agent.get_option(+ input_message) in critic_agent.options_dict.values()+++def test_parse_critic(critic_agent: CriticAgent):+ critic_msg = ChatMessage(+ role_name="critic",+ role_type=RoleType.CRITIC,+ meta_dict=dict(),+ role="assistant",+ content="I choose option 1",+ )+ expected_output = "1"+ assert critic_agent.parse_critic(critic_msg) == expected_output+++@openai_api_key_required+def test_step(critic_agent: CriticAgent):+ messages = [+ ChatMessage(+ role_name="user",+ role_type=RoleType.USER,+ meta_dict=dict(),+ role="user",+ content="Apple",+ ),+ ChatMessage(+ role_name="user",+ role_type=RoleType.USER,+ meta_dict=dict(),+ role="user",+ content="Banana",+ ),+ ]++ assert (critic_agent.step(messages)+ == messages[0]) or (critic_agent.step(messages) == messages[1])diff --git a/test/agents/test_role_playing.py b/test/agents/test_role_playing.pyindex 1d6a565b4..13c7c19f8 100644--- a/test/agents/test_role_playing.py+++ b/test/agents/test_role_playing.py@@ -15,13 +15,13 @@ def test_role_playing_init():task_prompt="Perform the task",with_task_specify=False,with_task_planner=False,- with_human_in_the_loop=False,+ with_critic_in_the_loop=False,mode_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_human_in_the_loop is False+ assert role_playing.with_critic_in_the_loop is Falseassert role_playing.mode_type == ModelType.GPT_3_5_TURBOassert role_playing.task_type == TaskType.AI_SOCIETYassert role_playing.task_prompt == "Perform the task"@@ -36,7 +36,7 @@ def test_role_playing_init():assert isinstance(role_playing.assistant_agent, ChatAgent)assert isinstance(role_playing.user_agent, ChatAgent)- assert role_playing.human is None+ assert role_playing.critic is None@openai_api_key_required