From b9ddf115837d0a5c92435b42c1750e8fde8a2395 Mon Sep 17 00:00:00 2001 From: Tianqi Xu <40522713+dandansamax@users.noreply.github.com> Date: Wed, 11 Sep 2024 20:53:44 +0300 Subject: [PATCH] style: Pass ruff check (#38) --- crab/agents/backend_models/gemini_model.py | 3 +- crab/agents/policies/multi_agent_by_env.py | 57 ++++++++++--------- crab/agents/policies/multi_agent_by_func.py | 20 +++---- crab/core/experiment.py | 6 +- .../backend_models/test_claude_model.py | 5 +- .../backend_models/test_gemini_model.py | 5 +- .../backend_models/test_openai_model.py | 5 +- 7 files changed, 58 insertions(+), 43 deletions(-) diff --git a/crab/agents/backend_models/gemini_model.py b/crab/agents/backend_models/gemini_model.py index 06d7e79..663aba2 100644 --- a/crab/agents/backend_models/gemini_model.py +++ b/crab/agents/backend_models/gemini_model.py @@ -117,7 +117,8 @@ def call_api(self, request_messages: list): ) except ResourceExhausted: print( - "ResourceExhausted: 429 Resource has been exhausted. Please waiting..." + "ResourceExhausted: 429 Resource has been exhausted.", + " Please waiting...", ) sleep(10) else: diff --git a/crab/agents/policies/multi_agent_by_env.py b/crab/agents/policies/multi_agent_by_env.py index 772c1aa..d2cfc2c 100644 --- a/crab/agents/policies/multi_agent_by_env.py +++ b/crab/agents/policies/multi_agent_by_env.py @@ -22,38 +22,38 @@ class MultiAgentByEnvPolicy(AgentPolicy): - _main_agent_prompt = """You are a main agent, and your goal is to plan and give - instructions to sub-agents in each environment to complete the final task. Now you have - to do a task as described below: {task_description}. - The description of each given environment: {env_description}. - For each step, you are required to provide high-level instructions detailing the next - actions to be taken. Additionally, you must specify which sub-agent in the designated - environment should execute these instructions. If a sub-agent is not needed for a - particular step, you may instruct it to skip that step.""" + _main_agent_prompt = """You are a main agent, and your goal is to plan and + give instructions to sub-agents in each environment to complete the final task. Now + you have to do a task as described below: {task_description}. The description of + each given environment: {env_description}. For each step, you are required to + provide high-level instructions detailing the next actions to be taken. + Additionally, you must specify which sub-agent in the designated environment should + execute these instructions. If a sub-agent is not needed for a particular step, you + may instruct it to skip that step.""" - _env_agent_prompt = """You are a sub-agent responsible for the {environment} environment. - The description of the {environment} environment is: {env_description}. - Your goal is to assist the main agent in completing the final task by performing actions - in the {environment} environment according to the instructions from the main agent. The - final task is described below: {task_description}. A unit operation you can perform is called - action in a given environment. You can only execute action in the {environment} - environment. For the {environment} environment, you are given a limited action space as - function calls: + _env_agent_prompt = """You are a sub-agent responsible for the {environment} + environment. The description of the {environment} environment is: + {env_description}. Your goal is to assist the main agent in completing the final + task by performing actions in the {environment} environment according to the + instructions from the main agent. The final task is described below: + {task_description}. A unit operation you can perform is called action in a given + environment. You can only execute action in the {environment} environment. For the + {environment} environment, you are given a limited action space as function calls: {action_descriptions} The interactive UI elements on the screenshot are labeled with numeric tags starting - from 1. For each step, You will receive an instruction telling you what you need to do - next. After analyzing the instruction you received and the current {environment} system, - if you think you don't need to do anything in the current {environment} system, you should - choose SKIP action. Otherwise, you must state what actions to take, what the parameters - are, and you MUST provide in which environment to perform these actions. Your answer - must be function calls. Please do not output any other information. You must make sure - all function calls get their required parameters.""" + from 1. For each step, You will receive an instruction telling you what you need to + do next. After analyzing the instruction you received and the current {environment} + system, if you think you don't need to do anything in the current {environment} + system, you should choose SKIP action. Otherwise, you must state what actions to + take, what the parameters are, and you MUST provide in which environment to perform + these actions. Your answer must be function calls. Please do not output any other + information. You must make sure all function calls get their required parameters.""" _root_agent_prompt = """You are a sub-agent responsible for the crab benchmark root environment. Your goal is to assist the main agent in completing the whole task: - "{task_description}". You can only complete the task or submit the result when the main - agent tells you the whole task has been completed. Otherwise, you can only call SKIP. - """ + "{task_description}". You can only complete the task or submit the result when the + main agent tells you the whole task has been completed. Otherwise, you can only call + SKIP. """ def __init__( self, @@ -119,7 +119,10 @@ def chat( main_prompt.extend(observation[env]) main_prompt.append( ( - f"Your target: {self.task_description}\nTell me the next step in each environment.", + ( + f"Your target: {self.task_description}\n" + "Tell me the next step in each environment." + ), MessageType.TEXT, ) ) diff --git a/crab/agents/policies/multi_agent_by_func.py b/crab/agents/policies/multi_agent_by_func.py index 7fcc9fb..8f95b72 100644 --- a/crab/agents/policies/multi_agent_by_func.py +++ b/crab/agents/policies/multi_agent_by_func.py @@ -23,19 +23,19 @@ class MultiAgentByFuncPolicy(AgentPolicy): _system_prompt = """You are a helpful assistant. Now you have to do a task as - described below: {task_description}. And this is the description of each given environment: - {env_description}. A unit operation you can perform is called action in a - given environment. For each environment, you are given a limited action space as + described below: {task_description}. And this is the description of each given + environment: {env_description}. A unit operation you can perform is called action in + a given environment. For each environment, you are given a limited action space as function calls: {action_descriptions} - You may receive a screenshot of the current system. The interactive UI elements on the - screenshot are labeled with numeric tags starting from 1. For each step, You must state - what actions to take, what the parameters are, and you MUST provide in which environment - to perform these actions. """ + You may receive a screenshot of the current system. The interactive UI elements on + the screenshot are labeled with numeric tags starting from 1. For each step, You + must state what actions to take, what the parameters are, and you MUST provide in + which environment to perform these actions. """ - _tool_prompt = """You are a helpful assistant in generating function calls. I will give - you a detailed description of what actions to take next, you should translate it into - function calls. please do not output any other information. + _tool_prompt = """You are a helpful assistant in generating function calls. I will + give you a detailed description of what actions to take next, you should translate + it into function calls. please do not output any other information. """ def __init__( diff --git a/crab/core/experiment.py b/crab/core/experiment.py index 33c059c..9e14c9e 100644 --- a/crab/core/experiment.py +++ b/crab/core/experiment.py @@ -141,7 +141,8 @@ def execute_action(self, response: list[ActionOutput]) -> bool: return True print( "\033[92m" - f'Action "{action.name}" in env "{action.env}" success. current evaluation results: {self.metrics}\n' + f'Action "{action.name}" in env "{action.env}" success. ' + f"Current evaluation results: {self.metrics}\n" "\033[0m" ) self.write_current_log_row(action) @@ -193,7 +194,8 @@ def start_benchmark(self): env_descriptions=env_description, ) print( - f'Start benchmark "{self.benchmark.name}", task id "{self.task.id}": "{self.task.description}"' + f'Start benchmark "{self.benchmark.name}", task id "{self.task.id}": ' + f'"{self.task.description}"' ) self.init_log_dir() self.step_cnt = 0 diff --git a/test/agents/backend_models/test_claude_model.py b/test/agents/backend_models/test_claude_model.py index 549e05c..ace602a 100644 --- a/test/agents/backend_models/test_claude_model.py +++ b/test/agents/backend_models/test_claude_model.py @@ -64,7 +64,10 @@ def test_text_chat(claude_model_text): def test_action_chat(claude_model_text): claude_model_text.reset("You are a helpful assistant.", [add]) message = ( - "I had 10 dollars. Miss Polaris gave me 15 dollars. How many money do I have now.", + ( + "I had 10 dollars. Miss Polaris gave me 15 dollars." + " How many money do I have now." + ), 0, ) output = claude_model_text.chat(message) diff --git a/test/agents/backend_models/test_gemini_model.py b/test/agents/backend_models/test_gemini_model.py index fe15519..86ece01 100644 --- a/test/agents/backend_models/test_gemini_model.py +++ b/test/agents/backend_models/test_gemini_model.py @@ -64,7 +64,10 @@ def test_text_chat(gemini_model_text): def test_action_chat(gemini_model_text): gemini_model_text.reset("You are a helpful assistant.", [add]) message = ( - "I had 10 dollars. Miss Polaris gave me 15 dollars. How many money do I have now.", + ( + "I had 10 dollars. Miss Polaris gave me 15 dollars. " + "How many money do I have now." + ), 0, ) output = gemini_model_text.chat(message) diff --git a/test/agents/backend_models/test_openai_model.py b/test/agents/backend_models/test_openai_model.py index 38b4648..51e56ab 100644 --- a/test/agents/backend_models/test_openai_model.py +++ b/test/agents/backend_models/test_openai_model.py @@ -145,7 +145,10 @@ def test_text_chat(mock_create, openai_model_text): def test_action_chat(mock_create, openai_model_text): openai_model_text.reset("You are a helpful assistant.", [add]) message = ( - "I had 10 dollars. Miss Polaris gave me 15 dollars. How many money do I have now.", + ( + "I had 10 dollars. Miss Polaris gave me 15 dollars. " + "How many money do I have now." + ), 0, ) output = openai_model_text.chat(message)