2024-07-16 19:13:29 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Enums;
|
|
|
|
|
using BotSharp.Abstraction.Files.Enums;
|
|
|
|
|
using BotSharp.Abstraction.Files.Models;
|
|
|
|
|
using BotSharp.Abstraction.Files;
|
2024-06-06 18:30:39 +00:00
|
|
|
using BotSharp.Abstraction.MLTasks;
|
2024-07-16 19:13:29 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using BotSharp.Core.Infrastructures;
|
2024-06-06 18:30:39 +00:00
|
|
|
|
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 ReadImageFn : IFunctionCallback
|
2024-06-06 18:30:39 +00:00
|
|
|
{
|
2024-07-16 19:13:29 +00:00
|
|
|
public string Name => "read_image";
|
|
|
|
|
public string Indication => "Reading images";
|
2024-06-06 18:30:39 +00:00
|
|
|
|
|
|
|
|
private readonly IServiceProvider _services;
|
2024-07-16 19:13:29 +00:00
|
|
|
private readonly ILogger<ReadImageFn> _logger;
|
2024-06-06 18:30:39 +00:00
|
|
|
|
2024-07-16 19:13:29 +00:00
|
|
|
private const string DEFAULT_IMAGE = "image";
|
|
|
|
|
private readonly IEnumerable<string> _targetImageTypes = new List<string> { "image", "images", "png", "jpg", "jpeg" };
|
|
|
|
|
|
|
|
|
|
public ReadImageFn(
|
2024-06-06 18:30:39 +00:00
|
|
|
IServiceProvider services,
|
2024-07-16 19:13:29 +00:00
|
|
|
ILogger<ReadImageFn> 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 image(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>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
2024-07-16 19:13:29 +00:00
|
|
|
var images = await fileService.GetChatImages(conversationId, FileSourceType.User, new List<string> { "image" }, dialogs);
|
2024-06-06 18:30:39 +00:00
|
|
|
|
|
|
|
|
foreach (var dialog in dialogs)
|
|
|
|
|
{
|
2024-07-16 19:13:29 +00:00
|
|
|
var found = images.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,
|
|
|
|
|
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 images.";
|
2024-06-06 18:30:39 +00:00
|
|
|
_logger.LogWarning($"{error} {ex.Message}");
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|