Add Extract Data function

This commit is contained in:
Wenbo Cao 2025-03-27 15:50:23 -05:00
parent 8eb2778e5b
commit cc4e757fe8
5 changed files with 77 additions and 4 deletions

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
@ -31,6 +31,9 @@
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-close_browser.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-extract_data_from_page.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-go_to_page.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
@ -103,4 +106,10 @@
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-extract-data.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View file

@ -9,8 +9,11 @@ public partial class PlaywrightWebDriver
await Task.Delay(3000);
// Retrieve the page raw html and infer the element path
var body = await _instance.GetPage(actionParams.ContextId).QuerySelectorAsync("body");
var content = await body.InnerTextAsync();
var pageContent = _instance.GetPage(actionParams.ContextId);
var body = await pageContent.QuerySelectorAsync("body");
var pageUrl = pageContent.Url;
var pageBody = await body.InnerTextAsync();
string content = $"PageUrl: {pageUrl} <br/> PageBody: {pageBody}";
var driverService = _services.GetRequiredService<WebDriverService>();
var answer = await driverService.ExtraData(actionParams.Agent, content, actionParams.Context.Question, actionParams.MessageId);

View file

@ -7,6 +7,7 @@ public class WebUtilityHook : IAgentUtilityHook
private const string GO_TO_PAGE_FN = $"{PREFIX}go_to_page";
private const string LOCATE_ELEMENT_FN = $"{PREFIX}locate_element";
private const string ACTION_ON_ELEMENT_FN = $"{PREFIX}action_on_element";
private const string EXTRACT_DATA_FN = $"{PREFIX}extract_data_from_page";
public void AddUtilities(List<AgentUtility> utilities)
{
@ -20,7 +21,8 @@ public class WebUtilityHook : IAgentUtilityHook
new(CLOSE_BROWSER_FN),
new(GO_TO_PAGE_FN),
new(LOCATE_ELEMENT_FN),
new(ACTION_ON_ELEMENT_FN)
new(ACTION_ON_ELEMENT_FN),
new(EXTRACT_DATA_FN)
],
Templates =
[

View file

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Playwright;
namespace BotSharp.Plugin.WebDriver.UtilFunctions
{
public class UtilWebExtractDataFn : IFunctionCallback
{
public string Name => "util-web-extract_data_from_page";
public string Indication => "Util Web Extract Data from web page";
private readonly IServiceProvider _services;
public UtilWebExtractDataFn(
IServiceProvider services)
{
_services = services;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var convService = _services.GetRequiredService<IConversationService>();
var args = JsonSerializer.Deserialize<BrowsingContextIn>(message.FunctionArgs);
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(message.CurrentAgentId);
var _browser = _services.GetRequiredService<IWebBrowser>();
var webDriverService = _services.GetRequiredService<WebDriverService>();
var contextId = webDriverService.GetMessageContext(message);
var browerActionParams = new BrowserActionParams(agent, args, contextId, message.MessageId);
message.Content = await _browser.ExtractData(browerActionParams);
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
message.Data = await _browser.ScreenshotAsync(new MessageInfo
{
AgentId = message.CurrentAgentId,
ContextId = contextId,
MessageId = message.MessageId
}, path);
return true;
}
}
}

View file

@ -0,0 +1,14 @@
{
"name": "util-web-extract_data_from_page",
"description": "Extract data from current web page.",
"parameters": {
"type": "object",
"properties": {
"question": {
"type": "string",
"description": "the information user wants to know"
}
},
"required": [ "question" ]
}
}