2023-12-12 18:37:49 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
2024-08-28 16:08:12 +00:00
|
|
|
using BotSharp.Abstraction.Files.Utilities;
|
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;
|
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}")]
|
2024-08-27 15:42:48 +00:00
|
|
|
public async Task<InstructResult> InstructCompletion([FromRoute] string agentId, [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)
|
2024-10-08 16:49:34 +00:00
|
|
|
.SetState("input_text", input.Text, source: StateSource.External)
|
|
|
|
|
.SetState("template_name", input.Template, 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")]
|
2025-02-21 04:52:02 +00:00
|
|
|
public async Task<string> TextCompletion([FromBody] IncomingInstructRequest 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);
|
2025-02-21 04:52:02 +00:00
|
|
|
return await textCompletion.GetCompletion(input.Text, input.AgentId ?? 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")]
|
2025-02-21 04:52:02 +00:00
|
|
|
public async Task<string> ChatCompletion([FromBody] IncomingInstructRequest input)
|
2023-12-12 18:32:04 +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);
|
2023-12-12 18:32:04 +00:00
|
|
|
|
2025-02-21 04:52:02 +00:00
|
|
|
var completion = CompletionProvider.GetChatCompletion(_services);
|
|
|
|
|
var message = await completion.GetChatCompletions(new Agent()
|
2023-12-12 18:32:04 +00:00
|
|
|
{
|
2025-02-21 04:52:02 +00:00
|
|
|
Id = input.AgentId ?? Guid.Empty.ToString(),
|
|
|
|
|
Instruction = input.Instruction
|
2023-12-12 18:32:04 +00:00
|
|
|
}, new List<RoleDialogModel>
|
|
|
|
|
{
|
|
|
|
|
new RoleDialogModel(AgentRole.User, input.Text)
|
2024-01-14 04:48:26 +00:00
|
|
|
});
|
2025-03-04 23:25:35 +00:00
|
|
|
|
|
|
|
|
var hooks = _services.GetServices<IInstructHook>();
|
|
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
|
|
|
|
await hook.OnResponseGenerated(new InstructResponseModel
|
|
|
|
|
{
|
|
|
|
|
AgentId = input.AgentId,
|
|
|
|
|
Provider = input.Provider,
|
|
|
|
|
Model = input.Model
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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")]
|
2024-08-27 15:42:48 +00:00
|
|
|
public async Task<string> MultiModalCompletion([FromBody] MultiModalRequest input)
|
2024-05-14 16:51:39 +00:00
|
|
|
{
|
|
|
|
|
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>();
|
2025-02-13 18:06:15 +00:00
|
|
|
var content = await fileInstruct.ReadImages(input.Provider, input.Model, input.Text, input.Files, input.AgentId);
|
2024-08-26 22:24:07 +00:00
|
|
|
return content;
|
2024-05-14 16:51:39 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-08-28 16:08:12 +00:00
|
|
|
var error = $"Error in reading images. {ex.Message}";
|
2024-06-24 19:32:52 +00:00
|
|
|
_logger.LogError(error);
|
|
|
|
|
return error;
|
2024-05-14 16:51:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-28 16:08:12 +00:00
|
|
|
|
|
|
|
|
[HttpPost("/instruct/multi-modal/upload")]
|
|
|
|
|
public async Task<MultiModalViewModel> MultiModalCompletion(IFormFile file, [FromForm] string text, [FromForm] string? provider = null,
|
2025-02-13 18:06:15 +00:00
|
|
|
[FromForm] string? model = null, [FromForm] List<MessageState>? states = null, [FromForm] string? agentId = null)
|
2024-08-28 16:08:12 +00:00
|
|
|
{
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
states?.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
var viewModel = new MultiModalViewModel();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var data = FileUtility.BuildFileDataFromFile(file);
|
|
|
|
|
var files = new List<InstructFileModel>
|
|
|
|
|
{
|
|
|
|
|
new InstructFileModel { FileData = data }
|
|
|
|
|
};
|
|
|
|
|
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
2025-02-13 18:06:15 +00:00
|
|
|
var content = await fileInstruct.ReadImages(provider, model, text, files, agentId);
|
2024-08-28 16:08:12 +00:00
|
|
|
viewModel.Content = content;
|
|
|
|
|
return viewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error in reading image upload. {ex.Message}";
|
|
|
|
|
_logger.LogError(error);
|
|
|
|
|
viewModel.Message = error;
|
|
|
|
|
return viewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
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")]
|
2024-08-27 15:42:48 +00:00
|
|
|
public async Task<ImageGenerationViewModel> ImageGeneration([FromBody] ImageGenerationRequest input)
|
2024-06-24 18:03:05 +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
|
|
|
|
|
{
|
2024-08-07 21:56:57 +00:00
|
|
|
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
2025-02-13 18:06:15 +00:00
|
|
|
var message = await fileInstruct.GenerateImage(input.Provider, input.Model, input.Text, input.AgentId);
|
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")]
|
2024-08-27 15:42:48 +00:00
|
|
|
public async Task<ImageGenerationViewModel> ImageVariation([FromBody] ImageVariationRequest input)
|
2024-07-18 22:07:14 +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
|
|
|
|
|
{
|
2024-08-27 15:42:48 +00:00
|
|
|
if (input.File == 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>();
|
2025-02-13 18:06:15 +00:00
|
|
|
var message = await fileInstruct.VaryImage(input.Provider, input.Model, input.File, input.AgentId);
|
2024-07-18 22:07:14 +00:00
|
|
|
imageViewModel.Content = message.Content;
|
|
|
|
|
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
|
2024-08-28 16:41:42 +00:00
|
|
|
|
2024-07-18 22:07:14 +00:00
|
|
|
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-08-28 16:08:12 +00:00
|
|
|
[HttpPost("/instruct/image-variation/upload")]
|
|
|
|
|
public async Task<ImageGenerationViewModel> ImageVariation(IFormFile file, [FromForm] string? provider = null,
|
2025-02-13 18:06:15 +00:00
|
|
|
[FromForm] string? model = null, [FromForm] List<MessageState>? states = null, [FromForm] string? agentId = null)
|
2024-08-28 16:08:12 +00:00
|
|
|
{
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
states?.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
var imageViewModel = new ImageGenerationViewModel();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-08-28 16:41:42 +00:00
|
|
|
using var stream = new MemoryStream();
|
|
|
|
|
file.CopyTo(stream);
|
|
|
|
|
stream.Position = 0;
|
|
|
|
|
|
|
|
|
|
var completion = CompletionProvider.GetImageCompletion(_services, provider: provider ?? "openai", model: model ?? "dall-e-2");
|
|
|
|
|
var message = await completion.GetImageVariation(new Agent()
|
2024-08-28 16:08:12 +00:00
|
|
|
{
|
2025-02-13 18:06:15 +00:00
|
|
|
Id = agentId ?? Guid.Empty.ToString()
|
2024-08-28 16:41:42 +00:00
|
|
|
}, new RoleDialogModel(AgentRole.User, string.Empty), stream, file.FileName);
|
|
|
|
|
|
2024-08-28 16:08:12 +00:00
|
|
|
imageViewModel.Content = message.Content;
|
|
|
|
|
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
|
2024-08-28 16:41:42 +00:00
|
|
|
stream.Close();
|
|
|
|
|
|
2024-08-28 16:08:12 +00:00
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error in image variation upload. {ex.Message}";
|
|
|
|
|
_logger.LogError(error);
|
|
|
|
|
imageViewModel.Message = error;
|
|
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-19 17:01:07 +00:00
|
|
|
[HttpPost("/instruct/image-edit")]
|
2024-08-27 15:42:48 +00:00
|
|
|
public async Task<ImageGenerationViewModel> ImageEdit([FromBody] ImageEditRequest input)
|
2024-07-19 17:01:07 +00:00
|
|
|
{
|
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
|
|
|
|
|
{
|
2024-08-27 15:42:48 +00:00
|
|
|
if (input.File == null)
|
2024-07-19 17:01:07 +00:00
|
|
|
{
|
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
|
|
|
}
|
2025-02-13 18:06:15 +00:00
|
|
|
var message = await fileInstruct.EditImage(input.Provider, input.Model, input.Text, input.File, input.AgentId);
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 16:08:12 +00:00
|
|
|
[HttpPost("/instruct/image-edit/upload")]
|
|
|
|
|
public async Task<ImageGenerationViewModel> ImageEdit(IFormFile file, [FromForm] string text, [FromForm] string? provider = null,
|
2025-02-13 18:06:15 +00:00
|
|
|
[FromForm] string? model = null, [FromForm] List<MessageState>? states = null, [FromForm] string? agentId = null)
|
2024-08-28 16:08:12 +00:00
|
|
|
{
|
|
|
|
|
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
states?.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
var imageViewModel = new ImageGenerationViewModel();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-08-28 16:41:42 +00:00
|
|
|
using var stream = new MemoryStream();
|
|
|
|
|
file.CopyTo(stream);
|
|
|
|
|
stream.Position = 0;
|
|
|
|
|
|
|
|
|
|
var completion = CompletionProvider.GetImageCompletion(_services, provider: provider ?? "openai", model: model ?? "dall-e-2");
|
|
|
|
|
var message = await completion.GetImageEdits(new Agent()
|
2024-08-28 16:08:12 +00:00
|
|
|
{
|
2025-02-13 18:06:15 +00:00
|
|
|
Id = agentId ?? Guid.Empty.ToString()
|
2024-08-28 16:41:42 +00:00
|
|
|
}, new RoleDialogModel(AgentRole.User, text), stream, file.FileName);
|
|
|
|
|
|
2024-08-28 16:08:12 +00:00
|
|
|
imageViewModel.Content = message.Content;
|
|
|
|
|
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
|
2024-08-28 16:41:42 +00:00
|
|
|
stream.Close();
|
|
|
|
|
|
2024-08-28 16:08:12 +00:00
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error in image edit upload. {ex.Message}";
|
|
|
|
|
_logger.LogError(error);
|
|
|
|
|
imageViewModel.Message = error;
|
|
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-19 17:01:07 +00:00
|
|
|
[HttpPost("/instruct/image-mask-edit")]
|
2024-08-27 15:42:48 +00:00
|
|
|
public async Task<ImageGenerationViewModel> ImageMaskEdit([FromBody] ImageMaskEditRequest input)
|
2024-07-19 17:01:07 +00:00
|
|
|
{
|
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
|
|
|
|
|
{
|
2024-08-27 15:42:48 +00:00
|
|
|
var image = input.File;
|
2024-07-19 17:01:07 +00:00
|
|
|
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
|
|
|
}
|
2025-02-13 18:06:15 +00:00
|
|
|
var message = await fileInstruct.EditImage(input.Provider, input.Model, input.Text, image, mask, input.AgentId);
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-28 16:08:12 +00:00
|
|
|
|
|
|
|
|
[HttpPost("/instruct/image-mask-edit/upload")]
|
|
|
|
|
public async Task<ImageGenerationViewModel> ImageMaskEdit(IFormFile image, IFormFile mask, [FromForm] string text, [FromForm] string? provider = null,
|
2025-02-13 18:06:15 +00:00
|
|
|
[FromForm] string? model = null, [FromForm] List<MessageState>? states = null, [FromForm] string? agentId = null)
|
2024-08-28 16:08:12 +00:00
|
|
|
{
|
|
|
|
|
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
states?.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
var imageViewModel = new ImageGenerationViewModel();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-08-28 16:41:42 +00:00
|
|
|
using var imageStream = new MemoryStream();
|
|
|
|
|
image.CopyTo(imageStream);
|
|
|
|
|
imageStream.Position = 0;
|
|
|
|
|
|
|
|
|
|
using var maskStream = new MemoryStream();
|
|
|
|
|
mask.CopyTo(maskStream);
|
|
|
|
|
maskStream.Position = 0;
|
2024-08-28 16:08:12 +00:00
|
|
|
|
2024-08-28 16:41:42 +00:00
|
|
|
var completion = CompletionProvider.GetImageCompletion(_services, provider: provider ?? "openai", model: model ?? "dall-e-2");
|
|
|
|
|
var message = await completion.GetImageEdits(new Agent()
|
2024-08-28 16:08:12 +00:00
|
|
|
{
|
2025-02-13 18:06:15 +00:00
|
|
|
Id = agentId ?? Guid.Empty.ToString()
|
2024-08-28 16:41:42 +00:00
|
|
|
}, new RoleDialogModel(AgentRole.User, text), imageStream, image.FileName, maskStream, mask.FileName);
|
2024-08-28 16:08:12 +00:00
|
|
|
|
|
|
|
|
imageViewModel.Content = message.Content;
|
|
|
|
|
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
|
2024-08-28 16:41:42 +00:00
|
|
|
imageStream.Close();
|
|
|
|
|
maskStream.Close();
|
|
|
|
|
|
2024-08-28 16:08:12 +00:00
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error in image mask edit upload. {ex.Message}";
|
|
|
|
|
_logger.LogError(error);
|
|
|
|
|
imageViewModel.Message = error;
|
|
|
|
|
return imageViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-19 17:01:07 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Pdf
|
2024-07-01 19:31:48 +00:00
|
|
|
[HttpPost("/instruct/pdf-completion")]
|
2024-08-27 15:42:48 +00:00
|
|
|
public async Task<PdfCompletionViewModel> PdfCompletion([FromBody] MultiModalRequest input)
|
2024-07-01 19:31:48 +00:00
|
|
|
{
|
|
|
|
|
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>();
|
2025-02-13 18:06:15 +00:00
|
|
|
var content = await fileInstruct.ReadPdf(input.Provider, input.Model, input.ModelId, input.Text, input.Files, input.AgentId);
|
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-08-28 16:08:12 +00:00
|
|
|
|
|
|
|
|
[HttpPost("/instruct/pdf-completion/upload")]
|
|
|
|
|
public async Task<PdfCompletionViewModel> PdfCompletion(IFormFile file, [FromForm] string text, [FromForm] string? provider = null,
|
2025-02-13 18:06:15 +00:00
|
|
|
[FromForm] string? model = null, [FromForm] string? modelId = null, [FromForm] List<MessageState>? states = null, [FromForm] string? agentId = null)
|
2024-08-28 16:08:12 +00:00
|
|
|
{
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
states?.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
var viewModel = new PdfCompletionViewModel();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var data = FileUtility.BuildFileDataFromFile(file);
|
|
|
|
|
var files = new List<InstructFileModel>
|
|
|
|
|
{
|
|
|
|
|
new InstructFileModel { FileData = data }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
2025-02-13 18:06:15 +00:00
|
|
|
var content = await fileInstruct.ReadPdf(provider, model, modelId, text, files, agentId);
|
2024-08-28 16:08:12 +00:00
|
|
|
viewModel.Content = content;
|
|
|
|
|
return viewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error in pdf completion upload. {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
|
2024-08-28 16:08:12 +00:00
|
|
|
[HttpPost("/instruct/speech-to-text")]
|
|
|
|
|
public async Task<SpeechToTextViewModel> SpeechToText([FromBody] SpeechToTextRequest input)
|
2024-08-26 22:24:07 +00:00
|
|
|
{
|
|
|
|
|
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));
|
2024-08-28 16:08:12 +00:00
|
|
|
var viewModel = new SpeechToTextViewModel();
|
2024-08-26 22:24:07 +00:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-08-27 15:42:48 +00:00
|
|
|
var audio = input.File;
|
2024-08-26 22:24:07 +00:00
|
|
|
if (audio == null)
|
|
|
|
|
{
|
2024-08-28 16:08:12 +00:00
|
|
|
return new SpeechToTextViewModel { Message = "Error! Cannot find a valid audio file!" };
|
2024-08-26 22:24:07 +00:00
|
|
|
}
|
2024-08-28 16:08:12 +00:00
|
|
|
var content = await fileInstruct.SpeechToText(input.Provider, input.Model, audio);
|
|
|
|
|
viewModel.Content = content;
|
|
|
|
|
return viewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error in speech to text. {ex.Message}";
|
|
|
|
|
_logger.LogError(error);
|
|
|
|
|
viewModel.Message = error;
|
|
|
|
|
return viewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("/instruct/speech-to-text/upload")]
|
|
|
|
|
public async Task<SpeechToTextViewModel> SpeechToText(IFormFile file, [FromForm] string? provider = null, [FromForm] string? model = null,
|
|
|
|
|
[FromForm] string? text = null, [FromForm] List<MessageState>? states = null)
|
|
|
|
|
{
|
|
|
|
|
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
states?.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
var viewModel = new SpeechToTextViewModel();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var stream = new MemoryStream();
|
|
|
|
|
file.CopyTo(stream);
|
|
|
|
|
stream.Position = 0;
|
|
|
|
|
|
|
|
|
|
var completion = CompletionProvider.GetAudioCompletion(_services, provider: provider ?? "openai", model: model ?? "whisper-1");
|
|
|
|
|
var content = await completion.GenerateTextFromAudioAsync(stream, file.FileName, text);
|
2024-08-26 22:24:07 +00:00
|
|
|
viewModel.Content = content;
|
2024-08-28 16:08:12 +00:00
|
|
|
stream.Close();
|
2024-08-26 22:24:07 +00:00
|
|
|
return viewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-08-28 16:08:12 +00:00
|
|
|
var error = $"Error in speech-to-text upload. {ex.Message}";
|
2024-08-26 22:24:07 +00:00
|
|
|
_logger.LogError(error);
|
|
|
|
|
viewModel.Message = error;
|
|
|
|
|
return viewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-28 18:43:37 +00:00
|
|
|
|
|
|
|
|
[HttpPost("/instruct/text-to-speech")]
|
|
|
|
|
public async Task<IActionResult> TextToSpeech([FromBody] TextToSpeechRequest input)
|
|
|
|
|
{
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
|
|
|
|
|
|
|
|
|
var completion = CompletionProvider.GetAudioCompletion(_services, provider: input.Provider ?? "openai", model: input.Model ?? "tts-1");
|
|
|
|
|
var binaryData = await completion.GenerateAudioFromTextAsync(input.Text);
|
|
|
|
|
var stream = binaryData.ToStream();
|
|
|
|
|
stream.Position = 0;
|
|
|
|
|
|
|
|
|
|
return new FileStreamResult(stream, "audio/mpeg") { FileDownloadName = "output.mp3" };
|
|
|
|
|
}
|
2024-08-26 22:24:07 +00:00
|
|
|
#endregion
|
2023-09-01 22:26:25 +00:00
|
|
|
}
|