|
6 | 6 | from browser_use.browser.views import BrowserState
|
7 | 7 | from langchain_core.messages import HumanMessage, SystemMessage
|
8 | 8 | from datetime import datetime
|
| 9 | +import importlib |
9 | 10 |
|
10 | 11 | from .custom_views import CustomAgentStepInfo
|
11 | 12 |
|
12 | 13 |
|
13 | 14 | class CustomSystemPrompt(SystemPrompt):
|
14 |
| - def important_rules(self) -> str: |
15 |
| - """ |
16 |
| - Returns the important rules for the agent. |
17 |
| - """ |
18 |
| - text = r""" |
19 |
| -1. RESPONSE FORMAT: You must ALWAYS respond with valid JSON in this exact format: |
20 |
| - { |
21 |
| - "current_state": { |
22 |
| - "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.", |
23 |
| - "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 ''.", |
24 |
| - "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.", |
25 |
| - "next_goal": "Please generate a brief natural language description for the goal of your next actions based on your thought." |
26 |
| - }, |
27 |
| - "action": [ |
28 |
| - * actions in sequences, please refer to **Common action sequences**. Each output action MUST be formated as: \{action_name\: action_params\}* |
29 |
| - ] |
30 |
| - } |
31 |
| -
|
32 |
| -2. ACTIONS: You can specify multiple actions to be executed in sequence. |
33 |
| -
|
34 |
| - Common action sequences: |
35 |
| - - Form filling: [ |
36 |
| - {"input_text": {"index": 1, "text": "username"}}, |
37 |
| - {"input_text": {"index": 2, "text": "password"}}, |
38 |
| - {"click_element": {"index": 3}} |
39 |
| - ] |
40 |
| - - Navigation and extraction: [ |
41 |
| - {"go_to_url": {"url": "https://example.com"}}, |
42 |
| - {"extract_page_content": {"goal": "extract the names"}} |
43 |
| - ] |
44 |
| -
|
45 |
| -
|
46 |
| -3. ELEMENT INTERACTION: |
47 |
| - - Only use indexes that exist in the provided element list |
48 |
| - - Elements marked with "[]Non-interactive text" are non-interactive |
49 |
| -
|
50 |
| -4. NAVIGATION & ERROR HANDLING: |
51 |
| - - If no suitable elements exist, use other functions to complete the task |
52 |
| - - If stuck, try alternative approaches |
53 |
| - - Handle popups/cookies by accepting or closing them |
54 |
| - - Use scroll to find elements you are looking for |
55 |
| -
|
56 |
| -5. TASK COMPLETION: |
57 |
| - - Use the done action as the last action as soon as the ultimate task is complete |
58 |
| - - Dont use "done" before you are done with everything the user asked you, except you reach the last step of max_steps. |
59 |
| - - If you reach your last step, use the done action even if the task is not fully finished. Provide all the information you have gathered so far. If the ultimate task is completly finished set success to true. If not everything the user asked for is completed set success in done to false! |
60 |
| - - If you have to do something repeatedly for example the task says for "each", or "for all", or "x times", count always inside "memory" how many times you have done it and how many remain. Don't stop until you have completed like the task asked you. Only call done after the last step. |
61 |
| - - Don't hallucinate actions |
62 |
| - - Make sure you include everything you found out for the ultimate task in the done text parameter. Do not just say you are done, but include the requested information of the task. |
63 |
| -
|
64 |
| -6. VISUAL CONTEXT: |
65 |
| - - When an image is provided, use it to understand the page layout |
66 |
| - - Bounding boxes with labels on their top right corner correspond to element indexes |
67 |
| -
|
68 |
| -7. Form filling: |
69 |
| - - If you fill an input field and your action sequence is interrupted, most often something changed e.g. suggestions popped up under the field. |
70 |
| -
|
71 |
| -8. Long tasks: |
72 |
| - - Keep track of the status and subresults in the memory. |
73 |
| -
|
74 |
| -9. Extraction: |
75 |
| - - If your task is to find information - call extract_content on the specific pages to get and store the information. |
76 |
| -
|
77 |
| -""" |
78 |
| - text += f" - use maximum {self.max_actions_per_step} actions per sequence" |
79 |
| - return text |
80 |
| - |
81 |
| - def input_format(self) -> str: |
82 |
| - return """ |
83 |
| -INPUT STRUCTURE: |
84 |
| -1. Task: The user\'s instructions you need to complete. |
85 |
| -2. Hints(Optional): Some hints to help you complete the user\'s instructions. |
86 |
| -3. Memory: Important contents are recorded during historical operations for use in subsequent operations. |
87 |
| -4. Current URL: The webpage you're currently on |
88 |
| -5. Available Tabs: List of open browser tabs |
89 |
| -6. Interactive Elements: List in the format: |
90 |
| - [index]<element_type>element_text</element_type> |
91 |
| - - index: Numeric identifier for interaction |
92 |
| - - element_type: HTML element type (button, input, etc.) |
93 |
| - - element_text: Visible text or element description |
94 |
| -
|
95 |
| -Example: |
96 |
| -[33]<button>Submit Form</button> |
97 |
| -[] Non-interactive text |
98 |
| -
|
99 |
| -
|
100 |
| -Notes: |
101 |
| -- Only elements with numeric indexes inside [] are interactive |
102 |
| -- [] elements provide context but cannot be interacted with |
103 |
| - """ |
| 15 | + def _load_prompt_template(self) -> None: |
| 16 | + """Load the prompt template from the markdown file.""" |
| 17 | + try: |
| 18 | + # This works both in development and when installed as a package |
| 19 | + with importlib.resources.files('src.agent').joinpath('custom_system_prompt.md').open('r') as f: |
| 20 | + self.prompt_template = f.read() |
| 21 | + except Exception as e: |
| 22 | + raise RuntimeError(f'Failed to load system prompt template: {e}') |
104 | 23 |
|
105 | 24 |
|
106 | 25 | class CustomAgentMessagePrompt(AgentMessagePrompt):
|
|
0 commit comments