2023-12-12 18:37:49 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
2023-09-01 22:26:25 +00:00
|
|
|
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]
|
2023-12-27 15:53:54 +00:00
|
|
|
public class InstructModeController : ControllerBase
|
2023-09-01 22:26:25 +00:00
|
|
|
{
|
|
|
|
|
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)
|
2023-12-15 17:54:44 +00:00
|
|
|
.SetState("instruction", input.Instruction)
|
2023-10-23 23:20:18 +00:00
|
|
|
.SetState("input_text", input.Text);
|
|
|
|
|
|
|
|
|
|
var instructor = _services.GetRequiredService<IInstructService>();
|
2023-10-28 20:59:26 +00:00
|
|
|
var result = await instructor.Execute(agentId,
|
|
|
|
|
new RoleDialogModel(AgentRole.User, input.Text),
|
2023-12-15 17:54:44 +00:00
|
|
|
templateName: input.Template,
|
|
|
|
|
instruction: input.Instruction);
|
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-12-05 00:12:57 +00:00
|
|
|
return await textCompletion.GetCompletion(input.Text, Guid.Empty.ToString(), Guid.NewGuid().ToString());
|
2023-10-13 20:08:59 +00:00
|
|
|
}
|
2023-12-12 18:32:04 +00:00
|
|
|
|
|
|
|
|
[HttpPost("/instruct/chat-completion")]
|
|
|
|
|
public async Task<string> ChatCompletion([FromBody] IncomingMessageModel input)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
var textCompletion = CompletionProvider.GetChatCompletion(_services);
|
2024-01-14 04:48:26 +00:00
|
|
|
var message = await textCompletion.GetChatCompletions(new Agent()
|
2023-12-12 18:32:04 +00:00
|
|
|
{
|
|
|
|
|
Id = Guid.Empty.ToString(),
|
|
|
|
|
}, new List<RoleDialogModel>
|
|
|
|
|
{
|
|
|
|
|
new RoleDialogModel(AgentRole.User, input.Text)
|
2024-01-14 04:48:26 +00:00
|
|
|
});
|
|
|
|
|
return message.Content;
|
2023-12-12 18:32:04 +00:00
|
|
|
}
|
2023-09-01 22:26:25 +00:00
|
|
|
}
|