forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebDriverService.InferElement.cs
More file actions
49 lines (43 loc) · 1.67 KB
/
WebDriverService.InferElement.cs
File metadata and controls
49 lines (43 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Core.Infrastructures;
namespace BotSharp.Plugin.WebDriver.Services;
public partial class WebDriverService
{
public async Task<HtmlElementContextOut> InferElement(Agent agent, string html, string elementName, string messageId)
{
var parserInstruction = agent.Templates.First(x => x.Name == "html_parser").Content;
var render = _services.GetRequiredService<ITemplateRender>();
var prompt = render.Render(parserInstruction, new Dictionary<string, object>
{
{ "html_content", html },
{ "element_name", elementName }
});
var completer = CompletionProvider.GetCompletion(_services,
agentConfig: agent.LlmConfig);
if (completer is ITextCompletion textCompleter)
{
var result = await textCompleter.GetCompletion(prompt, agent.Id, messageId);
return result.JsonContent<HtmlElementContextOut>();
}
else if (completer is IChatCompletion chatCompleter)
{
var dialogs = new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, prompt)
{
CurrentAgentId = agent.Id,
MessageId = messageId
}
};
var result = await chatCompleter.GetChatCompletions(new Agent
{
Id = agent.Id,
Name = agent.Name,
Instruction = "You're a HTML Parser."
}, dialogs);
return result.Content.JsonContent<HtmlElementContextOut>();
}
return null;
}
}