diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index e4d65452..3489d260 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -16,6 +16,11 @@ public partial class ConversationService { var conversation = await GetConversationRecord(agentId); + // Save message files + var fileService = _services.GetRequiredService(); + fileService.SaveMessageFiles(_conversationId, message.MessageId, FileSourceType.User, message.Files); + message.Files?.Clear(); + var agentService = _services.GetRequiredService(); Agent agent = await agentService.LoadAgent(agentId); @@ -46,11 +51,6 @@ public partial class ConversationService routing.Context.SetMessageId(_conversationId, message.MessageId); routing.Context.Push(agent.Id); - // Save message files - var fileService = _services.GetRequiredService(); - fileService.SaveMessageFiles(_conversationId, message.MessageId, FileSourceType.User, message.Files); - message.Files?.Clear(); - // Save payload if (replyMessage != null && !string.IsNullOrEmpty(replyMessage.Payload)) { diff --git a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs index d8f88b5c..99c42999 100644 --- a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs @@ -221,14 +221,16 @@ public partial class BotSharpFileService } var (_, bytes) = GetFileInfoFromData(file.FileData); - Thread.Sleep(100); var subDir = Path.Combine(dir, source, $"{i + 1}"); if (!ExistDirectory(subDir)) { Directory.CreateDirectory(subDir); } - File.WriteAllBytes(Path.Combine(subDir, file.FileName), bytes); + using var fs = new FileStream(Path.Combine(subDir, file.FileName), FileMode.Create); + fs.Write(bytes, 0, bytes.Length); + fs.Flush(true); + Thread.Sleep(2000); } return true; diff --git a/src/Infrastructure/BotSharp.Core/Files/Functions/LoadAttachmentFn.cs b/src/Infrastructure/BotSharp.Core/Files/Functions/LoadAttachmentFn.cs index db3abff3..a4f04149 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Functions/LoadAttachmentFn.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Functions/LoadAttachmentFn.cs @@ -31,7 +31,7 @@ public class LoadAttachmentFn : IFunctionCallback var wholeDialogs = conv.GetDialogHistory(); var fileTypes = args?.FileTypes?.Split(",")?.ToList() ?? new List(); var dialogs = await AssembleFiles(conv.ConversationId, wholeDialogs, fileTypes); - var agent = await agentService.LoadAgent(AIAssistant); + var agent = await agentService.LoadAgent(!string.IsNullOrEmpty(message.CurrentAgentId) ? message.CurrentAgentId : AIAssistant); var fileAgent = new Agent { Id = agent.Id, diff --git a/src/Infrastructure/BotSharp.Core/Files/Hooks/AttachmentProcessingHook.cs b/src/Infrastructure/BotSharp.Core/Files/Hooks/AttachmentProcessingHook.cs index 0a1d74b4..558657b8 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Hooks/AttachmentProcessingHook.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Hooks/AttachmentProcessingHook.cs @@ -1,9 +1,11 @@ + +using Microsoft.EntityFrameworkCore; + namespace BotSharp.Core.Files.Hooks; public class AttachmentProcessingHook : AgentHookBase { private readonly IServiceProvider _services; - private readonly AgentSettings _agentSettings; public override string SelfId => string.Empty; @@ -11,10 +13,9 @@ public class AttachmentProcessingHook : AgentHookBase : base(services, settings) { _services = services; - _agentSettings = settings; } - public override bool OnFunctionsLoaded(List functions) + public override void OnAgentLoaded(Agent agent) { var fileService = _services.GetRequiredService(); var conv = _services.GetRequiredService(); @@ -22,25 +23,29 @@ public class AttachmentProcessingHook : AgentHookBase if (hasConvFiles) { - var json = JsonSerializer.Serialize(new - { - user_request = new - { - type = "string", - description = "The request posted by user, which is related to analyzing requested files. User can request for multiple files to process at one time." - }, - file_types = new - { - type = "string", - description = "The file types requested by user to analyze, such as image, png, jpeg, and pdf. There can be multiple file types in a single request. An example output is, 'image,pdf'" - } - }); + agent.Instruction += "\r\n\r\nPlease call load_attachment if user wants to describe files, such as images, pdf.\r\n\r\n"; - functions.Add(new FunctionDef + if (agent.Functions != null) { - Name = "load_attachment", - Description = "If the user's request is related to analyzing files and/or images, you can call this function to analyze files and images.", - Parameters = + var json = JsonSerializer.Serialize(new + { + user_request = new + { + type = "string", + description = "The request posted by user, which is related to analyzing requested files. User can request for multiple files to process at one time." + }, + file_types = new + { + type = "string", + description = "The file types requested by user to analyze, such as image, png, jpeg, and pdf. There can be multiple file types in a single request. An example output is, 'image,pdf'" + } + }); + + agent.Functions.Add(new FunctionDef + { + Name = "load_attachment", + Description = "If the user's request is related to analyzing files and/or images, you can call this function to analyze files and images.", + Parameters = { Properties = JsonSerializer.Deserialize(json), Required = new List @@ -49,8 +54,10 @@ public class AttachmentProcessingHook : AgentHookBase "file_types" } } - }); + }); + } } - return base.OnFunctionsLoaded(functions); ; + + base.OnAgentLoaded(agent); } } diff --git a/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs b/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs index 97e997c7..ec33f609 100644 --- a/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs +++ b/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs @@ -121,7 +121,7 @@ public class TranslationService : ITranslationService if (data == null) return; var dataType = data.GetType(); - if (IsStringType(dataType)) + if (IsStringType(dataType) && !string.IsNullOrWhiteSpace(data.ToString())) { res.Add(data.ToString()); return; @@ -139,7 +139,7 @@ public class TranslationService : ITranslationService { foreach (var item in (data as IEnumerable)) { - if (item == null) continue; + if (string.IsNullOrWhiteSpace(item)) continue; res.Add(item); } } diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs index 2fcbf4c7..50fd33ce 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs @@ -5,7 +5,6 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Conversations; using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Files; -using BotSharp.Abstraction.Files.Models; using BotSharp.Abstraction.Loggers; using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Utilities; @@ -16,7 +15,6 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Runtime.InteropServices.ComTypes; using System.Threading.Tasks; namespace BotSharp.Plugin.AzureOpenAI.Providers;