1+ # =========== Copyright 2024 @ 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 2024 @ CAMEL-AI.org. All Rights Reserved. ===========
14+ from termcolor import colored
15+
16+ from camel .societies import RolePlaying
17+ from camel .utils import print_text_animated
18+
19+ from crab import Benchmark , create_benchmark
20+ from crab .agents .backend_models import OpenAIModel
21+ from crab .agents .policies import SingleAgentPolicy
22+ from crab .benchmarks .template import template_benchmark_config
23+
24+ def camel_task_generator ():
25+ task_prompt = "Design a custom game using pygame"
26+ print (colored (f"Original task prompt:\n { task_prompt } \n " , "yellow" ))
27+ role_play_session = RolePlaying ("Computer Programmer" , "Gamer" , task_prompt = task_prompt )
28+ print (colored (f"Specified task prompt:\n { role_play_session .task_prompt } \n " , "cyan" ))
29+
30+ chat_turn_limit , n = 50 , 0
31+ input_msg = role_play_session .init_chat ()
32+ while n < chat_turn_limit :
33+ n += 1
34+ assistant_response , user_response = role_play_session .step (input_msg )
35+ print_text_animated (colored (f"AI User:\n \n { user_response .msg .content } \n " , "blue" ))
36+ print_text_animated (colored (f"AI Assistant:\n \n { assistant_response .msg .content } \n " , "green" ))
37+
38+ if "CAMEL_TASK_DONE" in user_response .msg .content :
39+ break
40+
41+ input_msg = assistant_response .msg
42+
43+ return role_play_session .task_prompt
44+
45+ def start_benchmark (benchmark : Benchmark , agent : SingleAgentPolicy ):
46+ for step in range (20 ):
47+ print ("=" * 40 )
48+ print (f"Start agent step { step } :" )
49+ observation = benchmark .observe ()["template_env" ]
50+ print (f"Current environment observation: { observation } " )
51+ response = agent .chat (
52+ {
53+ "template_env" : [
54+ (f"Current environment observation: { observation } " , 0 ),
55+ ]
56+ }
57+ )
58+ print (colored (f"Agent take action: { response } " , "blue" ))
59+
60+ for action in response :
61+ response = benchmark .step (
62+ action = action .name ,
63+ parameters = action .arguments ,
64+ env_name = action .env ,
65+ )
66+ print (
67+ colored (
68+ f'Action "{ action .name } " success, stat: '
69+ f"{ response .evaluation_results } " ,
70+ "green" ,
71+ )
72+ )
73+ if response .terminated :
74+ print ("=" * 40 )
75+ print (
76+ colored (
77+ f"Task finished, result: { response .evaluation_results } " ,
78+ "green"
79+ )
80+ )
81+ return
82+
83+ if __name__ == "__main__" :
84+ task_description = camel_task_generator ()
85+
86+ benchmark = create_benchmark (template_benchmark_config )
87+ task , action_space = benchmark .start_task ("0" , task_description )
88+ env_descriptions = benchmark .get_env_descriptions ()
89+
90+ model = OpenAIModel (model = "gpt-4o" , history_messages_len = 5 )
91+ agent = SingleAgentPolicy (model_backend = model )
92+ agent .reset (task_description , action_space , env_descriptions )
93+
94+ print ("Start performing task: " + colored (f'"{ task_description } "' , "green" ))
95+ start_benchmark (benchmark , agent )
96+ benchmark .reset ()
0 commit comments