Skip to content

Commit f0ca723

Browse files
authored
chores: move role_playing_with_functions from utils into example (#549)
1 parent 8d4d005 commit f0ca723

File tree

6 files changed

+141
-205
lines changed

6 files changed

+141
-205
lines changed

camel/utils/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
get_system_information,
2424
get_task_list,
2525
print_text_animated,
26-
role_playing_with_function,
2726
to_pascal,
2827
)
2928
from .token_counting import (
@@ -52,5 +51,4 @@
5251
'OpenSourceTokenCounter',
5352
'dependencies_required',
5453
'api_keys_required',
55-
'role_playing_with_function',
5654
]

camel/utils/commons.py

Lines changed: 0 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,6 @@
3030
F = TypeVar('F', bound=Callable[..., Any])
3131

3232

33-
# Set lazy import
34-
def get_lazy_imported_functions_module():
35-
from camel.functions import (
36-
MAP_FUNCS,
37-
MATH_FUNCS,
38-
OPENAPI_FUNCS,
39-
SEARCH_FUNCS,
40-
TWITTER_FUNCS,
41-
WEATHER_FUNCS,
42-
)
43-
44-
return [
45-
*MATH_FUNCS,
46-
*SEARCH_FUNCS,
47-
*WEATHER_FUNCS,
48-
*MAP_FUNCS,
49-
*TWITTER_FUNCS,
50-
*OPENAPI_FUNCS,
51-
]
52-
53-
54-
# Set lazy import
55-
def get_lazy_imported_types_module():
56-
from camel.types import ModelType
57-
58-
return ModelType.GPT_3_5_TURBO
59-
60-
6133
def api_key_required(func: F) -> F:
6234
r"""Decorator that checks if the API key is available either as an environment variable or passed directly.
6335
@@ -354,145 +326,3 @@ def to_pascal(snake: str) -> str:
354326

355327

356328
PYDANTIC_V2 = pydantic.VERSION.startswith("2.")
357-
358-
359-
def role_playing_with_function(
360-
task_prompt: str = (
361-
"Assume now is 2024 in the Gregorian calendar, "
362-
"estimate the current age of University of Oxford "
363-
"and then add 10 more years to this age, "
364-
"and get the current weather of the city where "
365-
"the University is located. And tell me what time "
366-
"zone University of Oxford is in. And use my twitter "
367-
"account infomation to create a tweet. Search basketball"
368-
"course from coursera And help me to choose a basketball by klarna."
369-
),
370-
tools: Optional[List] = None,
371-
model_type=None,
372-
chat_turn_limit=10,
373-
assistant_role_name: str = "Searcher",
374-
user_role_name: str = "Professor",
375-
) -> None:
376-
r"""Initializes and conducts a `RolePlaying` with `ChatGPTConfig`
377-
session. The function creates an interactive and dynamic role-play session
378-
where the AI Assistant and User engage based on the given task, roles, and
379-
available functions. It demonstrates the versatility of AI in handling
380-
diverse tasks and user interactions within a structured `RolePlaying`
381-
framework.
382-
383-
Args:
384-
task_prompt (str): The initial task or scenario description to start
385-
the `RolePlaying` session. Defaults to a prompt involving the
386-
estimation of KAUST's age and weather information.
387-
tools (list): A list of functions that the agent can utilize
388-
during the session. Defaults to a combination of math, search, and
389-
weather functions.
390-
model_type (ModelType): The type of chatbot model used for both the
391-
assistant and the user. Defaults to `GPT-4 Turbo`.
392-
chat_turn_limit (int): The maximum number of turns (exchanges) in the
393-
chat session. Defaults to 10.
394-
assistant_role_name (str): The role name assigned to the AI Assistant.
395-
Defaults to 'Searcher'.
396-
user_role_name (str): The role name assigned to the User. Defaults to
397-
'Professor'.
398-
399-
Returns:
400-
None: This function does not return any value but prints out the
401-
session's dialogues and outputs.
402-
"""
403-
404-
# Run lazy import
405-
if tools is None:
406-
tools = get_lazy_imported_functions_module()
407-
if model_type is None:
408-
model_type = get_lazy_imported_types_module()
409-
410-
from colorama import Fore
411-
412-
from camel.agents.chat_agent import FunctionCallingRecord
413-
from camel.configs import ChatGPTConfig
414-
from camel.societies import RolePlaying
415-
416-
task_prompt = task_prompt
417-
user_model_config = ChatGPTConfig(temperature=0.0)
418-
419-
assistant_model_config = ChatGPTConfig(
420-
tools=tools,
421-
temperature=0.0,
422-
)
423-
424-
role_play_session = RolePlaying(
425-
assistant_role_name=assistant_role_name,
426-
user_role_name=user_role_name,
427-
assistant_agent_kwargs=dict(
428-
model_type=model_type,
429-
model_config=assistant_model_config,
430-
tools=tools,
431-
),
432-
user_agent_kwargs=dict(
433-
model_type=model_type,
434-
model_config=user_model_config,
435-
),
436-
task_prompt=task_prompt,
437-
with_task_specify=False,
438-
)
439-
440-
print(
441-
Fore.GREEN
442-
+ f"AI Assistant sys message:\n{role_play_session.assistant_sys_msg}\n"
443-
)
444-
print(
445-
Fore.BLUE + f"AI User sys message:\n{role_play_session.user_sys_msg}\n"
446-
)
447-
448-
print(Fore.YELLOW + f"Original task prompt:\n{task_prompt}\n")
449-
print(
450-
Fore.CYAN
451-
+ f"Specified task prompt:\n{role_play_session.specified_task_prompt}\n"
452-
)
453-
print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
454-
455-
n = 0
456-
input_msg = role_play_session.init_chat()
457-
while n < chat_turn_limit:
458-
n += 1
459-
assistant_response, user_response = role_play_session.step(input_msg)
460-
461-
if assistant_response.terminated:
462-
print(
463-
Fore.GREEN
464-
+ (
465-
"AI Assistant terminated. Reason: "
466-
f"{assistant_response.info['termination_reasons']}."
467-
)
468-
)
469-
break
470-
if user_response.terminated:
471-
print(
472-
Fore.GREEN
473-
+ (
474-
"AI User terminated. "
475-
f"Reason: {user_response.info['termination_reasons']}."
476-
)
477-
)
478-
break
479-
480-
# Print output from the user
481-
print_text_animated(
482-
Fore.BLUE + f"AI User:\n\n{user_response.msg.content}\n"
483-
)
484-
485-
# Print output from the assistant, including any function
486-
# execution information
487-
print_text_animated(Fore.GREEN + "AI Assistant:")
488-
tool_calls: List[FunctionCallingRecord] = assistant_response.info[
489-
'tool_calls'
490-
]
491-
for func_record in tool_calls:
492-
print_text_animated(f"{func_record}")
493-
print_text_animated(f"{assistant_response.msg.content}\n")
494-
495-
if "CAMEL_TASK_DONE" in user_response.msg.content:
496-
break
497-
498-
input_msg = assistant_response.msg

examples/function_call/github_examples.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from colorama import Fore
1515

1616
from camel.agents import ChatAgent
17-
from camel.configs import FunctionCallingConfig
17+
from camel.configs import ChatGPTConfig
1818
from camel.messages import BaseMessage
1919
from camel.toolkits import GithubToolkit
2020
from camel.utils import print_text_animated
@@ -46,15 +46,15 @@ def solve_issue(
4646
content="""You are an experienced software engineer who
4747
specializes on data structures and algorithms tasks.""",
4848
)
49-
assistant_model_config = FunctionCallingConfig.from_openai_function_list(
50-
function_list=toolkit.get_tools(),
51-
kwargs=dict(temperature=0.0),
49+
assistant_model_config = ChatGPTConfig(
50+
tools=toolkit.get_tools(),
51+
temperature=0.0,
5252
)
5353
agent = ChatAgent(
5454
assistant_sys_msg,
5555
model_type=model,
5656
model_config=assistant_model_config,
57-
function_list=toolkit.get_tools(),
57+
tools=toolkit.get_tools(),
5858
)
5959
agent.reset()
6060

examples/function_call/role_playing_with_function.py

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
2+
# Licensed under the Apache License, Version 2.0 (the “License”);
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an “AS IS” BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
14+
15+
from typing import List
16+
17+
from colorama import Fore
18+
19+
from camel.agents.chat_agent import FunctionCallingRecord
20+
from camel.configs import ChatGPTConfig
21+
from camel.functions import (
22+
MAP_FUNCS,
23+
MATH_FUNCS,
24+
SEARCH_FUNCS,
25+
TWITTER_FUNCS,
26+
WEATHER_FUNCS,
27+
)
28+
from camel.societies import RolePlaying
29+
from camel.types import ModelType
30+
from camel.utils import print_text_animated
31+
32+
33+
def main(model_type=ModelType.GPT_3_5_TURBO, chat_turn_limit=10) -> None:
34+
task_prompt = (
35+
"Assume now is 2024 in the Gregorian calendar, "
36+
"estimate the current age of University of Oxford "
37+
"and then add 10 more years to this age, "
38+
"and get the current weather of the city where "
39+
"the University is located."
40+
)
41+
42+
user_model_config = ChatGPTConfig(temperature=0.0)
43+
44+
function_list = [
45+
*MATH_FUNCS,
46+
*SEARCH_FUNCS,
47+
*WEATHER_FUNCS,
48+
*MAP_FUNCS,
49+
*TWITTER_FUNCS,
50+
]
51+
assistant_model_config = ChatGPTConfig(
52+
tools=function_list,
53+
temperature=0.0,
54+
)
55+
56+
role_play_session = RolePlaying(
57+
assistant_role_name="Searcher",
58+
user_role_name="Professor",
59+
assistant_agent_kwargs=dict(
60+
model_type=model_type,
61+
model_config=assistant_model_config,
62+
tools=function_list,
63+
),
64+
user_agent_kwargs=dict(
65+
model_type=model_type,
66+
model_config=user_model_config,
67+
),
68+
task_prompt=task_prompt,
69+
with_task_specify=False,
70+
)
71+
72+
print(
73+
Fore.GREEN
74+
+ f"AI Assistant sys message:\n{role_play_session.assistant_sys_msg}\n"
75+
)
76+
print(
77+
Fore.BLUE + f"AI User sys message:\n{role_play_session.user_sys_msg}\n"
78+
)
79+
80+
print(Fore.YELLOW + f"Original task prompt:\n{task_prompt}\n")
81+
print(
82+
Fore.CYAN
83+
+ f"Specified task prompt:\n{role_play_session.specified_task_prompt}\n"
84+
)
85+
print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
86+
87+
n = 0
88+
input_msg = role_play_session.init_chat()
89+
while n < chat_turn_limit:
90+
n += 1
91+
assistant_response, user_response = role_play_session.step(input_msg)
92+
93+
if assistant_response.terminated:
94+
print(
95+
Fore.GREEN
96+
+ (
97+
"AI Assistant terminated. Reason: "
98+
f"{assistant_response.info['termination_reasons']}."
99+
)
100+
)
101+
break
102+
if user_response.terminated:
103+
print(
104+
Fore.GREEN
105+
+ (
106+
"AI User terminated. "
107+
f"Reason: {user_response.info['termination_reasons']}."
108+
)
109+
)
110+
break
111+
112+
# Print output from the user
113+
print_text_animated(
114+
Fore.BLUE + f"AI User:\n\n{user_response.msg.content}\n"
115+
)
116+
117+
# Print output from the assistant, including any function
118+
# execution information
119+
print_text_animated(Fore.GREEN + "AI Assistant:")
120+
tool_calls: List[FunctionCallingRecord] = assistant_response.info[
121+
'tool_calls'
122+
]
123+
for func_record in tool_calls:
124+
print_text_animated(f"{func_record}")
125+
print_text_animated(f"{assistant_response.msg.content}\n")
126+
127+
if "CAMEL_TASK_DONE" in user_response.msg.content:
128+
break
129+
130+
input_msg = assistant_response.msg
131+
132+
133+
if __name__ == "__main__":
134+
main()

0 commit comments

Comments
 (0)