From 4de38f2e9e65ede4f59fa9df22ce03d056574695 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Tue, 2 Jul 2024 18:35:09 -0500 Subject: [PATCH 1/4] simplify image generate --- .../Files/Functions/GenerateImageFn.cs | 35 +++++-------------- .../Providers/ChatCompletionProvider.cs | 2 +- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Files/Functions/GenerateImageFn.cs b/src/Infrastructure/BotSharp.Core/Files/Functions/GenerateImageFn.cs index b7d89b36..eb9a49e3 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Functions/GenerateImageFn.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Functions/GenerateImageFn.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Functions; -using System.Net.Http; namespace BotSharp.Core.Files.Functions; @@ -55,11 +54,8 @@ public class GenerateImageFn : IFunctionCallback private void SetImageOptions() { var state = _services.GetRequiredService(); - var size = state.SetState("image_size", "1024x1024"); - var quality = state.SetState("image_quality", "standard"); - var style = state.SetState("image_style", "natural"); - var format = state.SetState("image_format", "bytes"); - var count = state.SetState("image_count", "1"); + state.SetState("image_format", "bytes"); + state.SetState("image_count", "1"); } private async Task GetImageGeneration(Agent agent, RoleDialogModel message, string? description) @@ -70,7 +66,7 @@ public class GenerateImageFn : IFunctionCallback var text = !string.IsNullOrWhiteSpace(description) ? description : message.Content; var dialog = RoleDialogModel.From(message, AgentRole.User, text); var result = await completion.GetImageGeneration(agent, new List { dialog }); - await SaveGeneratedImages(result?.GeneratedImages); + SaveGeneratedImages(result?.GeneratedImages); return result?.Content ?? string.Empty; } catch (Exception ex) @@ -81,40 +77,27 @@ public class GenerateImageFn : IFunctionCallback } } - private async Task SaveGeneratedImages(List? images) + private void SaveGeneratedImages(List? images) { if (images.IsNullOrEmpty()) return; var files = new List(); foreach (var image in images) { - if (string.IsNullOrEmpty(image?.ImageUrl) - && string.IsNullOrEmpty(image?.ImageData)) + if (string.IsNullOrEmpty(image?.ImageData)) { continue; } try { - var data = image.ImageData; - if (!string.IsNullOrEmpty(image.ImageUrl)) - { - var http = _services.GetRequiredService(); - using var client = http.CreateClient(); - var bytes = await client.GetByteArrayAsync(image.ImageUrl); - data = Convert.ToBase64String(bytes); - } - - if (!string.IsNullOrEmpty(data)) - { - var imageName = $"{Guid.NewGuid().ToString()}.png"; - var imageData = $"data:image/png;base64,{data}"; - files.Add(new BotSharpFile { FileName = imageName, FileData = imageData }); - } + var name = $"{Guid.NewGuid()}.png"; + var data = $"data:image/png;base64,{image.ImageData}"; + files.Add(new BotSharpFile { FileName = name, FileData = data }); } catch (Exception ex) { - _logger.LogWarning($"Error when saving generated image: {image.ImageUrl ?? image.ImageData}\r\n{ex.Message}"); + _logger.LogWarning($"Error when saving generated image: {image.ImageData}\r\n{ex.Message}"); continue; } } diff --git a/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs index c4556c36..d15345e6 100644 --- a/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.SparkDesk/Providers/ChatCompletionProvider.cs @@ -20,7 +20,7 @@ public class ChatCompletionProvider : IChatCompletion _services = services; _settings = settings; _logger = logger; - _model = $"general{settings.ModelVersion.ToString()}"; + _model = $"general{settings.ModelVersion}"; } From 4cdfeacce6bffb50fdabdc835d7a33e7ebfd9c8f Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Tue, 2 Jul 2024 18:39:06 -0500 Subject: [PATCH 2/4] minor change --- .../Files/Functions/GenerateImageFn.cs | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Files/Functions/GenerateImageFn.cs b/src/Infrastructure/BotSharp.Core/Files/Functions/GenerateImageFn.cs index eb9a49e3..18c34c12 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Functions/GenerateImageFn.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Functions/GenerateImageFn.cs @@ -81,26 +81,11 @@ public class GenerateImageFn : IFunctionCallback { if (images.IsNullOrEmpty()) return; - var files = new List(); - foreach (var image in images) + var files = images.Where(x => !string.IsNullOrEmpty(x?.ImageData)).Select(x => new BotSharpFile { - if (string.IsNullOrEmpty(image?.ImageData)) - { - continue; - } - - try - { - var name = $"{Guid.NewGuid()}.png"; - var data = $"data:image/png;base64,{image.ImageData}"; - files.Add(new BotSharpFile { FileName = name, FileData = data }); - } - catch (Exception ex) - { - _logger.LogWarning($"Error when saving generated image: {image.ImageData}\r\n{ex.Message}"); - continue; - } - } + FileName = $"{Guid.NewGuid()}.png", + FileData = $"data:image/png;base64,{x.ImageData}" + }).ToList(); var fileService = _services.GetRequiredService(); fileService.SaveMessageFiles(_conversationId, _messageId, FileSourceType.Bot, files); From 1ab5947e6103e17d89d90e87f8627603634ad7f9 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Tue, 2 Jul 2024 18:39:28 -0500 Subject: [PATCH 3/4] add error msg --- .../Files/Services/BotSharpFileService.Conversation.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Conversation.cs b/src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Conversation.cs index 34f6b74a..ba01ce9d 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Conversation.cs @@ -218,7 +218,7 @@ public partial class BotSharpFileService } catch (Exception ex) { - _logger.LogWarning($"Error when saving conversation files: {ex.Message}"); + _logger.LogWarning($"Error when saving conversation files: {ex.Message}\r\n{ex.InnerException}"); return false; } } From 2997d4a63cdc0ece0d75aa434f14e3a09d63c349 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Tue, 2 Jul 2024 19:16:40 -0500 Subject: [PATCH 4/4] refine message file save --- .../BotSharpFileService.Conversation.cs | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Conversation.cs b/src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Conversation.cs index ba01ce9d..a3908b2b 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Conversation.cs @@ -188,16 +188,16 @@ public partial class BotSharpFileService var dir = GetConversationFileDirectory(conversationId, messageId, createNewDir: true); if (!ExistDirectory(dir)) return false; - try + for (int i = 0; i < files.Count; i++) { - for (int i = 0; i < files.Count; i++) + var file = files[i]; + if (string.IsNullOrEmpty(file.FileData)) { - var file = files[i]; - if (string.IsNullOrEmpty(file.FileData)) - { - continue; - } + continue; + } + try + { var (_, bytes) = GetFileInfoFromData(file.FileData); var subDir = Path.Combine(dir, source, $"{i + 1}"); if (!ExistDirectory(subDir)) @@ -213,14 +213,14 @@ public partial class BotSharpFileService Thread.Sleep(100); } } + catch (Exception ex) + { + _logger.LogWarning($"Error when saving message file {file.FileName}: {ex.Message}\r\n{ex.InnerException}"); + continue; + } + } - return true; - } - catch (Exception ex) - { - _logger.LogWarning($"Error when saving conversation files: {ex.Message}\r\n{ex.InnerException}"); - return false; - } + return true; }