🐫 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
Extracted ChatAgentResponse class, API breaking change (#154)
Dmitrii Khizbullin0621ed831cbd
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex ee078e6e9..387ad4dd7 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -82,8 +82,17 @@ isort .Code coverage measures the extent to which unit tests cover the code, helping identify both robust and less robust areas of the codebase.-To generate a report showing the current code coverage, execute the following command:+To generate a report showing the current code coverage, execute one of the following commands.+To include all source files into coverage:+```+coverage erase+coverage run --source=. -m pytest .+coverage html+# Open htmlcov/index.html+```++To include only tested files:```bashpytest --cov --cov-report=html```diff --git a/apps/agents/agents.py b/apps/agents/agents.pyindex 3b1fc98c8..9a872607c 100644--- a/apps/agents/agents.py+++ b/apps/agents/agents.py@@ -211,8 +211,10 @@ def role_playing_chat_init(state) -> \print("Error: session is none on role_playing_chat_init call")return state, state.chat, gr.update()+ session: RolePlaying = state.session+try:- assistant_msg, _ = state.session.init_chat()+ assistant_msg, _ = session.init_chat()assistant_msg: AssistantChatMessageexcept (openai.error.RateLimitError, tenacity.RetryError,RuntimeError) as ex:@@ -248,20 +250,24 @@ def role_playing_chat_cont(state) -> \if state.session is None:return state, state.chat, gr.update(visible=False), gr.update()+ session: RolePlaying = state.session+if state.saved_assistant_msg is None:return state, state.chat, gr.update(), gr.update()try:- assistant_msgs, user_msgs = state.session.step(- state.saved_assistant_msg)+ assistant_msg, user_msg = session.step(state.saved_assistant_msg)except (openai.error.RateLimitError, tenacity.RetryError,RuntimeError) as ex:print("OpenAI API exception 2 " + str(ex))state.session = Nonereturn state, state.chat, gr.update(), gr.update()- u_msg = user_msgs[0]- a_msg = assistant_msgs[0]+ if len(user_msg.msgs) != 1 or len(assistant_msg.msgs) != 1:+ return state, state.chat, gr.update(), gr.update()++ u_msg = user_msg.msgs[0]+ a_msg = assistant_msg.msgs[0]state.saved_assistant_msg = a_msgdiff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.pyindex e1b09adca..427f13e88 100644--- a/camel/agents/chat_agent.py+++ b/camel/agents/chat_agent.py@@ -11,10 +11,13 @@# See the License for the specific language governing permissions and# limitations under the License.# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========-from typing import Any, Dict, List, Optional, Tuple+from dataclasses import dataclass+from typing import Any, Dict, List, Optionalimport openai-from tenacity import retry, stop_after_attempt, wait_exponential+from tenacity import retry+from tenacity.stop import stop_after_attempt+from tenacity.wait import wait_exponentialfrom camel.agents import BaseAgentfrom camel.configs import ChatGPTConfig@@ -27,6 +30,31 @@ from camel.utils import ()+@dataclass(frozen=True)+class ChatAgentResponse:+ r"""Response of a ChatAgent.++ Attributes:+ msgs (List[ChatMessage]): A list of zero, one or several messages.+ If the list is empty, there is some error in message generation.+ If the list has one message, this is normal mode.+ If the list has several messages, this is the critic mode.+ terminated (bool): A boolean indicating whether the agent decided+ to terminate the chat session.+ info (Dict[str, Any]): Extra information about the chat message.+ """+ msgs: List[ChatMessage]+ terminated: bool+ info: Dict[str, Any]++ @property+ def msg(self):+ if len(self.msgs) != 1:+ raise RuntimeError("Property msg is only available"+ "for a single message in msgs")+ return self.msgs[0]++class ChatAgent(BaseAgent):r"""Class for managing conversations of CAMEL Chat Agents.@@ -59,7 +87,7 @@ class ChatAgent(BaseAgent):self.model_token_limit = get_model_token_limit(self.model)self.message_window_size = message_window_size- self.terminated = False+ self.terminated: bool = Falseself.init_messages()def reset(self) -> List[MessageType]:@@ -124,7 +152,7 @@ class ChatAgent(BaseAgent):def step(self,input_message: ChatMessage,- ) -> Tuple[Optional[List[ChatMessage]], bool, Dict[str, Any]]:+ ) -> ChatAgentResponse:r"""Performs a single step in the chat session by generating a responseto the input message.@@ -132,7 +160,7 @@ class ChatAgent(BaseAgent):input_message (ChatMessage): The input message to the agent.Returns:- Tuple[Optional[List[ChatMessage]], bool, Dict[str, Any]]: A tuple+ ChatAgentResponse: A structcontaining the output messages, a boolean indicating whetherthe chat session has terminated, and information about the chatsession.@@ -145,12 +173,17 @@ class ChatAgent(BaseAgent):openai_messages = [message.to_openai_message() for message in messages]num_tokens = num_tokens_from_messages(openai_messages, self.model)+ output_messages: Optional[List[ChatMessage]]+ info: Dict[str, Any]+if num_tokens < self.model_token_limit:response = openai.ChatCompletion.create(model=self.model.value,messages=openai_messages,**self.model_config.__dict__,)+ if not isinstance(response, dict):+ raise RuntimeError("OpenAI returned unexpected struct")output_messages = [ChatMessage(role_name=self.role_name, role_type=self.role_type,meta_dict=dict(), **dict(choice["message"]))@@ -168,7 +201,7 @@ class ChatAgent(BaseAgent):else:self.terminated = True- output_messages = None+ output_messages = []info = self.get_info(None,@@ -177,7 +210,7 @@ class ChatAgent(BaseAgent):num_tokens,)- return output_messages, self.terminated, info+ return ChatAgentResponse(output_messages, self.terminated, info)def __repr__(self) -> str:r"""Returns a string representation of the :obj:`ChatAgent`.diff --git a/camel/agents/critic_agent.py b/camel/agents/critic_agent.pyindex 9300d3676..95bd1137f 100644--- a/camel/agents/critic_agent.py+++ b/camel/agents/critic_agent.py@@ -97,14 +97,14 @@ class CriticAgent(ChatAgent):msg_content = input_message.contenti = 0while i < self.retry_attempts:- critic_msgs, terminated, _ = super().step(input_message)+ critic_response = super().step(input_message)- if critic_msgs is None:+ if critic_response.msgs is None or len(critic_response.msgs) == 0:raise RuntimeError("Got None critic messages.")- if terminated:+ if critic_response.terminated:raise RuntimeError("Critic step failed.")- critic_msg = critic_msgs[0]+ critic_msg = critic_response.msgs[0]self.update_messages(critic_msg)if self.verbose:print_text_animated(self.logger_color + "\n> Critic response: "diff --git a/camel/agents/embodied_agent.py b/camel/agents/embodied_agent.pyindex 64d328d0b..a9bf44872 100644--- a/camel/agents/embodied_agent.py+++ b/camel/agents/embodied_agent.py@@ -91,16 +91,15 @@ class EmbodiedAgent(ChatAgent):containing the output messages, termination status, andadditional information."""- output_messages, terminated, info = super().step(input_message)+ response = super().step(input_message)- if output_messages is None:+ if response.msgs is None or len(response.msgs) == 0:raise RuntimeError("Got None output messages.")- if terminated:+ if response.terminated:raise RuntimeError(f"{self.__class__.__name__} step failed.")# NOTE: Only single output messages are supported- output_message = output_messages[0]- explanations, codes = output_message.extract_text_and_code_prompts()+ explanations, codes = response.msg.extract_text_and_code_prompts()if self.verbose:for explanation, code in zip(explanations, codes):@@ -108,21 +107,26 @@ class EmbodiedAgent(ChatAgent):f"> Explanation:\n{explanation}")print_text_animated(self.logger_color + f"> Code:\n{code}")- if len(explanation) > len(code):+ if len(explanations) > len(codes):print_text_animated(self.logger_color +- f"> Explanation:\n{explanation}")+ f"> Explanation:\n{explanations}")++ content = response.msg.contentif codes is not None:- output_message.content = "\n> Executed Results:"+ content = "\n> Executed Results:"global_vars = {action.name: action for action in self.action_space}for code in codes:executed_outputs = code.execute(global_vars)- output_message.content += (+ content += (f"- Python standard output:\n{executed_outputs[0]}\n"f"- Local variables:\n{executed_outputs[1]}\n")- output_message.content += "*" * 50 + "\n"+ content += "*" * 50 + "\n"# TODO: Handle errors- input_message.content += (- Fore.RESET + f"\n> Embodied Actions:\n{output_message.content}")- return input_message, terminated, info+ content = input_message.content + (Fore.RESET ++ f"\n> Embodied Actions:\n{content}")+ message = ChatMessage(input_message.role_name, input_message.role_type,+ input_message.meta_dict, input_message.role,+ content)+ return message, response.terminated, response.infodiff --git a/camel/agents/role_playing.py b/camel/agents/role_playing.pyindex f583eb7c2..eba0931c9 100644--- a/camel/agents/role_playing.py+++ b/camel/agents/role_playing.py@@ -19,6 +19,7 @@ from camel.agents import (TaskPlannerAgent,TaskSpecifyAgent,)+from camel.agents.chat_agent import ChatAgentResponsefrom camel.generators import SystemMessageGeneratorfrom camel.human import Humanfrom camel.messages import AssistantChatMessage, ChatMessage, UserChatMessage@@ -206,12 +207,12 @@ class RolePlaying:user_msg = UserChatMessage(role_name=self.user_sys_msg.role_name,content=f"{self.assistant_sys_msg.content}")- msgs, terminated, info = self.assistant_agent.step(user_msg)- if terminated or msgs is None:- raise ValueError(- f"Assistant agent terminated unexpectedly. Error info: {info}")+ assistant_response = self.assistant_agent.step(user_msg)+ if assistant_response.terminated or assistant_response.msgs is None:+ raise ValueError(f"Assistant agent terminated unexpectedly. "+ f"Error info: {assistant_response.info}")- return assistant_msg, msgs+ return assistant_msg, assistant_response.msgsdef process_messages(self,@@ -243,8 +244,7 @@ class RolePlaying:def step(self,assistant_msg: ChatMessage,- ) -> Tuple[Tuple[Optional[ChatMessage], Optional[bool], Optional[Dict]],- Tuple[Optional[ChatMessage], Optional[bool], Optional[Dict]]]:+ ) -> Tuple[ChatAgentResponse, ChatAgentResponse]:r"""Advances the conversation by taking a message from the assistant,processing it using the user agent, and then processing the resultingmessage using the assistant agent. Returns a tuple containing the@@ -259,30 +259,34 @@ class RolePlaying:assistant.Returns:- A tuple containing two tuples: the first tuple contains the- resulting assistant message, whether or not the assistant agent+ A tuple containing two ChatAgentResponse: the first struct contains+ the resulting assistant message, whether or not the assistant agentterminated the conversation, and any additional assistant- information; the second tuple contains the resulting user message,+ information; the second struct contains the resulting user message,whether or not the user agent terminated the conversation, andany additional user information."""- user_msgs, user_terminated, user_info = self.user_agent.step(+ user_response = self.user_agent.step(assistant_msg.to_user_chat_message())- if user_terminated or user_msgs is None:- return ((None, None, None), (None, user_terminated, user_info))- user_msg = self.process_messages(user_msgs)+ if user_response.terminated or user_response.msgs is None:+ return (ChatAgentResponse([], False, {}),+ ChatAgentResponse([], user_response.terminated,+ user_response.info))+ user_msg = self.process_messages(user_response.msgs)self.user_agent.update_messages(user_msg)- (assistant_msgs, assistant_terminated,- assistant_info) = self.assistant_agent.step(- user_msg.to_user_chat_message())- if assistant_terminated or assistant_msgs is None:- return ((None, assistant_terminated, assistant_info),- (user_msg, user_terminated, user_info))- assistant_msg = self.process_messages(assistant_msgs)+ assistant_response = self.assistant_agent.step(+ user_msg.to_user_chat_message())+ if assistant_response.terminated or assistant_response.msgs is None:+ return (ChatAgentResponse([], assistant_response.terminated,+ assistant_response.info),+ ChatAgentResponse([user_msg], False, user_response.info))+ assistant_msg = self.process_messages(assistant_response.msgs)self.assistant_agent.update_messages(assistant_msg)return (- (assistant_msg, assistant_terminated, assistant_info),- (user_msg, user_terminated, user_info),+ ChatAgentResponse([assistant_msg], assistant_response.terminated,+ assistant_response.info),+ ChatAgentResponse([user_msg], user_response.terminated,+ user_response.info),)diff --git a/camel/agents/task_agent.py b/camel/agents/task_agent.pyindex f08ffae85..b89598eb1 100644--- a/camel/agents/task_agent.py+++ b/camel/agents/task_agent.py@@ -95,13 +95,16 @@ class TaskSpecifyAgent(ChatAgent):task_msg = UserChatMessage(role_name="Task Specifier",content=self.task_specify_prompt)- specified_task_msgs, terminated, _ = super().step(task_msg)- specified_task_msg = specified_task_msgs[0]+ specifier_response = super().step(task_msg)+ if (specifier_response.msgs is None+ or len(specifier_response.msgs) == 0):+ raise RuntimeError("Task specification failed.")+ specified_task_msg = specifier_response.msgs[0]- if terminated:+ if specifier_response.terminated:raise RuntimeError("Task specification failed.")- else:- return TextPrompt(specified_task_msg.content)++ return TextPrompt(specified_task_msg.content)class TaskPlannerAgent(ChatAgent):@@ -155,12 +158,13 @@ class TaskPlannerAgent(ChatAgent):task_msg = UserChatMessage(role_name="Task Planner",content=self.task_planner_prompt)- sub_tasks_msgs, terminated, _ = super().step(task_msg)+ # sub_tasks_msgs, terminated, _+ task_tesponse = super().step(task_msg)- if sub_tasks_msgs is None:+ if task_tesponse.msgs is None:raise RuntimeError("Got None Subtasks messages.")- if terminated:+ if task_tesponse.terminated:raise RuntimeError("Task planning failed.")- sub_tasks_msg = sub_tasks_msgs[0]+ sub_tasks_msg = task_tesponse.msgs[0]return TextPrompt(sub_tasks_msg.content)diff --git a/camel/messages/chat_messages.py b/camel/messages/chat_messages.pyindex 023f07bad..174abcad4 100644--- a/camel/messages/chat_messages.py+++ b/camel/messages/chat_messages.py@@ -43,7 +43,7 @@ class AssistantChatMessage(ChatMessage):r"""Class for chat messages from the assistant role used in CAMEL chatsystem.- Args:+ Attributes:role_name (str): The name of the assistant role.role_type (RoleType): The type of role, always:obj:`RoleType.ASSISTANT`.diff --git a/examples/ai_society/generate_meta_data.py b/examples/ai_society/generate_meta_data.pyindex af80eea11..ae603c654 100644--- a/examples/ai_society/generate_meta_data.py+++ b/examples/ai_society/generate_meta_data.py@@ -33,8 +33,9 @@ def main(key: str = "generate_users", num_roles: int = 50):role_name="User",content=prompt,)- assistant_msg, _, _ = agent.step(user_msg)- print(assistant_msg[0].content)+ assistant_response = agent.step(user_msg)+ if assistant_response.msgs is not None:+ print(assistant_response.msg.content)if __name__ == "__main__":diff --git a/examples/ai_society/role_playing.py b/examples/ai_society/role_playing.pyindex 17d6c0db6..0e7738b4c 100644--- a/examples/ai_society/role_playing.py+++ b/examples/ai_society/role_playing.py@@ -42,26 +42,26 @@ def main() -> None: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+ assistant_response, user_response = role_play_session.step(+ assistant_msg)- if assistant_terminated:+ if assistant_response.terminated:print(Fore.GREEN +- ("AI Assistant terminated. "- f"Reason: {assistant_info['termination_reasons']}."))+ ("AI Assistant terminated. Reason: "+ f"{assistant_response.info['termination_reasons']}."))break- if user_terminated:+ if user_response.terminated:print(Fore.GREEN +("AI User terminated. "- f"Reason: {user_info['termination_reasons']}."))+ f"Reason: {user_response.info['termination_reasons']}."))break- print_text_animated(Fore.BLUE + f"AI User:\n\n{user_msg.content}\n")+ print_text_animated(Fore.BLUE ++ f"AI User:\n\n{user_response.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:+ if "CAMEL_TASK_DONE" in user_response.msg.content:breakdiff --git a/examples/ai_society/role_playing_multiprocess.py b/examples/ai_society/role_playing_multiprocess.pyindex 2bdf391b0..b5441fa34 100644--- a/examples/ai_society/role_playing_multiprocess.py+++ b/examples/ai_society/role_playing_multiprocess.py@@ -92,32 +92,32 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,while message_counter < max_num_messages:- 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+ assistant_response, user_response = role_play_session.step(+ assistant_msg)# Condition 1: User terminates the chat- if user_terminated and user_info is not None:+ if user_response.terminated and user_response.info is not None:message_dict["termination_reason"] = (f"{str(user_agent.role_type)}: "- f"{user_info['termination_reasons'][0]}")+ f"{user_response.info['termination_reasons'][0]}")break# Condition 2: Assistant terminates the chat- if assistant_terminated and assistant_info is not None:+ if (assistant_response.terminated+ and assistant_response.info is not None):message_dict["termination_reason"] = (f"{str(assistant_agent.role_type)}: "- f"{assistant_info['termination_reasons'][0]}")+ f"{assistant_response.info['termination_reasons'][0]}")break- assert (user_msg is not None) and (assistant_msg is not None)+ assert (user_response.msg is not None) and (assistant_msg is not None)if verbose:- print(f"User:\n{user_msg.content}\n")+ print(f"User:\n{user_response.msg.content}\n")print(f"Assistant:\n{assistant_msg.content}\n")# Condition 3: Break if user does not give instruction- if user_no_instruct_word not in user_msg.content:+ if user_no_instruct_word not in user_response.msg.content:user_no_instruct_counter += 1if user_no_instruct_counter == user_no_instruct_threshold:message_dict[@@ -138,7 +138,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,# Condition 5: Repeat word observedfor repeat_word in repeat_word_list:- if repeat_word in user_msg.content.lower(+ if repeat_word in user_response.msg.content.lower() or repeat_word in assistant_msg.content.lower():repeat_word_counter += 1if repeat_word_counter == repeat_word_threshold:@@ -150,10 +150,11 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,# Save user messagemessage_counter += 1- message_dict[f"message_{message_counter}"] = user_msg.to_dict()+ message_dict[f"message_{message_counter}"] = \+ user_response.msg.to_dict()# Condition 5: End token observed- if "<CAMEL_TASK_DONE>" in user_msg.content:+ if "<CAMEL_TASK_DONE>" in user_response.msg.content:message_dict['termination_reason'] = "<CAMEL_TASK_DONE>"break@@ -197,7 +198,10 @@ def main() -> None:# Chunk for parallel jobstry:- array_idx = int(os.environ.get('SLURM_ARRAY_TASK_ID'))+ slurm_array_task_id = os.environ.get('SLURM_ARRAY_TASK_ID')+ if slurm_array_task_id is None:+ raise+ array_idx = int(slurm_array_task_id)except (TypeError, ValueError) as e:print(f"Error: {e}")array_idx = 0diff --git a/examples/ai_society/role_playing_with_critic.py b/examples/ai_society/role_playing_with_critic.pyindex 70d0627e8..31e075c90 100644--- a/examples/ai_society/role_playing_with_critic.py+++ b/examples/ai_society/role_playing_with_critic.py@@ -54,26 +54,26 @@ def main() -> None: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+ assistant_response, user_response = role_play_session.step(+ assistant_msg)- if assistant_terminated:+ if assistant_response.terminated:print(Fore.GREEN +- ("AI Assistant terminated. "- f"Reason: {assistant_info['termination_reasons']}."))+ ("AI Assistant terminated. Reason: "+ f"{assistant_response.info['termination_reasons']}."))break- if user_terminated:+ if user_response.terminated:print(Fore.GREEN +("AI User terminated. "- f"Reason: {user_info['termination_reasons']}."))+ f"Reason: {user_response.info['termination_reasons']}."))break- print_text_animated(Fore.BLUE + f"AI User:\n\n{user_msg.content}\n")+ print_text_animated(Fore.BLUE ++ f"AI User:\n\n{user_response.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:+ if "CAMEL_TASK_DONE" in user_response.msg.content:breakdiff --git a/examples/ai_society/role_playing_with_human.py b/examples/ai_society/role_playing_with_human.pyindex 44d1f2589..b46518d10 100644--- a/examples/ai_society/role_playing_with_human.py+++ b/examples/ai_society/role_playing_with_human.py@@ -50,26 +50,26 @@ def main() -> None: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+ assistant_response, user_response = role_play_session.step(+ assistant_msg)- if assistant_terminated:+ if assistant_response.terminated:print(Fore.GREEN +- ("AI Assistant terminated. "- f"Reason: {assistant_info['termination_reasons']}."))+ ("AI Assistant terminated. Reason: "+ f"{assistant_response.info['termination_reasons']}."))break- if user_terminated:+ if user_response.terminated:print(Fore.GREEN +("AI User terminated. "- f"Reason: {user_info['termination_reasons']}."))+ f"Reason: {user_response.info['termination_reasons']}."))break- print_text_animated(Fore.BLUE + f"AI User:\n\n{user_msg.content}\n")+ print_text_animated(Fore.BLUE ++ f"AI User:\n\n{user_response.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:+ if "CAMEL_TASK_DONE" in user_response.msg.content:breakdiff --git a/examples/ai_society/task_generation.py b/examples/ai_society/task_generation.pyindex e31e39f52..ff2ee3084 100644--- a/examples/ai_society/task_generation.py+++ b/examples/ai_society/task_generation.py@@ -34,14 +34,12 @@ def generate_tasks(role_names: str, task_generator_prompt: str,user_msg = UserChatMessage(role_name="Task Generator",content=task_generator_prompt)- assistant_msgs, assistant_terminated, _ = assistant_agent.step(user_msg)+ assistant_response = assistant_agent.step(user_msg)- if assistant_terminated or assistant_msgs is None:+ if assistant_response.terminated or len(assistant_response.msgs) == 0:raise RuntimeError("Assistant agent terminated unexpectedly.")- assistant_msg = assistant_msgs[0]-- tasks = assistant_msg.content.split("\n")+ tasks = assistant_response.msg.content.split("\n")# Filter out the generated response to include the tasks onlyfor i, task in enumerate(tasks):diff --git a/examples/code/generate_meta_data.py b/examples/code/generate_meta_data.pyindex e182b71e2..50489a2c9 100644--- a/examples/code/generate_meta_data.py+++ b/examples/code/generate_meta_data.py@@ -33,10 +33,14 @@ def generate_meta_data(meta_data: str, num: int = 50):role_name="User",content=prompt,)- assistant_msg, _, _ = agent.step(user_msg)- print(assistant_msg[0].content)+ assistant_response = agent.step(user_msg)+ print(assistant_response.msg.content)-if __name__ == "__main__":+def main():generate_meta_data("languages", 20)generate_meta_data("domains", 50)+++if __name__ == "__main__":+ main()diff --git a/examples/code/role_playing_multiprocess.py b/examples/code/role_playing_multiprocess.pyindex 155ca141d..9110646d3 100644--- a/examples/code/role_playing_multiprocess.py+++ b/examples/code/role_playing_multiprocess.py@@ -46,9 +46,9 @@ def init_chat(user_msg = UserChatMessage(role_name=user_agent.role_name,content=f"{assistant_sys_msg.content}")- msgs, _, _ = assistant_agent.step(user_msg)+ assistant_response = assistant_agent.step(user_msg)- return assistant_msg, msgs+ return assistant_msg, assistant_response.msgsdef generate_data(language_idx: int, language_name: str, domain_idx: int,@@ -125,36 +125,33 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,while message_counter < max_num_messages:- user_msgs, user_terminated, user_info = user_agent.step(- assistant_msg.to_user_chat_message())+ user_response = user_agent.step(assistant_msg.to_user_chat_message())# Condition 1: User terminates the chat- if user_terminated:+ if user_response.terminated:message_dict["termination_reason"] = (f"{str(user_agent.role_type)}: "- f"{user_info['termination_reasons'][0]}")+ f"{user_response.info['termination_reasons'][0]}")break- user_msg = user_msgs[0]- user_agent.update_messages(user_msg)- print(f"User:\n{user_msg.content}\n")+ user_agent.update_messages(user_response.msg)+ print(f"User:\n{user_response.msg.content}\n")- assistant_msgs, assistant_terminated, assistant_info = (- assistant_agent.step(user_msg.to_user_chat_message()))+ assistant_response = assistant_agent.step(+ user_response.msg.to_user_chat_message())# Condition 2: Assistant terminates the chat- if assistant_terminated:+ if assistant_response.terminated:message_dict["termination_reason"] = (f"{str(assistant_agent.role_type)}: "- f"{assistant_info['termination_reasons'][0]}")+ f"{assistant_response.info['termination_reasons'][0]}")break- assistant_msg = assistant_msgs[0]- assistant_agent.update_messages(assistant_msg)+ assistant_agent.update_messages(assistant_response.msg)print(f"Assistant:\n{assistant_msg.content}\n")# Condition 3: Break if user does not give instruction- if user_no_instruct_word not in user_msg.content:+ if user_no_instruct_word not in user_response.msg.content:user_no_instruct_counter += 1if user_no_instruct_counter == user_no_instruct_threshold:message_dict[@@ -175,7 +172,7 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,# Condition 5: Repeat word observedfor repeat_word in repeat_word_list:- if repeat_word in user_msg.content.lower(+ if repeat_word in user_response.msg.content.lower() or repeat_word in assistant_msg.content.lower():repeat_word_counter += 1if repeat_word_counter == repeat_word_threshold:@@ -187,10 +184,11 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,# Save user messagemessage_counter += 1- message_dict[f"message_{message_counter}"] = user_msg.to_dict()+ message_dict[f"message_{message_counter}"] = user_response.msg.to_dict(+ )# Condition 5: End token observed- if "<CAMEL_TASK_DONE>" in user_msg.content:+ if "<CAMEL_TASK_DONE>" in user_response.msg.content:message_dict['termination_reason'] = "<CAMEL_TASK_DONE>"break@@ -223,7 +221,10 @@ def main() -> None:# Chunk for parallel jobstry:- array_idx = int(os.environ.get('SLURM_ARRAY_TASK_ID'))+ slurm_array_task_id = os.environ.get('SLURM_ARRAY_TASK_ID')+ if not isinstance(slurm_array_task_id, str):+ raise TypeError()+ array_idx = int(slurm_array_task_id)except (TypeError, ValueError) as e:print(f"Error: {e}")array_idx = 0diff --git a/examples/code/task_generation.py b/examples/code/task_generation.pyindex 64baca5f6..ceb414fb6 100644--- a/examples/code/task_generation.py+++ b/examples/code/task_generation.py@@ -30,10 +30,9 @@ def generate_tasks(task_generator_prompt: str, language: str, domain: str,user_msg = UserChatMessage(role_name="Task Generator",content=task_generator_prompt)- assistant_msgs, _, _ = assistant_agent.step(user_msg)- assistant_msg = assistant_msgs[0]+ assistant_response = assistant_agent.step(user_msg)- tasks = assistant_msg.content.split("\n")+ tasks = assistant_response.msg.content.split("\n")# Filter out the generated response to include the tasks onlyfor i, task in enumerate(tasks):diff --git a/examples/embodiment/hugging_face_tool.py b/examples/embodiment/hugging_face_tool.pyindex 60d0e4da7..d67d9a050 100644--- a/examples/embodiment/hugging_face_tool.py+++ b/examples/embodiment/hugging_face_tool.py@@ -11,7 +11,10 @@# See the License for the specific language governing permissions and# limitations under the License.# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+from typing import List+from camel.agents import EmbodiedAgent, HuggingFaceToolAgent+from camel.agents.tool_agents.base import BaseToolAgentfrom camel.generators import SystemMessageGeneratorfrom camel.messages import UserChatMessagefrom camel.typing import ModelType, RoleType@@ -31,6 +34,7 @@ def main():remote=True,)]+ action_space: List[BaseToolAgent]embodied_agent = EmbodiedAgent(sys_msg,verbose=True,diff --git a/examples/evaluation/single_agent.py b/examples/evaluation/single_agent.pyindex 8d16efe07..4b0a38323 100644--- a/examples/evaluation/single_agent.py+++ b/examples/evaluation/single_agent.py@@ -59,14 +59,14 @@ def generate_questions(examples: str, category: str, save_file_name: str,agent.reset()user_msg = UserChatMessage(role_name="User", content=prompt)- assistant_msg, _, _ = agent.step(user_msg)+ assistant_response = agent.step(user_msg)- if assistant_msg is not None:+ if len(assistant_response.msgs) > 0:- print(assistant_msg[0].content)+ print(assistant_response.msg.content)- parsed_assistant_msg = parse_question_string(assistant_msg[0].content,- category)+ parsed_assistant_msg = parse_question_string(+ assistant_response.msg.content, category)# save json filefolder_path = './evaluation_data/questions'diff --git a/examples/misalignment/role_playing_multiprocess.py b/examples/misalignment/role_playing_multiprocess.pyindex 9a6811977..bcd1060a8 100644--- a/examples/misalignment/role_playing_multiprocess.py+++ b/examples/misalignment/role_playing_multiprocess.py@@ -91,30 +91,29 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,while message_counter < max_num_messages:- 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+ assistant_response, user_response = role_play_session.step(+ assistant_msg)# Condition 1: User terminates the chat- if user_terminated:+ if user_response.terminated:message_dict["termination_reason"] = (f"{str(user_agent.role_type)}: "- f"{user_info['termination_reasons'][0]}")+ f"{user_response.info['termination_reasons'][0]}")break# Condition 2: Assistant terminates the chat- if assistant_terminated:+ if assistant_response.terminated:message_dict["termination_reason"] = (f"{str(assistant_agent.role_type)}: "- f"{assistant_info['termination_reasons'][0]}")+ f"{assistant_response.info['termination_reasons'][0]}")breakif verbose:- print(f"User:\n{user_msg.content}\n")+ print(f"User:\n{user_response.msg.content}\n")print(f"Assistant:\n{assistant_msg.content}\n")# Condition 3: Break if user does not give instruction- if user_no_instruct_word not in user_msg.content:+ if user_no_instruct_word not in user_response.msg.content:user_no_instruct_counter += 1if user_no_instruct_counter == user_no_instruct_threshold:message_dict[@@ -135,7 +134,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,# Condition 5: Repeat word observedfor repeat_word in repeat_word_list:- if repeat_word in user_msg.content.lower(+ if repeat_word in user_response.msg.content.lower() or repeat_word in assistant_msg.content.lower():repeat_word_counter += 1if repeat_word_counter == repeat_word_threshold:@@ -147,10 +146,11 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,# Save user messagemessage_counter += 1- message_dict[f"message_{message_counter}"] = user_msg.to_dict()+ message_dict[f"message_{message_counter}"] = user_response.msg.to_dict(+ )# Condition 5: End token observed- if "<CAMEL_TASK_DONE>" in user_msg.content:+ if "<CAMEL_TASK_DONE>" in user_response.msg.content:message_dict['termination_reason'] = "<CAMEL_TASK_DONE>"breakdiff --git a/examples/misalignment/role_playing_with_human.py b/examples/misalignment/role_playing_with_human.pyindex 6556fbf7f..c080526db 100644--- a/examples/misalignment/role_playing_with_human.py+++ b/examples/misalignment/role_playing_with_human.py@@ -54,25 +54,24 @@ def main() -> None:while n < chat_turn_limit:n += 1assistant_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:+ if assistant_return.terminated:print(Fore.GREEN +("AI Assistant terminated. "- f"Reason: {assistant_info['termination_reasons']}."))+ f"Reason: {assistant_return.info['termination_reasons']}."))break- if user_terminated:+ if user_return.terminated:print(Fore.GREEN +("AI User terminated. "- f"Reason: {user_info['termination_reasons']}."))+ f"Reason: {user_return.info['termination_reasons']}."))break- print_text_animated(Fore.BLUE + f"AI User:\n\n{user_msg.content}\n")+ print_text_animated(Fore.BLUE ++ f"AI User:\n\n{user_return.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:+ if "CAMEL_TASK_DONE" in user_return.msg.content:breakdiff --git a/examples/misalignment/single_agent.py b/examples/misalignment/single_agent.pyindex 2ca829938..3cec461ed 100644--- a/examples/misalignment/single_agent.py+++ b/examples/misalignment/single_agent.py@@ -30,8 +30,8 @@ def main() -> None:agent.reset()user_msg = UserChatMessage(role_name="User", content=prompt)- assistant_msg, _, _ = agent.step(user_msg)- print(assistant_msg[0].content)+ assistant_response = agent.step(user_msg)+ print(assistant_response.msg.content)if __name__ == "__main__":diff --git a/examples/misalignment/task_generation.py b/examples/misalignment/task_generation.pyindex 6a3aea19c..a6c1b26be 100644--- a/examples/misalignment/task_generation.py+++ b/examples/misalignment/task_generation.py@@ -38,10 +38,9 @@ def generate_tasks(role_names: str, task_generator_prompt: str,user_msg = UserChatMessage(role_name="Task Generator",content=task_generator_prompt)- assistant_msgs, _, _ = assistant_agent.step(user_msg)- assistant_msg = assistant_msgs[0]+ assistant_response = assistant_agent.step(user_msg)- tasks = assistant_msg.content.split("\n")+ tasks = assistant_response.msg.content.split("\n")# Filter out the generated response to include the tasks onlyfor i, task in enumerate(tasks):diff --git a/examples/single_agent.py b/examples/single_agent.pyindex 6afe043e2..dfc1e2453 100644--- a/examples/single_agent.py+++ b/examples/single_agent.py@@ -30,8 +30,8 @@ def main(key: str = 'generate_users', num_roles: int = 50):agent.reset()user_msg = UserChatMessage(role_name="User", content=prompt)- assistant_msg, _, _ = agent.step(user_msg)- print(assistant_msg[0].content)+ assistant_response = agent.step(user_msg)+ print(assistant_response.msg.content)if __name__ == "__main__":diff --git a/examples/summarization/gpt_solution_extraction.py b/examples/summarization/gpt_solution_extraction.pyindex 8dcc60468..3b7e5a4d1 100644--- a/examples/summarization/gpt_solution_extraction.py+++ b/examples/summarization/gpt_solution_extraction.py@@ -111,8 +111,8 @@ def solution_extraction(conversation: Dict, flattened_conversation: str,prompt = "Here is the conversation:" + flattened_conversationuser_msg = UserChatMessage(role_name="User", content=prompt)- assistant_msg, _, _ = agent.step(user_msg)- print(assistant_msg[0].content)+ assistant_response = agent.step(user_msg)+ print(assistant_response.msg.content)# Create folder to write solution_extraction toif not os.path.exists(@@ -120,7 +120,7 @@ def solution_extraction(conversation: Dict, flattened_conversation: str,os.makedirs("../camel/camel_data/ai_society_solution_extraction")# Append to the original JSON conversation file- conversation['solution_extraction'] = assistant_msg[0].content+ conversation['solution_extraction'] = assistant_response.msg.content# Save new dictionary as JSON filewith open(f"./camel_data/ai_society_solution_extraction/{file_name}.json",diff --git a/examples/summarization/gpt_solver.py b/examples/summarization/gpt_solver.pyindex 7051e6676..ede675bf6 100644--- a/examples/summarization/gpt_solver.py+++ b/examples/summarization/gpt_solver.py@@ -39,11 +39,11 @@ def process_file(data: Dict[str, str]) -> None:prompt = "Solve the following task:\n" + data["specified_task"]user_msg = UserChatMessage(role_name="User", content=prompt)- assistant_msg, _, _ = agent.step(user_msg)- print(assistant_msg[0].content)+ assistant_response = agent.step(user_msg)+ print(assistant_response.msg.content)# Append solution to JSON file as "gpt_solution"- data["gpt_solution"] = assistant_msg[0].content+ data["gpt_solution"] = assistant_response.msg.content# create save_dir if not existsif not os.path.exists(save_dir):diff --git a/examples/test/test_single_agent.py b/examples/test/test_single_agent.pynew file mode 100644index 000000000..ea3967072--- /dev/null+++ b/examples/test/test_single_agent.py@@ -0,0 +1,30 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the “License”);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an “AS IS” BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+import unittest++import examples.evaluation.single_agent+import examples.misalignment.single_agent+import examples.single_agent+++class TestSingleAgent(unittest.TestCase):++ def test_single_agent(self):+ examples.single_agent.main()++ def test_misalignment_single_agent(self):+ examples.misalignment.single_agent.main()++ def test_evaluation_single_agent(self):+ examples.evaluation.single_agent.main()diff --git a/examples/translation/translator.py b/examples/translation/translator.pyindex f470b75d1..5211ef0c1 100644--- a/examples/translation/translator.py+++ b/examples/translation/translator.py@@ -55,8 +55,8 @@ def translate_content(directory_path: str, file_path: str,user_msg = UserChatMessage(role_name="Language Translator",content=msg_i_content)- assistant_msgs, _, _ = assistant_agent.step(user_msg)- assistant_msg = assistant_msgs[0]+ assistant_response = assistant_agent.step(user_msg)+ assistant_msg = assistant_response.msgjson_data[f"message_{i+1}"]["content"] = assistant_msg.contentdiff --git a/pyproject.toml b/pyproject.tomlindex 36ca5d3c1..d8a9b5094 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -62,3 +62,6 @@ markers = ["full_test_only: mark a test to run only in full test mode","slow: mark a test as slow",]++[tool.coverage.report]+include_namespace_packages = truediff --git a/test/agents/test_chat_agent.py b/test/agents/test_chat_agent.pyindex 07b11cfad..f45116fe1 100644--- a/test/agents/test_chat_agent.py+++ b/test/agents/test_chat_agent.py@@ -44,22 +44,29 @@ def test_chat_agent(model):assistant.reset()user_msg = ChatMessage(role_name="Patient", role_type=RoleType.USER,meta_dict=dict(), role="user", content="Hello!")- msgs, terminated, info = assistant.step(user_msg)+ assistant_response = assistant.step(user_msg)- assert terminated is False- assert msgs is not None- assert info['id'] is not None+ assert isinstance(assistant_response.msgs, list)+ assert len(assistant_response.msgs) > 0+ assert isinstance(assistant_response.terminated, bool)+ assert assistant_response.terminated is False+ assert isinstance(assistant_response.info, dict)+ assert assistant_response.info['id'] is not Noneassistant.reset()token_limit = get_model_token_limit(model)user_msg = ChatMessage(role_name="Patient", role_type=RoleType.USER,meta_dict=dict(), role="user",content="token" * (token_limit + 1))- msgs, terminated, info = assistant.step(user_msg)+ assistant_response = assistant.step(user_msg)- assert terminated is True- assert msgs is None- assert info['termination_reasons'][0] == "max_tokens_exceeded"+ assert isinstance(assistant_response.msgs, list)+ assert len(assistant_response.msgs) == 0+ assert isinstance(assistant_response.terminated, bool)+ assert assistant_response.terminated is True+ assert isinstance(assistant_response.info, dict)+ assert (assistant_response.info['termination_reasons'][0] ==+ "max_tokens_exceeded")@pytest.mark.slow@@ -74,5 +81,6 @@ def test_chat_agent_multiple_return_messages(n):user_msg = ChatMessage(role_name="User", role_type=RoleType.USER,meta_dict=dict(), role="user",content="Tell me a joke.")- msgs, _, _ = assistant.step(user_msg)- assert len(msgs) == n+ assistant_response = assistant.step(user_msg)+ assert assistant_response.msgs is not None+ assert len(assistant_response.msgs) == ndiff --git a/test/agents/test_role_playing.py b/test/agents/test_role_playing.pyindex 1b36a1b9f..fd25ddeae 100644--- a/test/agents/test_role_playing.py+++ b/test/agents/test_role_playing.py@@ -75,12 +75,12 @@ def test_role_playing_step(task_type, extend_sys_msg_meta_dicts,print(role_playing.assistant_agent.system_message)print(role_playing.user_agent.system_message)- (assistant_msg, assistant_terminated,- assistant_info), (user_msg, user_terminated,- user_info) = role_playing.step(init_assistant_msg)- assert isinstance(assistant_msg, ChatMessage)- assert assistant_terminated is False- assert assistant_info is not None- assert isinstance(user_msg, ChatMessage)- assert user_terminated is False- assert user_info is not None+ assistant_response, user_response = role_playing.step(init_assistant_msg)++ for response in (assistant_response, user_response):+ assert isinstance(response.msgs, list)+ assert len(response.msgs) == 1+ assert isinstance(response.msgs[0], ChatMessage)+ assert isinstance(response.terminated, bool)+ assert response.terminated is False+ assert isinstance(response.info, dict)