🐫 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
Fix roleplay API changes in examples and apps (#162)
Dmitrii Khizbullin2e3c771a62e0
diff --git a/apps/agents/agents.py b/apps/agents/agents.pyindex 9a872607c..f96579502 100644--- a/apps/agents/agents.py+++ b/apps/agents/agents.py@@ -214,15 +214,15 @@ def role_playing_chat_init(state) -> \session: RolePlaying = state.sessiontry:- assistant_msg, _ = session.init_chat()- assistant_msg: AssistantChatMessage+ init_assistant_msg, _ = session.init_chat()+ init_assistant_msg: AssistantChatMessageexcept (openai.error.RateLimitError, tenacity.RetryError,RuntimeError) as ex:print("OpenAI API exception 1 " + str(ex))state.session = Nonereturn state, state.chat, gr.update()- state.saved_assistant_msg = assistant_msg+ state.saved_assistant_msg = init_assistant_msgprogress_update = gr.update(maximum=state.max_messages, value=1,visible=True)@@ -256,18 +256,19 @@ def role_playing_chat_cont(state) -> \return state, state.chat, gr.update(), gr.update()try:- assistant_msg, user_msg = session.step(state.saved_assistant_msg)+ assistant_response, user_response = 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()- if len(user_msg.msgs) != 1 or len(assistant_msg.msgs) != 1:+ if len(user_response.msgs) != 1 or len(assistant_response.msgs) != 1:return state, state.chat, gr.update(), gr.update()- u_msg = user_msg.msgs[0]- a_msg = assistant_msg.msgs[0]+ u_msg = user_response.msg+ a_msg = assistant_response.msgstate.saved_assistant_msg = a_msgdiff --git a/examples/ai_society/role_playing.py b/examples/ai_society/role_playing.pyindex 0e7738b4c..54363cf2e 100644--- a/examples/ai_society/role_playing.py+++ b/examples/ai_society/role_playing.py@@ -39,11 +39,13 @@ def main() -> None: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()+ input_assistant_msg, _ = role_play_session.init_chat()while n < chat_turn_limit:n += 1assistant_response, user_response = role_play_session.step(- assistant_msg)+ input_assistant_msg)++ input_assistant_msg = assistant_response.msgif assistant_response.terminated:print(Fore.GREEN +@@ -58,8 +60,8 @@ def main() -> None: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")+ print_text_animated(Fore.GREEN + "AI Assistant:\n\n"+ f"{assistant_response.msg.content}\n")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 b5441fa34..4ab1282d6 100644--- a/examples/ai_society/role_playing_multiprocess.py+++ b/examples/ai_society/role_playing_multiprocess.py@@ -42,7 +42,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,temperature=1.4)),)- assistant_msg, _ = role_play_session.init_chat()+ input_assistant_msg, _ = role_play_session.init_chat()if verbose:print(Fore.GREEN + "AI Assistant sys message:\n"@@ -93,7 +93,9 @@ 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(- assistant_msg)+ input_assistant_msg)++ input_assistant_msg = assistant_response.msg# Condition 1: User terminates the chatif user_response.terminated and user_response.info is not None:@@ -110,11 +112,12 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,f"{assistant_response.info['termination_reasons'][0]}")break- assert (user_response.msg is not None) and (assistant_msg is not None)+ assert (user_response.msg is not None+ and assistant_response.msg is not None)if verbose:print(f"User:\n{user_response.msg.content}\n")- print(f"Assistant:\n{assistant_msg.content}\n")+ print(f"Assistant:\n{assistant_response.msg.content}\n")# Condition 3: Break if user does not give instructionif user_no_instruct_word not in user_response.msg.content:@@ -127,7 +130,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,user_no_instruct_counter = 0# Condition 4: Break if assistant gives instruction (flipped role)- if assistant_instruct_word in assistant_msg.content:+ if assistant_instruct_word in assistant_response.msg.content:assistant_instruct_counter += 1if assistant_instruct_counter == assistant_instruct_threshold:message_dict[@@ -139,7 +142,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_response.msg.content.lower(- ) or repeat_word in assistant_msg.content.lower():+ ) or repeat_word in assistant_response.msg.content.lower():repeat_word_counter += 1if repeat_word_counter == repeat_word_threshold:message_dict[@@ -160,7 +163,8 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,# Save assistant messagemessage_counter += 1- message_dict[f"message_{message_counter}"] = assistant_msg.to_dict()+ message_dict[+ f"message_{message_counter}"] = assistant_response.msg.to_dict()message_dict["num_messages"] = message_counterdiff --git a/examples/ai_society/role_playing_with_critic.py b/examples/ai_society/role_playing_with_critic.pyindex 31e075c90..f3c162582 100644--- a/examples/ai_society/role_playing_with_critic.py+++ b/examples/ai_society/role_playing_with_critic.py@@ -51,11 +51,13 @@ def main() -> None: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()+ input_assistant_msg, _ = role_play_session.init_chat()while n < chat_turn_limit:n += 1assistant_response, user_response = role_play_session.step(- assistant_msg)+ input_assistant_msg)++ input_assistant_msg = assistant_response.msgif assistant_response.terminated:print(Fore.GREEN +@@ -70,8 +72,9 @@ def main() -> None: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")+ print_text_animated(+ Fore.GREEN ++ f"AI Assistant:\n\n{assistant_response.msg.content}\n")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 b46518d10..be7d274b8 100644--- a/examples/ai_society/role_playing_with_human.py+++ b/examples/ai_society/role_playing_with_human.py@@ -47,11 +47,13 @@ def main() -> None: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()+ input_assistant_msg, _ = role_play_session.init_chat()while n < chat_turn_limit:n += 1assistant_response, user_response = role_play_session.step(- assistant_msg)+ input_assistant_msg)++ input_assistant_msg = assistant_response.msgif assistant_response.terminated:print(Fore.GREEN +@@ -66,8 +68,9 @@ def main() -> None: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")+ print_text_animated(+ Fore.GREEN ++ f"AI Assistant:\n\n{assistant_response.msg.content}\n")if "CAMEL_TASK_DONE" in user_response.msg.content:breakdiff --git a/examples/code/role_playing_multiprocess.py b/examples/code/role_playing_multiprocess.pyindex 446b919fd..d84fbeb1f 100644--- a/examples/code/role_playing_multiprocess.py+++ b/examples/code/role_playing_multiprocess.py@@ -88,8 +88,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)- 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)@@ -126,7 +126,7 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,while message_counter < max_num_messages:user_response = user_agent.step(- assistant_msg.set_user_role_at_backend())+ input_assistant_msg.set_user_role_at_backend())# Condition 1: User terminates the chatif user_response.terminated:@@ -149,7 +149,7 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,breakassistant_agent.update_messages(assistant_response.msg)- print(f"Assistant:\n{assistant_msg.content}\n")+ print(f"Assistant:\n{assistant_response.msg.content}\n")# Condition 3: Break if user does not give instructionif user_no_instruct_word not in user_response.msg.content:@@ -162,7 +162,7 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,user_no_instruct_counter = 0# Condition 4: Break if assistant gives instruction (flipped role)- if assistant_instruct_word in assistant_msg.content:+ if assistant_instruct_word in assistant_response.msg.content:assistant_instruct_counter += 1if assistant_instruct_counter == assistant_instruct_threshold:message_dict[@@ -174,7 +174,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_response.msg.content.lower(- ) or repeat_word in assistant_msg.content.lower():+ ) or repeat_word in assistant_response.msg.content.lower():repeat_word_counter += 1if repeat_word_counter == repeat_word_threshold:message_dict[@@ -195,7 +195,10 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,# Save assistant messagemessage_counter += 1- message_dict[f"message_{message_counter}"] = assistant_msg.to_dict()+ message_dict[+ f"message_{message_counter}"] = assistant_response.msg.to_dict()++ input_assistant_msg = assistant_response.msgmessage_dict["num_messages"] = message_counterdiff --git a/examples/misalignment/role_playing_multiprocess.py b/examples/misalignment/role_playing_multiprocess.pyindex bcd1060a8..b19099135 100644--- a/examples/misalignment/role_playing_multiprocess.py+++ b/examples/misalignment/role_playing_multiprocess.py@@ -41,7 +41,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,temperature=1.4)),)- assistant_msg, _ = role_play_session.init_chat()+ input_assistant_msg, _ = role_play_session.init_chat()if verbose:print(Fore.GREEN + "AI Assistant sys message:\n"@@ -92,7 +92,9 @@ 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(- assistant_msg)+ input_assistant_msg)++ input_assistant_msg = assistant_response.msg# Condition 1: User terminates the chatif user_response.terminated:@@ -110,7 +112,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,if verbose:print(f"User:\n{user_response.msg.content}\n")- print(f"Assistant:\n{assistant_msg.content}\n")+ print(f"Assistant:\n{assistant_response.msg.content}\n")# Condition 3: Break if user does not give instructionif user_no_instruct_word not in user_response.msg.content:@@ -123,7 +125,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,user_no_instruct_counter = 0# Condition 4: Break if assistant gives instruction (flipped role)- if assistant_instruct_word in assistant_msg.content:+ if assistant_instruct_word in assistant_response.msg.content:assistant_instruct_counter += 1if assistant_instruct_counter == assistant_instruct_threshold:message_dict[@@ -135,7 +137,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_response.msg.content.lower(- ) or repeat_word in assistant_msg.content.lower():+ ) or repeat_word in assistant_response.msg.content.lower():repeat_word_counter += 1if repeat_word_counter == repeat_word_threshold:message_dict[@@ -156,7 +158,8 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,# Save assistant messagemessage_counter += 1- message_dict[f"message_{message_counter}"] = assistant_msg.to_dict()+ message_dict[+ f"message_{message_counter}"] = assistant_response.msg.to_dict()message_dict["num_messages"] = message_counterdiff --git a/examples/misalignment/role_playing_with_human.py b/examples/misalignment/role_playing_with_human.pyindex c080526db..d22757c9f 100644--- a/examples/misalignment/role_playing_with_human.py+++ b/examples/misalignment/role_playing_with_human.py@@ -50,28 +50,32 @@ def main() -> None: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()+ input_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_response, user_response = role_play_session.step(+ input_assistant_msg)- if assistant_return.terminated:- print(Fore.GREEN +- ("AI Assistant terminated. "- f"Reason: {assistant_return.info['termination_reasons']}."))+ input_assistant_msg = assistant_response.msg++ if assistant_response.terminated:+ print(Fore.GREEN + (+ "AI Assistant terminated. "+ f"Reason: {assistant_response.info['termination_reasons']}."))break- if user_return.terminated:+ if user_response.terminated:print(Fore.GREEN +("AI User terminated. "- f"Reason: {user_return.info['termination_reasons']}."))+ f"Reason: {user_response.info['termination_reasons']}."))breakprint_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")+ f"AI User:\n\n{user_response.msg.content}\n")+ print_text_animated(+ Fore.GREEN ++ f"AI Assistant:\n\n{assistant_response.msg.content}\n")- if "CAMEL_TASK_DONE" in user_return.msg.content:+ if "CAMEL_TASK_DONE" in user_response.msg.content:break