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-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-10-23 23:20:18 +00:00
|
|
|
public async Task<InstructResult> Execute(Agent agent, RoleDialogModel message)
|
2023-09-08 17:03:49 +00:00
|
|
|
{
|
|
|
|
|
// Trigger before completion hooks
|
|
|
|
|
var hooks = _services.GetServices<IInstructHook>();
|
|
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
|
|
|
|
await hook.BeforeCompletion(message);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 23:20:18 +00:00
|
|
|
var completer = CompletionProvider.GetTextCompletion(_services);
|
|
|
|
|
|
|
|
|
|
var result = await completer.GetCompletion(agent.Instruction);
|
|
|
|
|
var response = new InstructResult
|
|
|
|
|
{
|
|
|
|
|
Text = result
|
|
|
|
|
};
|
2023-09-08 17:03:49 +00:00
|
|
|
|
|
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
|
|
|
|
await hook.AfterCompletion(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
2023-09-01 22:26:25 +00:00
|
|
|
}
|