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

45 lines
1.6 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}")]
public async Task<InstructResult> NewConversation([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
if (!string.IsNullOrEmpty(input.TemplateName))
{
var agentSettings = _services.GetRequiredService<AgentSettings>();
var filePath = Path.Combine(agentService.GetAgentDataDir(agentId), $"{input.TemplateName}.{agentSettings.TemplateFormat}");
agent.Instruction = System.IO.File.ReadAllText(filePath);
}
2023-09-01 22:26:25 +00:00
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
}
}