Skip to content

Commit

Permalink
better context for select and auto-complete
Browse files Browse the repository at this point in the history
  • Loading branch information
LawyZheng committed Sep 12, 2024
1 parent 4a599bb commit 3bd0d2d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Reply in JSON format with the following keys:

Context:
```
{{ context_reasoning }}
Choose an auto-completion suggestion for "{{ field_information }}"
```

Input value:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Reply in JSON format with the following keys:

Context:
```
{{ context_reasoning }}
Choose an auto-completion suggestion for "{{ field_information }}"
```

Current Value:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Reply in JSON format with the following keys:

Context:
```
{{ context_reasoning }}
Choose an auto-completion suggestion for "{{ field_information }}"
```

Current Value:
Expand Down
2 changes: 1 addition & 1 deletion skyvern/forge/prompts/skyvern/custom-select.j2
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Reply in JSON format with the following keys:

Context:
```
{{ context_reasoning }}
Select an option for "{{ field_information }}". It's {{ "a required" if required_field else "an optional" }} field.
```
{% if target_value %}
Target value:
Expand Down
2 changes: 2 additions & 0 deletions skyvern/forge/prompts/skyvern/extract-action.j2
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Reply in JSON format with the following keys:
"reasoning": str, // The reasoning behind the action. Be specific, referencing any user information and their fields and element ids in your reasoning. Mention why you chose the action type, and why you chose the element id. Keep the reasoning short and to the point.
"confidence_float": float, // The confidence of the action. Pick a number between 0.0 and 1.0. 0.0 means no confidence, 1.0 means full confidence
"action_type": str, // It's a string enum: "CLICK", "INPUT_TEXT", "UPLOAD_FILE", "SELECT_OPTION", "WAIT", "SOLVE_CAPTCHA", "COMPLETE", "TERMINATE". "CLICK" is an element you'd like to click. "INPUT_TEXT" is an element you'd like to input text into. "UPLOAD_FILE" is an element you'd like to upload a file into. "SELECT_OPTION" is an element you'd like to select an option from. "WAIT" action should be used if there are no actions to take and there is some indication on screen that waiting could yield more actions. "WAIT" should not be used if there are actions to take. "SOLVE_CAPTCHA" should be used if there's a captcha to solve on the screen. "COMPLETE" is used when the user goal has been achieved AND if there's any data extraction goal, you should be able to get data from the page. Never return a COMPLETE action unless the user goal is achieved. "TERMINATE" is used to terminate the whole task with a failure when it doesn't seem like the user goal can be achieved. Do not use "TERMINATE" if waiting could lead the user towards the goal. Only return "TERMINATE" if you are on a page where the user goal cannot be achieved. All other actions are ignored when "TERMINATE" is returned.
"field_information": str, // The target field for the action. Only for INPUT_TEXT and SELECT_OPTION actions. Otherwise it should be null.
"required_field": bool, // True if it's a required field, otherwise false.
"id": str, // The id of the element to take action on. The id has to be one from the elements list
"text": str, // Text for INPUT_TEXT action only
"file_url": str, // The url of the file to upload if applicable. This field must be present for UPLOAD_FILE but can also be present for CLICK only if the click is to upload the file. It should be null otherwise.
Expand Down
16 changes: 16 additions & 0 deletions skyvern/webeye/actions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def __repr__(self) -> str:

