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

94 lines
3.4 KiB
C#
Raw Normal View History

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-07-16 19:13:29 +00:00
public string Name => "read_pdf";
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>();
var agentService = _services.GetRequiredService<IAgentService>();
2024-07-16 19:13:29 +00:00
2024-06-06 18:30:39 +00:00
var wholeDialogs = conv.GetDialogHistory();
2024-07-16 19:13:29 +00:00
var dialogs = await AssembleFiles(conv.ConversationId, wholeDialogs);
2024-07-10 04:36:49 +00:00
var agent = await agentService.LoadAgent(BuiltInAgentId.UtilityAssistant);
2024-06-06 18:30:39 +00:00
var fileAgent = new Agent
{
2024-06-24 21:39:08 +00:00
Id = agent?.Id ?? Guid.Empty.ToString(),
Name = agent?.Name ?? "Unkown",
2024-07-16 19:13:29 +00:00
Instruction = !string.IsNullOrWhiteSpace(args?.UserRequest) ? args.UserRequest : "Please describe the pdf file(s).",
2024-06-06 18:30:39 +00:00
TemplateDict = new Dictionary<string, object>()
};
var response = await GetChatCompletion(fileAgent, dialogs);
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();
var screenshots = await fileStorage.GetMessageFileScreenshots(conversationId, messageIds);
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
{
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)
{
2024-07-16 19:13:29 +00:00
var error = $"Error when analyzing pdf file(s).";
2024-07-16 22:09:54 +00:00
_logger.LogWarning($"{error} {ex.Message}\r\n{ex.InnerException}");
2024-06-06 18:30:39 +00:00
return error;
}
}
}