2025-03-17 19:13:31 +00:00
|
|
|
using BotSharp.Abstraction.Functions;
|
2025-03-17 22:02:24 +00:00
|
|
|
using BotSharp.Abstraction.Instructs;
|
2025-03-17 19:13:31 +00:00
|
|
|
using BotSharp.Abstraction.Instructs.Models;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Instructs.Functions;
|
|
|
|
|
|
|
|
|
|
public class ExecuteTemplateFn : IFunctionCallback
|
|
|
|
|
{
|
|
|
|
|
public string Name => "util-instruct-execute_template";
|
|
|
|
|
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly ILogger<ExecuteTemplateFn> _logger;
|
|
|
|
|
|
|
|
|
|
public ExecuteTemplateFn(
|
|
|
|
|
IServiceProvider services,
|
|
|
|
|
ILogger<ExecuteTemplateFn> logger)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> Execute(RoleDialogModel message)
|
|
|
|
|
{
|
|
|
|
|
var args = JsonSerializer.Deserialize<ExecuteTemplateArgs>(message.FunctionArgs);
|
|
|
|
|
if (string.IsNullOrEmpty(args.TemplateName))
|
|
|
|
|
{
|
2025-03-17 22:02:24 +00:00
|
|
|
message.Content = $"Invalid template name.";
|
2025-03-17 19:13:31 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
var agent = await agentService.GetAgent(message.CurrentAgentId);
|
|
|
|
|
var template = agent.Templates.FirstOrDefault(x => x.Name.IsEqualTo(args.TemplateName));
|
|
|
|
|
|
|
|
|
|
if (template == null)
|
|
|
|
|
{
|
|
|
|
|
message.Content = $"Cannot find template ({args.TemplateName}) in agent {agent.Name}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 22:02:24 +00:00
|
|
|
var response = await GetAiResponse(agent, args.TemplateName);
|
2025-03-17 19:13:31 +00:00
|
|
|
message.Content = response;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 22:02:24 +00:00
|
|
|
private async Task<string> GetAiResponse(Agent agent, string templateName)
|
2025-03-17 19:13:31 +00:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-03-17 22:02:24 +00:00
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
var text = agentService.RenderedTemplate(agent, templateName);
|
|
|
|
|
|
2025-03-17 19:13:31 +00:00
|
|
|
var completion = CompletionProvider.GetChatCompletion(_services, provider: agent.LlmConfig?.Provider, model: agent.LlmConfig?.Model);
|
|
|
|
|
var response = await completion.GetChatCompletions(new Agent()
|
|
|
|
|
{
|
|
|
|
|
Id = agent.Id
|
|
|
|
|
},
|
|
|
|
|
new List<RoleDialogModel>
|
|
|
|
|
{
|
|
|
|
|
new(AgentRole.User, text)
|
|
|
|
|
});
|
2025-03-17 22:02:24 +00:00
|
|
|
|
|
|
|
|
var hooks = _services.GetServices<IInstructHook>();
|
|
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
2025-03-19 04:32:06 +00:00
|
|
|
if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agent.Id)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 22:02:24 +00:00
|
|
|
await hook.OnResponseGenerated(new InstructResponseModel
|
|
|
|
|
{
|
|
|
|
|
AgentId = agent.Id,
|
|
|
|
|
TemplateName = templateName,
|
|
|
|
|
Provider = completion.Provider,
|
|
|
|
|
Model = completion.Model,
|
|
|
|
|
UserMessage = text,
|
|
|
|
|
CompletionText = response.Content
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 19:13:31 +00:00
|
|
|
return response.Content;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error when getting agent {agent.Name} instruction response.";
|
|
|
|
|
_logger.LogWarning($"{error} {ex.Message}\r\n{ex.InnerException}");
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|