Merge pull request #491 from iceljc/features/add-pdf-upload

Features/add pdf upload
This commit is contained in:
C. Oceania 2024-06-12 02:56:11 +00:00 committed by GitHub
commit 37babd5aed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 417 additions and 66 deletions

View file

@ -0,0 +1,7 @@
namespace BotSharp.Abstraction.Files.Enums;
public static class FileSourceType
{
public const string User = "user";
public const string Bot = "bot";
}

View file

@ -3,10 +3,11 @@ namespace BotSharp.Abstraction.Files;
public interface IBotSharpFileService
{
string GetDirectory(string conversationId);
IEnumerable<MessageFileModel> GetChatImages(string conversationId, List<RoleDialogModel> conversations, int offset = 2);
IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, bool imageOnly = false);
string GetMessageFile(string conversationId, string messageId, string fileName);
bool SaveMessageFiles(string conversationId, string messageId, List<BotSharpFile> files);
Task<IEnumerable<MessageFileModel>> GetChatImages(string conversationId, string source, IEnumerable<string> fileTypes, List<RoleDialogModel> conversations, int? offset = null);
IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, string source, bool imageOnly = false);
string GetMessageFile(string conversationId, string messageId, string source, string index, string fileName);
bool HasConversationUserFiles(string conversationId);
bool SaveMessageFiles(string conversationId, string messageId, string source, List<BotSharpFile> files);
string GetUserAvatar();
bool SaveUserAvatar(BotSharpFile file);

View file

@ -14,4 +14,10 @@ public class BotSharpFile
[JsonPropertyName("file_url")]
public string FileUrl { get; set; } = string.Empty;
[JsonPropertyName("content_type")]
public string ContentType { get; set; } = string.Empty;
[JsonPropertyName("file_storage_url")]
public string FileStorageUrl { get; set; } = string.Empty;
}

View file

@ -0,0 +1,10 @@
namespace BotSharp.Abstraction.Files.Models;
public class LlmFileContext
{
[JsonPropertyName("user_request")]
public string UserRequest { get; set; }
[JsonPropertyName("file_types")]
public string FileTypes { get; set; }
}

View file

