diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/IFileInstructService.cs b/src/Infrastructure/BotSharp.Abstraction/Files/IFileInstructService.cs index 433a582f..78a400e1 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Files/IFileInstructService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Files/IFileInstructService.cs @@ -21,9 +21,6 @@ public interface IFileInstructService #endregion #region Select file - Task> SelectMessageFiles(string conversationId, - string? agentId = null, string? template = null, string? description = null, - bool includeBotFile = false, bool fromBreakpoint = false, - int? offset = null, IEnumerable? contentTypes = null); + Task> SelectMessageFiles(string conversationId, SelectFileOptions options); #endregion } diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/Models/SelectFileOptions.cs b/src/Infrastructure/BotSharp.Abstraction/Files/Models/SelectFileOptions.cs new file mode 100644 index 00000000..6ba6cdd2 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Files/Models/SelectFileOptions.cs @@ -0,0 +1,14 @@ +namespace BotSharp.Abstraction.Files.Models; + +public class SelectFileOptions +{ + public string? Provider { get; set; } + public string? ModelId { get; set; } + public string? AgentId { get; set; } + public string? Template { get; set; } + public string? Description { get; set; } + public bool IncludeBotFile { get; set; } + public bool FromBreakpoint { get; set; } + public int? Offset { get; set; } + public IEnumerable? ContentTypes { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.SelectFile.cs b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.SelectFile.cs index dfed9f77..c6a9e06c 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.SelectFile.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.SelectFile.cs @@ -5,10 +5,7 @@ namespace BotSharp.Core.Files.Services; public partial class FileInstructService { - public async Task> SelectMessageFiles(string conversationId, - string? agentId = null, string? template = null, string? description = null, - bool includeBotFile = false, bool fromBreakpoint = false, - int? offset = null, IEnumerable? contentTypes = null) + public async Task> SelectMessageFiles(string conversationId, SelectFileOptions options) { if (string.IsNullOrEmpty(conversationId)) { @@ -16,13 +13,13 @@ public partial class FileInstructService } var convService = _services.GetRequiredService(); - var dialogs = convService.GetDialogHistory(fromBreakpoint: fromBreakpoint); - var messageIds = GetMessageIds(dialogs, offset); + var dialogs = convService.GetDialogHistory(fromBreakpoint: options.FromBreakpoint); + var messageIds = GetMessageIds(dialogs, options.Offset); - var files = _fileBasic.GetMessageFiles(conversationId, messageIds, FileSourceType.User, contentTypes); - if (includeBotFile) + var files = _fileBasic.GetMessageFiles(conversationId, messageIds, FileSourceType.User, options.ContentTypes); + if (options.IncludeBotFile) { - var botFiles = _fileBasic.GetMessageFiles(conversationId, messageIds, FileSourceType.Bot, contentTypes); + var botFiles = _fileBasic.GetMessageFiles(conversationId, messageIds, FileSourceType.Bot, options.ContentTypes); files = files.Concat(botFiles); } @@ -31,11 +28,10 @@ public partial class FileInstructService return Enumerable.Empty(); } - return await SelectFiles(agentId, template, description, files, dialogs); + return await SelectFiles(files, dialogs, options); } - private async Task> SelectFiles(string? agentId, string? template, string? description, - IEnumerable files, List dialogs) + private async Task> SelectFiles(IEnumerable files, IEnumerable dialogs, SelectFileOptions options) { if (files.IsNullOrEmpty()) return new List(); @@ -50,8 +46,8 @@ public partial class FileInstructService return $"id: {idx + 1}, file_name: {x.FileName}.{x.FileType}, content_type: {x.ContentType}, author: {x.FileSource}"; }).ToList(); - agentId = !string.IsNullOrWhiteSpace(agentId) ? agentId : BuiltInAgentId.UtilityAssistant; - template = !string.IsNullOrWhiteSpace(template) ? template : "select_file_prompt"; + var agentId = !string.IsNullOrWhiteSpace(options.AgentId) ? options.AgentId : BuiltInAgentId.UtilityAssistant; + var template = !string.IsNullOrWhiteSpace(options.Template) ? options.Template : "select_file_prompt"; var foundAgent = db.GetAgent(agentId); var prompt = db.GetAgentTemplate(BuiltInAgentId.UtilityAssistant, template); @@ -67,16 +63,23 @@ public partial class FileInstructService Instruction = prompt }; - var provider = llmProviderService.GetProviders().FirstOrDefault(x => x == "openai"); - var model = llmProviderService.GetProviderModel(provider: provider, id: "gpt-4"); + var message = dialogs.LastOrDefault(); + var text = !string.IsNullOrWhiteSpace(options.Description) ? options.Description : message?.Content; + if (message == null) + { + message = new RoleDialogModel(AgentRole.User, text); + } + else + { + message = RoleDialogModel.From(message, AgentRole.User, text); + } + + var providerName = options.Provider ?? "openai"; + var modelId = options?.ModelId ?? "gpt-4"; + var provider = llmProviderService.GetProviders().FirstOrDefault(x => x == providerName); + var model = llmProviderService.GetProviderModel(provider: provider, id: modelId); var completion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model.Name); - var message = dialogs.Last(); - if (!string.IsNullOrWhiteSpace(description)) - { - message = RoleDialogModel.From(message, AgentRole.User, description); - } - var response = await completion.GetChatCompletions(agent, new List { message }); var content = response?.Content ?? string.Empty; var selecteds = JsonSerializer.Deserialize(content); diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailSenderFn.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailSenderFn.cs index 8169844e..634be215 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailSenderFn.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailSenderFn.cs @@ -77,7 +77,7 @@ public class HandleEmailSenderFn : IFunctionCallback var conversationId = convService.ConversationId; var fileInstruct = _services.GetRequiredService(); - var selecteds = await fileInstruct.SelectMessageFiles(conversationId, includeBotFile: true); + var selecteds = await fileInstruct.SelectMessageFiles(conversationId, new SelectFileOptions { IncludeBotFile = true }); return selecteds; } diff --git a/src/Plugins/BotSharp.Plugin.FileHandler/Functions/EditImageFn.cs b/src/Plugins/BotSharp.Plugin.FileHandler/Functions/EditImageFn.cs index e9d1851b..1f99c287 100644 --- a/src/Plugins/BotSharp.Plugin.FileHandler/Functions/EditImageFn.cs +++ b/src/Plugins/BotSharp.Plugin.FileHandler/Functions/EditImageFn.cs @@ -51,7 +51,11 @@ public class EditImageFn : IFunctionCallback private async Task SelectImage(string? description) { var fileInstruct = _services.GetRequiredService(); - var selecteds = await fileInstruct.SelectMessageFiles(_conversationId, description: description, contentTypes: new List { MediaTypeNames.Image.Png }); + var selecteds = await fileInstruct.SelectMessageFiles(_conversationId, new SelectFileOptions + { + Description = description, + ContentTypes = new List { MediaTypeNames.Image.Png } + }); return selecteds?.FirstOrDefault(); }