Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

fix duplicate sys msg & consecutive user in ass agent memory (#424)

Junrong Linc7ec055b24de
diff --git a/apps/agents/agents.py b/apps/agents/agents.py
index e1dbd5d12..a425d1366 100644
--- a/apps/agents/agents.py
+++ b/apps/agents/agents.py
@@ -242,14 +242,14 @@ def role_playing_chat_init(state) -> \
session: RolePlaying = state.session
try:
- init_assistant_msg: BaseMessage
- init_assistant_msg, _ = session.init_chat()
+ input_msg: BaseMessage
+ input_msg = session.init_chat()
except (openai.RateLimitError, RuntimeError) as ex:
print("OpenAI API exception 1 " + str(ex))
state.session = None
return state, state.chat, gr.update()
- state.saved_assistant_msg = init_assistant_msg
+ state.saved_assistant_msg = input_msg
progress_update = gr.update(maximum=state.max_messages, value=1,
visible=True)
diff --git a/camel/societies/role_playing.py b/camel/societies/role_playing.py
index 3867b0589..58a8a9430 100644
--- a/camel/societies/role_playing.py
+++ b/camel/societies/role_playing.py
@@ -34,9 +34,9 @@ class RolePlaying:
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.
- 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`.
+ critic_role_name (str, optional): 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:`""`)
@@ -104,44 +104,65 @@ class RolePlaying:
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._init_specified_task_prompt(
+ assistant_role_name,
+ user_role_name,
+ task_specify_agent_kwargs=task_specify_agent_kwargs,
+ extend_task_specify_meta_dict=extend_task_specify_meta_dict,
+ output_language=output_language,
+ )
self.planned_task_prompt: Optional[TextPrompt] = None
- self.init_planned_task_prompt(task_planner_agent_kwargs,
- output_language)
+ self._init_planned_task_prompt(
+ task_planner_agent_kwargs=task_planner_agent_kwargs,
+ output_language=output_language,
+ )
sys_msg_generator = SystemMessageGenerator(
- task_type=self.task_type, **(sys_msg_generator_kwargs or {}))
+ 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)
+ (
+ 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=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(
+ self._init_agents(
init_assistant_sys_msg,
- assistant_agent_kwargs,
init_user_sys_msg,
- user_agent_kwargs,
- output_language,
+ assistant_agent_kwargs=assistant_agent_kwargs,
+ user_agent_kwargs=user_agent_kwargs,
+ output_language=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]):
+ self._init_critic(
+ sys_msg_generator,
+ sys_msg_meta_dicts,
+ critic_role_name,
+ critic_criteria=critic_criteria,
+ critic_kwargs=critic_kwargs,
+ )
+
+ def _init_specified_task_prompt(
+ self,
+ assistant_role_name: str,
+ user_role_name: str,
+ task_specify_agent_kwargs: Optional[Dict] = None,
+ extend_task_specify_meta_dict: Optional[Dict] = None,
+ output_language: Optional[str] = None,
+ ) -> None:
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
@@ -152,11 +173,11 @@ class RolePlaying:
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.
+ to pass to the task specify agent. (default: :obj:`None`)
extend_task_specify_meta_dict (Dict, optional): A dict to extend
- the task specify meta dict with.
+ the task specify meta dict with. (default: :obj:`None`)
output_language (str, optional): The language to be output by the
- agents.
+ agents. (default: :obj:`None`)
"""
if self.with_task_specify:
task_specify_meta_dict = dict()
@@ -181,9 +202,11 @@ class RolePlaying:
)
self.task_prompt = self.specified_task_prompt
- def init_planned_task_prompt(self,
- task_planner_agent_kwargs: Optional[Dict],
- output_language: Optional[str]):
+ def _init_planned_task_prompt(
+ self,
+ task_planner_agent_kwargs: Optional[Dict] = None,
+ output_language: Optional[str] = None,
+ ) -> None:
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
@@ -192,9 +215,9 @@ class RolePlaying:
Args:
task_planner_agent_kwargs (Dict, optional): Additional arguments
- to pass to the task planner agent.
+ to pass to the task planner agent. (default: :obj:`None`)
output_language (str, optional): The language to be output by the
- agents.
+ agents. (default: :obj:`None`)
"""
if self.with_task_planner:
if self.model_type is not None:
@@ -212,7 +235,7 @@ class RolePlaying:
else:
self.planned_task_prompt = None
- def get_sys_message_info(
+ def _get_sys_message_info(
self,
assistant_role_name: str,
user_role_name: str,
@@ -230,11 +253,13 @@ class RolePlaying:
generator for agents.
extend_sys_msg_meta_dicts (List[Dict], optional): A list of dicts
to extend the system message meta dicts with.
+ (default: :obj:`None`)
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.
+ Tuple[BaseMessage, BaseMessage, List[Dict]]: 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_type in [
@@ -263,27 +288,27 @@ class RolePlaying:
))
return init_assistant_sys_msg, init_user_sys_msg, sys_msg_meta_dicts
- def init_agents(
+ 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],
- ):
+ assistant_agent_kwargs: Optional[Dict] = None,
+ user_agent_kwargs: Optional[Dict] = None,
+ output_language: Optional[str] = None,
+ ) -> None:
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.
+ assistant_agent_kwargs (Dict, optional): Additional arguments to
+ pass to the assistant agent. (default: :obj:`None`)
user_agent_kwargs (Dict, optional): Additional arguments to
- pass to the user agent.
+ pass to the user agent. (default: :obj:`None`)
output_language (str, optional): The language to be output by the
- agents.
+ agents. (default: :obj:`None`)
"""
if self.model_type is not None:
if assistant_agent_kwargs is None:
@@ -307,26 +332,29 @@ class RolePlaying:
)
self.user_sys_msg = self.user_agent.system_message
- 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]):
+ def _init_critic(
+ self,
+ sys_msg_generator: SystemMessageGenerator,
+ sys_msg_meta_dicts: List[Dict],
+ critic_role_name: str,
+ critic_criteria: Optional[str] = None,
+ critic_kwargs: Optional[Dict] = None,
+ ) -> None:
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:
+ sys_msg_generator (SystemMessageGenerator): A system message
+ generator for agents.
+ sys_msg_meta_dicts (list): A list of system message meta dicts.
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.
+ improve task performance. (default: :obj:`None`)
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.
+ pass to the critic. (default: :obj:`None`)
"""
if self.with_critic_in_the_loop:
if critic_role_name.lower() == "human":
@@ -350,37 +378,7 @@ class RolePlaying:
**(critic_kwargs or {}),
)
- def init_chat(self) -> Tuple[BaseMessage, List[BaseMessage]]:
- r"""Initializes the chat by resetting both of the assistant and user
- agents, and sending the system messages again to the agents using
- chat messages. Returns the assistant's introductory message and the
- user's response messages.
-
- Returns:
- 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()
-
- # Send the system messages again to the agents using chat messages
- assistant_msg = BaseMessage.make_assistant_message(
- role_name=self.assistant_sys_msg.role_name,
- content=(f"{self.user_sys_msg.content}. "
- "Now start to give me instructions one by one. "
- "Only reply with Instruction and Input."))
- user_msg = BaseMessage.make_user_message(
- role_name=self.user_sys_msg.role_name,
- content=f"{self.assistant_sys_msg.content}")
- 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, assistant_response.msgs
-
- def reduce_message_options(
+ def _reduce_message_options(
self,
messages: Sequence[BaseMessage],
) -> BaseMessage:
@@ -390,10 +388,12 @@ class RolePlaying:
If no messages are provided, a `ValueError` will be raised.
Args:
- messages: A sequence of `BaseMessage` objects to process.
+ messages (Sequence[BaseMessage]): A sequence of `BaseMessage`
+ objects to process.
Returns:
- A single `BaseMessage` representing the processed message.
+ BaseMessage: A single `BaseMessage` representing the processed
+ message.
"""
if len(messages) == 0:
raise ValueError("No messages to process.")
@@ -408,6 +408,34 @@ class RolePlaying:
return processed_msg
+ def init_chat(self, init_msg_content: Optional[str] = None) -> BaseMessage:
+ r"""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.
+ """
+ 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=self.assistant_sys_msg.role_name,
+ content=init_msg_content,
+ )
+
+ return init_msg
+
def step(
self,
assistant_msg: BaseMessage,
@@ -425,19 +453,20 @@ class RolePlaying:
assistant.
Returns:
- 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.
+ 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 = self.user_agent.step(assistant_msg)
if user_response.terminated or user_response.msgs is None:
return (ChatAgentResponse([], False, {}),
ChatAgentResponse([], user_response.terminated,
user_response.info))
- user_msg = self.reduce_message_options(user_response.msgs)
+ user_msg = self._reduce_message_options(user_response.msgs)
self.user_agent.record_message(user_msg)
assistant_response = self.assistant_agent.step(user_msg)
@@ -445,7 +474,7 @@ class RolePlaying:
return (ChatAgentResponse([], assistant_response.terminated,
assistant_response.info),
ChatAgentResponse([user_msg], False, user_response.info))
- assistant_msg = self.reduce_message_options(assistant_response.msgs)
+ assistant_msg = self._reduce_message_options(assistant_response.msgs)
self.assistant_agent.record_message(assistant_msg)
return (
diff --git a/examples/ai_society/role_playing.py b/examples/ai_society/role_playing.py
index b08880249..e9bf551e0 100644
--- a/examples/ai_society/role_playing.py
+++ b/examples/ai_society/role_playing.py
@@ -42,11 +42,10 @@ def main(model_type=None, chat_turn_limit=50) -> None:
print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
n = 0
- input_assistant_msg, _ = role_play_session.init_chat()
+ input_msg = role_play_session.init_chat()
while n < chat_turn_limit:
n += 1
- assistant_response, user_response = role_play_session.step(
- input_assistant_msg)
+ assistant_response, user_response = role_play_session.step(input_msg)
if assistant_response.terminated:
print(Fore.GREEN +
@@ -67,7 +66,7 @@ def main(model_type=None, chat_turn_limit=50) -> None:
if "CAMEL_TASK_DONE" in user_response.msg.content:
break
- input_assistant_msg = assistant_response.msg
+ input_msg = assistant_response.msg
if __name__ == "__main__":
diff --git a/examples/ai_society/role_playing_multi_lingual.py b/examples/ai_society/role_playing_multi_lingual.py
index f66d0997a..9a54250a6 100644
--- a/examples/ai_society/role_playing_multi_lingual.py
+++ b/examples/ai_society/role_playing_multi_lingual.py
@@ -43,11 +43,10 @@ def main(model_type=None) -> None:
print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
chat_turn_limit, n = 50, 0
- input_assistant_msg, _ = role_play_session.init_chat()
+ input_msg = role_play_session.init_chat()
while n < chat_turn_limit:
n += 1
- assistant_response, user_response = role_play_session.step(
- input_assistant_msg)
+ assistant_response, user_response = role_play_session.step(input_msg)
if assistant_response.terminated:
print(Fore.GREEN +
@@ -68,7 +67,7 @@ def main(model_type=None) -> None:
if "CAMEL_TASK_DONE" in user_response.msg.content:
break
- input_assistant_msg = assistant_response.msg
+ input_msg = assistant_response.msg
if __name__ == "__main__":
diff --git a/examples/ai_society/role_playing_multiprocess.py b/examples/ai_society/role_playing_multiprocess.py
index 40240e04f..4e709bfb2 100644
--- a/examples/ai_society/role_playing_multiprocess.py
+++ b/examples/ai_society/role_playing_multiprocess.py
@@ -43,7 +43,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
temperature=1.4)),
)
- input_assistant_msg, _ = role_play_session.init_chat()
+ input_msg = role_play_session.init_chat()
if verbose:
print(Fore.GREEN + "AI Assistant sys message:\n"
@@ -93,8 +93,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
while message_counter < max_num_messages:
- assistant_response, user_response = role_play_session.step(
- input_assistant_msg)
+ assistant_response, user_response = role_play_session.step(input_msg)
# Condition 1: User terminates the chat
if user_response.terminated and user_response.info is not None:
@@ -165,7 +164,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
message_dict[
f"message_{message_counter}"] = assistant_response.msg.to_dict()
- input_assistant_msg = assistant_response.msg
+ input_msg = assistant_response.msg
message_dict["num_messages"] = message_counter
diff --git a/examples/ai_society/role_playing_with_critic.py b/examples/ai_society/role_playing_with_critic.py
index 6fd0ece82..507dedbb3 100644
--- a/examples/ai_society/role_playing_with_critic.py
+++ b/examples/ai_society/role_playing_with_critic.py
@@ -51,11 +51,10 @@ def main() -> None:
print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
chat_turn_limit, n = 50, 0
- input_assistant_msg, _ = role_play_session.init_chat()
+ input_msg = role_play_session.init_chat()
while n < chat_turn_limit:
n += 1
- assistant_response, user_response = role_play_session.step(
- input_assistant_msg)
+ assistant_response, user_response = role_play_session.step(input_msg)
if assistant_response.terminated:
print(Fore.GREEN +
@@ -77,7 +76,7 @@ def main() -> None:
if "CAMEL_TASK_DONE" in user_response.msg.content:
break
- input_assistant_msg = assistant_response.msg
+ input_msg = assistant_response.msg
if __name__ == "__main__":
diff --git a/examples/ai_society/role_playing_with_human.py b/examples/ai_society/role_playing_with_human.py
index 2da9b9988..667875ec8 100644
--- a/examples/ai_society/role_playing_with_human.py
+++ b/examples/ai_society/role_playing_with_human.py
@@ -47,11 +47,10 @@ def main() -> None:
print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
chat_turn_limit, n = 50, 0
- input_assistant_msg, _ = role_play_session.init_chat()
+ input_msg = role_play_session.init_chat()
while n < chat_turn_limit:
n += 1
- assistant_response, user_response = role_play_session.step(
- input_assistant_msg)
+ assistant_response, user_response = role_play_session.step(input_msg)
if assistant_response.terminated:
print(Fore.GREEN +
@@ -73,7 +72,7 @@ def main() -> None:
if "CAMEL_TASK_DONE" in user_response.msg.content:
break
- input_assistant_msg = assistant_response.msg
+ input_msg = assistant_response.msg
if __name__ == "__main__":
diff --git a/examples/code/role_playing.py b/examples/code/role_playing.py
index 67d621d4d..5b153a357 100644
--- a/examples/code/role_playing.py
+++ b/examples/code/role_playing.py
@@ -49,11 +49,10 @@ def main(model_type=None, chat_turn_limit=50) -> None:
print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
n = 0
- input_assistant_msg, _ = role_play_session.init_chat()
+ input_msg = role_play_session.init_chat()
while n < chat_turn_limit:
n += 1
- assistant_response, user_response = role_play_session.step(
- input_assistant_msg)
+ assistant_response, user_response = role_play_session.step(input_msg)
if assistant_response.terminated:
print(Fore.GREEN +
@@ -74,7 +73,7 @@ def main(model_type=None, chat_turn_limit=50) -> None:
if "CAMEL_TASK_DONE" in user_response.msg.content:
break
- input_assistant_msg = assistant_response.msg
+ input_msg = assistant_response.msg
if __name__ == "__main__":
diff --git a/examples/code/role_playing_multiprocess.py b/examples/code/role_playing_multiprocess.py
index 35384eb36..759f37ea4 100644
--- a/examples/code/role_playing_multiprocess.py
+++ b/examples/code/role_playing_multiprocess.py
@@ -42,9 +42,9 @@ def init_chat(
user_msg = BaseMessage.make_user_message(
role_name=user_agent.role_name, content=f"{assistant_sys_msg.content}")
- assistant_response = assistant_agent.step(user_msg)
+ assistant_agent.step(user_msg)
- return assistant_msg, assistant_response.msgs
+ return assistant_msg
def generate_data(language_idx: int, language_name: str, domain_idx: int,
@@ -84,8 +84,8 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,
message_window_size=max_num_messages)
user_agent = ChatAgent(user_sys_msg, message_window_size=max_num_messages)
- input_assistant_msg, _ = init_chat(assistant_agent, user_agent,
- user_sys_msg, assistant_sys_msg)
+ input_assistant_msg = init_chat(assistant_agent, user_agent, user_sys_msg,
+ assistant_sys_msg)
print("Assistant System Message: ", assistant_sys_msg.content)
print("User System Message: ", user_sys_msg.content)
diff --git a/examples/function_call/role_playing_with_function.py b/examples/function_call/role_playing_with_function.py
index cca0cf3f2..a21c78331 100644
--- a/examples/function_call/role_playing_with_function.py
+++ b/examples/function_call/role_playing_with_function.py
@@ -67,11 +67,10 @@ def main(model_type=ModelType.GPT_4, chat_turn_limit=10) -> None:
print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
n = 0
- input_assistant_msg, _ = role_play_session.init_chat()
+ input_msg = role_play_session.init_chat()
while n < chat_turn_limit:
n += 1
- assistant_response, user_response = role_play_session.step(
- input_assistant_msg)
+ assistant_response, user_response = role_play_session.step(input_msg)
if assistant_response.terminated:
print(Fore.GREEN +
@@ -101,7 +100,7 @@ def main(model_type=ModelType.GPT_4, chat_turn_limit=10) -> None:
if "CAMEL_TASK_DONE" in user_response.msg.content:
break
- input_assistant_msg = assistant_response.msg
+ input_msg = assistant_response.msg
if __name__ == "__main__":
diff --git a/examples/misalignment/role_playing_multiprocess.py b/examples/misalignment/role_playing_multiprocess.py
index 43a33cc83..8fe8691e6 100644
--- a/examples/misalignment/role_playing_multiprocess.py
+++ b/examples/misalignment/role_playing_multiprocess.py
@@ -42,7 +42,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
temperature=1.4)),
)
- input_assistant_msg, _ = role_play_session.init_chat()
+ input_msg = role_play_session.init_chat()
if verbose:
print(Fore.GREEN + "AI Assistant sys message:\n"
@@ -92,8 +92,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
while message_counter < max_num_messages:
- assistant_response, user_response = role_play_session.step(
- input_assistant_msg)
+ assistant_response, user_response = role_play_session.step(input_msg)
# Condition 1: User terminates the chat
if user_response.terminated:
@@ -160,7 +159,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
message_dict[
f"message_{message_counter}"] = assistant_response.msg.to_dict()
- input_assistant_msg = assistant_response.msg
+ input_msg = assistant_response.msg
message_dict["num_messages"] = message_counter
diff --git a/examples/misalignment/role_playing_with_human.py b/examples/misalignment/role_playing_with_human.py
index 9150a0def..3b62c7c4e 100644
--- a/examples/misalignment/role_playing_with_human.py
+++ b/examples/misalignment/role_playing_with_human.py
@@ -50,11 +50,10 @@ def main() -> None:
print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
chat_turn_limit, n = 50, 0
- input_assistant_msg, _ = role_play_session.init_chat()
+ input_msg = role_play_session.init_chat()
while n < chat_turn_limit:
n += 1
- assistant_response, user_response = role_play_session.step(
- input_assistant_msg)
+ assistant_response, user_response = role_play_session.step(input_msg)
if assistant_response.terminated:
print(Fore.GREEN + (
@@ -76,7 +75,7 @@ def main() -> None:
if "CAMEL_TASK_DONE" in user_response.msg.content:
break
- input_assistant_msg = assistant_response.msg
+ input_msg = assistant_response.msg
if __name__ == "__main__":
diff --git a/examples/open_source_models/role_playing_with_open_source_model.py b/examples/open_source_models/role_playing_with_open_source_model.py
index f6b227c84..c268f4538 100644
--- a/examples/open_source_models/role_playing_with_open_source_model.py
+++ b/examples/open_source_models/role_playing_with_open_source_model.py
@@ -58,11 +58,10 @@ def main(model_type=None, chat_turn_limit=50, model_path=" ",
print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
n = 0
- input_assistant_msg, _ = role_play_session.init_chat()
+ input_msg = role_play_session.init_chat()
while n < chat_turn_limit:
n += 1
- assistant_response, user_response = role_play_session.step(
- input_assistant_msg)
+ assistant_response, user_response = role_play_session.step(input_msg)
if assistant_response.terminated:
print(Fore.GREEN +
@@ -83,7 +82,7 @@ def main(model_type=None, chat_turn_limit=50, model_path=" ",
if "CAMEL_TASK_DONE" in user_response.msg.content:
break
- input_assistant_msg = assistant_response.msg
+ input_msg = assistant_response.msg
if __name__ == "__main__":
diff --git a/examples/role_description/role_playing_with_role_description.py b/examples/role_description/role_playing_with_role_description.py
index 2905b33a0..b581bc184 100644
--- a/examples/role_description/role_playing_with_role_description.py
+++ b/examples/role_description/role_playing_with_role_description.py
@@ -77,11 +77,10 @@ def main(model_type_for_role_generation=None, model_type=None,
print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
n = 0
- input_assistant_msg, _ = role_play_session.init_chat()
+ input_msg = role_play_session.init_chat()
while n < chat_turn_limit:
n += 1
- assistant_response, user_response = role_play_session.step(
- input_assistant_msg)
+ assistant_response, user_response = role_play_session.step(input_msg)
if assistant_response.terminated:
print(Fore.GREEN + (
@@ -104,7 +103,7 @@ def main(model_type_for_role_generation=None, model_type=None,
if "CAMEL_TASK_DONE" in user_response.msg.content:
break
- input_assistant_msg = assistant_response.msg
+ input_msg = assistant_response.msg
if __name__ == "__main__":
diff --git a/test/agents/test_role_playing.py b/test/agents/test_role_playing.py
index 679a7cd89..ef1412d3e 100644
--- a/test/agents/test_role_playing.py
+++ b/test/agents/test_role_playing.py
@@ -137,8 +137,8 @@ def test_role_playing_with_function():
task_type=TaskType.AI_SOCIETY,
)
- init_assistant_msg, _ = role_playing.init_chat()
- assistant_response, user_response = role_playing.step(init_assistant_msg)
+ input_msg = role_playing.init_chat()
+ 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
@@ -162,10 +162,10 @@ def test_role_playing_role_sequence(model_type=None):
assistant_role_sequence = []
user_role_sequence = []
- input_assistant_msg, _ = role_playing.init_chat()
- assistant_response, user_response = role_playing.step(input_assistant_msg)
- input_assistant_msg = assistant_response.msg
- assistant_response, user_response = role_playing.step(input_assistant_msg)
+ input_msg = role_playing.init_chat()
+ assistant_response, user_response = role_playing.step(input_msg)
+ input_msg = assistant_response.msg
+ assistant_response, user_response = role_playing.step(input_msg)
for record in role_playing.user_agent.memory.get_context()[0]:
user_role_sequence.append(record["role"])
@@ -176,5 +176,5 @@ def test_role_playing_role_sequence(model_type=None):
'system', 'user', 'assistant', 'user', 'assistant'
]
assert assistant_role_sequence == [
- 'system', 'user', 'user', 'assistant', 'user', 'assistant'
+ 'system', 'user', 'assistant', 'user', 'assistant'
]