class Action(BaseModel):
action_type: ActionType
field_information: str | None = None
required_field: bool | None = None
confidence_float: float | None = None
description: str | None = None
reasoning: str | None = None
Expand Down Expand Up @@ -160,6 +162,8 @@ def parse_action(action: Dict[str, Any], data_extraction_goal: str | None = None

reasoning = action["reasoning"] if "reasoning" in action else None
confidence_float = action["confidence_float"] if "confidence_float" in action else None
field_information = action["field_information"] if "field_information" in action else None
required_field = action["required_field"] if "required_field" in action else None

if "action_type" not in action or action["action_type"] is None:
return NullAction(reasoning=reasoning, confidence_float=confidence_float)
Expand All @@ -177,6 +181,8 @@ def parse_action(action: Dict[str, Any], data_extraction_goal: str | None = None
if action_type == ActionType.CLICK:
file_url = action["file_url"] if "file_url" in action else None
return ClickAction(
field_information=field_information,
required_field=required_field,
element_id=element_id,
reasoning=reasoning,
confidence_float=confidence_float,
Expand All @@ -186,6 +192,8 @@ def parse_action(action: Dict[str, Any], data_extraction_goal: str | None = None

if action_type == ActionType.INPUT_TEXT:
return InputTextAction(
field_information=field_information,
required_field=required_field,
element_id=element_id,
text=action["text"],
reasoning=reasoning,
Expand All @@ -195,6 +203,8 @@ def parse_action(action: Dict[str, Any], data_extraction_goal: str | None = None
if action_type == ActionType.UPLOAD_FILE:
# TODO: see if the element is a file input element. if it's not, convert this action into a click action
return UploadFileAction(
field_information=field_information,
required_field=required_field,
element_id=element_id,
confidence_float=confidence_float,
file_url=action["file_url"],
Expand All @@ -204,6 +214,8 @@ def parse_action(action: Dict[str, Any], data_extraction_goal: str | None = None
# This action is not used in the current implementation. Click actions are used instead.
if action_type == ActionType.DOWNLOAD_FILE:
return DownloadFileAction(
field_information=field_information,
required_field=required_field,
element_id=element_id,
file_name=action["file_name"],
reasoning=reasoning,
Expand All @@ -220,6 +232,8 @@ def parse_action(action: Dict[str, Any], data_extraction_goal: str | None = None
if label is None and value is None and index is None:
raise ValueError("At least one of 'label', 'value', or 'index' must be provided for a SelectOption")
return SelectOptionAction(
field_information=field_information,
required_field=required_field,
element_id=element_id,
option=SelectOption(
label=label,
Expand All @@ -232,6 +246,8 @@ def parse_action(action: Dict[str, Any], data_extraction_goal: str | None = None

if action_type == ActionType.CHECKBOX:
return CheckboxAction(
field_information=field_information,
required_field=required_field,
element_id=element_id,
is_checked=action["is_checked"],
reasoning=reasoning,
Expand Down
25 changes: 18 additions & 7 deletions skyvern/webeye/actions/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,11 @@ async def handle_input_text_action(
if skyvern_element.get_tag_name() == InteractiveElement.INPUT and not await skyvern_element.is_spinbtn_input():
await skyvern_element.scroll_into_view()
select_action = SelectOptionAction(
reasoning=action.reasoning, element_id=skyvern_element.get_id(), option=SelectOption(label=text)
field_information=action.field_information,
required_field=action.required_field,
reasoning=action.reasoning,
element_id=skyvern_element.get_id(),
option=SelectOption(label=text),
)
if skyvern_element.get_selectable():
LOG.info(
Expand Down Expand Up @@ -685,7 +689,13 @@ async def handle_select_option_action(
tag_name=selectable_child.get_tag_name(),
element_id=selectable_child.get_id(),
)
select_action = SelectOptionAction(element_id=selectable_child.get_id(), option=action.option)
select_action = SelectOptionAction(
reasoning=action.reasoning,
field_information=action.field_information,
required_field=action.required_field,
element_id=selectable_child.get_id(),
option=action.option,
)
return await handle_select_option_action(select_action, page, scraped_page, task, step)

if tag_name == InteractiveElement.SELECT:
Expand Down Expand Up @@ -1122,7 +1132,7 @@ async def choose_auto_completion_dropdown(
html = incremental_scraped.build_html_tree(incremental_element)
auto_completion_confirm_prompt = prompt_engine.load_prompt(
"auto-completion-choose-option",
context_reasoning=action.reasoning,
field_information=action.field_information,
filled_value=text,
navigation_goal=task.navigation_goal,
navigation_payload_str=json.dumps(task.navigation_payload),
Expand Down Expand Up @@ -1241,7 +1251,7 @@ async def input_or_auto_complete_input(

prompt = prompt_engine.load_prompt(
"auto-completion-potential-answers",
context_reasoning=context_reasoning,
field_information=action.field_information,
current_value=current_value,
)

Expand Down Expand Up @@ -1296,7 +1306,7 @@ async def input_or_auto_complete_input(
)
prompt = prompt_engine.load_prompt(
"auto-completion-tweak-value",
context_reasoning=context_reasoning,
field_information=action.field_information,
current_value=current_value,
tried_values=json.dumps(tried_values),
popped_up_elements="".join([json_to_html(element) for element in whole_new_elements]),
Expand All @@ -1310,7 +1320,7 @@ async def input_or_auto_complete_input(
"Ask LLM tweaked the current value with a new value",
step_id=step.step_id,
task_id=task.task_id,
reasoning=context_reasoning,
field_information=action.field_information,
current_value=current_value,
new_value=new_current_value,
)
Expand Down Expand Up @@ -1485,7 +1495,8 @@ async def select_from_dropdown(

prompt = prompt_engine.load_prompt(
"custom-select",
context_reasoning=action.reasoning,
field_information=action.field_information,
required_field=action.required_field,
target_value=target_value if not force_select and should_relevant else "",
navigation_goal=task.navigation_goal,
navigation_payload_str=json.dumps(task.navigation_payload),
Expand Down

0 comments on commit 3bd0d2d

Please sign in to comment.