From 768a7f6c6f338d121b643d61558950862edff78c Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 18 Mar 2025 14:42:58 +0800 Subject: [PATCH] fix deepresearch --- src/agent/custom_agent.py | 40 ++++++++++++++++++------------------- src/agent/custom_prompts.py | 12 +++++------ src/utils/deep_research.py | 2 +- webui.py | 7 +++---- 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/agent/custom_agent.py b/src/agent/custom_agent.py index b6471fd..f06f133 100644 --- a/src/agent/custom_agent.py +++ b/src/agent/custom_agent.py @@ -54,26 +54,6 @@ from .custom_views import CustomAgentOutput, CustomAgentStepInfo, CustomAgentSta logger = logging.getLogger(__name__) - -def _log_response(response: CustomAgentOutput) -> None: - """Log the model's response""" - if "Success" in response.current_state.evaluation_previous_goal: - emoji = "✅" - elif "Failed" in response.current_state.evaluation_previous_goal: - emoji = "❌" - else: - emoji = "🤷" - - logger.info(f"{emoji} Eval: {response.current_state.evaluation_previous_goal}") - logger.info(f"🧠 New Memory: {response.current_state.important_contents}") - logger.info(f"🤔 Thought: {response.current_state.thought}") - logger.info(f"🎯 Next Goal: {response.current_state.next_goal}") - for i, action in enumerate(response.action): - logger.info( - f"🛠️ Action {i + 1}/{len(response.action)}: {action.model_dump_json(exclude_unset=True)}" - ) - - Context = TypeVar('Context') @@ -180,6 +160,24 @@ class CustomAgent(Agent): state=self.state.message_manager_state, ) + def _log_response(self, response: CustomAgentOutput) -> None: + """Log the model's response""" + if "Success" in response.current_state.evaluation_previous_goal: + emoji = "✅" + elif "Failed" in response.current_state.evaluation_previous_goal: + emoji = "❌" + else: + emoji = "🤷" + + logger.info(f"{emoji} Eval: {response.current_state.evaluation_previous_goal}") + logger.info(f"🧠 New Memory: {response.current_state.important_contents}") + logger.info(f"🤔 Thought: {response.current_state.thought}") + logger.info(f"🎯 Next Goal: {response.current_state.next_goal}") + for i, action in enumerate(response.action): + logger.info( + f"🛠️ Action {i + 1}/{len(response.action)}: {action.model_dump_json(exclude_unset=True)}" + ) + def _setup_action_models(self) -> None: """Setup dynamic action models from controller's registry""" # Get the dynamic action model from controller's registry @@ -236,7 +234,7 @@ class CustomAgent(Agent): # cut the number of actions to max_actions_per_step if needed if len(parsed.action) > self.settings.max_actions_per_step: parsed.action = parsed.action[: self.settings.max_actions_per_step] - _log_response(parsed) + self._log_response(parsed) return parsed async def _run_planner(self) -> Optional[str]: diff --git a/src/agent/custom_prompts.py b/src/agent/custom_prompts.py index d576a13..6ec6cff 100644 --- a/src/agent/custom_prompts.py +++ b/src/agent/custom_prompts.py @@ -88,15 +88,15 @@ class CustomAgentMessagePrompt(AgentMessagePrompt): for i, result in enumerate(self.result): action = self.actions[i] state_description += f"Previous action {i + 1}/{len(self.result)}: {action.model_dump_json(exclude_unset=True)}\n" + if result.error: + # only use last 300 characters of error + error = result.error.split('\n')[-1] + state_description += ( + f"Error of previous action {i + 1}/{len(self.result)}: ...{error}\n" + ) if result.include_in_memory: if result.extracted_content: state_description += f"Result of previous action {i + 1}/{len(self.result)}: {result.extracted_content}\n" - if result.error: - # only use last 300 characters of error - error = result.error.split('\n')[-1] - state_description += ( - f"Error of previous action {i + 1}/{len(self.result)}: ...{error}\n" - ) if self.state.screenshot and use_vision == True: # Format message for vision model diff --git a/src/utils/deep_research.py b/src/utils/deep_research.py index 122028e..ab538e0 100644 --- a/src/utils/deep_research.py +++ b/src/utils/deep_research.py @@ -89,7 +89,7 @@ async def deep_research(task, llm, agent_state=None, **kwargs): ) # go back to org url await page.go_back() - msg = f'Extracted page content:\n {content}\n' + msg = f'Extracted page content:\n{content}\n' logger.info(msg) return ActionResult(extracted_content=msg) diff --git a/webui.py b/webui.py index 19851b0..ec51779 100644 --- a/webui.py +++ b/webui.py @@ -338,7 +338,7 @@ async def run_org_agent( ) history = await _global_agent.run(max_steps=max_steps) - history_file = os.path.join(save_agent_history_path, f"{_global_agent.agent_id}.json") + history_file = os.path.join(save_agent_history_path, f"{_global_agent.state.agent_id}.json") _global_agent.save_history(history_file) final_result = history.final_result() @@ -450,7 +450,7 @@ async def run_custom_agent( ) history = await _global_agent.run(max_steps=max_steps) - history_file = os.path.join(save_agent_history_path, f"{_global_agent.agent_id}.json") + history_file = os.path.join(save_agent_history_path, f"{_global_agent.state.agent_id}.json") _global_agent.save_history(history_file) final_result = history.final_result() @@ -985,12 +985,11 @@ def create_ui(config, theme_name="Ocean"): markdown_output_display = gr.Markdown(label="Research Report") markdown_download = gr.File(label="Download Research Report") - # Bind the stop button click event after errors_output is defined stop_button.click( fn=stop_agent, inputs=[], - outputs=[errors_output, stop_button, run_button], + outputs=[stop_button, run_button], ) # Run button click handler