refine file handling
This commit is contained in:
parent
bcff1dff3a
commit
57bd04bf7b
|
|
@ -110,6 +110,7 @@ public class RoleDialogModel : ITrackableMessage
|
|||
/// <summary>
|
||||
/// Files to be used in conversation
|
||||
/// </summary>
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public List<BotSharpFile>? Files { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -133,6 +134,12 @@ public class RoleDialogModel : ITrackableMessage
|
|||
public bool IsStreaming { get; set; }
|
||||
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
|
||||
public bool IsFromUser => Role == AgentRole.User;
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
|
||||
public bool IsFromAssistant => Role == AgentRole.Assistant || Role == AgentRole.Model;
|
||||
|
||||
public RoleDialogModel()
|
||||
{
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,12 +5,5 @@ namespace BotSharp.Abstraction.Files;
|
|||
public class FileCoreSettings
|
||||
{
|
||||
public string Storage { get; set; } = FileStorageEnum.LocalFileStorage;
|
||||
public SettingBase? Pdf2TextConverter { get; set; }
|
||||
public SettingBase? Pdf2ImageConverter { get; set; }
|
||||
public SettingBase? ImageConverter { get; set; }
|
||||
}
|
||||
|
||||
public class SettingBase
|
||||
{
|
||||
public string Provider { get; set; }
|
||||
}
|
||||
|
|
@ -19,12 +19,12 @@ public interface IFileStorageService
|
|||
|
||||
#region Conversation
|
||||
/// <summary>
|
||||
/// Get the message file screenshots for specific content types, e.g., pdf
|
||||
/// Get the message file screenshots for pdf
|
||||
/// </summary>
|
||||
/// <param name="conversationId"></param>
|
||||
/// <param name="messageIds"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<MessageFileModel>> GetMessageFileScreenshotsAsync(string conversationId, IEnumerable<string> messageIds);
|
||||
Task<IEnumerable<MessageFileModel>> GetMessageFileScreenshotsAsync(string conversationId, IEnumerable<string> messageIds, MessageFileScreenshotOptions options);
|
||||
|
||||
/// <summary>
|
||||
/// Get the files that have been uploaded in the chat. No screenshot images are included.
|
||||
|
|
|
|||
|
|
@ -12,3 +12,9 @@ public class MessageFileOptions
|
|||
/// </summary>
|
||||
public IEnumerable<string>? ContentTypes { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public class MessageFileScreenshotOptions : MessageFileOptions
|
||||
{
|
||||
public string ImageConvertProvider { get; set; }
|
||||
}
|
||||
|
|
@ -20,9 +20,4 @@ public class KnowledgeTextEmbeddingSetting : SettingBase
|
|||
{
|
||||
public string Model { get; set; }
|
||||
public int Dimension { get; set; }
|
||||
}
|
||||
|
||||
public class SettingBase
|
||||
{
|
||||
public string Provider { get; set; }
|
||||
}
|
||||
|
|
@ -1,17 +1,7 @@
|
|||
namespace BotSharp.Abstraction.Models;
|
||||
|
||||
public class LlmConfigBase
|
||||
public class LlmConfigBase : LlmBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Llm provider
|
||||
/// </summary>
|
||||
public string? LlmProvider { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Llm model
|
||||
/// </summary>
|
||||
public string? LlmModel { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Llm maximum output tokens
|
||||
/// </summary>
|
||||
|
|
@ -22,3 +12,16 @@ public class LlmConfigBase
|
|||
/// </summary>
|
||||
public string? ReasoningEffortLevel { get; set; }
|
||||
}
|
||||
|
||||
public class LlmBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Llm provider
|
||||
/// </summary>
|
||||
public string? LlmProvider { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Llm model
|
||||
/// </summary>
|
||||
public string? LlmModel { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
namespace BotSharp.Abstraction.Settings;
|
||||
|
||||
public class SettingBase
|
||||
{
|
||||
public string Provider { get; set; }
|
||||
}
|
||||
|
|
@ -20,4 +20,5 @@ global using BotSharp.Abstraction.Files.Models;
|
|||
global using BotSharp.Abstraction.Files.Enums;
|
||||
global using BotSharp.Abstraction.Knowledges.Models;
|
||||
global using BotSharp.Abstraction.Crontab.Models;
|
||||
global using BotSharp.Abstraction.MCP.Models;
|
||||
global using BotSharp.Abstraction.MCP.Models;
|
||||
global using BotSharp.Abstraction.Settings;
|
||||
|
|
@ -86,19 +86,18 @@ public partial class FileInstructService
|
|||
var render = _services.GetRequiredService<ITemplateRender>();
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
|
||||
// Handle dialogs and files
|
||||
var innerDialogs = (dialogs ?? []).ToList();
|
||||
var text = !string.IsNullOrWhiteSpace(options.Description) ? options.Description : "Please follow the instruction and select file(s).";
|
||||
innerDialogs = innerDialogs.Concat([new RoleDialogModel(AgentRole.User, text)]).ToList();
|
||||
|
||||
if (options.IsAttachFiles)
|
||||
{
|
||||
AssembleMessageFiles(innerDialogs, files, options);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Handle dialogs and files
|
||||
var innerDialogs = (dialogs ?? []).ToList();
|
||||
var text = !string.IsNullOrWhiteSpace(options.Description) ? options.Description : "Please follow the instruction and select file(s).";
|
||||
innerDialogs = innerDialogs.Concat([new RoleDialogModel(AgentRole.User, text)]).ToList();
|
||||
|
||||
if (options.IsAttachFiles)
|
||||
{
|
||||
AssembleMessageFiles(innerDialogs, files, options);
|
||||
}
|
||||
|
||||
|
||||
// Handle instruction
|
||||
var promptMessages = innerDialogs.Select(x =>
|
||||
{
|
||||
|
|
@ -176,6 +175,10 @@ public partial class FileInstructService
|
|||
_logger.LogWarning(ex, $"Error when selecting files.");
|
||||
return [];
|
||||
}
|
||||
finally
|
||||
{
|
||||
innerDialogs.ForEach(x => x.Files = null);
|
||||
}
|
||||
}
|
||||
|
||||
private void AssembleMessageFiles(IEnumerable<RoleDialogModel> dialogs, IEnumerable<MessageFileModel> files, SelectFileOptions options)
|
||||
|
|
@ -196,7 +199,7 @@ public partial class FileInstructService
|
|||
continue;
|
||||
}
|
||||
|
||||
var userMsg = group.FirstOrDefault(x => x.Role == AgentRole.User);
|
||||
var userMsg = group.FirstOrDefault(x => x.IsFromUser);
|
||||
if (userMsg != null)
|
||||
{
|
||||
var userFiles = found.Where(x => x.FileSource == FileSource.User);
|
||||
|
|
@ -211,7 +214,7 @@ public partial class FileInstructService
|
|||
}).ToList();
|
||||
}
|
||||
|
||||
var botMsg = group.LastOrDefault(x => x.Role == AgentRole.Assistant);
|
||||
var botMsg = group.LastOrDefault(x => x.IsFromAssistant);
|
||||
if (botMsg != null)
|
||||
{
|
||||
var botFiles = found.Where(x => x.FileSource == FileSource.Bot);
|
||||
|
|
|
|||
|
|
@ -6,16 +6,17 @@ namespace BotSharp.Core.Files.Services;
|
|||
|
||||
public partial class LocalFileStorageService
|
||||
{
|
||||
public async Task<IEnumerable<MessageFileModel>> GetMessageFileScreenshotsAsync(string conversationId, IEnumerable<string> messageIds)
|
||||
public async Task<IEnumerable<MessageFileModel>> GetMessageFileScreenshotsAsync(string conversationId, IEnumerable<string> messageIds, MessageFileScreenshotOptions options)
|
||||
{
|
||||
var files = new List<MessageFileModel>();
|
||||
if (string.IsNullOrEmpty(conversationId) || messageIds.IsNullOrEmpty())
|
||||
if (string.IsNullOrEmpty(conversationId)
|
||||
|| messageIds.IsNullOrEmpty()
|
||||
|| options.Sources.IsNullOrEmpty())
|
||||
{
|
||||
return files;
|
||||
}
|
||||
|
||||
var source = FileSource.User;
|
||||
var pathPrefix = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER);
|
||||
var baseUrl = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER);
|
||||
|
||||
foreach (var messageId in messageIds)
|
||||
{
|
||||
|
|
@ -24,27 +25,30 @@ public partial class LocalFileStorageService
|
|||
continue;
|
||||
}
|
||||
|
||||
var dir = Path.Combine(pathPrefix, messageId, FileSource.User);
|
||||
if (!ExistDirectory(dir))
|
||||
foreach (var source in options.Sources)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var subDir in Directory.GetDirectories(dir))
|
||||
{
|
||||
var file = Directory.GetFiles(subDir).FirstOrDefault();
|
||||
if (file == null)
|
||||
var dir = Path.Combine(baseUrl, messageId, source);
|
||||
if (!ExistDirectory(dir))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var screenshots = await GetScreenshots(file, subDir, messageId, source);
|
||||
if (screenshots.IsNullOrEmpty())
|
||||
foreach (var subDir in Directory.GetDirectories(dir))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var file = Directory.GetFiles(subDir).FirstOrDefault();
|
||||
if (file == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
files.AddRange(screenshots);
|
||||
var screenshots = await GetScreenshotsAsync(file, subDir, messageId, source, options);
|
||||
if (screenshots.IsNullOrEmpty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
files.AddRange(screenshots);
|
||||
}
|
||||
}
|
||||
}
|
||||
return files;
|
||||
|
|
@ -283,40 +287,9 @@ public partial class LocalFileStorageService
|
|||
return dir;
|
||||
}
|
||||
|
||||
private IEnumerable<string> GetMessageIds(IEnumerable<RoleDialogModel> dialogs, int? offset = null)
|
||||
private async Task<IEnumerable<string>> ConvertPdfToImagesAsync(string pdfLoc, string imageLoc, MessageFileScreenshotOptions options)
|
||||
{
|
||||
if (dialogs.IsNullOrEmpty())
|
||||
{
|
||||
return Enumerable.Empty<string>();
|
||||
}
|
||||
|
||||
if (offset.HasValue && offset < 1)
|
||||
{
|
||||
offset = 1;
|
||||
}
|
||||
|
||||
var messageIds = new List<string>();
|
||||
if (offset.HasValue)
|
||||
{
|
||||
messageIds = dialogs.Select(x => x.MessageId).Distinct().TakeLast(offset.Value).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
messageIds = dialogs.Select(x => x.MessageId).Distinct().ToList();
|
||||
}
|
||||
|
||||
return messageIds;
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<string>> ConvertPdfToImages(string pdfLoc, string imageLoc)
|
||||
{
|
||||
var converters = _services.GetServices<IImageConverter>();
|
||||
if (converters.IsNullOrEmpty())
|
||||
{
|
||||
return Enumerable.Empty<string>();
|
||||
}
|
||||
|
||||
var converter = GetPdf2ImageConverter();
|
||||
var converter = _services.GetServices<IImageConverter>().FirstOrDefault(x => x.Provider == options.ImageConvertProvider);
|
||||
if (converter == null)
|
||||
{
|
||||
return Enumerable.Empty<string>();
|
||||
|
|
@ -325,14 +298,7 @@ public partial class LocalFileStorageService
|
|||
return await converter.ConvertPdfToImages(pdfLoc, imageLoc);
|
||||
}
|
||||
|
||||
private IImageConverter? GetPdf2ImageConverter()
|
||||
{
|
||||
var settings = _services.GetRequiredService<FileCoreSettings>();
|
||||
var converter = _services.GetServices<IImageConverter>().FirstOrDefault(x => x.Provider == settings.Pdf2ImageConverter.Provider);
|
||||
return converter;
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<MessageFileModel>> GetScreenshots(string file, string parentDir, string messageId, string source)
|
||||
private async Task<IEnumerable<MessageFileModel>> GetScreenshotsAsync(string file, string parentDir, string messageId, string source, MessageFileScreenshotOptions options)
|
||||
{
|
||||
var files = new List<MessageFileModel>();
|
||||
|
||||
|
|
@ -362,7 +328,7 @@ public partial class LocalFileStorageService
|
|||
}
|
||||
else if (contentType == MediaTypeNames.Application.Pdf)
|
||||
{
|
||||
var images = await ConvertPdfToImages(file, screenshotDir);
|
||||
var images = await ConvertPdfToImagesAsync(file, screenshotDir, options);
|
||||
foreach (var image in images)
|
||||
{
|
||||
var fileName = Path.GetFileNameWithoutExtension(image);
|
||||
|
|
|
|||
|
|
@ -150,7 +150,6 @@ public class EditImageFn : IFunctionCallback
|
|||
{
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
var llmProviderService = _services.GetRequiredService<ILlmProviderService>();
|
||||
var fileSettings = _services.GetRequiredService<FileHandlerSettings>();
|
||||
|
||||
var provider = state.GetState("image_edit_llm_provider");
|
||||
var model = state.GetState("image_edit_llm_provider");
|
||||
|
|
@ -160,8 +159,8 @@ public class EditImageFn : IFunctionCallback
|
|||
return (provider, model);
|
||||
}
|
||||
|
||||
provider = fileSettings?.Image?.Edit?.LlmProvider;
|
||||
model = fileSettings?.Image?.Edit?.LlmModel;
|
||||
provider = _settings?.Image?.Edit?.LlmProvider;
|
||||
model = _settings?.Image?.Edit?.LlmModel;
|
||||
|
||||
if (!string.IsNullOrEmpty(provider) && !string.IsNullOrEmpty(model))
|
||||
{
|
||||
|
|
@ -197,7 +196,7 @@ public class EditImageFn : IFunctionCallback
|
|||
|
||||
private async Task<BinaryData> ConvertImageToPngWithRgba(BinaryData binaryFile)
|
||||
{
|
||||
var provider = _settings?.ImageConverter?.Provider;
|
||||
var provider = _settings?.Image?.Edit?.ImageConverter?.Provider;
|
||||
var converter = _services.GetServices<IImageConverter>().FirstOrDefault(x => x.Provider == provider);
|
||||
if (converter == null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
|
||||
namespace BotSharp.Plugin.FileHandler.Functions;
|
||||
|
||||
public class GenerateImageFn : IFunctionCallback
|
||||
|
|
@ -9,6 +7,7 @@ public class GenerateImageFn : IFunctionCallback
|
|||
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger<GenerateImageFn> _logger;
|
||||
private readonly FileHandlerSettings _settings;
|
||||
|
||||
private Agent _agent;
|
||||
private string _conversationId;
|
||||
|
|
@ -16,10 +15,12 @@ public class GenerateImageFn : IFunctionCallback
|
|||
|
||||
public GenerateImageFn(
|
||||
IServiceProvider services,
|
||||
ILogger<GenerateImageFn> logger)
|
||||
ILogger<GenerateImageFn> logger,
|
||||
FileHandlerSettings settings)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
|
|
@ -113,7 +114,6 @@ public class GenerateImageFn : IFunctionCallback
|
|||
{
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
var llmProviderService = _services.GetRequiredService<ILlmProviderService>();
|
||||
var fileSettings = _services.GetRequiredService<FileHandlerSettings>();
|
||||
|
||||
var provider = state.GetState("image_generate_llm_provider");
|
||||
var model = state.GetState("image_generate_llm_model");
|
||||
|
|
@ -123,8 +123,8 @@ public class GenerateImageFn : IFunctionCallback
|
|||
return (provider, model);
|
||||
}
|
||||
|
||||
provider = fileSettings?.Image?.Generation?.LlmProvider;
|
||||
model = fileSettings?.Image?.Generation?.LlmModel;
|
||||
provider = _settings?.Image?.Generation?.LlmProvider;
|
||||
model = _settings?.Image?.Generation?.LlmModel;
|
||||
|
||||
if (!string.IsNullOrEmpty(provider) && !string.IsNullOrEmpty(model))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ public class ReadImageFn : IFunctionCallback
|
|||
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger<ReadImageFn> _logger;
|
||||
private readonly FileHandlerSettings _settings;
|
||||
|
||||
private readonly IEnumerable<string> _imageContentTypes = new List<string>
|
||||
{
|
||||
|
|
@ -18,10 +19,12 @@ public class ReadImageFn : IFunctionCallback
|
|||
|
||||
public ReadImageFn(
|
||||
IServiceProvider services,
|
||||
ILogger<ReadImageFn> logger)
|
||||
ILogger<ReadImageFn> logger,
|
||||
FileHandlerSettings settings)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
|
|
@ -53,6 +56,7 @@ public class ReadImageFn : IFunctionCallback
|
|||
|
||||
var dialogs = AssembleFiles(conv.ConversationId, args?.ImageUrls, wholeDialogs);
|
||||
var response = await GetChatCompletion(agent, dialogs);
|
||||
dialogs.ForEach(x => x.Files = null);
|
||||
message.Content = response;
|
||||
return true;
|
||||
}
|
||||
|
|
@ -77,7 +81,17 @@ public class ReadImageFn : IFunctionCallback
|
|||
var found = images.Where(x => x.MessageId == dialog.MessageId).ToList();
|
||||
if (found.IsNullOrEmpty()) continue;
|
||||
|
||||
dialog.Files = found.Select(x => new BotSharpFile
|
||||
var targets = found;
|
||||
if (dialog.Role == AgentRole.User)
|
||||
{
|
||||
targets = found.Where(x => x.FileSource.IsEqualTo(FileSource.User)).ToList();
|
||||
}
|
||||
else if (dialog.Role == AgentRole.Assistant || dialog.Role == AgentRole.Model)
|
||||
{
|
||||
targets = found.Where(x => x.FileSource.IsEqualTo(FileSource.Bot)).ToList();
|
||||
}
|
||||
|
||||
dialog.Files = targets.Select(x => new BotSharpFile
|
||||
{
|
||||
ContentType = x.ContentType,
|
||||
FileUrl = x.FileUrl,
|
||||
|
|
@ -121,7 +135,6 @@ public class ReadImageFn : IFunctionCallback
|
|||
{
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
var llmProviderService = _services.GetRequiredService<ILlmProviderService>();
|
||||
var fileSettings = _services.GetRequiredService<FileHandlerSettings>();
|
||||
|
||||
var provider = state.GetState("image_read_llm_provider");
|
||||
var model = state.GetState("image_read_llm_model");
|
||||
|
|
@ -131,8 +144,8 @@ public class ReadImageFn : IFunctionCallback
|
|||
return (provider, model);
|
||||
}
|
||||
|
||||
provider = fileSettings?.Image?.Reading?.LlmProvider;
|
||||
model = fileSettings?.Image?.Reading?.LlmModel;
|
||||
provider = _settings?.Image?.Reading?.LlmProvider;
|
||||
model = _settings?.Image?.Reading?.LlmModel;
|
||||
|
||||
if (!string.IsNullOrEmpty(provider) && !string.IsNullOrEmpty(model))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ public class ReadPdfFn : IFunctionCallback
|
|||
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger<ReadPdfFn> _logger;
|
||||
private readonly FileHandlerSettings _settings;
|
||||
|
||||
private readonly IEnumerable<string> _pdfContentTypes = new List<string>
|
||||
{
|
||||
|
|
@ -17,10 +18,12 @@ public class ReadPdfFn : IFunctionCallback
|
|||
|
||||
public ReadPdfFn(
|
||||
IServiceProvider services,
|
||||
ILogger<ReadPdfFn> logger)
|
||||
ILogger<ReadPdfFn> logger,
|
||||
FileHandlerSettings settings)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
|
|
@ -52,6 +55,7 @@ public class ReadPdfFn : IFunctionCallback
|
|||
|
||||
var dialogs = await AssembleFiles(conv.ConversationId, wholeDialogs);
|
||||
var response = await GetChatCompletion(agent, dialogs);
|
||||
dialogs.ForEach(x => x.Files = null);
|
||||
message.Content = response;
|
||||
return true;
|
||||
}
|
||||
|
|
@ -65,16 +69,47 @@ public class ReadPdfFn : IFunctionCallback
|
|||
|
||||
var fileStorage = _services.GetRequiredService<IFileStorageService>();
|
||||
var messageIds = dialogs.Select(x => x.MessageId).Distinct().ToList();
|
||||
var screenshots = await fileStorage.GetMessageFileScreenshotsAsync(conversationId, messageIds);
|
||||
|
||||
if (screenshots.IsNullOrEmpty()) return dialogs;
|
||||
IEnumerable<MessageFileModel> files;
|
||||
if (_settings.Pdf?.Reading?.ConvertToImage == true)
|
||||
{
|
||||
files = await fileStorage.GetMessageFileScreenshotsAsync(conversationId, messageIds, options: new()
|
||||
{
|
||||
Sources = [FileSource.User],
|
||||
ImageConvertProvider = _settings.Pdf?.Reading?.ImageConverter?.Provider
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
files = fileStorage.GetMessageFiles(conversationId, messageIds, options: new()
|
||||
{
|
||||
Sources = [FileSource.User],
|
||||
ContentTypes = _pdfContentTypes
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
if (files.IsNullOrEmpty())
|
||||
{
|
||||
return dialogs;
|
||||
}
|
||||
|
||||
foreach (var dialog in dialogs)
|
||||
{
|
||||
var found = screenshots.Where(x => x.MessageId == dialog.MessageId).ToList();
|
||||
var found = files.Where(x => x.MessageId == dialog.MessageId).ToList();
|
||||
if (found.IsNullOrEmpty()) continue;
|
||||
|
||||
dialog.Files = found.Select(x => new BotSharpFile
|
||||
var targets = found;
|
||||
if (dialog.IsFromUser)
|
||||
{
|
||||
targets = found.Where(x => x.FileSource.IsEqualTo(FileSource.User)).ToList();
|
||||
}
|
||||
else if (dialog.IsFromAssistant)
|
||||
{
|
||||
targets = found.Where(x => x.FileSource.IsEqualTo(FileSource.Bot)).ToList();
|
||||
}
|
||||
|
||||
dialog.Files = targets.Select(x => new BotSharpFile
|
||||
{
|
||||
ContentType = x.ContentType,
|
||||
FileUrl = x.FileUrl,
|
||||
|
|
@ -107,7 +142,6 @@ public class ReadPdfFn : IFunctionCallback
|
|||
{
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
var llmProviderService = _services.GetRequiredService<ILlmProviderService>();
|
||||
var fileSettings = _services.GetRequiredService<FileHandlerSettings>();
|
||||
|
||||
var provider = state.GetState("pdf_read_llm_provider");
|
||||
var model = state.GetState("pdf_read_llm_model");
|
||||
|
|
@ -117,8 +151,8 @@ public class ReadPdfFn : IFunctionCallback
|
|||
return (provider, model);
|
||||
}
|
||||
|
||||
provider = fileSettings?.Image?.Reading?.LlmProvider;
|
||||
model = fileSettings?.Image?.Reading?.LlmModel;
|
||||
provider = _settings?.Pdf?.Reading?.LlmProvider;
|
||||
model = _settings?.Pdf?.Reading?.LlmModel;
|
||||
|
||||
if (!string.IsNullOrEmpty(provider) && !string.IsNullOrEmpty(model))
|
||||
{
|
||||
|
|
@ -133,6 +167,11 @@ public class ReadPdfFn : IFunctionCallback
|
|||
|
||||
private void SetImageDetailLevel()
|
||||
{
|
||||
if (_settings.Pdf?.Reading?.ConvertToImage != true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
var fileSettings = _services.GetRequiredService<FileHandlerSettings>();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
using BotSharp.Abstraction.Models;
|
||||
|
||||
namespace BotSharp.Plugin.FileHandler.Settings;
|
||||
|
||||
public class FileHandlerSettings
|
||||
{
|
||||
public ImageSettings? Image { get; set; }
|
||||
public PdfSettings? Pdf { get; set; }
|
||||
public SettingBase? ImageConverter { get; set; }
|
||||
}
|
||||
|
||||
#region Image
|
||||
|
|
@ -16,22 +17,22 @@ public class ImageSettings
|
|||
public ImageVariationSettings? Variation { get; set; }
|
||||
}
|
||||
|
||||
public class ImageReadSettings : FileLlmSettingBase
|
||||
public class ImageReadSettings : LlmBase
|
||||
{
|
||||
public string? ImageDetailLevel { get; set; }
|
||||
}
|
||||
|
||||
public class ImageGenerationSettings : FileLlmSettingBase
|
||||
public class ImageGenerationSettings : LlmBase
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class ImageEditSettings : FileLlmSettingBase
|
||||
public class ImageEditSettings : LlmBase
|
||||
{
|
||||
|
||||
public SettingBase? ImageConverter { get; set; }
|
||||
}
|
||||
|
||||
public class ImageVariationSettings : FileLlmSettingBase
|
||||
public class ImageVariationSettings : LlmBase
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -43,15 +44,10 @@ public class PdfSettings
|
|||
public PdfReadSettings? Reading { get; set; }
|
||||
}
|
||||
|
||||
public class PdfReadSettings : FileLlmSettingBase
|
||||
public class PdfReadSettings : LlmBase
|
||||
{
|
||||
public bool ConvertToImage { get; set; }
|
||||
public string? ImageDetailLevel { get; set; }
|
||||
public SettingBase? ImageConverter { get; set; }
|
||||
}
|
||||
#endregion
|
||||
|
||||
public class FileLlmSettingBase
|
||||
{
|
||||
public string? LlmProvider { get; set; }
|
||||
public string? LlmModel { get; set; }
|
||||
}
|
||||
#endregion
|
||||
|
|
@ -8,18 +8,10 @@ public partial class ImageCompletionProvider
|
|||
{
|
||||
public async Task<RoleDialogModel> GetImageGeneration(Agent agent, RoleDialogModel message)
|
||||
{
|
||||
var hooks = _services.GetHooks<IContentGeneratingHook>(agent.Id);
|
||||
|
||||
var client = ProviderHelper.GetClient(Provider, _model, _services);
|
||||
var (prompt, imageCount, options) = PrepareGenerationOptions(message);
|
||||
var imageClient = client.GetImageClient(_model);
|
||||
|
||||
// Before generation
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
await hook.BeforeGenerating(agent, [new RoleDialogModel(AgentRole.User, prompt)]);
|
||||
}
|
||||
|
||||
var response = imageClient.GenerateImages(prompt, imageCount, options);
|
||||
var images = response.Value;
|
||||
|
||||
|
|
@ -32,16 +24,6 @@ public partial class ImageCompletionProvider
|
|||
GeneratedImages = generatedImages
|
||||
};
|
||||
|
||||
// After generation
|
||||
var usage = response.Value.Usage;
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
await hook.AfterGenerated(responseMessage, new TokenStatsModel
|
||||
{
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
return await Task.FromResult(responseMessage);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
using BotSharp.Abstraction.Files;
|
||||
using BotSharp.Abstraction.Files.Converters;
|
||||
using BotSharp.Abstraction.Files.Enums;
|
||||
using BotSharp.Abstraction.Files.Utilities;
|
||||
using System.Net.Mime;
|
||||
|
||||
|
|
@ -8,28 +6,37 @@ namespace BotSharp.Plugin.TencentCos.Services;
|
|||
|
||||
public partial class TencentCosService
|
||||
{
|
||||
public async Task<IEnumerable<MessageFileModel>> GetMessageFileScreenshotsAsync(string conversationId, IEnumerable<string> messageIds)
|
||||
public async Task<IEnumerable<MessageFileModel>> GetMessageFileScreenshotsAsync(string conversationId, IEnumerable<string> messageIds, MessageFileScreenshotOptions options)
|
||||
{
|
||||
var files = new List<MessageFileModel>();
|
||||
if (string.IsNullOrEmpty(conversationId) || messageIds.IsNullOrEmpty())
|
||||
if (string.IsNullOrEmpty(conversationId)
|
||||
|| messageIds.IsNullOrEmpty()
|
||||
|| options.Sources.IsNullOrEmpty())
|
||||
{
|
||||
return files;
|
||||
}
|
||||
|
||||
var source = FileSource.User;
|
||||
var pathPrefix = $"{CONVERSATION_FOLDER}/{conversationId}/{FILE_FOLDER}";
|
||||
var baseDir = $"{CONVERSATION_FOLDER}/{conversationId}/{FILE_FOLDER}";
|
||||
foreach (var messageId in messageIds)
|
||||
{
|
||||
var dir = $"{pathPrefix}/{messageId}/{source}";
|
||||
foreach (var subDir in _cosClient.BucketClient.GetDirectories(dir))
|
||||
if (string.IsNullOrWhiteSpace(messageId))
|
||||
{
|
||||
var file = _cosClient.BucketClient.GetDirFiles(subDir).FirstOrDefault();
|
||||
if (file == null) continue;
|
||||
continue;
|
||||
}
|
||||
|
||||
var screenshots = await GetScreenshots(file, subDir, messageId, source);
|
||||
if (screenshots.IsNullOrEmpty()) continue;
|
||||
foreach (var source in options.Sources)
|
||||
{
|
||||
var dir = $"{baseDir}/{messageId}/{source}";
|
||||
foreach (var subDir in _cosClient.BucketClient.GetDirectories(dir))
|
||||
{
|
||||
var file = _cosClient.BucketClient.GetDirFiles(subDir).FirstOrDefault();
|
||||
if (file == null) continue;
|
||||
|
||||
files.AddRange(screenshots);
|
||||
var screenshots = await GetScreenshotsAsync(file, subDir, messageId, source, options);
|
||||
if (screenshots.IsNullOrEmpty()) continue;
|
||||
|
||||
files.AddRange(screenshots);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -214,35 +221,9 @@ public partial class TencentCosService
|
|||
return dir;
|
||||
}
|
||||
|
||||
private IEnumerable<string> GetMessageIds(IEnumerable<RoleDialogModel> dialogs, int? offset = null)
|
||||
private async Task<IEnumerable<string>> ConvertPdfToImagesAsync(string pdfLoc, string imageLoc, MessageFileScreenshotOptions options)
|
||||
{
|
||||
if (dialogs.IsNullOrEmpty()) return Enumerable.Empty<string>();
|
||||
|
||||
if (offset.HasValue && offset < 1)
|
||||
{
|
||||
offset = 1;
|
||||
}
|
||||
|
||||
var messageIds = new List<string>();
|
||||
if (offset.HasValue)
|
||||
{
|
||||
messageIds = dialogs.Select(x => x.MessageId).Distinct().TakeLast(offset.Value).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
messageIds = dialogs.Select(x => x.MessageId).Distinct().ToList();
|
||||
}
|
||||
|
||||
return messageIds;
|
||||
}
|
||||
|
||||
|
||||
private async Task<IEnumerable<string>> ConvertPdfToImages(string pdfLoc, string imageLoc)
|
||||
{
|
||||
var converters = _services.GetServices<IImageConverter>();
|
||||
if (converters.IsNullOrEmpty()) return Enumerable.Empty<string>();
|
||||
|
||||
var converter = GetPdf2ImageConverter();
|
||||
var converter = _services.GetServices<IImageConverter>().FirstOrDefault(x => x.Provider == options.ImageConvertProvider);
|
||||
if (converter == null)
|
||||
{
|
||||
return Enumerable.Empty<string>();
|
||||
|
|
@ -250,19 +231,12 @@ public partial class TencentCosService
|
|||
return await converter.ConvertPdfToImages(pdfLoc, imageLoc);
|
||||
}
|
||||
|
||||
private IImageConverter? GetPdf2ImageConverter()
|
||||
{
|
||||
var settings = _services.GetRequiredService<FileCoreSettings>();
|
||||
var converter = _services.GetServices<IImageConverter>().FirstOrDefault(x => x.Provider == settings.Pdf2ImageConverter.Provider);
|
||||
return converter;
|
||||
}
|
||||
|
||||
private string BuilFileUrl(string file)
|
||||
{
|
||||
return $"https://{_fullBuketName}.cos.{_settings.Region}.myqcloud.com/{file}";
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<MessageFileModel>> GetScreenshots(string file, string parentDir, string messageId, string source)
|
||||
private async Task<IEnumerable<MessageFileModel>> GetScreenshotsAsync(string file, string parentDir, string messageId, string source, MessageFileScreenshotOptions options)
|
||||
{
|
||||
var files = new List<MessageFileModel>();
|
||||
|
||||
|
|
@ -293,7 +267,7 @@ public partial class TencentCosService
|
|||
}
|
||||
else if (contentType == MediaTypeNames.Application.Pdf)
|
||||
{
|
||||
var images = await ConvertPdfToImages(file, screenshotDir);
|
||||
var images = await ConvertPdfToImagesAsync(file, screenshotDir, options);
|
||||
foreach (var image in images)
|
||||
{
|
||||
var fileName = Path.GetFileNameWithoutExtension(image);
|
||||
|
|
|
|||
|
|
@ -439,12 +439,6 @@
|
|||
|
||||
"FileCore": {
|
||||
"Storage": "LocalFileStorage",
|
||||
"Pdf2TextConverter": {
|
||||
"Provider": ""
|
||||
},
|
||||
"Pdf2ImageConverter": {
|
||||
"Provider": ""
|
||||
},
|
||||
"ImageConverter": {
|
||||
"Provider": ""
|
||||
}
|
||||
|
|
@ -463,7 +457,10 @@
|
|||
},
|
||||
"Edit": {
|
||||
"LlmProvider": "openai",
|
||||
"LlmModel": "gpt-image-1"
|
||||
"LlmModel": "gpt-image-1",
|
||||
"ImageConverter": {
|
||||
"Provider": "file-handler"
|
||||
}
|
||||
},
|
||||
"Variation": {
|
||||
"LlmProvider": "",
|
||||
|
|
@ -472,14 +469,14 @@
|
|||
},
|
||||
"Pdf": {
|
||||
"Reading": {
|
||||
"LlmProvider": "openai",
|
||||
"LlmModel": "gpt-5-mini",
|
||||
"ConvertToImage": true,
|
||||
"ImageDetailLevel": "auto"
|
||||
"LlmProvider": "google-ai",
|
||||
"LlmModel": "gemini-2.0-flash",
|
||||
"ConvertToImage": false,
|
||||
"ImageDetailLevel": "auto",
|
||||
"ImageConverter": {
|
||||
"Provider": null
|
||||
}
|
||||
}
|
||||
},
|
||||
"ImageConverter": {
|
||||
"Provider": "file-handler"
|
||||
}
|
||||
},
|
||||
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ namespace BotSharp.Plugin.Google.Core
|
|||
return string.Join("/", segments);
|
||||
}
|
||||
|
||||
public Task<IEnumerable<MessageFileModel>> GetMessageFileScreenshotsAsync(string conversationId, IEnumerable<string> messageIds)
|
||||
public Task<IEnumerable<MessageFileModel>> GetMessageFileScreenshotsAsync(string conversationId, IEnumerable<string> messageIds, MessageFileScreenshotOptions options)
|
||||
{
|
||||
return Task.FromResult<IEnumerable<MessageFileModel>>(new List<MessageFileModel>
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue