Skip to content

Commit b7dcbfe

Browse files
committed
update extract_content action for BrowserUseTool
1 parent 4023555 commit b7dcbfe

1 file changed

Lines changed: 48 additions & 14 deletions

File tree

app/tool/browser_use_tool.py

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -418,17 +418,7 @@ async def execute(
418418

419419
# Create prompt for LLM
420420
prompt_text = """
421-
Your task is to extract the content of the page. You will be given a page and a goal, and you should extract all relevant information around this goal from the page.
422-
423-
Examples of extraction goals:
424-
- Extract all company names
425-
- Extract specific descriptions
426-
- Extract all information about a topic
427-
- Extract links with companies in structured format
428-
- Extract all links
429-
430-
If the goal is vague, summarize the page. Respond in JSON format.
431-
421+
Your task is to extract the content of the page. You will be given a page and a goal, and you should extract all relevant information around this goal from the page. If the goal is vague, summarize the page. Respond in json format.
432422
Extraction goal: {goal}
433423
434424
Page content:
@@ -445,10 +435,54 @@ async def execute(
445435

446436
messages = [Message.user_message(formatted_prompt)]
447437

448-
# Use LLM to extract content based on the goal
449-
response = await self.llm.ask(messages)
438+
# Define extraction function for the tool
439+
extraction_function = {
440+
"type": "function",
441+
"function": {
442+
"name": "extract_content",
443+
"description": "Extract specific information from a webpage based on a goal",
444+
"parameters": {
445+
"type": "object",
446+
"properties": {
447+
"extracted_content": {
448+
"type": "object",
449+
"description": "The content extracted from the page according to the goal",
450+
}
451+
},
452+
"required": ["extracted_content"],
453+
},
454+
},
455+
}
456+
457+
# Use LLM to extract content with required function calling
458+
response = await self.llm.ask_tool(
459+
messages,
460+
tools=[extraction_function],
461+
tool_choice="required",
462+
)
463+
464+
# Extract content from function call response
465+
if (
466+
response
467+
and response.tool_calls
468+
and len(response.tool_calls) > 0
469+
):
470+
# Get the first tool call arguments
471+
tool_call = response.tool_calls[0]
472+
# Parse the JSON arguments
473+
try:
474+
args = json.loads(tool_call.function.arguments)
475+
extracted_content = args.get("extracted_content", {})
476+
# Format extracted content as JSON string
477+
content_json = json.dumps(
478+
extracted_content, indent=2, ensure_ascii=False
479+
)
480+
msg = f"Extracted from page:\n{content_json}\n"
481+
except Exception as e:
482+
msg = f"Error parsing extraction result: {str(e)}\nRaw response: {tool_call.function.arguments}"
483+
else:
484+
msg = "No content was extracted from the page."
450485

451-
msg = f"Extracted from page:\n{response}\n"
452486
return ToolResult(output=msg)
453487
except Exception as e:
454488
# Provide a more helpful error message

0 commit comments

Comments
 (0)