add web intelligent search
This commit is contained in:
parent
7a56dd5f71
commit
3c1c599b60
|
|
@ -99,6 +99,9 @@
|
|||
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-file-select_file_instruction.liquid" />
|
||||
<None Remove="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\functions\get_weather.json" />
|
||||
<None Remove="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\functions\get_fun_events.json" />
|
||||
|
||||
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-intelligent_search.json" />
|
||||
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-web-intelligent_search.fn.liquid" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
@ -215,6 +218,12 @@
|
|||
<Content Include="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\functions\get_fun_events.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-intelligent_search.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-web-intelligent_search.fn.liquid">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ using BotSharp.Core.Messaging;
|
|||
using BotSharp.Core.Routing.Reasoning;
|
||||
using BotSharp.Core.Templating;
|
||||
using BotSharp.Core.Translation;
|
||||
using BotSharp.Core.WebSearch.Hooks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
|
|
@ -67,6 +68,8 @@ public class ConversationPlugin : IBotSharpPlugin
|
|||
services.AddScoped<IExecutor, InstructExecutor>();
|
||||
services.AddScoped<IInstructService, InstructService>();
|
||||
services.AddScoped<ITokenStatistics, TokenStatistics>();
|
||||
|
||||
services.AddScoped<IAgentUtilityHook, WebSearchUtilityHook>();
|
||||
}
|
||||
|
||||
public bool AttachMenu(List<PluginMenuDef> menu)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
using BotSharp.Abstraction.Functions;
|
||||
using BotSharp.Core.MessageHub;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.Core.Demo.Functions;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,68 @@
|
|||
using BotSharp.Abstraction.Functions;
|
||||
using BotSharp.Abstraction.MLTasks;
|
||||
|
||||
namespace BotSharp.Core.WebSearch.Functions;
|
||||
|
||||
public class WebIntelligentSearchFn : IFunctionCallback
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger<WebIntelligentSearchFn> _logger;
|
||||
|
||||
public string Name => "util-web-intelligent_search";
|
||||
public string Indication => "Searching web";
|
||||
|
||||
public WebIntelligentSearchFn(
|
||||
IServiceProvider services,
|
||||
ILogger<WebIntelligentSearchFn> logger)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var routingCtx = _services.GetRequiredService<IRoutingContext>();
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
|
||||
Agent? fromAgent = null;
|
||||
if (!string.IsNullOrEmpty(message.CurrentAgentId))
|
||||
{
|
||||
fromAgent = await agentService.GetAgent(message.CurrentAgentId);
|
||||
}
|
||||
|
||||
var agent = new Agent
|
||||
{
|
||||
Id = fromAgent?.Id ?? BuiltInAgentId.UtilityAssistant,
|
||||
Name = fromAgent?.Name ?? "AI Agent",
|
||||
Instruction = "Please search the websites to handle user's request."
|
||||
};
|
||||
|
||||
var dialogs = routingCtx.GetDialogs();
|
||||
if (dialogs.IsNullOrEmpty())
|
||||
{
|
||||
dialogs = conv.GetDialogHistory();
|
||||
}
|
||||
|
||||
var response = await GetChatCompletion(agent, dialogs);
|
||||
message.Content = response;
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task<string> GetChatCompletion(Agent agent, List<RoleDialogModel> dialogs)
|
||||
{
|
||||
try
|
||||
{
|
||||
var llmProviderService = _services.GetRequiredService<ILlmProviderService>();
|
||||
var completion = CompletionProvider.GetChatCompletion(_services, provider: "openai", model: "gpt-4o-search-preview");
|
||||
var response = await completion.GetChatCompletions(agent, dialogs);
|
||||
return response.Content;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var error = $"Error when searching web.";
|
||||
_logger.LogWarning(ex, $"{error}");
|
||||
return error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
namespace BotSharp.Core.WebSearch.Hooks;
|
||||
|
||||
public class WebSearchUtilityHook : IAgentUtilityHook
|
||||
{
|
||||
private const string WEB_INTELLIGENT_SEARCH_FN = "util-web-intelligent_search";
|
||||
|
||||
public void AddUtilities(List<AgentUtility> utilities)
|
||||
{
|
||||
utilities.Add(new AgentUtility
|
||||
{
|
||||
Category = "web",
|
||||
Name = "intelligent.search",
|
||||
Items = [
|
||||
new UtilityItem
|
||||
{
|
||||
FunctionName = WEB_INTELLIGENT_SEARCH_FN,
|
||||
TemplateName = $"{WEB_INTELLIGENT_SEARCH_FN}.fn"
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"name": "util-web-intelligent_search",
|
||||
"description": "Search websites to handle user's request",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
"required": []
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
Please call "util-web-intelligent_search" if user wants you to search content from websites.
|
||||
|
|
@ -28,7 +28,7 @@ public class ReadImageFn : IFunctionCallback
|
|||
Agent? fromAgent = null;
|
||||
if (!string.IsNullOrEmpty(message.CurrentAgentId))
|
||||
{
|
||||
fromAgent = await agentService.LoadAgent(message.CurrentAgentId);
|
||||
fromAgent = await agentService.GetAgent(message.CurrentAgentId);
|
||||
}
|
||||
|
||||
var agent = new Agent
|
||||
|
|
|
|||
Loading…
Reference in a new issue