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;
|
2024-08-26 22:24:07 +00:00
|
|
|
using static System.Net.Mime.MediaTypeNames;
|
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;
|
2024-05-14 16:51:39 +00:00
|
|
|
private readonly ILogger<InstructModeController> _logger;
|
2023-09-01 22:26:25 +00:00
|
|
|
|
2024-05-14 16:51:39 +00:00
|
|
|
public InstructModeController(IServiceProvider services, ILogger<InstructModeController> logger)
|
2023-09-01 22:26:25 +00:00
|
|
|
{
|
|
|
|
|
_services = services;
|
2024-05-14 16:51:39 +00:00
|
|
|
_logger = logger;
|
2023-09-01 22:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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>();
|
2024-04-05 18:53:37 +00:00
|
|
|
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
state.SetState("provider", input.Provider, source: StateSource.External)
|
|
|
|
|
.SetState("model", input.Model, source: StateSource.External)
|
|
|
|
|
.SetState("model_id", input.ModelId, source: StateSource.External)
|
|
|
|
|
.SetState("instruction", input.Instruction, source: StateSource.External)
|
|
|
|
|
.SetState("input_text", input.Text,source: StateSource.External);
|
2023-10-23 23:20:18 +00:00
|
|
|
|
|
|
|
|
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>();
|
2024-04-05 18:53:37 +00:00
|
|
|
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
2024-07-24 15:03:12 +00:00
|
|
|
state.SetState("provider", input.Provider ?? "azure-openai", source: StateSource.External)
|
2024-04-05 18:53:37 +00:00
|
|
|
.SetState("model", input.Model, source: StateSource.External)
|
|
|
|
|
.SetState("model_id", input.ModelId, source: StateSource.External);
|
2023-10-23 00:37:33 +00:00
|
|
|
|
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
|
|
|
|
2024-07-19 17:01:07 +00:00
|
|
|
#region Chat
|
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>();
|
2024-04-05 18:53:37 +00:00
|
|
|
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
state.SetState("provider", input.Provider, source: StateSource.External)
|
|
|
|
|
.SetState("model", input.Model, source: StateSource.External)
|
|
|
|
|
.SetState("model_id", input.ModelId, source: StateSource.External);
|
2023-12-12 18:32:04 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2024-07-19 17:01:07 +00:00
|
|
|
#endregion
|
2024-05-14 16:51:39 +00:00
|
|
|
|
2024-07-19 17:01:07 +00:00
|
|
|
#region Read image
|
2024-05-14 16:51:39 +00:00
|
|
|
[HttpPost("/instruct/multi-modal")]
|
|
|
|
|
public async Task<string> MultiModalCompletion([FromBody] IncomingMessageModel input)
|
|
|
|
|
{
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-08-07 21:56:57 +00:00
|
|
|
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
2024-08-26 22:24:07 +00:00
|
|
|
var content = await fileInstruct.ReadImages(input.Provider, input.Model, input.Text, input.Files);
|
|
|
|
|
return content;
|
2024-05-14 16:51:39 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-06-24 19:32:52 +00:00
|
|
|
var error = $"Error in analyzing files. {ex.Message}";
|
|
|
|
|
_logger.LogError(error);
|
|
|
|
|
return error;
|
2024-05-14 16:51:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-19 17:01:07 +00:00
|
|
|
#endregion
|
2024-06-24 18:03:05 +00:00
|
|
|
|
2024-07-19 17:01:07 +00:00
|
|
|
#region Generate image
|
2024-06-24 18:03:05 +00:00
|
|
|
[HttpPost("/instruct/image-generation")]
|
|
|
|
|
public async Task<ImageGenerationViewModel> ImageGeneration([FromBody] IncomingMessageModel input)
|
|
|
|
|
{
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
var imageViewModel = new ImageGenerationViewModel();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-08-07 21:56:57 +00:00
|
|
|
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
|
|
|
|
var message = await fileInstruct.GenerateImage(input.Provider, input.Model, input.Text);
|
2024-07-01 16:03:42 +00:00
|
|
|
imageViewModel.Content = message.Content;
|
|
|
|
|
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
|
2024-06-24 18:03:05 +00:00
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-06-24 19:32:52 +00:00
|
|
|
var error = $"Error in image generation. {ex.Message}";
|
|
|
|
|
_logger.LogError(error);
|
2024-06-24 18:03:05 +00:00
|
|
|
imageViewModel.Message = error;
|
|
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-19 17:01:07 +00:00
|
|
|
#endregion
|
2024-07-01 19:31:48 +00:00
|
|
|
|
2024-07-19 17:01:07 +00:00
|
|
|
#region Edit image
|
2024-07-18 22:07:14 +00:00
|
|
|
[HttpPost("/instruct/image-variation")]
|
|
|
|
|
public async Task<ImageGenerationViewModel> ImageVariation([FromBody] IncomingMessageModel input)
|
|
|
|
|
{
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
var imageViewModel = new ImageGenerationViewModel();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-07-19 17:01:07 +00:00
|
|
|
var image = input.Files.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.FileUrl) || !string.IsNullOrWhiteSpace(x.FileData));
|
|
|
|
|
if (image == null)
|
2024-07-19 03:49:24 +00:00
|
|
|
{
|
|
|
|
|
return new ImageGenerationViewModel { Message = "Error! Cannot find an image!" };
|
|
|
|
|
}
|
2024-08-07 21:56:57 +00:00
|
|
|
|
|
|
|
|
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
|
|
|
|
var message = await fileInstruct.VaryImage(input.Provider, input.Model, image);
|
2024-07-18 22:07:14 +00:00
|
|
|
imageViewModel.Content = message.Content;
|
|
|
|
|
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
|
|
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error in image variation. {ex.Message}";
|
|
|
|
|
_logger.LogError(error);
|
|
|
|
|
imageViewModel.Message = error;
|
|
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-18 20:17:40 +00:00
|
|
|
|
2024-07-19 17:01:07 +00:00
|
|
|
[HttpPost("/instruct/image-edit")]
|
|
|
|
|
public async Task<ImageGenerationViewModel> ImageEdit([FromBody] IncomingMessageModel input)
|
|
|
|
|
{
|
2024-08-07 21:56:57 +00:00
|
|
|
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
2024-07-19 17:01:07 +00:00
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
var imageViewModel = new ImageGenerationViewModel();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var image = input.Files.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.FileUrl) || !string.IsNullOrWhiteSpace(x.FileData));
|
|
|
|
|
if (image == null)
|
|
|
|
|
{
|
2024-08-26 22:24:07 +00:00
|
|
|
return new ImageGenerationViewModel { Message = "Error! Cannot find a valid image file!" };
|
2024-07-19 17:01:07 +00:00
|
|
|
}
|
2024-08-07 21:56:57 +00:00
|
|
|
var message = await fileInstruct.EditImage(input.Provider, input.Model, input.Text, image);
|
2024-07-19 17:01:07 +00:00
|
|
|
imageViewModel.Content = message.Content;
|
|
|
|
|
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
|
|
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error in image edit. {ex.Message}";
|
|
|
|
|
_logger.LogError(error);
|
|
|
|
|
imageViewModel.Message = error;
|
|
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("/instruct/image-mask-edit")]
|
|
|
|
|
public async Task<ImageGenerationViewModel> ImageMaskEdit([FromBody] IncomingMessageModel input)
|
|
|
|
|
{
|
2024-08-07 21:56:57 +00:00
|
|
|
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
2024-07-19 17:01:07 +00:00
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
var imageViewModel = new ImageGenerationViewModel();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var image = input.Files.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.FileUrl) || !string.IsNullOrWhiteSpace(x.FileData));
|
|
|
|
|
var mask = input.Mask;
|
|
|
|
|
if (image == null || mask == null)
|
|
|
|
|
{
|
2024-08-26 22:24:07 +00:00
|
|
|
return new ImageGenerationViewModel { Message = "Error! Cannot find a valid image or mask!" };
|
2024-07-19 17:01:07 +00:00
|
|
|
}
|
2024-08-07 21:56:57 +00:00
|
|
|
var message = await fileInstruct.EditImage(input.Provider, input.Model, input.Text, image, mask);
|
2024-07-19 17:01:07 +00:00
|
|
|
imageViewModel.Content = message.Content;
|
|
|
|
|
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
|
|
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error in image mask edit. {ex.Message}";
|
|
|
|
|
_logger.LogError(error);
|
|
|
|
|
imageViewModel.Message = error;
|
|
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Pdf
|
2024-07-01 19:31:48 +00:00
|
|
|
[HttpPost("/instruct/pdf-completion")]
|
|
|
|
|
public async Task<PdfCompletionViewModel> PdfCompletion([FromBody] IncomingMessageModel input)
|
|
|
|
|
{
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
var viewModel = new PdfCompletionViewModel();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-08-07 21:56:57 +00:00
|
|
|
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
|
|
|
|
var content = await fileInstruct.ReadPdf(input.Provider, input.Model, input.ModelId, input.Text, input.Files);
|
2024-07-01 19:31:48 +00:00
|
|
|
viewModel.Content = content;
|
|
|
|
|
return viewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error in pdf completion. {ex.Message}";
|
|
|
|
|
_logger.LogError(error);
|
|
|
|
|
viewModel.Message = error;
|
|
|
|
|
return viewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-19 17:01:07 +00:00
|
|
|
#endregion
|
2024-08-26 22:24:07 +00:00
|
|
|
|
|
|
|
|
#region Audio
|
|
|
|
|
[HttpPost("/instruct/audio-completion")]
|
|
|
|
|
public async Task<AudioCompletionViewModel> AudioCompletion([FromBody] IncomingMessageModel input)
|
|
|
|
|
{
|
|
|
|
|
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
var viewModel = new AudioCompletionViewModel();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var audio = input.Files.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.FileUrl) || !string.IsNullOrWhiteSpace(x.FileData));
|
|
|
|
|
if (audio == null)
|
|
|
|
|
{
|
|
|
|
|
return new AudioCompletionViewModel { Message = "Error! Cannot find a valid audio file!" };
|
|
|
|
|
}
|
|
|
|
|
var content = await fileInstruct.ReadAudio(input.Provider, input.Model, audio);
|
|
|
|
|
viewModel.Content = content;
|
|
|
|
|
return viewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error in audio completion. {ex.Message}";
|
|
|
|
|
_logger.LogError(error);
|
|
|
|
|
viewModel.Message = error;
|
|
|
|
|
return viewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2023-09-01 22:26:25 +00:00
|
|
|
}
|