Josedcape commited on
Commit
acdb322
·
verified ·
1 Parent(s): e3afff6

Update src/agent/custom_massage_manager.py

Browse files
Files changed (1) hide show
  1. 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 langchain_anthropic import ChatAnthropic
13
- from langchain_core.language_models import BaseChatModel
14
- from langchain_core.messages import (
15
- AIMessage,
16
- BaseMessage,
17
- HumanMessage,
18
- )
19
- from langchain_openai import ChatOpenAI
20
- from ..utils.llm import DeepSeekR1ChatOpenAI
21
- from .custom_prompts import CustomAgentMessagePrompt
22
-
23
- logger = logging.getLogger(__name__)
24
-
25
-
26
- class CustomMassageManager(MessageManager):
27
- def __init__(
28
- self,
29
- llm: BaseChatModel,
30
- task: str,
31
- action_descriptions: str,
32
- system_prompt_class: Type[SystemPrompt],
33
- max_input_tokens: int = 128000,
34
- estimated_tokens_per_character: int = 3,
35
- image_tokens: int = 800,
36
- include_attributes: list[str] = [],
37
- max_error_length: int = 400,
38
- max_actions_per_step: int = 10,
39
- tool_call_in_content: bool = False,
40
- use_function_calling: bool = True
41
- ):
42
- super().__init__(
43
- llm=llm,
44
- task=task,
45
- action_descriptions=action_descriptions,
46
- system_prompt_class=system_prompt_class,
47
- max_input_tokens=max_input_tokens,
48
- estimated_tokens_per_character=estimated_tokens_per_character,
49
- image_tokens=image_tokens,
50
- include_attributes=include_attributes,
51
- max_error_length=max_error_length,
52
- max_actions_per_step=max_actions_per_step,
53
- tool_call_in_content=tool_call_in_content,
54
- )
55
- self.use_function_calling = use_function_calling
56
- # Custom: Move Task info to state_message
57
- self.history = MessageHistory()
58
- self._add_message_with_tokens(self.system_prompt)
59
-
60
- if self.use_function_calling:
61
- tool_calls = [
62
- {
63
- 'name': 'CustomAgentOutput',
64
- 'args': {
65
- 'current_state': {
66
- 'prev_action_evaluation': 'Unknown - No previous actions to evaluate.',
67
- 'important_contents': '',
68
- 'completed_contents': '',
69
- 'thought': 'Now Google is open. Need to type OpenAI to search.',
70
- 'summary': 'Type OpenAI to search.',
71
- },
72
- 'action': [],
73
- },
74
- 'id': '',
75
- 'type': 'tool_call',
76
- }
77
- ]
78
- if self.tool_call_in_content:
79
- # openai throws error if tool_calls are not responded -> move to content
80
- example_tool_call = AIMessage(
81
- content=f'{tool_calls}',
82
- tool_calls=[],
83
- )
84
- else:
85
- example_tool_call = AIMessage(
86
- content=f'',
87
- tool_calls=tool_calls,
88
- )
89
-
90
- self._add_message_with_tokens(example_tool_call)
91
-
92
- def cut_messages(self):
93
- """Get current message list, potentially trimmed to max tokens"""
94
- diff = self.history.total_tokens - self.max_input_tokens
95
- while diff > 0 and len(self.history.messages) > 1:
96
- self.history.remove_message(1) # alway remove the oldest one
97
- diff = self.history.total_tokens - self.max_input_tokens
98
-
99
- def add_state_message(
100
- self,
101
- state: BrowserState,
102
- result: Optional[List[ActionResult]] = None,
103
- step_info: Optional[AgentStepInfo] = None,
104
- ) -> None:
105
- """Add browser state as human message"""
106
- # otherwise add state message and result to next message (which will not stay in memory)
107
- state_message = CustomAgentMessagePrompt(
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