Update src/agent/custom_massage_manager.py
Browse files- src/agent/custom_massage_manager.py +107 -128
src/agent/custom_massage_manager.py
CHANGED
|
@@ -1,128 +1,107 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
|
| 3 |
-
import logging
|
| 4 |
-
from typing import List, Optional, Type
|
| 5 |
-
|
| 6 |
-
from browser_use.agent.message_manager.service import MessageManager
|
| 7 |
-
from browser_use.agent.message_manager.views import MessageHistory
|
| 8 |
-
from browser_use.agent.prompts import SystemPrompt
|
| 9 |
-
from browser_use.agent.views import ActionResult, AgentStepInfo
|
| 10 |
-
from browser_use.browser.views import BrowserState
|
| 11 |
-
from langchain_core.language_models import BaseChatModel
|
| 12 |
-
from
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
from
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
'
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
self.
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
state,
|
| 109 |
-
result,
|
| 110 |
-
include_attributes=self.include_attributes,
|
| 111 |
-
max_error_length=self.max_error_length,
|
| 112 |
-
step_info=step_info,
|
| 113 |
-
).get_user_message()
|
| 114 |
-
self._add_message_with_tokens(state_message)
|
| 115 |
-
|
| 116 |
-
def _count_text_tokens(self, text: str) -> int:
|
| 117 |
-
if isinstance(self.llm, (ChatOpenAI, ChatAnthropic, DeepSeekR1ChatOpenAI)):
|
| 118 |
-
try:
|
| 119 |
-
tokens = self.llm.get_num_tokens(text)
|
| 120 |
-
except Exception:
|
| 121 |
-
tokens = (
|
| 122 |
-
len(text) // self.ESTIMATED_TOKENS_PER_CHARACTER
|
| 123 |
-
) # Rough estimate if no tokenizer available
|
| 124 |
-
else:
|
| 125 |
-
tokens = (
|
| 126 |
-
len(text) // self.ESTIMATED_TOKENS_PER_CHARACTER
|
| 127 |
-
) # Rough estimate if no tokenizer available
|
| 128 |
-
return tokens
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import logging
|
| 4 |
+
from typing import List, Optional, Type
|
| 5 |
+
|
| 6 |
+
from browser_use.agent.message_manager.service import MessageManager
|
| 7 |
+
from browser_use.agent.message_manager.views import MessageHistory
|
| 8 |
+
from browser_use.agent.prompts import SystemPrompt
|
| 9 |
+
from browser_use.agent.views import ActionResult, AgentStepInfo
|
| 10 |
+
from browser_use.browser.views import BrowserState
|
| 11 |
+
from langchain_core.language_models import BaseChatModel
|
| 12 |
+
from langchain_core.messages import (
|
| 13 |
+
AIMessage,
|
| 14 |
+
BaseMessage,
|
| 15 |
+
HumanMessage,
|
| 16 |
+
)
|
| 17 |
+
from langchain_openai import ChatOpenAI
|
| 18 |
+
from ..utils.llm import DeepSeekR1ChatOpenAI
|
| 19 |
+
from .custom_prompts import CustomAgentMessagePrompt
|
| 20 |
+
|
| 21 |
+
logger = logging.getLogger(__name__)
|
| 22 |
+
|
| 23 |
+
class CustomMassageManager(MessageManager):
|
| 24 |
+
def __init__(
|
| 25 |
+
self,
|
| 26 |
+
llm: BaseChatModel,
|
| 27 |
+
task: str,
|
| 28 |
+
action_descriptions: str,
|
| 29 |
+
system_prompt_class: Type[SystemPrompt],
|
| 30 |
+
max_input_tokens: int = 128000,
|
| 31 |
+
estimated_tokens_per_character: int = 3,
|
| 32 |
+
image_tokens: int = 800,
|
| 33 |
+
include_attributes: list[str] = [],
|
| 34 |
+
max_error_length: int = 400,
|
| 35 |
+
max_actions_per_step: int = 10,
|
| 36 |
+
tool_call_in_content: bool = False,
|
| 37 |
+
use_function_calling: bool = True
|
| 38 |
+
):
|
| 39 |
+
super().__init__(
|
| 40 |
+
llm=llm,
|
| 41 |
+
task=task,
|
| 42 |
+
action_descriptions=action_descriptions,
|
| 43 |
+
system_prompt_class=system_prompt_class,
|
| 44 |
+
max_input_tokens=max_input_tokens,
|
| 45 |
+
estimated_tokens_per_character=estimated_tokens_per_character,
|
| 46 |
+
image_tokens=image_tokens,
|
| 47 |
+
include_attributes=include_attributes,
|
| 48 |
+
max_error_length=max_error_length,
|
| 49 |
+
max_actions_per_step=max_actions_per_step,
|
| 50 |
+
tool_call_in_content=tool_call_in_content,
|
| 51 |
+
)
|
| 52 |
+
self.use_function_calling = use_function_calling
|
| 53 |
+
self.history = MessageHistory()
|
| 54 |
+
self._add_message_with_tokens(self.system_prompt)
|
| 55 |
+
|
| 56 |
+
if self.use_function_calling:
|
| 57 |
+
tool_calls = [
|
| 58 |
+
{
|
| 59 |
+
'name': 'CustomAgentOutput',
|
| 60 |
+
'args': {
|
| 61 |
+
'current_state': {
|
| 62 |
+
'prev_action_evaluation': 'Unknown',
|
| 63 |
+
'important_contents': '',
|
| 64 |
+
'completed_contents': '',
|
| 65 |
+
'thought': 'Ready to proceed with the task.',
|
| 66 |
+
'summary': 'Initializing task.',
|
| 67 |
+
},
|
| 68 |
+
'action': [],
|
| 69 |
+
},
|
| 70 |
+
'id': '',
|
| 71 |
+
'type': 'tool_call',
|
| 72 |
+
}
|
| 73 |
+
]
|
| 74 |
+
example_tool_call = AIMessage(
|
| 75 |
+
content='',
|
| 76 |
+
tool_calls=tool_calls if not self.tool_call_in_content else []
|
| 77 |
+
)
|
| 78 |
+
self._add_message_with_tokens(example_tool_call)
|
| 79 |
+
|
| 80 |
+
def cut_messages(self):
|
| 81 |
+
diff = self.history.total_tokens - self.max_input_tokens
|
| 82 |
+
while diff > 0 and len(self.history.messages) > 1:
|
| 83 |
+
self.history.remove_message(1)
|
| 84 |
+
diff = self.history.total_tokens - self.max_input_tokens
|
| 85 |
+
|
| 86 |
+
def add_state_message(
|
| 87 |
+
self,
|
| 88 |
+
state: BrowserState,
|
| 89 |
+
result: Optional[List[ActionResult]] = None,
|
| 90 |
+
step_info: Optional[AgentStepInfo] = None,
|
| 91 |
+
) -> None:
|
| 92 |
+
state_message = CustomAgentMessagePrompt(
|
| 93 |
+
state,
|
| 94 |
+
result,
|
| 95 |
+
include_attributes=self.include_attributes,
|
| 96 |
+
max_error_length=self.max_error_length,
|
| 97 |
+
step_info=step_info,
|
| 98 |
+
).get_user_message()
|
| 99 |
+
self._add_message_with_tokens(state_message)
|
| 100 |
+
|
| 101 |
+
def _count_text_tokens(self, text: str) -> int:
|
| 102 |
+
try:
|
| 103 |
+
if isinstance(self.llm, (ChatOpenAI, DeepSeekR1ChatOpenAI)):
|
| 104 |
+
return self.llm.get_num_tokens(text)
|
| 105 |
+
except Exception:
|
| 106 |
+
pass
|
| 107 |
+
return len(text) // self.ESTIMATED_TOKENS_PER_CHARACTER
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|