From 512d459c6b29c4d4028834b5f5839a9f1c246fdd Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 27 Aug 2024 10:42:48 -0500 Subject: [PATCH] refine instruct request model --- .../Files/Models/InputMessageFiles.cs | 7 -- .../BotSharp.Abstraction/Models/AiModel.cs | 10 --- .../Models/MessageConfig.cs | 6 +- .../Controllers/ConversationController.cs | 6 +- .../Controllers/InstructModeController.cs | 32 ++++----- .../Conversations/InputMessageFiles.cs | 7 ++ .../Instructs/InstructBaseRequest.cs | 66 +++++++++++++++++++ 7 files changed, 92 insertions(+), 42 deletions(-) delete mode 100644 src/Infrastructure/BotSharp.Abstraction/Files/Models/InputMessageFiles.cs delete mode 100644 src/Infrastructure/BotSharp.Abstraction/Models/AiModel.cs create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/InputMessageFiles.cs create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructBaseRequest.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/Models/InputMessageFiles.cs b/src/Infrastructure/BotSharp.Abstraction/Files/Models/InputMessageFiles.cs deleted file mode 100644 index 29749bce..00000000 --- a/src/Infrastructure/BotSharp.Abstraction/Files/Models/InputMessageFiles.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace BotSharp.Abstraction.Files.Models; - -public class InputMessageFiles -{ - public List Files { get; set; } = new List(); - public BotSharpFile? Mask { get; set; } -} diff --git a/src/Infrastructure/BotSharp.Abstraction/Models/AiModel.cs b/src/Infrastructure/BotSharp.Abstraction/Models/AiModel.cs deleted file mode 100644 index 348eb457..00000000 --- a/src/Infrastructure/BotSharp.Abstraction/Models/AiModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace BotSharp.Abstraction.Models; - -public class AiModel -{ - public string Id { get; set; } = string.Empty; - public string Model { get; set; } = string.Empty; - public string Name { get; set; } = string.Empty; - public int MaxLength { get; set; } - public int TokenLimit { get; set; } -} diff --git a/src/Infrastructure/BotSharp.Abstraction/Models/MessageConfig.cs b/src/Infrastructure/BotSharp.Abstraction/Models/MessageConfig.cs index 24e4152c..9ea5917c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Models/MessageConfig.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Models/MessageConfig.cs @@ -1,6 +1,6 @@ namespace BotSharp.Abstraction.Models; -public class MessageConfig : InputMessageFiles +public class MessageConfig { /// /// Completion Provider @@ -15,7 +15,7 @@ public class MessageConfig : InputMessageFiles public virtual string? Model { get; set; } = null; /// - /// Model name + /// Model id /// [JsonPropertyName("model_id")] public virtual string? ModelId { get; set; } = null; @@ -34,7 +34,7 @@ public class MessageConfig : InputMessageFiles /// /// Conversation states from input /// - public List States { get; set; } = new List(); + public List States { get; set; } = new(); /// /// Agent task id diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 9b964dff..565677f7 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -294,9 +294,7 @@ public class ConversationController : ControllerBase } [HttpPost("/conversation/{agentId}/{conversationId}/sse")] - public async Task SendMessageSse([FromRoute] string agentId, - [FromRoute] string conversationId, - [FromBody] NewMessageModel input) + public async Task SendMessageSse([FromRoute] string agentId, [FromRoute] string conversationId, [FromBody] NewMessageModel input) { var conv = _services.GetRequiredService(); var inputMsg = new RoleDialogModel(AgentRole.User, input.Text) @@ -391,7 +389,7 @@ public class ConversationController : ControllerBase } [HttpPost("/agent/{agentId}/conversation/{conversationId}/upload")] - public async Task UploadConversationMessageFiles([FromRoute] string agentId, [FromRoute] string conversationId, [FromBody] NewMessageModel input) + public async Task UploadConversationMessageFiles([FromRoute] string agentId, [FromRoute] string conversationId, [FromBody] InputMessageFiles input) { var convService = _services.GetRequiredService(); convService.SetConversationId(conversationId, input.States); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs index 0e60f331..b0ecd905 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs @@ -3,7 +3,6 @@ using BotSharp.Abstraction.Instructs; using BotSharp.Abstraction.Instructs.Models; using BotSharp.Core.Infrastructures; using BotSharp.OpenAPI.ViewModels.Instructs; -using static System.Net.Mime.MediaTypeNames; namespace BotSharp.OpenAPI.Controllers; @@ -21,8 +20,7 @@ public class InstructModeController : ControllerBase } [HttpPost("/instruct/{agentId}")] - public async Task InstructCompletion([FromRoute] string agentId, - [FromBody] InstructMessageModel input) + public async Task InstructCompletion([FromRoute] string agentId, [FromBody] InstructMessageModel input) { var state = _services.GetRequiredService(); input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External)); @@ -80,7 +78,7 @@ public class InstructModeController : ControllerBase #region Read image [HttpPost("/instruct/multi-modal")] - public async Task MultiModalCompletion([FromBody] IncomingMessageModel input) + public async Task MultiModalCompletion([FromBody] MultiModalRequest input) { var state = _services.GetRequiredService(); input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External)); @@ -102,7 +100,7 @@ public class InstructModeController : ControllerBase #region Generate image [HttpPost("/instruct/image-generation")] - public async Task ImageGeneration([FromBody] IncomingMessageModel input) + public async Task ImageGeneration([FromBody] ImageGenerationRequest input) { var state = _services.GetRequiredService(); input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External)); @@ -128,7 +126,7 @@ public class InstructModeController : ControllerBase #region Edit image [HttpPost("/instruct/image-variation")] - public async Task ImageVariation([FromBody] IncomingMessageModel input) + public async Task ImageVariation([FromBody] ImageVariationRequest input) { var state = _services.GetRequiredService(); input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External)); @@ -136,14 +134,13 @@ public class InstructModeController : ControllerBase try { - var image = input.Files.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.FileUrl) || !string.IsNullOrWhiteSpace(x.FileData)); - if (image == null) + if (input.File == null) { return new ImageGenerationViewModel { Message = "Error! Cannot find an image!" }; } var fileInstruct = _services.GetRequiredService(); - var message = await fileInstruct.VaryImage(input.Provider, input.Model, image); + var message = await fileInstruct.VaryImage(input.Provider, input.Model, input.File); imageViewModel.Content = message.Content; imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList(); return imageViewModel; @@ -158,7 +155,7 @@ public class InstructModeController : ControllerBase } [HttpPost("/instruct/image-edit")] - public async Task ImageEdit([FromBody] IncomingMessageModel input) + public async Task ImageEdit([FromBody] ImageEditRequest input) { var fileInstruct = _services.GetRequiredService(); var state = _services.GetRequiredService(); @@ -167,12 +164,11 @@ public class InstructModeController : ControllerBase try { - var image = input.Files.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.FileUrl) || !string.IsNullOrWhiteSpace(x.FileData)); - if (image == null) + if (input.File == null) { return new ImageGenerationViewModel { Message = "Error! Cannot find a valid image file!" }; } - var message = await fileInstruct.EditImage(input.Provider, input.Model, input.Text, image); + var message = await fileInstruct.EditImage(input.Provider, input.Model, input.Text, input.File); imageViewModel.Content = message.Content; imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList(); return imageViewModel; @@ -187,7 +183,7 @@ public class InstructModeController : ControllerBase } [HttpPost("/instruct/image-mask-edit")] - public async Task ImageMaskEdit([FromBody] IncomingMessageModel input) + public async Task ImageMaskEdit([FromBody] ImageMaskEditRequest input) { var fileInstruct = _services.GetRequiredService(); var state = _services.GetRequiredService(); @@ -196,7 +192,7 @@ public class InstructModeController : ControllerBase try { - var image = input.Files.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.FileUrl) || !string.IsNullOrWhiteSpace(x.FileData)); + var image = input.File; var mask = input.Mask; if (image == null || mask == null) { @@ -219,7 +215,7 @@ public class InstructModeController : ControllerBase #region Pdf [HttpPost("/instruct/pdf-completion")] - public async Task PdfCompletion([FromBody] IncomingMessageModel input) + public async Task PdfCompletion([FromBody] MultiModalRequest input) { var state = _services.GetRequiredService(); input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External)); @@ -244,7 +240,7 @@ public class InstructModeController : ControllerBase #region Audio [HttpPost("/instruct/audio-completion")] - public async Task AudioCompletion([FromBody] IncomingMessageModel input) + public async Task AudioCompletion([FromBody] AudioCompletionRequest input) { var fileInstruct = _services.GetRequiredService(); var state = _services.GetRequiredService(); @@ -253,7 +249,7 @@ public class InstructModeController : ControllerBase try { - var audio = input.Files.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.FileUrl) || !string.IsNullOrWhiteSpace(x.FileData)); + var audio = input.File; if (audio == null) { return new AudioCompletionViewModel { Message = "Error! Cannot find a valid audio file!" }; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/InputMessageFiles.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/InputMessageFiles.cs new file mode 100644 index 00000000..5656f7ae --- /dev/null +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/InputMessageFiles.cs @@ -0,0 +1,7 @@ +namespace BotSharp.OpenAPI.ViewModels.Conversations; + +public class InputMessageFiles +{ + public List States { get; set; } = new(); + public List Files { get; set; } = new(); +} diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructBaseRequest.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructBaseRequest.cs new file mode 100644 index 00000000..1b5d551d --- /dev/null +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructBaseRequest.cs @@ -0,0 +1,66 @@ +using System.Text.Json.Serialization; + +namespace BotSharp.Abstraction.Instructs.Models; + +public class InstructBaseRequest +{ + [JsonPropertyName("provider")] + public virtual string? Provider { get; set; } = null; + + [JsonPropertyName("model")] + public virtual string? Model { get; set; } = null; + + [JsonPropertyName("model_id")] + public virtual string? ModelId { get; set; } = null; + + [JsonPropertyName("states")] + public List States { get; set; } = new(); +} + +public class MultiModalRequest : InstructBaseRequest +{ + [JsonPropertyName("text")] + public string Text { get; set; } = string.Empty; + + [JsonPropertyName("files")] + public List Files { get; set; } = new(); +} + +public class ImageGenerationRequest : InstructBaseRequest +{ + [JsonPropertyName("text")] + public string Text { get; set; } = string.Empty; +} + +public class ImageVariationRequest : InstructBaseRequest +{ + [JsonPropertyName("file")] + public BotSharpFile File { get; set; } +} + +public class ImageEditRequest : InstructBaseRequest +{ + [JsonPropertyName("text")] + public string Text { get; set; } = string.Empty; + + [JsonPropertyName("file")] + public BotSharpFile File { get; set; } +} + +public class ImageMaskEditRequest : InstructBaseRequest +{ + [JsonPropertyName("text")] + public string Text { get; set; } = string.Empty; + + [JsonPropertyName("file")] + public BotSharpFile File { get; set; } + + [JsonPropertyName("mask")] + public BotSharpFile Mask { get; set; } +} + +public class AudioCompletionRequest : InstructBaseRequest +{ + [JsonPropertyName("file")] + public BotSharpFile File { get; set; } +} \ No newline at end of file