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

55 lines
1.9 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-10-13 20:08:59 +00:00
using BotSharp.Core.Infrastructures;
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
{
2023-10-16 21:56:32 +00:00
agent.Instruction = agent.Templates.First(x => x.Name == input.Template).Content;
2023-09-08 17:03:49 +00:00
}
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
}
2023-10-13 20:08:59 +00:00
[HttpPost("/instruct/text-completion")]
public async Task<string> TextCompletion([FromBody] IncomingMessageModel message)
{
var textCompletion = CompletionProvider.GetTextCompletion(_services);
return await textCompletion.GetCompletion(message.Text);
}
2023-09-01 22:26:25 +00:00
}