🐫 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
chores: move `role_playing_with_functions` from utils into example (#549)
Wendong-Fanf0ca723d9810
diff --git a/camel/utils/__init__.py b/camel/utils/__init__.pyindex e551155a6..b615d0863 100644--- a/camel/utils/__init__.py+++ b/camel/utils/__init__.py@@ -23,7 +23,6 @@ from .commons import (get_system_information,get_task_list,print_text_animated,- role_playing_with_function,to_pascal,)from .token_counting import (@@ -52,5 +51,4 @@ __all__ = ['OpenSourceTokenCounter','dependencies_required','api_keys_required',- 'role_playing_with_function',]diff --git a/camel/utils/commons.py b/camel/utils/commons.pyindex a45e6ed9c..abf32ff1c 100644--- a/camel/utils/commons.py+++ b/camel/utils/commons.py@@ -30,34 +30,6 @@ from camel.types import TaskTypeF = TypeVar('F', bound=Callable[..., Any])-# Set lazy import-def get_lazy_imported_functions_module():- from camel.functions import (- MAP_FUNCS,- MATH_FUNCS,- OPENAPI_FUNCS,- SEARCH_FUNCS,- TWITTER_FUNCS,- WEATHER_FUNCS,- )-- return [- *MATH_FUNCS,- *SEARCH_FUNCS,- *WEATHER_FUNCS,- *MAP_FUNCS,- *TWITTER_FUNCS,- *OPENAPI_FUNCS,- ]---# Set lazy import-def get_lazy_imported_types_module():- from camel.types import ModelType-- return ModelType.GPT_3_5_TURBO--def api_key_required(func: F) -> F:r"""Decorator that checks if the API key is available either as an environment variable or passed directly.@@ -354,145 +326,3 @@ def to_pascal(snake: str) -> str:PYDANTIC_V2 = pydantic.VERSION.startswith("2.")---def role_playing_with_function(- task_prompt: str = (- "Assume now is 2024 in the Gregorian calendar, "- "estimate the current age of University of Oxford "- "and then add 10 more years to this age, "- "and get the current weather of the city where "- "the University is located. And tell me what time "- "zone University of Oxford is in. And use my twitter "- "account infomation to create a tweet. Search basketball"- "course from coursera And help me to choose a basketball by klarna."- ),- tools: Optional[List] = None,- model_type=None,- chat_turn_limit=10,- assistant_role_name: str = "Searcher",- user_role_name: str = "Professor",-) -> None:- r"""Initializes and conducts a `RolePlaying` with `ChatGPTConfig`- session. The function creates an interactive and dynamic role-play session- where the AI Assistant and User engage based on the given task, roles, and- available functions. It demonstrates the versatility of AI in handling- diverse tasks and user interactions within a structured `RolePlaying`- framework.-- Args:- task_prompt (str): The initial task or scenario description to start- the `RolePlaying` session. Defaults to a prompt involving the- estimation of KAUST's age and weather information.- tools (list): A list of functions that the agent can utilize- during the session. Defaults to a combination of math, search, and- weather functions.- model_type (ModelType): The type of chatbot model used for both the- assistant and the user. Defaults to `GPT-4 Turbo`.- chat_turn_limit (int): The maximum number of turns (exchanges) in the- chat session. Defaults to 10.- assistant_role_name (str): The role name assigned to the AI Assistant.- Defaults to 'Searcher'.- user_role_name (str): The role name assigned to the User. Defaults to- 'Professor'.-- Returns:- None: This function does not return any value but prints out the- session's dialogues and outputs.- """-- # Run lazy import- if tools is None:- tools = get_lazy_imported_functions_module()- if model_type is None:- model_type = get_lazy_imported_types_module()-- from colorama import Fore-- from camel.agents.chat_agent import FunctionCallingRecord- from camel.configs import ChatGPTConfig- from camel.societies import RolePlaying-- task_prompt = task_prompt- user_model_config = ChatGPTConfig(temperature=0.0)-- assistant_model_config = ChatGPTConfig(- tools=tools,- temperature=0.0,- )-- role_play_session = RolePlaying(- assistant_role_name=assistant_role_name,- user_role_name=user_role_name,- assistant_agent_kwargs=dict(- model_type=model_type,- model_config=assistant_model_config,- tools=tools,- ),- user_agent_kwargs=dict(- model_type=model_type,- model_config=user_model_config,- ),- task_prompt=task_prompt,- with_task_specify=False,- )-- 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")-- n = 0- input_msg = role_play_session.init_chat()- while n < chat_turn_limit:- n += 1- assistant_response, user_response = role_play_session.step(input_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 output from the user- print_text_animated(- Fore.BLUE + f"AI User:\n\n{user_response.msg.content}\n"- )-- # Print output from the assistant, including any function- # execution information- print_text_animated(Fore.GREEN + "AI Assistant:")- tool_calls: List[FunctionCallingRecord] = assistant_response.info[- 'tool_calls'- ]- for func_record in tool_calls:- print_text_animated(f"{func_record}")- print_text_animated(f"{assistant_response.msg.content}\n")-- if "CAMEL_TASK_DONE" in user_response.msg.content:- break-- input_msg = assistant_response.msgdiff --git a/examples/function_call/github_examples.py b/examples/function_call/github_examples.pyindex 82a4222a8..c49c046d4 100644--- a/examples/function_call/github_examples.py+++ b/examples/function_call/github_examples.py@@ -14,7 +14,7 @@from colorama import Forefrom camel.agents import ChatAgent-from camel.configs import FunctionCallingConfig+from camel.configs import ChatGPTConfigfrom camel.messages import BaseMessagefrom camel.toolkits import GithubToolkitfrom camel.utils import print_text_animated@@ -46,15 +46,15 @@ def solve_issue(content="""You are an experienced software engineer whospecializes on data structures and algorithms tasks.""",)- assistant_model_config = FunctionCallingConfig.from_openai_function_list(- function_list=toolkit.get_tools(),- kwargs=dict(temperature=0.0),+ assistant_model_config = ChatGPTConfig(+ tools=toolkit.get_tools(),+ temperature=0.0,)agent = ChatAgent(assistant_sys_msg,model_type=model,model_config=assistant_model_config,- function_list=toolkit.get_tools(),+ tools=toolkit.get_tools(),)agent.reset()diff --git a/examples/function_call/role_playing_with_function.py b/examples/function_call/role_playing_with_function.pydeleted file mode 100644index d3c629933..000000000--- a/examples/function_call/role_playing_with_function.py+++ /dev/null@@ -1,26 +0,0 @@-# =========== 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 camel.types import ModelType-from camel.utils import role_playing_with_function---def main(model_type=ModelType.GPT_4, chat_turn_limit=10) -> None:- role_playing_with_function(- model_type=model_type, chat_turn_limit=chat_turn_limit- )---if __name__ == "__main__":- main()diff --git a/examples/function_call/role_playing_with_functions.py b/examples/function_call/role_playing_with_functions.pynew file mode 100644index 000000000..c4d6154d2--- /dev/null+++ b/examples/function_call/role_playing_with_functions.py@@ -0,0 +1,134 @@+# =========== 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 typing import List++from colorama import Fore++from camel.agents.chat_agent import FunctionCallingRecord+from camel.configs import ChatGPTConfig+from camel.functions import (+ MAP_FUNCS,+ MATH_FUNCS,+ SEARCH_FUNCS,+ TWITTER_FUNCS,+ WEATHER_FUNCS,+)+from camel.societies import RolePlaying+from camel.types import ModelType+from camel.utils import print_text_animated+++def main(model_type=ModelType.GPT_3_5_TURBO, chat_turn_limit=10) -> None:+ task_prompt = (+ "Assume now is 2024 in the Gregorian calendar, "+ "estimate the current age of University of Oxford "+ "and then add 10 more years to this age, "+ "and get the current weather of the city where "+ "the University is located."+ )++ user_model_config = ChatGPTConfig(temperature=0.0)++ function_list = [+ *MATH_FUNCS,+ *SEARCH_FUNCS,+ *WEATHER_FUNCS,+ *MAP_FUNCS,+ *TWITTER_FUNCS,+ ]+ assistant_model_config = ChatGPTConfig(+ tools=function_list,+ temperature=0.0,+ )++ role_play_session = RolePlaying(+ assistant_role_name="Searcher",+ user_role_name="Professor",+ assistant_agent_kwargs=dict(+ model_type=model_type,+ model_config=assistant_model_config,+ tools=function_list,+ ),+ user_agent_kwargs=dict(+ model_type=model_type,+ model_config=user_model_config,+ ),+ task_prompt=task_prompt,+ with_task_specify=False,+ )++ 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")++ n = 0+ input_msg = role_play_session.init_chat()+ while n < chat_turn_limit:+ n += 1+ assistant_response, user_response = role_play_session.step(input_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 output from the user+ print_text_animated(+ Fore.BLUE + f"AI User:\n\n{user_response.msg.content}\n"+ )++ # Print output from the assistant, including any function+ # execution information+ print_text_animated(Fore.GREEN + "AI Assistant:")+ tool_calls: List[FunctionCallingRecord] = assistant_response.info[+ 'tool_calls'+ ]+ for func_record in tool_calls:+ print_text_animated(f"{func_record}")+ print_text_animated(f"{assistant_response.msg.content}\n")++ if "CAMEL_TASK_DONE" in user_response.msg.content:+ break++ input_msg = assistant_response.msg+++if __name__ == "__main__":+ main()diff --git a/examples/test/test_ai_society_example.py b/examples/test/test_ai_society_example.pyindex 9adbeb8a9..18689b36e 100644--- a/examples/test/test_ai_society_example.py+++ b/examples/test/test_ai_society_example.py@@ -14,7 +14,7 @@from mock import patchimport examples.ai_society.role_playing-import examples.function_call.role_playing_with_function+import examples.function_call.role_playing_with_functionsimport examples.open_source_models.role_playing_with_open_source_modelfrom camel.types import ModelType@@ -26,7 +26,7 @@ def test_ai_society_role_playing_example():def test_role_playing_with_function_example():with patch('time.sleep', return_value=None):- examples.function_call.role_playing_with_function.main(+ examples.function_call.role_playing_with_functions.main(ModelType.STUB, chat_turn_limit=2)