diff --git a/src/agent/custom_agent.py b/src/agent/custom_agent.py index ed4ceb6..4b0eff3 100644 --- a/src/agent/custom_agent.py +++ b/src/agent/custom_agent.py @@ -208,8 +208,9 @@ class CustomAgent(Agent): @time_execution_async("--get_next_action") async def get_next_action(self, input_messages: list[BaseMessage]) -> AgentOutput: """Get next action from LLM based on current state""" - - ai_message = self.llm.invoke(input_messages) + fixed_input_messages = self._convert_input_messages(input_messages) + ai_message = self.llm.invoke(fixed_input_messages) + self.message_manager._add_message_with_tokens(ai_message) if hasattr(ai_message, "reasoning_content"): logger.info("🤯 Start Deep Thinking: ") @@ -221,10 +222,16 @@ class CustomAgent(Agent): else: ai_content = ai_message.content - ai_content = ai_content.replace("```json", "").replace("```", "") - ai_content = repair_json(ai_content) - parsed_json = json.loads(ai_content) - parsed: AgentOutput = self.AgentOutput(**parsed_json) + try: + ai_content = ai_content.replace("```json", "").replace("```", "") + ai_content = repair_json(ai_content) + parsed_json = json.loads(ai_content) + parsed: AgentOutput = self.AgentOutput(**parsed_json) + except Exception as e: + import traceback + traceback.print_exc() + logger.debug(ai_message.content) + raise ValueError('Could not parse response.') if parsed is None: logger.debug(ai_message.content) @@ -234,7 +241,6 @@ class CustomAgent(Agent): if len(parsed.action) > self.settings.max_actions_per_step: parsed.action = parsed.action[: self.settings.max_actions_per_step] self._log_response(parsed) - self.message_manager._add_message_with_tokens(ai_message) return parsed async def _run_planner(self) -> Optional[str]: @@ -335,7 +341,6 @@ class CustomAgent(Agent): await self._raise_if_stopped_or_paused() except Exception as e: # model call failed, remove last state message from history - pdb.set_trace() self.message_manager._remove_state_message_by_index(-1) raise e diff --git a/src/agent/custom_message_manager.py b/src/agent/custom_message_manager.py index 3831385..212c3fb 100644 --- a/src/agent/custom_message_manager.py +++ b/src/agent/custom_message_manager.py @@ -1,6 +1,7 @@ from __future__ import annotations import logging +import pdb from typing import List, Optional, Type, Dict from browser_use.agent.message_manager.service import MessageManager diff --git a/src/agent/custom_system_prompt.md b/src/agent/custom_system_prompt.md index 13efbdb..9cefaa2 100644 --- a/src/agent/custom_system_prompt.md +++ b/src/agent/custom_system_prompt.md @@ -18,11 +18,17 @@ Example: # Response Rules 1. RESPONSE FORMAT: You must ALWAYS respond with valid JSON in this exact format: -{{"current_state": {{"evaluation_previous_goal": "Success|Failed|Unknown - Analyze the current elements and the image to check if the previous goals/actions are successful like intended by the task. Mention if something unexpected happened. Shortly state why/why not.", -"important_contents": "Output important contents closely related to user's instruction on the current page. If there is, please output the contents. If not, please output ''.", -"thought": "Think about the requirements that have been completed in previous operations and the requirements that need to be completed in the next one operation. If your output of evaluation_previous_goal is 'Failed', please reflect and output your reflection here.", -"next_goal": "Please generate a brief natural language description for the goal of your next actions based on your thought."}}, -"action":[{{"one_action_name": {{// action-specific parameter}}}}, // ... more actions in sequence]}} +{{ + "current_state": {{ + "evaluation_previous_goal": "Success|Failed|Unknown - Analyze the current elements and the image to check if the previous goals/actions are successful like intended by the task. Mention if something unexpected happened. Shortly state why/why not.", + "important_contents": "Output important contents closely related to user\'s instruction on the current page. If there is, please output the contents. If not, please output empty string ''.", + "thought": "Think about the requirements that have been completed in previous operations and the requirements that need to be completed in the next one operation. If your output of evaluation_previous_goal is 'Failed', please reflect and output your reflection here.", + "next_goal": "Please generate a brief natural language description for the goal of your next actions based on your thought." + }}, + "action": [ + {{"one_action_name": {{// action-specific parameter}}}}, // ... more actions in sequence + ] +}} 2. ACTIONS: You can specify multiple actions in the list to be executed in sequence. But always specify only one action name per item. Use maximum {{max_actions}} actions per sequence. Common action sequences: diff --git a/tests/test_browser_use.py b/tests/test_browser_use.py index db35c5f..6ef4210 100644 --- a/tests/test_browser_use.py +++ b/tests/test_browser_use.py @@ -133,11 +133,11 @@ async def test_browser_use_custom(): api_key=os.getenv("GOOGLE_API_KEY", "") ) - # llm = utils.get_llm_model( - # provider="deepseek", - # model_name="deepseek-reasoner", - # temperature=0.8 - # ) + llm = utils.get_llm_model( + provider="deepseek", + model_name="deepseek-reasoner", + temperature=0.8 + ) # llm = utils.get_llm_model( # provider="deepseek",