BotSharp/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs

49 lines
1.8 KiB
C#
Raw Normal View History

2023-09-08 17:03:49 +00:00
using BotSharp.Abstraction.Agents.Enums;
2023-09-01 22:26:25 +00:00
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Instructs;
using BotSharp.Abstraction.Instructs.Models;
2023-09-07 22:06:41 +00:00
using BotSharp.OpenAPI.ViewModels.Instructs;
2023-09-01 22:26:25 +00:00
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]
public class InstructModeController : ControllerBase, IApiAdapter
{
private readonly IServiceProvider _services;
2023-09-08 17:03:49 +00:00
public InstructModeController(IServiceProvider services)
2023-09-01 22:26:25 +00:00
{
_services = services;
}
[HttpPost("/instruct/{agentId}")]
2023-09-19 18:05:51 +00:00
public async Task<InstructResult> InstructCompletion([FromRoute] string agentId,
2023-09-07 22:06:41 +00:00
[FromBody] InstructMessageModel input)
2023-09-01 22:26:25 +00:00
{
var instructor = _services.GetRequiredService<IInstructService>();
var agentService = _services.GetRequiredService<IAgentService>();
Agent agent = await agentService.LoadAgent(agentId);
2023-09-08 17:03:49 +00:00
// switch to different instruction template
2023-09-19 18:05:51 +00:00
if (!string.IsNullOrEmpty(input.Template))
2023-09-08 17:03:49 +00:00
{
var agentSettings = _services.GetRequiredService<AgentSettings>();
2023-09-19 18:05:51 +00:00
var filePath = Path.Combine(agentService.GetAgentDataDir(agentId), $"{input.Template}.{agentSettings.TemplateFormat}");
2023-09-08 17:03:49 +00:00
agent.Instruction = System.IO.File.ReadAllText(filePath);
}
2023-09-01 22:26:25 +00:00
2023-09-19 18:05:51 +00:00
var conv = _services.GetRequiredService<IConversationService>();
conv.States.SetState("provider", input.Provider)
.SetState("model", input.Model);
2023-09-08 17:03:49 +00:00
return await instructor.ExecuteInstruction(agent,
2023-09-14 01:41:51 +00:00
new RoleDialogModel(AgentRole.User, input.Text),
2023-09-08 17:03:49 +00:00
fn => Task.CompletedTask,
fn => Task.CompletedTask,
fn => Task.CompletedTask);
2023-09-01 22:26:25 +00:00
}
}