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-23 23:20:18 +00:00
|
|
|
using BotSharp.Abstraction.Templating;
|
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
|
|
|
{
|
2023-10-23 23:20:18 +00:00
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
input.States.ForEach(x => state.SetState(x.Split('=')[0], x.Split('=')[1]));
|
|
|
|
|
state.SetState("provider", input.Provider)
|
|
|
|
|
.SetState("model", input.Model)
|
|
|
|
|
.SetState("input_text", input.Text);
|
|
|
|
|
|
2023-09-01 22:26:25 +00:00
|
|
|
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-23 23:20:18 +00:00
|
|
|
var template = agent.Templates.First(x => x.Name == input.Template).Content;
|
|
|
|
|
var render = _services.GetRequiredService<ITemplateRender>();
|
|
|
|
|
var dict = new Dictionary<string, object>();
|
|
|
|
|
state.GetStates().Select(x => dict[x.Key] = x.Value).ToArray();
|
|
|
|
|
var prompt = render.Render(template, dict);
|
|
|
|
|
agent.Instruction = prompt;
|
2023-09-08 17:03:49 +00:00
|
|
|
}
|
2023-09-01 22:26:25 +00:00
|
|
|
|
2023-10-23 23:20:18 +00:00
|
|
|
var instructor = _services.GetRequiredService<IInstructService>();
|
2023-10-25 00:36:02 +00:00
|
|
|
var result = await instructor.Execute(agent,
|
2023-10-23 23:20:18 +00:00
|
|
|
new RoleDialogModel(AgentRole.User, input.Text));
|
2023-10-25 00:36:02 +00:00
|
|
|
|
|
|
|
|
result.States = state.GetStates();
|
|
|
|
|
|
|
|
|
|
return result;
|
2023-09-01 22:26:25 +00:00
|
|
|
}
|
2023-10-13 20:08:59 +00:00
|
|
|
|
|
|
|
|
[HttpPost("/instruct/text-completion")]
|
2023-10-23 00:37:33 +00:00
|
|
|
public async Task<string> TextCompletion([FromBody] IncomingMessageModel input)
|
2023-10-13 20:08:59 +00:00
|
|
|
{
|
2023-10-23 23:20:18 +00:00
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
input.States.ForEach(x => state.SetState(x.Split('=')[0], x.Split('=')[1]));
|
|
|
|
|
state.SetState("provider", input.Provider)
|
2023-10-23 00:37:33 +00:00
|
|
|
.SetState("model", input.Model);
|
|
|
|
|
|
2023-10-13 20:08:59 +00:00
|
|
|
var textCompletion = CompletionProvider.GetTextCompletion(_services);
|
2023-10-23 00:37:33 +00:00
|
|
|
return await textCompletion.GetCompletion(input.Text);
|
2023-10-13 20:08:59 +00:00
|
|
|
}
|
2023-09-01 22:26:25 +00:00
|
|
|
}
|