BotSharp/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadPdfFn.cs

106 lines
3.5 KiB
C#
Raw Normal View History

2025-05-26 05:23:31 +00:00
using BotSharp.Abstraction.Routing;
2024-07-16 19:13:29 +00:00
namespace BotSharp.Plugin.FileHandler.Functions;
2024-06-06 18:30:39 +00:00
2024-07-16 19:13:29 +00:00
public class ReadPdfFn : IFunctionCallback
2024-06-06 18:30:39 +00:00
{
2024-12-04 22:12:02 +00:00
public string Name => "util-file-read_pdf";
2024-07-16 19:13:29 +00:00
public string Indication => "Reading pdf";
2024-06-06 18:30:39 +00:00
private readonly IServiceProvider _services;
2024-07-16 19:13:29 +00:00
private readonly ILogger<ReadPdfFn> _logger;
2024-06-06 18:30:39 +00:00
2024-07-16 22:09:54 +00:00
private readonly IEnumerable<string> _pdfContentTypes = new List<string>
{
MediaTypeNames.Application.Pdf
};
2024-07-16 19:13:29 +00:00
public ReadPdfFn(
2024-06-06 18:30:39 +00:00
IServiceProvider services,
2024-07-16 19:13:29 +00:00
ILogger<ReadPdfFn> logger)
2024-06-06 18:30:39 +00:00
{
_services = services;
_logger = logger;
}
public async Task<bool> Execute(RoleDialogModel message)
{
2024-07-16 19:13:29 +00:00
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs);
2024-06-06 18:30:39 +00:00
var conv = _services.GetRequiredService<IConversationService>();
2025-05-26 05:23:31 +00:00
var routingCtx = _services.GetRequiredService<IRoutingContext>();
2024-06-06 18:30:39 +00:00
var agentService = _services.GetRequiredService<IAgentService>();
2024-07-16 19:13:29 +00:00
2025-05-26 05:23:31 +00:00
Agent? fromAgent = null;
if (!string.IsNullOrEmpty(message.CurrentAgentId))
{
2025-08-22 22:00:52 +00:00
fromAgent = await agentService.GetAgent(message.CurrentAgentId);
2025-05-26 05:23:31 +00:00
}
var agent = new Agent
2024-06-06 18:30:39 +00:00
{
2025-08-22 22:00:52 +00:00
Id = fromAgent?.Id ?? BuiltInAgentId.UtilityAssistant,
Name = fromAgent?.Name ?? "Utility Assistant",
2025-05-26 05:23:31 +00:00
Instruction = fromAgent?.Instruction ?? args?.UserRequest ?? "Please describe the pdf file(s).",
2024-06-06 18:30:39 +00:00
TemplateDict = new Dictionary<string, object>()
};
2025-05-26 05:23:31 +00:00
var wholeDialogs = routingCtx.GetDialogs();
if (wholeDialogs.IsNullOrEmpty())
{
wholeDialogs = conv.GetDialogHistory();
}
var dialogs = await AssembleFiles(conv.ConversationId, wholeDialogs);
var response = await GetChatCompletion(agent, dialogs);
2024-06-06 18:30:39 +00:00
message.Content = response;
return true;
}
2024-07-16 19:13:29 +00:00
private async Task<List<RoleDialogModel>> AssembleFiles(string conversationId, List<RoleDialogModel> dialogs)
2024-06-06 18:30:39 +00:00
{
if (dialogs.IsNullOrEmpty())
{
return new List<RoleDialogModel>();
}
2024-08-08 01:56:29 +00:00
var fileStorage = _services.GetRequiredService<IFileStorageService>();
2024-08-08 18:34:50 +00:00
var messageIds = dialogs.Select(x => x.MessageId).Distinct().ToList();
2024-08-28 20:34:41 +00:00
var screenshots = await fileStorage.GetMessageFileScreenshotsAsync(conversationId, messageIds);
2024-08-08 18:34:50 +00:00
if (screenshots.IsNullOrEmpty()) return dialogs;
2024-06-06 18:30:39 +00:00
foreach (var dialog in dialogs)
{
2024-08-08 18:34:50 +00:00
var found = screenshots.Where(x => x.MessageId == dialog.MessageId).ToList();
2024-06-06 18:30:39 +00:00
if (found.IsNullOrEmpty()) continue;
dialog.Files = found.Select(x => new BotSharpFile
{
ContentType = x.ContentType,
2024-08-08 01:56:29 +00:00
FileUrl = x.FileUrl,
2024-06-06 18:30:39 +00:00
FileStorageUrl = x.FileStorageUrl
}).ToList();
}
return dialogs;
}
private async Task<string> GetChatCompletion(Agent agent, List<RoleDialogModel> dialogs)
{
try
{
2025-09-02 21:59:49 +00:00
var provider = "openai";
var model = "gpt-5-mini";
var completion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model);
2024-06-06 18:30:39 +00:00
var response = await completion.GetChatCompletions(agent, dialogs);
return response.Content;
}
catch (Exception ex)
{
2024-07-16 19:13:29 +00:00
var error = $"Error when analyzing pdf file(s).";
2025-05-02 16:31:28 +00:00
_logger.LogWarning(ex, $"{error}");
2024-06-06 18:30:39 +00:00
return error;
}
}
}