BotSharp/src/Infrastructure/BotSharp.Core/Files/Hooks/AttachmentProcessingHook.cs

64 lines
2.3 KiB
C#
Raw Normal View History

2024-06-18 18:18:24 +00:00
2024-06-19 09:24:49 +00:00
using Microsoft.EntityFrameworkCore;
2024-06-06 18:30:39 +00:00
namespace BotSharp.Core.Files.Hooks;
public class AttachmentProcessingHook : AgentHookBase
{
private readonly IServiceProvider _services;
public override string SelfId => string.Empty;
public AttachmentProcessingHook(IServiceProvider services, AgentSettings settings)
: base(services, settings)
{
_services = services;
}
2024-06-18 18:18:24 +00:00
public override void OnAgentLoaded(Agent agent)
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
var conv = _services.GetRequiredService<IConversationService>();
var hasConvFiles = fileService.HasConversationUserFiles(conv.ConversationId);
if (hasConvFiles)
{
2024-06-19 09:24:49 +00:00
agent.Instruction += "\r\n\r\nPlease call load_attachment if user wants to describe files, such as images, pdf.\r\n\r\n";
2024-06-06 18:30:39 +00:00
2024-06-19 09:24:49 +00:00
if (agent.Functions != null)
2024-06-06 18:30:39 +00:00
{
2024-06-19 09:24:49 +00:00
var json = JsonSerializer.Serialize(new
2024-06-07 04:02:10 +00:00
{
2024-06-19 09:24:49 +00:00
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'"
}
});
2024-06-06 18:30:39 +00:00
2024-06-19 09:24:49 +00:00
agent.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 =
2024-06-06 18:30:39 +00:00
{
Properties = JsonSerializer.Deserialize<JsonDocument>(json),
Required = new List<string>
{
2024-06-07 04:02:10 +00:00
"user_request",
"file_types"
2024-06-06 18:30:39 +00:00
}
}
2024-06-19 09:24:49 +00:00
});
}
2024-06-06 18:30:39 +00:00
}
2024-06-19 09:24:49 +00:00
base.OnAgentLoaded(agent);
2024-06-06 18:30:39 +00:00
}
}