@ -20,6 +20,9 @@ public class MessageFileModel
[JsonPropertyName("content_type")]
public string ContentType { get; set; }
[JsonPropertyName("file_source")]
public string FileSource { get; set; } = FileSourceType.User;
public MessageFileModel()
{

View file

@ -16,4 +16,5 @@ global using BotSharp.Abstraction.Routing.Planning;
global using BotSharp.Abstraction.Templating;
global using BotSharp.Abstraction.Translation.Attributes;
global using BotSharp.Abstraction.Messaging.Enums;
global using BotSharp.Abstraction.Files.Models;
global using BotSharp.Abstraction.Files.Models;
global using BotSharp.Abstraction.Files.Enums;

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Files;
using BotSharp.Abstraction.Google.Settings;
using BotSharp.Abstraction.Instructs;
using BotSharp.Abstraction.Messaging;
@ -6,7 +5,6 @@ using BotSharp.Abstraction.Plugins.Models;
using BotSharp.Abstraction.Routing.Planning;
using BotSharp.Abstraction.Settings;
using BotSharp.Abstraction.Templating;
using BotSharp.Core.Files;
using BotSharp.Core.Instructs;
using BotSharp.Core.Messaging;
using BotSharp.Core.Routing.Planning;
@ -44,7 +42,6 @@ public class ConversationPlugin : IBotSharpPlugin
services.AddScoped<IConversationStorage, ConversationStorage>();
services.AddScoped<IConversationService, ConversationService>();
services.AddScoped<IConversationStateService, ConversationStateService>();
services.AddScoped<IBotSharpFileService, BotSharpFileService>();
services.AddScoped<ITranslationService, TranslationService>();
// Rich content messaging

View file

@ -48,7 +48,7 @@ public partial class ConversationService
// Save message files
var fileService = _services.GetRequiredService<IBotSharpFileService>();
fileService.SaveMessageFiles(_conversationId, message.MessageId, message.Files);
fileService.SaveMessageFiles(_conversationId, message.MessageId, FileSourceType.User, message.Files);
message.Files?.Clear();
// Save payload

View file

@ -1,17 +1,20 @@
using Microsoft.AspNetCore.StaticFiles;
using BotSharp.Abstraction.Browsing;
using Microsoft.EntityFrameworkCore;
using System.IO;
using System.Linq;
using System.Threading;
namespace BotSharp.Core.Files;
public partial class BotSharpFileService
{
public IEnumerable<MessageFileModel> GetChatImages(string conversationId, List<RoleDialogModel> conversations, int offset = 1)
public async Task<IEnumerable<MessageFileModel>> GetChatImages(string conversationId, string source, IEnumerable<string> fileTypes,
List<RoleDialogModel> conversations, int? offset = null)
{
var files = new List<MessageFileModel>();
if (string.IsNullOrEmpty(conversationId) || conversations.IsNullOrEmpty())
{
return files;
return new List<MessageFileModel>();
}
if (offset <= 0)
@ -23,55 +26,161 @@ public partial class BotSharpFileService
offset = MAX_OFFSET;
}
var messageIds = conversations.Select(x => x.MessageId).Distinct().TakeLast(offset).ToList();
files = GetMessageFiles(conversationId, messageIds, imageOnly: true).ToList();
var messageIds = new List<string>();
if (offset.HasValue)
{
messageIds = conversations.Select(x => x.MessageId).Distinct().TakeLast(offset.Value).ToList();
}
else
{
messageIds = conversations.Select(x => x.MessageId).Distinct().ToList();
}
files = await GetMessageFiles(conversationId, messageIds, source, fileTypes);
return files;
}
public IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, bool imageOnly = false)
private async Task<List<MessageFileModel>> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, string source, IEnumerable<string> fileTypes)
{
var files = new List<MessageFileModel>();
if (string.IsNullOrEmpty(conversationId) || messageIds.IsNullOrEmpty() || fileTypes.IsNullOrEmpty()) return files;
var isNeedScreenShot = fileTypes.Any(x => _allowScreenShotTypes.Contains(x));
var onlyScreenShot = fileTypes.All(x => _allowScreenShotTypes.Contains(x));
try
{
var contextId = string.Empty;
var web = _services.GetRequiredService<IWebBrowser>();
var preFixPath = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER);
if (isNeedScreenShot)
{
contextId = Guid.NewGuid().ToString();
await web.LaunchBrowser(contextId, string.Empty);
}
foreach (var messageId in messageIds)
{
var dir = Path.Combine(preFixPath, messageId, source);
if (!ExistDirectory(dir)) continue;
foreach (var subDir in Directory.GetDirectories(dir))
{
var file = Directory.GetFiles(subDir).FirstOrDefault();
if (file == null) continue;
var index = subDir.Split(Path.DirectorySeparatorChar).Last();
var contentType = GetFileContentType(file);
if ((!isNeedScreenShot || (isNeedScreenShot && !onlyScreenShot)) && _allowedImageTypes.Contains(contentType))
{
var model = new MessageFileModel()
{
MessageId = messageId,
FileStorageUrl = file,
ContentType = contentType
};
files.Add(model);
}
else if ((isNeedScreenShot && !onlyScreenShot || onlyScreenShot) && !_allowedImageTypes.Contains(contentType))
{
var screenShotDir = Path.Combine(subDir, SCREENSHOT_FILE_FOLDER);
if (ExistDirectory(screenShotDir) && Directory.GetFiles(screenShotDir).Any())
{
file = Directory.GetFiles(screenShotDir).First();
contentType = GetFileContentType(file);
var model = new MessageFileModel()
{
MessageId = messageId,
FileStorageUrl = file,
ContentType = contentType
};
files.Add(model);
}
else
{
await web.GoToPage(contextId, file);
var path = Path.Combine(subDir, SCREENSHOT_FILE_FOLDER, $"{Guid.NewGuid()}.png");
await web.ScreenshotAsync(contextId, path);
contentType = GetFileContentType(path);
var model = new MessageFileModel()
{
MessageId = messageId,
FileStorageUrl = path,
ContentType = contentType
};
files.Add(model);
}
}
}
}
if (isNeedScreenShot)
{
await web.CloseBrowser(contextId);
}
}
catch (Exception ex)
{
_logger.LogWarning($"Error when reading conversation ({conversationId}) files: {ex.Message}");
}
return files;
}
public IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds,
string source, bool imageOnly = false)
{
var files = new List<MessageFileModel>();
if (messageIds.IsNullOrEmpty()) return files;
foreach (var messageId in messageIds)
{
var dir = GetConversationFileDirectory(conversationId, messageId);
var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId, source);
if (!ExistDirectory(dir))
{
continue;
}
foreach (var file in Directory.GetFiles(dir))
foreach (var subDir in Directory.GetDirectories(dir))
{
var contentType = GetFileContentType(file);
if (imageOnly && !_allowedTypes.Contains(contentType))
var index = subDir.Split(Path.DirectorySeparatorChar).Last();
foreach (var file in Directory.GetFiles(subDir))
{
continue;
var contentType = GetFileContentType(file);
if (imageOnly && !_allowedImageTypes.Contains(contentType))
{
continue;
}
var fileName = Path.GetFileNameWithoutExtension(file);
var extension = Path.GetExtension(file);
var fileType = extension.Substring(1);
var model = new MessageFileModel()
{
MessageId = messageId,
FileUrl = $"/conversation/{conversationId}/message/{messageId}/{source}/file/{index}/{fileName}",
FileStorageUrl = file,
FileName = fileName,
FileType = fileType,
ContentType = contentType
};
files.Add(model);
}
var fileName = Path.GetFileNameWithoutExtension(file);
var extension = Path.GetExtension(file);
var fileType = extension.Substring(1);
var model = new MessageFileModel()
{
MessageId = messageId,
FileUrl = $"/conversation/{conversationId}/message/{messageId}/file/{fileName}",
FileStorageUrl = file,
FileName = fileName,
FileType = fileType,
ContentType = contentType
};
files.Add(model);
}
}
return files;
}
public string GetMessageFile(string conversationId, string messageId, string fileName)
public string GetMessageFile(string conversationId, string messageId, string source, string index, string fileName)
{
var dir = GetConversationFileDirectory(conversationId, messageId);
var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId, source, index);
if (!ExistDirectory(dir))
{
return string.Empty;
@ -81,7 +190,17 @@ public partial class BotSharpFileService
return found;
}
public bool SaveMessageFiles(string conversationId, string messageId, List<BotSharpFile> files)
public bool HasConversationUserFiles(string conversationId)
{
if (string.IsNullOrEmpty(conversationId)) return false;
var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER);
if (!ExistDirectory(dir)) return false;
return Directory.GetDirectories(dir).Any();
}
public bool SaveMessageFiles(string conversationId, string messageId, string source, List<BotSharpFile> files)
{
if (files.IsNullOrEmpty()) return false;
@ -99,11 +218,16 @@ public partial class BotSharpFileService
}
var (_, bytes) = GetFileInfoFromData(file.FileData);
var fileType = Path.GetExtension(file.FileName);
var fileName = $"{i + 1}{fileType}";
Thread.Sleep(100);
File.WriteAllBytes(Path.Combine(dir, fileName), bytes);
var subDir = Path.Combine(dir, source, $"{i + 1}");
if (!ExistDirectory(subDir))
{
Directory.CreateDirectory(subDir);
}
File.WriteAllBytes(Path.Combine(subDir, file.FileName), bytes);
}
return true;
}
catch (Exception ex)
@ -114,7 +238,6 @@ public partial class BotSharpFileService
}
public bool DeleteMessageFiles(string conversationId, IEnumerable<string> messageIds, string targetMessageId, string? newMessageId = null)
{
if (string.IsNullOrEmpty(conversationId) || messageIds == null) return false;
@ -132,6 +255,13 @@ public partial class BotSharpFileService
}
Directory.Move(prevDir, newDir);
Thread.Sleep(100);
var botDir = Path.Combine(newDir, BOT_FILE_FOLDER);
if (ExistDirectory(botDir))
{
Directory.Delete(botDir, true);
}
}
}

