BotSharp/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs

94 lines
2.8 KiB
C#
Raw Normal View History

2023-09-01 22:26:25 +00:00
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Instructs;
2023-09-08 17:03:49 +00:00
using BotSharp.Abstraction.Instructs.Models;
2023-12-15 16:31:11 +00:00
using BotSharp.Abstraction.MLTasks;
2023-09-01 22:26:25 +00:00
namespace BotSharp.Core.Instructs;
public partial class InstructService : IInstructService
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
public InstructService(IServiceProvider services, ILogger<InstructService> logger)
{
_services = services;
_logger = logger;
}
2023-12-15 17:54:44 +00:00
public async Task<InstructResult> Execute(string agentId, RoleDialogModel message, string? templateName = null, string? instruction = null)
2023-09-08 17:03:49 +00:00
{
var agentService = _services.GetRequiredService<IAgentService>();
Agent agent = await agentService.LoadAgent(agentId);
2023-09-08 17:03:49 +00:00
// Trigger before completion hooks
var hooks = _services.GetServices<IInstructHook>();
foreach (var hook in hooks)
{
2023-10-28 20:59:26 +00:00
if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId)
2023-10-25 15:48:25 +00:00
{
continue;
}
await hook.BeforeCompletion(agent, message);
// Interrupted by hook
if (message.StopCompletion)
{
return new InstructResult
{
2023-10-27 15:18:48 +00:00
MessageId = message.MessageId,
Text = message.Content
};
}
2023-09-08 17:03:49 +00:00
}
2023-10-28 20:59:26 +00:00
// Render prompt
var prompt = string.IsNullOrEmpty(templateName) ?
2023-10-28 20:59:26 +00:00
agentService.RenderedInstruction(agent) :
agentService.RenderedTemplate(agent, templateName);
2023-12-15 17:54:44 +00:00
var completer = CompletionProvider.GetCompletion(_services,
agentConfig: agent.LlmConfig);
2023-10-23 23:20:18 +00:00
var response = new InstructResult
{
2023-12-15 16:31:11 +00:00
MessageId = message.MessageId
2023-10-23 23:20:18 +00:00
};
2023-12-15 16:31:11 +00:00
if (completer is ITextCompletion textCompleter)
{
var result = await textCompleter.GetCompletion(prompt, agentId, message.MessageId);
response.Text = result;
}
else if (completer is IChatCompletion chatCompleter)
{
var result = chatCompleter.GetChatCompletions(new Agent
{
Id = agentId,
2023-12-15 17:54:44 +00:00
Name = agent.Name,
Instruction = instruction
2023-12-15 16:31:11 +00:00
}, new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, prompt)
{
CurrentAgentId = agentId,
MessageId = message.MessageId
}
});
response.Text = result.Content;
}
2023-09-08 17:03:49 +00:00
foreach (var hook in hooks)
{
2023-10-28 20:59:26 +00:00
if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId)
2023-10-25 15:48:25 +00:00
{
continue;
}
await hook.AfterCompletion(agent, response);
2023-09-08 17:03:49 +00:00
}
return response;
}
2023-09-01 22:26:25 +00:00
}