🐫 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
`Code` `RolePlaying` example (#211)
Guohao Lif60e17d62088
diff --git a/examples/code/role_playing.py b/examples/code/role_playing.pynew file mode 100644index 000000000..f1c8723c8--- /dev/null+++ b/examples/code/role_playing.py@@ -0,0 +1,79 @@+# =========== 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. ===========+from colorama import Fore++from camel.societies import RolePlaying+from camel.typing import TaskType+from camel.utils import print_text_animated+++def main(model_type=None) -> None:+ task_prompt = "Develop a poll app"+ language = "JavaScript"+ domain = "Sociology"+ meta_dict = {"language": language, "domain": domain}+ role_play_session = RolePlaying(+ f"{language} Programmer",+ f"Person working in {domain}",+ task_prompt=task_prompt,+ with_task_specify=True,+ model_type=model_type,+ task_type=TaskType.CODE,+ extend_sys_msg_meta_dicts=[meta_dict, meta_dict],+ extend_task_specify_meta_dict=meta_dict,+ )++ 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.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+ input_assistant_msg, _ = role_play_session.init_chat()+ while n < chat_turn_limit:+ n += 1+ assistant_response, user_response = role_play_session.step(+ input_assistant_msg)++ input_assistant_msg = assistant_response.msg++ if assistant_response.terminated:+ print(Fore.GREEN ++ ("AI Assistant terminated. Reason: "+ f"{assistant_response.info['termination_reasons']}."))+ break+ if user_response.terminated:+ print(Fore.GREEN ++ ("AI User terminated. "+ f"Reason: {user_response.info['termination_reasons']}."))+ break++ print_text_animated(Fore.BLUE ++ f"AI User:\n\n{user_response.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:+ break+++if __name__ == "__main__":+ main()diff --git a/examples/test/test_code_example.py b/examples/test/test_code_example.pynew file mode 100644index 000000000..a08c08993--- /dev/null+++ b/examples/test/test_code_example.py@@ -0,0 +1,22 @@+# =========== 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 pytest++import examples.code.role_playing+from camel.typing import ModelType+++def test_code_role_playing_example():+ examples.code.role_playing.main(ModelType.STUB)