View file

@ -12,10 +12,14 @@ public partial class BotSharpFileService : IBotSharpFileService
private readonly IUserIdentity _user;
private readonly ILogger<BotSharpFileService> _logger;
private readonly string _baseDir;
private readonly IEnumerable<string> _allowedTypes = new List<string> { "image/png", "image/jpeg" };
private readonly IEnumerable<string> _allowedImageTypes = new List<string> { "image/png", "image/jpeg" };
private readonly IEnumerable<string> _allowScreenShotTypes = new List<string> { "pdf" };
private const string CONVERSATION_FOLDER = "conversations";
private const string FILE_FOLDER = "files";
private const string USER_FILE_FOLDER = "user";
private const string SCREENSHOT_FILE_FOLDER = "screenshot";
private const string BOT_FILE_FOLDER = "bot";
private const string USERS_FOLDER = "users";
private const string USER_AVATAR_FOLDER = "avatar";

View file

@ -0,0 +1,21 @@
using BotSharp.Core.Files.Hooks;
using Microsoft.Extensions.Configuration;
namespace BotSharp.Core.Files;
public class FilePlugin : IBotSharpPlugin
{
public string Id => "6a8473c0-04eb-4346-be32-24755ce5973d";
public string Name => "File";
public string Description => "Provides file analysis.";
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddScoped<IBotSharpFileService, BotSharpFileService>();
services.AddScoped<IAgentHook, AttachmentProcessingHook>();
}
}

View file

@ -0,0 +1,125 @@
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.MLTasks;
namespace BotSharp.Core.Files.Functions;
public class LoadAttachmentFn : IFunctionCallback
{
public string Name => "load_attachment";
public string Indication => "Analyzing files";
private readonly IServiceProvider _services;
private readonly ILogger<LoadAttachmentFn> _logger;
private const string AIAssistant = "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a";
private readonly IEnumerable<string> _imageTypes = new List<string> { "image", "images", "png", "jpg", "jpeg" };
private readonly IEnumerable<string> _pdfTypes = new List<string> { "pdf" };
public LoadAttachmentFn(
IServiceProvider services,
ILogger<LoadAttachmentFn> logger)
{
_services = services;
_logger = logger;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<LlmFileContext>(message.FunctionArgs);
var conv = _services.GetRequiredService<IConversationService>();
var agentService = _services.GetRequiredService<IAgentService>();
var wholeDialogs = conv.GetDialogHistory();
var fileTypes = args?.FileTypes?.Split(",")?.ToList() ?? new List<string>();
var dialogs = await AssembleFiles(conv.ConversationId, wholeDialogs, fileTypes);
var agent = await agentService.LoadAgent(AIAssistant);
var fileAgent = new Agent
{
Id = agent.Id,
Name = agent.Name,
Instruction = !string.IsNullOrWhiteSpace(args?.UserRequest) ? args.UserRequest : "Please describe the files.",
TemplateDict = new Dictionary<string, object>()
};
var response = await GetChatCompletion(fileAgent, dialogs);
message.Content = response;
message.StopCompletion = true;
return true;
}
private async Task<List<RoleDialogModel>> AssembleFiles(string conversationId, List<RoleDialogModel> dialogs, List<string> fileTypes)
{
if (dialogs.IsNullOrEmpty())
{
return new List<RoleDialogModel>();
}
var parsedTypes = ParseFileTypes(fileTypes);
var fileService = _services.GetRequiredService<IBotSharpFileService>();
var files = await fileService.GetChatImages(conversationId, FileSourceType.User, parsedTypes, dialogs);
foreach (var dialog in dialogs)
{
var found = files.Where(x => x.MessageId == dialog.MessageId).ToList();
if (found.IsNullOrEmpty()) continue;
dialog.Files = found.Select(x => new BotSharpFile
{
ContentType = x.ContentType,
FileStorageUrl = x.FileStorageUrl
}).ToList();
}
return dialogs;
}
private IEnumerable<string> ParseFileTypes(IEnumerable<string> fileTypes)
{
var imageType = "image";
var pdfType = "pdf";
var parsed = new List<string>();
if (fileTypes.IsNullOrEmpty())
{
return new List<string> { imageType };
}
foreach (var fileType in fileTypes)
{
var type = fileType?.Trim();
if (string.IsNullOrWhiteSpace(type) || _imageTypes.Any(x => type.IsEqualTo(x)))
{
parsed.Add(imageType);
}
else if (_pdfTypes.Any(x => type.IsEqualTo(x)))
{
parsed.Add(pdfType);
}
}
if (parsed.IsNullOrEmpty())
{
parsed.Add(imageType);
}
return parsed.Distinct();
}
private async Task<string> GetChatCompletion(Agent agent, List<RoleDialogModel> dialogs)
{
try
{
var llmProviderService = _services.GetRequiredService<ILlmProviderService>();
var provider = llmProviderService.GetProviders().FirstOrDefault(x => x == "openai");
var model = llmProviderService.GetProviderModel(provider: provider, id: "gpt-4", multiModal: true);
var completion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model.Name);
var response = await completion.GetChatCompletions(agent, dialogs);
return response.Content;
}
catch (Exception ex)
{
var error = $"Error when analyzing files.";
_logger.LogWarning($"{error} {ex.Message}");
return error;
}
}
}

View file

@ -0,0 +1,56 @@
namespace BotSharp.Core.Files.Hooks;
public class AttachmentProcessingHook : AgentHookBase
{
private readonly IServiceProvider _services;
private readonly AgentSettings _agentSettings;
public override string SelfId => string.Empty;
public AttachmentProcessingHook(IServiceProvider services, AgentSettings settings)
: base(services, settings)
{
_services = services;
_agentSettings = settings;
}
public override bool OnFunctionsLoaded(List<FunctionDef> functions)
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
var conv = _services.GetRequiredService<IConversationService>();
var hasConvFiles = fileService.HasConversationUserFiles(conv.ConversationId);
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'"
}
});
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<JsonDocument>(json),
Required = new List<string>
{
"user_request",
"file_types"
}
}
});
}
return base.OnFunctionsLoaded(functions); ;
}
}

View file

@ -25,6 +25,7 @@ global using BotSharp.Abstraction.Repositories.Filters;
global using BotSharp.Abstraction.Translation;
global using BotSharp.Abstraction.Files;
global using BotSharp.Abstraction.Files.Models;
global using BotSharp.Abstraction.Files.Enums;
global using BotSharp.Abstraction.Translation.Attributes;
global using BotSharp.Abstraction.Messaging.Enums;
global using BotSharp.Abstraction.Http.Settings;

View file

@ -370,19 +370,19 @@ public class ConversationController : ControllerBase
return BadRequest(new { message = "Invalid file." });
}
[HttpGet("/conversation/{conversationId}/files/{messageId}")]
public IEnumerable<MessageFileViewModel> GetMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId)
[HttpGet("/conversation/{conversationId}/files/{messageId}/{source}")]
public IEnumerable<MessageFileViewModel> GetMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string source)
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
var files = fileService.GetMessageFiles(conversationId, new List<string> { messageId });
var files = fileService.GetMessageFiles(conversationId, new List<string> { messageId }, source, imageOnly: false);
return files?.Select(x => MessageFileViewModel.Transform(x))?.ToList() ?? new List<MessageFileViewModel>();
}
[HttpGet("/conversation/{conversationId}/message/{messageId}/file/{fileName}")]
public IActionResult GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string fileName)
[HttpGet("/conversation/{conversationId}/message/{messageId}/{source}/file/{index}/{fileName}")]
public IActionResult GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string source, [FromRoute] string index, [FromRoute] string fileName)
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
var file = fileService.GetMessageFile(conversationId, messageId, fileName);
var file = fileService.GetMessageFile(conversationId, messageId, source, index, fileName);
if (string.IsNullOrEmpty(file))
{
return NotFound();

View file

@ -229,12 +229,6 @@ public class ChatCompletionProvider : IChatCompletion
var settings = settingsService.GetSetting(Provider, _model);
var allowMultiModal = settings != null && settings.MultiModal;
var chatFiles = new List<MessageFileModel>();
if (allowMultiModal)
{
chatFiles = fileService.GetChatImages(state.GetConversationId(), conversations, offset: 2).ToList();
}
var chatCompletionsOptions = new ChatCompletionsOptions();
if (!string.IsNullOrEmpty(agent.Instruction))
@ -304,16 +298,6 @@ public class ChatCompletionProvider : IChatCompletion
new ChatMessageTextContentItem(text)
};
var files = chatFiles.Where(x => x.MessageId == message.MessageId).ToList();
if (!files.IsNullOrEmpty())
{
foreach (var file in files)
{
using var stream = File.OpenRead(file.FileStorageUrl);
chatItems.Add(new ChatMessageImageContentItem(stream, file.ContentType, ChatMessageImageDetailLevel.Low));
}
}
if (!message.Files.IsNullOrEmpty())
{
foreach (var file in message.Files)
@ -329,6 +313,11 @@ public class ChatCompletionProvider : IChatCompletion
using var stream = new MemoryStream(bytes, 0, bytes.Length);
chatItems.Add(new ChatMessageImageContentItem(stream, contentType, ChatMessageImageDetailLevel.Low));
}
else if (!string.IsNullOrEmpty(file.FileStorageUrl))
{
using var stream = File.OpenRead(file.FileStorageUrl);
chatItems.Add(new ChatMessageImageContentItem(stream, file.ContentType, ChatMessageImageDetailLevel.Low));
}
}
}

View file

@ -36,7 +36,7 @@ public class WebSocketsMiddleware
{
var regexes = new List<Regex>
{
new Regex(@"/conversation/[a-z0-9-]+/message/[a-z0-9-]+/file/[a-z0-9-]+", RegexOptions.IgnoreCase),
new Regex(@"/conversation/[a-z0-9-]+/message/[a-z0-9-]+/[a-z]+/file/[a-z0-9-]+/[a-z0-9-]+", RegexOptions.IgnoreCase),
new Regex(@"/user/avatar", RegexOptions.IgnoreCase)
};